gshaw

gshaw

Idiomatic guard clause for checking not nil

What is the idiomatic way of matching for not nil in Elixir?

E.g.,

First way:
defp halt_if_not_signed_in(conn, signed_in_account) when signed_in_account != nil do

Second way:
defp halt_if_not_signed_in(conn, signed_in_account) when not is_nil(signed_in_account) do

If there is a preferred way does anybody know the reason?

Personally I prefer the first way because it reads more naturally and takes less horizontal space but examples I’ve seen have used not is_nil(...) and I am wondering why.

Most Liked

OvermindDL1

OvermindDL1

Personally I do:

defp halt_if_not_signed_in(conn, nil), do: :whatever
defp halt_if_not_signed_in(conn, signed_in_account) do
  ...
end

Unless you just want nil to error out, in which case I’d optimally do a precise match:

defp halt_if_not_signed_in(conn, signed_in_account) when is_binary(signed_in_account)

Or so.

OvermindDL1

OvermindDL1

I’m personally a fan of val != nil because nil is just an atom, so I don’t even know why it has a specialized function like is_nil/1 anyway, especially since the empty list [] is the nil value on the system, those confused me for a bit when I started Elixir (nil should have been [] as the empty list is named nil standardly, not :nil, blah).

fmcgeough

fmcgeough

agreed. I find using pattern matching to handle this is very readable.

I’ve helped introduce two Java engineers to Elixir and they both found this a really useful language feature, especially as they discovered some of the powerful variations of pattern matching.

As for other uses of not is_nil(val) vs val != nil I’d say just pick one and be consistent within your project. Personally not is_nil(val) is not my favorite. :slight_smile:

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
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
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
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
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
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
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
yawaramin
In the Dialyzer docs ( dialyzer — OTP 29.0.2 (dialyzer 6.0.1) ), there is a way to turn off a specific warning for a function: -dialyzer...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
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

malloryerik
Hi, this is for people who, like me, have had some friction using .html.heex templates in VSCode. The solution seems to be, in a hyphena...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? Ecto.Repo — Ecto v3.14.0 has exampl...
New
chrismccord
This release brings a number of exciting features, including integration with the new Phoenix LiveDashboard and Phoenix LiveView. There h...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 39523 209
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
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
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

We're in Beta

About us Mission Statement