MeerKatDev
I was wondering what Elixir developers think about this: how to treat piping into an ok_tuple? I have no idea if there was a similar question, I searched but I could found only posts on how to treat ok/error tuples cases.
I mean, between these, which ones would you prefer? I guess they’re all valid to a certain extent, just curious about different points of views ![]()
-
{ :ok, conn |> assign(:attr_one, :value_one) |> assign(:attr_two, :value_two) |> assign(:attr_three, :value_three) } -
{ :ok, assign( conn, attr_one: :value_one, attr_two: :value_two, attr_three: :value_three ) } -
conn |> assign(:attr_one, :value_one) |> assign(:attr_two, :value_two) |> assign(:attr_three, :value_three) |> (&{:ok, &1}).()
or there might be something different better, can’t think at any other right now.
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello!
Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app.
I creat...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
I’m trying to set up Emacs with elixir-ls via lsp-mode and credo via Flycheck. This should mostly be preconfigured as Flycheck picks up c...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
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
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
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
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve.
They are GUI (Emerge) and State management (S...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixirconf-us
- #elixir-ls
- #ai
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
benwilson512
When constructing literals (like tuples, but also lists and maps) I try to always have the elements of that literal just be simple variables, or other literals. That is to say, when reading the literal, you can just focus on its structure, and you don’t need to also read various function calls along the way. eg:
I favor
And stuff like:
If you mix a bunch of function calls inside the literal you have to read it sort of inside out.
gregvaughn
I typically agree with @benwilson512 's preference there. However since Elixir 1.12 there’s a new option that I haven’t had much chance to use, so I haven’t fully formed an opinion on it.
I prefer the single call to
assignwith a keyword list, and I preferthento the anonymous function syntax used in option 3.egze
In LiveView I usually do
thiagomajesk
I always prefer to use the pipe operator in cases I can express clearly that a chain of transformations is going to be applied to a particular type of data. I do that by not mixing purely data transformation pipelines with control flow. When I see functions that return an ok-result or error-reason tuple, that function tells me that it wants me to make a decision based on the return value, which might have succeeded or not. For those cases, I rather use something like
withto chain operations that require decision making.I’d prefer either: 1) with a function that does the assigns for me, so:
{:ok, assign_values(conn)}or returns the ok-tuple directly likemaybe_assign_values(conn). I particularly prefer to split code like that and I find it to be a little more readable.trisolaran
The winner for me: 2.
I find 3. to be the most unreadable, because you don’t even see clearly the structure of what you’re returning - namely, a tuple.
Between 1. and 2., I prefer 2. because using a pipe is unnecessary in this case, as you can do everything with one single call to
assign/2.tomkonidas
I am a fan of
1. Something about each line doing its own thing. If it was like2, then I would probably make anassign_somenamefunction if I felt they belonged together, which I would then make it like option 1eahanson
kartheek
I prefer readability over everything else:
I tend to keep creation of tuples, maps, etc as simple as possible. Someone else who is going to read and maintain code I have written should not have any cognitive overhead.
soup
Yeah I follow benwilsons and kartheeks pattern. I think it’s more readable if you have clear
do-work -> return-workseparation.Though I will construct basic types in the return if they’re small enough:
eksperimental
The “problem” with this approach is that the formatter will make it look like this:
It looks much nicer the way you presented it.