aratz-lasa
I have searched, but I have not found it. If it is a duplicated question I am so sorry.
I want to know how and when to format the returned values in a function. Because a lot of times, it is returned {:ok, value} and others just value . So, when should I use the status along the value?
Trending in Questions
One of my LiveView view’s has a very heavy javascript dependency so I want to avoid loading it for every page on the site, while still us...
New
Hi all!
I know that quite a few people use Phoenix LiveView but it is hard to get a more up-to-date list of folks/companies using it. I ...
New
Hello everyone!
I am new to coding with Elixir/Erlang. And i am looking for some utilities for fuzzing testing like AFL++.
Does anyone ...
New
I’m very new to Elixir, so sorry if this is a silly question:
I am wondering what happens to the mailbox of a process when the machine i...
New
I load tested a plain Phoenix chat app backed by Postgres with 100 million messages in it, everything on default settings, to see what br...
New
I don’t suppose you can lazy load a library depending on runtime data? For example if it’s a certain user they can lazy load ‘library A’ ...
New
Hi all,
I’m trying to get Pi working well as an external agent inside Tidewave.ai via ACP.
Tidewave’s settings make it possible to add ...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Hi everyone :waving_hand:
I’ve been working on PhiaUI, an open-source UI component library for Phoenix LiveView, and I wanted to share i...
New
On November 23, 2026, an unauthorized third party exploited a vulnerability in hex.pm (CVE-2026-129514, CVSS 10.0) that let them run comm...
New
@hugobarauna, Dr. Dimitrios Koutmos (my brother) and I (Alex Koutmos) have been hard at work on writing a book on how you can use Elixir ...
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
- #code-sync
- #exunit
- #discussion
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #ai
- #authentication
- #elixirconf-us
- #umbrella
- #full-time-contract
- #blog-post
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-eu
- #distillery
- #security
- #processes
- #api
- #forms
- #hex










Showing Posts 1 to 6- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
tty
From laziness we treat
valueas{:ok, value}but ifvalueis an error code then it should be{:error, value}.A status should be descriptive and makes sense for the callee when pattern matching.
E.g. if a function spec is
{:ok, :connected} | {:ok, :received} | {:error, error_code}it can easily be changed to:connected | :received | {:error, error_code}without losing information.kokolegorille
Often functions ending with ! will return the value, or raise an error.
While those without ! at the end return {:ok, value} | {:error, reason}
aratz-lasa
Currently, I am working on a project. I will use it as an example to explain my point.
There is a function that returns a list if you pass a
struct. And a second function that creates astructif you pass a string. However the second function’s string parameter, must have a specific format, otherwise it fails.So, the return formats I use for each function are:
struct{:ok, struct}or{:error, reason}Is it a good choice?
tme_317
I’d say #2 is the good choice since it’s much more idiomatic and found throughout the ecosystem for functions that can fail. And as @kokolegorille said earlier naming the function with a bang (i.e.
func!()) if the function raises instead of returning an error tuple.That said occasionally I find something like
Integer.parse/2in the stdlib can return{42, ""}or:error. OrRepo.get/3from Ecto which returns%Struct{...}ornilif the query returns 0 rows. I guess the idea if there are no records it is not an error condition and should return the concept of nothing which isnilor[]for a list of nothing.tty
For your first function it takes a
but yes I do understand what you mean. What happens when it is given an empty list ? Will the function fail or do you expect passing back error codes ?
structand returns alist()aratz-lasa
The first function receives an
structand returns alist()as you said. So, it is never “given a list”, and it is hard (almost impossible to fail?).So, I just return the
list()without any status (:ok,:error).However, the second function receives a
stringand returns astruct. But this can fail or do receive wrong values. That is why, the second function returns a status along with thestruct.