lessless

lessless

Horizontal vs vertical piping

Hello,

In the Elixir codebase most wide-adopted pipe operator usage style is vertical piping:

variable
|> transform1(:another_option)
|> transform2

but horizontal piping is also present, though it’s rarer

variable |> transform1(:another_option) |> transform2

What are your thoughts about horizontal vs vertical piping? When use which? What’s your opinion about passing variable directly to the first function call vs piping it, i.e. transform1(variable, :another_option) vs variable |> transform1(:another_option)?

Most Liked

belaustegui

belaustegui

I have nothing against horizontal piping as long at it doesn’t generate too long lines (my limit is about 80 characters).
If the line gets too long I would break it to use vertical piping.

I have stronger opinions about directly passing the variable to the first function call though. Consider the following example:

transform1(variable, :option)
|> transform2(:another_option)

The variable that gets passed to transform1() is the beginning of the piping, but I have to read the code carefully to notice that. The thing gets aggravated when using horizontal piping or functions with long names and multiple arguments.

transform1(variable, :option) |> transform2(:another_option)

On the other side, piping the variable to the first function call makes it explicit and I can recognise the actual data flow and transformations while quickly scanning the code.

variable
|> transform1(:option)
|> transform2(:another_option)

#or

variable |> transform1(:option) |> transform2(:another_option)

EDIT: I’ve been taking a look at Credo’s style guide and found this:

It is preferred to start pipe chains with a “pure” value rather than a function call.

# preferred way - this is very readable due to the clear flow of data
username
|> String.strip
|> String.downcase

# also okay - but often slightly less readable
String.strip(username)
|> String.downcase
NobbZ

NobbZ

I think this is much of a personal style or habbit.

I do prefer vertical pipes, but I do use horizontals in single-line assignments if there are not more than 2 pipe operators.

The main reason for prefering vertical pipes is, that you can spot them on a glance even when you are not in a highlighting editor, just because the aligned pipe operators set a very bold statement.

Qqwy

Qqwy

TypeCheck Core Team

The only uses for horizontal piping are either very trivial (i.e. short) sequences, or in IEx, as it will end a statement as soon as you hit return, even if you actually wanted to place a |> on the next line.

In most code, I (and I believe most of the other people in the community) agree that it is better to use vertical pipes, because they remain readable even when the amount of nested statements increases at a later time.

Where Next?

Popular in Discussions Top

blackode
Elixir Upgrading is so Simple in Ubuntu and It worked for me Ubuntu 16.04 git clone https://github.com/elixir-lang/elixir.git cd elixir...
New
sashaafm
I’m trying to evaluate the best combo/stack for a BEAM Web app. Right now I’m exploring Yaws a bit, after having dealt with Phoenix for a...
New
sashaafm
Piggy backing a bit on @dvcrn topic BEAM optimization for functions with static return type?, I’ve been trying to understand in a deeper ...
New
AlexMcConnell
The reason that Rails is as popular as it is is because it’s very easy for relatively inexperienced developers to get a lot of work done....
588 19568 166
New
New
rower687
Hi all, I’ve been reading a lot about the “let it crash” term and how supervising processes and the whole messaging passing make an elixi...
New
jer
I’ve been using umbrellas for a while, and generally started off (on greenfield projects at least) by isolating subapps based on clearly ...
New
AstonJ
If so I (and hopefully others!) might have some tips for you :slight_smile: But first, please say which area you’re finding most challen...
New
scouten
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

Other popular topics 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
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
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
AngeloChecked
What learn first? Rust or Elixir Hi Elixir community! I’m here because i want learn a new language. I’m a junior developer and mainly i ...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
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
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
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

We're in Beta

About us Mission Statement