Onor.io
Style Question
Just wondering about opinions on how to format code where I’m capturing the result of a few pipelined operations. For example:
v =
op1
|> op2
|> op3
I’m inclined to put the variable the result is going into up on a line by itself so it’s more obvious that the whole thing is getting captured to a variable. But I’d like to hear the opinions of others regarding how I might handle this.
BTW, Could someone create a “coding-style” or “coding-standards” tag and attach it to this message? I don’t have sufficient privileges to create new tags.
Most Liked
Qqwy
I really like the indented style of
first_admins =
users
|> Enum.filter(&User.is_admin?/1)
|> Enum.take(10)
When you don’t indent there, it is less clear that the pipeline will still end up affecting the value to the left of the equals-sign.
Indentation in Elixir usually don’t matters for execution, but it does significantly affect the readability.
sashaafm
I usually do
v = op1
|> op2
|> op3
This leads the code to be condensed, explicit and easy to read. I’ve also seen this way a lot in a lot of Elixir code.
EDIT: I’ve created the tag ‘coding-standards’
Last Post!
riverrun
You can also use the pipe after the case statement.
Here’s an example which includes concatenating the string before sending it to url_decode64 function:
defp urldec64(data) do data <> case rem(byte_size(data), 4) do 2 -> "==" 3 -> "=" _ -> "" end |> url_decode64! end
Now I think that’s really expressive and easy to read. Having said that, I don’t know how many people will agree with me ![]()
Popular in Questions
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
- #code-sync
- #javascript
- #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
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #hex
- #security









