minhajuddin
Transforming data and Elixir syntax style
While writing Elixir, you are encouraged to start your pipe chain with a raw value. Credo has a check for this at credo/lib/credo/check/refactor/pipe_chain_start.ex at master · rrrene/credo · GitHub
However, I think blindly applying this decreases the readability of the code. Here are a few examples:
Instances where this rule makes sense
# 1
list
|> Enum.take(5)
|> Enum.shuffle
|> pick_winner()
#2
%User{}
|> User.changeset(%{"email" => "danny@m.com"})
|> Repo.insert()
Instances where this rule does not make sense
# 3
User
|> Repo.all
|> Enum.map(fn(user) -> csv_fields(user) end)
|> CSV.encode(headers: true)
#4
20
|> :crypto.strong_rand_bytes
|> Base.encode16(case: :lower)
I think the question boils down to whether you are transforming something. In case of 20 |> :crypto.strong_rand_bytes you are not really transforming 20 into bytes and instead getting 20 random bytes. Just because it is an argument to a function doesn’t mean it is being transformed. What are your thoughts on this?
After writing this, I looked if there was similar discussion elsewhere and found Proposal: Ignore refactoring opportunities in pipe chain when using erlang functions · Issue #14 · rrrene/credo-proposals · GitHub
Most Liked
LostKobrakai
I don’t think enforcing that style is sensible. I just loosely follow the rule.
The following example is what I most dislike with it. Strictly following the rule would mean I need to split up arguments, which really belong to each other. There’s not even a hierarchy in those arguments.
SpeedDate.new(user1, user2)
|> SpeedDate.start()
|> SpeedDate.triggerHints()
michalmuskala
I agree, I generally disable this check for credo.
easco
I agree with you and I have disabled that check in Credo because I don’t care for the resulting pipelines in many cases.
Popular in Discussions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #javascript
- #code-sync
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








