Onor.io

Onor.io

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.

Showing Posts 1 to 9

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’

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.

Onor.io

Onor.io OP

And readability is what I’m most concerned about.

Onor.io

Onor.io OP

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

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.

Onor.io

Onor.io OP

That’s very interesting @sashaafm. I didn’t realize that sort of code would work. :slight_smile:

Qqwy

Qqwy

TypeCheck Core Team

Interesting that this works. It is a great example of how Elixir is very regular in the syntax it provides (what I mean by that, is that there are nearly now ‘exceptions’ to when certain syntax is possible).

Of course, in the example above, readability is probably improved by extracting the case-statement to its own function.

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:

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
subsaharancoder
I’ve followed the Phoenix LiveView file upload code here Uploads — Phoenix LiveView v1.0.0-rc.7 and so far everything works just fine wit...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews