thojanssens1

thojanssens1

When to suffix a function by a bang!

Sometimes you have two versions for a function, one returning a tuple (ok or error tuple), and the other raising. The latter will be then have a bang ! suffix.

For example Map.fetch/2 and Map.fetch!/2

But not every function that raise will have the bang suffix. Random example: Ecto.Changeset.put_assoc/4.
If the given key is not found it will raise, but the function still has no bang !.

What is the rule as to when to adopt the bang for your functions and when not?

Most Liked

wolf4earth

wolf4earth

From my perspective a good rule of thumb is:

  • is there a non-raising version of the function?
  • is it a realistic scenario that the passed data might lead to a raised error?

Let’s inspect your examples with these rules.

Map.fetch!/2

  • is there a non-raising version of the function?
    Yes - Map.fetch/2
  • is it a realistic scenario that the passed data might lead to a raised error?
    Yes - as Map operates on maps, these might or might not contain the given key

Ecto.Changeset.put_assoc/4

  • is there a non-raising version of the function?
    No
  • is it a realistic scenario that the passed data might lead to a raised error?
    Noish - passing an invalid key is certainly a programmer error and should be caught as early as possible

So in that sense it makes sense that Map.fetch!/2 has a bang and Ecto.Changeset.put_assoc/4 has not.

christhekeele

christhekeele

Bangs in Elixir, as well as Ruby, are a way of indicating there is a similar, safer way to perform an operation. The intuition I’ve developed around them is:

  • When writing an API, present bang variants of functions for operations where both safe and unsafe versions have strong use-cases.

  • When choosing a variant, use the existence of the bang as an opportunity to ask yourself if you might really want the safer version.

  • When reading code with a bang variant, assume the author assessed the risk/failure modes in play, and chose the bang variant for some hopefully evident and important reason.

Not much more to it than that! It’s mostly a convention for developers to communicate a decision of caution and careful thought across time and space.

The only real rule is that there shouldn’t be a bang function without a non-bang equivalent. The bang doesn’t represent danger so much as an important decision made with intent, so it must be something that you decided to do in contrast to a more benign option.

Where Next?

Popular in Questions Top

jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
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
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
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
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
sabri
Can someone explain the settings of pool_size of Ecto in config file? and what is the recommend size? Thanks
New
idi527
I’ve been re-reading swift book again and noticed that multiline strings there don’t have a trailing line break, unlike in elixir iex(2)...
New

Other popular topics Top

vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
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 42633 214
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
electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
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
9mm
I am constructing a JSON object (map) and I need to conditionally set a field. I’m trying to write proper elixir-way code… and I’m at a l...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New

We're in Beta

About us Mission Statement