Like most folks new to Elixir, discovering the pipe operator `|>` and understanding how to use it was a ‘Eureka moment’ that convinced me to explore the language further.

In short, the pipe operator feeds the result of whatever is on its left side as the first parameter to a function on its right side.

For example, using Map.put/3 to update the key value to “Bar” is commonly written:

Map.put(user, :name, “Bar”)

It can also be written using a pipe:

user |> Map.put(:name, “Bar”)

To pipe or not to pipe

I don’t know about you, but when I have a choice of two ways to achieve the same result I end up trying out both and asking myself “which choice makes the most logical sense in this situation?”. An internal bike-shedding war may or may not take place (ahem) and I end up going with my gut feeling.

Fast-forward a few weeks and my code is littered with both implementations causing me to re-evaluate my pipe decision making strategy.

What if I never use pipe?

Longer transformations would need lots of intermediate variables… no thanks.

What if I only use pipe when my function expands to more than one transformation?

Seems logical, lets have a look at it in action.

The conversion is simple to perform, move the `user` from the parentheses to the start of the line and add a `|>`.

This is the strategy I was using and it worked out pretty well apart from a couple of niggling caveats that seemed minor then became harder to ignore.

The focus of the transformation `user` is harder to see when used in the parentheses along with other parameters

Its slightly harder to expand the pipeline, compared to starting out using pipe operator, and I might perform the conversion many times everyday

What if I always use the pipe?

Clear indication that a transformation is taking place and the focus of the transformation is on the left of the pipe operator

Removes ‘To pipe or not to pipe’ decision making

Consistent code

Easy to re-order pipeline transformations

Easy to add / remove pipeline transformations

Easy to re-use pipeline transformations

Others…

“Seems silly to use the pipe to feed the collection into the first map call. Otherwise pretty nifty, I’ve been enjoying learning Elixir the last few months.” — hacker news comment

This post was partially inspired by the comment above. From my perspective its perfectly reasonable to feed the collection into the first map call!