MeerKatDev

MeerKatDev

How do you return your ok tuples?

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 :slight_smile:

  1. {
      :ok,
      conn
      |> assign(:attr_one, :value_one)
      |> assign(:attr_two, :value_two)
      |> assign(:attr_three, :value_three)
    }
    
  2. {
      :ok,
      assign(
        conn, 
        attr_one: :value_one,
        attr_two: :value_two,
        attr_three: :value_three
      )
    }
    
  3. 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.

Most Liked

benwilson512

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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

conn =

{:ok, conn}

And stuff like:

comments =  
posts = fetch_posts(...)
users = posts |> Enum.map(& &1.author)

%{
  users: users,
  posts: posts,
  comments: comments
}

If you mix a bunch of function calls inside the literal you have to read it sort of inside out.

12
Post #2
kartheek

kartheek

I prefer readability over everything else:

conn = 
  conn
  |> assign(:attr_one, :value_one)
  |> assign(:attr_two, :value_two)
  |> assign(:attr_three, :value_three)
{:ok, conn}

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.

gregvaughn

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.

conn
|> assign(attr_one: :value_one, attr_two: :value_two, attr_three: :value_three)
|> then(&{:ok, &1})

I prefer the single call to assign with a keyword list, and I prefer then to the anonymous function syntax used in option 3.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
johnnyicon
Hi all, I've just started learning Elixir and Phoenix Framework, so please pardon my n00bness at this stage. I'm trying to use Postg...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
lucidguppy
I have a super simple question about elixir - how would I take a file like this foo bar baz and output a new file that enumerates th...
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

Other popular topics Top

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
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 43591 214
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "eq...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3271 126226 1237
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

We're in Beta

About us Mission Statement