niels_bom

niels_bom

Idiomatic: pattern matching with two function heads or boolean guard clause?

I just found myself writing this:

def foo([], _), do: []
def foo(_, 0), do: []

But then I thought I could also use a guard clause with a boolean OR, like so:

def foo(list, n) when list == [] or n == 0, do: []

Is either version more idiomatic?
Would it maybe depend on the amount of code in the function? (DRY and all)

Because pattern matching is so nice and in this case the function body is very small I’ll probably choose the first version.

Thanks!

Most Liked

NobbZ

NobbZ

The first one, as it leaves more room for optimisation by the compiler. Also matching 0 in the function head is more strict than x == 0 since 0.0 == 0.

PS: sometimes, if from business perspective both cases are to treat the same and not only by accident, I tend to use a guard using ===, as it better communicates indent of treating this cases the same.

dimitarvp

dimitarvp

I use both of these idioms – and others – quite often. My main heuristic is: “is the code readable?”

The only non-idiomatic Elixir in such cases would be a load of if-s. The rest is quite fine and the exact idiom should vary depending on your priorities and the situation.

Last Post!

niels_bom

niels_bom

Thanks all!

Yeah readability is definitely important.

Other things being equal (like readability) leaning towards faster code is good of course.

@NobbZ good catch of the x == 0!

Where Next?

Popular in Questions Top

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
nobody
Hi! In PHP: $_SERVER[‘SERVER_ADDR’] - in Elixir? Searched the docs for ip address and the web, no good results. Thanks!
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
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
gshaw
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...
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
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 54260 488
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
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 49266 226
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
New
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42716 114
New

We're in Beta

About us Mission Statement