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’
Onor.io
Thanks for that link. I had a link to a different Elixir Style guide as well. I’m asking partially because I’d like to hear if there are points for and points against that style. It strikes me as good coding style but if there’s some strong reason not to use it, I figure the folks here would be most likely to know it.
sashaafm
I honestly prefer the way I described mainly because the code is written with a “block shape”. I find it way easier on the eyes and provided better readability. When skimming over code catching these code blocks from “the corner of your eye” seems to make it easier to remember and associate what the code block does as well. Just my opinion, of course.
Yesterday I actually found that the following piece of code works:
var
|> case do
:ok -> 200
:error -> 404
end
but I couldn’t find an actual style I enjoyed for it. In these cases I prefer to not use syntactic sugar and shortcuts in order to keep the code in more standard and popular ways of styling.








