minhajuddin

minhajuddin

Transforming data and Elixir syntax style

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

Most Liked

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()
michalmuskala

michalmuskala

I agree, I generally disable this check for credo.

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.

Where Next?

Popular in Discussions Top

PragTob
Hello everyone, I know we had quite some threads (read through lots of them) about background job processing but it remains a hotly deba...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1144 53690 245
New
nunobernardes99
Hi there Elixir friends :vulcan_salute: In a recent task I was on, I needed to check in two dates which of them is the maximum and which...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39297 209
New
nburkley
AWS re:Invent is on at the moment with some interesting announcements. One new feature in particular is the Lambda Runtime API for AWS La...
New
CharlesO
Erlang :list.nth simple, but 1 - based nth(1, [H|_]) -> H; nth(N, [_|T]) when N > 1 -> nth(N - 1, T). Elixir Enum.at … coo...
New
Ankhers
Just a little information upfront. Generally speaking, if I feel like I need to either break a pipe chain or use an anonymous function in...
New
shishini
I think this twitter post and youtube video didn’t get as much attention as I hoped I am still new to Elixir, so can’t really judge ...
New
restack_oslo
Hello, Please pardon me for any faux paux. I am 46 and this is my first time on a forum of any kind. I wanted to to get answers from tho...
New
Owens
Hello all, I am developing a new mobile app with Flutter frontend and Phoenix backend. The mobile app has real-time task management and c...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
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
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement