travisf

travisf

I’m curious what the consensus (if there is one) on case vs if statements for conditionals is. Specifically, in cases where you are only dealing with boolean options for example not is_nil(value).

Personally, when I write Elixir I find that I reach for case way, way more than if even when if would work fine. I think I do it because I don’t always know all the potential conditionals when I start writing the function, I like the sort of open ended nature of case as one can add other cases over time. I also just find something visually unappealing about if statements compared to case when I read Elixir code.

I’m not totally against if I use it from time to time when I know that there will only ever be two conditions or when I can do a single line: if foo, do: func(foo), else: func(bar).

Is this an anti-pattern is there a good reason why I should start using if more regularly?

Showing Posts 1 to 10

stefanchrobot

stefanchrobot

I think that if works great for boolean conditions and it’s often used in the Elixir’s codebase. I’d say that case with just true/false feels weird.

eksperimental

eksperimental

I pretty much feel the same way you do.

I think if/2 is a good fit when you have:

  • no other nested ifs under it,
  • when you check for truthy/false values.

Other than that I prefer to use case and cond. cond is particularly useful for replacing nested ifs or if/elseifs.

travisf

travisf OP

Thanks for the responses. I was worried I was overusing it from a style perspective but this all makes sense.

pierrelegall

pierrelegall

I experienced this in a Credo report today:

Refactoring opportunities
┃ [F] → Cond statements should contain at least two conditions besides `true`,
┃           consider using `if`  instead.

Not convinced by this rules :confused:

t0nghe

t0nghe

I was doing LeetCode and was solving a problem by reducing an enumerable, where the reducer does different things to the accumulator based on whether a condition is true or false.

I didn’t think much about the difference between if-else and case. So I did this:

case MapSet.member?(acc, item) do
  true -> {:halt, true}
  false -> {:cont, MapSet.put(acc, item)}
end

And the runtime was 655 ms, better than 25% solutions.

I tried using if instead, the runtime was 525 ms, that is 100 ms shorter and better than 100% solutions.

In conclusion, if you are only dealing with true or false, if-else is definitely faster.

D4no0

D4no0

Yeah absolutely doesn’t make any sense. case is an erlang construct that is considered a core operation, while if is a syntactic sugar (AKA macro) that uses case under the hood, you can inspect the source code yourself.

ityonemo

ityonemo

More than that if does more work: it’s checking falsiness.

But I still use if. Usually your conditionals are not bottlenecks and it’s easier to read.

D4no0

D4no0

Absolutely, however I would strongly recommend to read this topic: https://forum.elixirforum.com/t/my-thoughts-on-the-if-statement/61985 .

There is a clear intent difference between using case with true/false and if.

t0nghe

t0nghe

Thanks for sharing! I didn’t know that until now. (This is mental!)

I’m reading the source code and saw optimize_boolean, tbh I have zero idea what this is. Is it this what makes if slightly faster?

D4no0

D4no0

My assumption is this is a compile-time optimization, for example you have the following case:

a = true

if a == true do
...
end

This statement can be evaluated at compile-time, hence there is no need to execute the case statement.

In your case, if that is the case, then you literally can optimize if out of your codebase, so it makes no sense in benchmarking if vs case.

Where Next? Top

Trending in Discussions Top

cblavier
Hey there, It’s been more than a year since we started using LiveView as our main UI library and building a whole library of UI componen...
New
wmnnd
Hey folks, I wanted to share with you a project I’ve been working on for a while: It’s an Open Source (AGPLv3) newsletter tool. Think Ma...
New
axelson
Hi there! :wave: @frigidcode and I (but mostly him) have been running an Elixir Book club, we’re almost done with Designing Elixir Syste...
New
alexslade
Fly’s CEO posted this recently - Turn And Face The Strange · The Fly Blog It says that Fly is going all-in on sprites, which is a worry ...
New
sntran
In my third year of college, around 2010, I found Erlang. A few years later, I found Elixir. I liked the ideas at once: many small proces...
New
Ganna
Hi everyone, I have 8 years of production experience with Erlang/OTP and I’m just starting to get to know Elixir. What would you learn f...
New
gtcode
I’ve been tinkering with Lynx (by José) via hold_ledger. I used AI to write the code, a separate specification, the tests, and the Lean p...
New

Other Trending Topics Top

dominicletz
Hey there, I haven’t seen a list of useful Elixir specific skills for the popular AI tools yet so I’m starting one here with my first. ...
New
mudasobwa
I fully migrated to my own harness from Anthropic/Gemini and I think it’s time to share it. Welcome DSH, the DeepSeek Harness, fully writ...
New
KristerV
Hey. Is there anyone here who creates agents in their apps? Not talking about using agents, but creating them. I’m finding it pretty diff...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New
OndrejValenta
(I just needed to vent somewhere and LinkedIn is full of hope, or hype, I’m not sure which exactly) AI dream has many faces, but general...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews