heatmiser

heatmiser

Why would you create two near-identical functions but one does not have a bang (!)?

I am currently going through the Programming Phoenix book and noticed that the author has two get_user functions:

def get_user(id), do: Repo.get(User, id)
def get_user!(id), do: Repo.get(User, id)

From what I understand today the typical Elixir convention is to affix a ! at the end of a function that could potentially throw but I don’t think the latter one can. Looking at the Ecto.Query docs I know there’s Repo.get! and Repo.get, did the author potentially mean to use that in the latter function?

Thanks for any clarity I can get on my Elixir learning journey!

:fire:h

Most Liked

al2o3cr

al2o3cr

This looks like a typo - the expectation would be that get_user! would call get!

ityonemo

ityonemo

Typically the conventions are as follows:

fetch: returns {:ok, result} or {:error, error}
fetch!: returns result or raises on not available
get: returns result or nil on not available.

All functions are liable to raise on truly exceptional conditions, for example, it’s legit for a get to raise if there’s no database connection (nil implies not found)

I don’t know why ecto decided to differentiate between get and get! but get! raises on nil (ecto does not provide fetch functions), so you should use get! when the lack of an entry is not an actionable condition and you want to ‘stop the presses’ or ‘stop the assembly line’ (in Toyota six sigma speak)

LostKobrakai

LostKobrakai

Bang functions are useful to forego error handling where errors are not supposed to be handled. Personally I see them mostly as a convenience over matching on success cases, with the added benefit of less catching of errors and re-raising on different places (for the match error instead of the original error).

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
New
Kurisu
For example for a current url like http://localhost:4000/cosmetic/products?_utf8=✓&query=perfume&page=2, I would like to get: ...
New
qwerescape
Is there a way to get the call stack or stack trace at any point in the code? Not from exceptions, but an expression that returns how the...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: <h1>Create Post</h1> <%= ...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

Other popular topics Top

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, "equa...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
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
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
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

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement