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

chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
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
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
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
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
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
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
WestKeys
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
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
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
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
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
nobody
Hi! In PHP: $SERVER['SERVERADDR'] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
marick
I had some trouble figuring out how to make many-to-many associations work. Once I got it working, I wrote a blog post. Because I'm a nov...
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

We're in Beta

About us Mission Statement