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

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
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
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
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