minhajuddin

minhajuddin

While writing Elixir, you are encouraged to start your pipe chain with a raw value. Credo has a check for this at credo/lib/credo/check/refactor/pipe_chain_start.ex at master · rrrene/credo · GitHub

However, I think blindly applying this decreases the readability of the code. Here are a few examples:

Instances where this rule makes sense

# 1
 list
      |> Enum.take(5)
      |> Enum.shuffle
      |> pick_winner()

#2
      %User{}
      |> User.changeset(%{"email" => "danny@m.com"})
      |> Repo.insert()

Instances where this rule does not make sense

# 3
User
    |> Repo.all
    |> Enum.map(fn(user) -> csv_fields(user) end)
    |> CSV.encode(headers: true)


#4
    20
    |> :crypto.strong_rand_bytes
    |> Base.encode16(case: :lower)

I think the question boils down to whether you are transforming something. In case of 20 |> :crypto.strong_rand_bytes you are not really transforming 20 into bytes and instead getting 20 random bytes. Just because it is an argument to a function doesn’t mean it is being transformed. What are your thoughts on this?

After writing this, I looked if there was similar discussion elsewhere and found Proposal: Ignore refactoring opportunities in pipe chain when using erlang functions · Issue #14 · rrrene/credo-proposals · GitHub

Showing Posts 1 to 7

LostKobrakai

LostKobrakai

I don’t think enforcing that style is sensible. I just loosely follow the rule.
The following example is what I most dislike with it. Strictly following the rule would mean I need to split up arguments, which really belong to each other. There’s not even a hierarchy in those arguments.

SpeedDate.new(user1, user2)
|> SpeedDate.start()
|> SpeedDate.triggerHints()
yurko

yurko

I consider this issue to be the “perfect is the enemy of good” case, this rule clearly has lots of exceptions but it never makes the code really unreadable. For me the idea is to make the code somewhat cleaner and more consistent as well as to avoid thinking about each particular case.

Because of this I just let credo warn me and blindly comply with this rule which lets me concentrate on other things :slight_smile:

minhajuddin

minhajuddin OP

Doesn’t it hamper readability? I find it a bit jarring when I see something like 20 |> :crypto.strong_rand_bytes, it may be because of the way I read the pipe operator, I always read it as transform 20 using this function.

yurko

yurko

It does, your examples are valid. My point is: It’s not always confusing and not in a big way, for me the benefit of having a consistent way to do things outweigh this cost.

How I read it is “pass (result of) 20 to this function” which might be a bit easier on the brain :slight_smile:

easco

easco

I agree with you and I have disabled that check in Credo because I don’t care for the resulting pipelines in many cases.

peerreynders

peerreynders

Depends on your perspective.

I initially didn’t like the style at all and always wanted to start the pipeline with a fully populated function. The way I made my peace with it was to accept that the pipeline is in effect emulating (lambda calculus style) function composition, something that only works when all the functions involved only have a single parameter.

|> Enum.take(5)

is suggestive of a partial application to bring Enum.take/2 down to an arity of 1. In the case of

|>  :crypto.strong_rand_bytes

:crypto.strong_rand_bytes is already at an arity of 1 - so it is ready to be composed as is.

This in effect places more emphasis on the composition than the value that enters it.

michalmuskala

michalmuskala

I agree, I generally disable this check for credo.

— All posts loaded —

Where Next? Top

Trending in Discussions Top

AstonJ
As the title says, please share what you’ve been up to with Elixir. Whether that’s been learning it, looking into it, making stuff with i...
2977 94592 917
New
cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
heathen
Quite interesting article Google brought me. Didn’t find any mentions about it here. What do you think in general? Would you use togethe...
New
mhanberg
Hi everyone! The first release candidate for the Expert language server project is now available! We’ve published a press release detai...
New
AstonJ
Since we have deprecated our Erlang sections (as we have dedicated Erlang Forums now) let’s add this thread for those who’d like to post ...
New
Null-logic-0
What IDE or editor are you using for Elixir development? Personally, I use Zed, and I really like it, but sometimes I wish there were a ...
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
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
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
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews