Onor.io

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

Qqwy

TypeCheck Core Team

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

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

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 :slight_smile:

Where Next?

Popular in Questions Top

vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

Other popular topics Top

hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
sergio_101
I am VERY much an elixir newbie. I have taken one elixir course and one phoenix course on Udemy. During that course, I saw the instructor...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement