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?
Trending in Discussions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #blog-post
- #elixir-ls
- #ai
- #elixirconf-us
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming










Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
stefanchrobot
I think that
ifworks great for boolean conditions and it’s often used in the Elixir’s codebase. I’d say thatcasewith justtrue/falsefeels weird.eksperimental
I pretty much feel the same way you do.
I think
if/2is a good fit when you have:ifs under it,Other than that I prefer to use
caseandcond.condis particularly useful for replacing nestedifs orif/elseifs.travisf
Thanks for the responses. I was worried I was overusing it from a style perspective but this all makes sense.
pierrelegall
I experienced this in a Credo report today:
Not convinced by this rules
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-elseandcase. So I did this:And the runtime was 655 ms, better than 25% solutions.
I tried using
ifinstead, 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-elseis definitely faster.D4no0
Yeah absolutely doesn’t make any sense.
caseis an erlang construct that is considered a core operation, whileifis a syntactic sugar (AKA macro) that usescaseunder the hood, you can inspect the source code yourself.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
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
casewith true/false andif.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
My assumption is this is a compile-time optimization, for example you have the following case:
This statement can be evaluated at compile-time, hence there is no need to execute the
casestatement.In your case, if that is the case, then you literally can optimize
ifout of your codebase, so it makes no sense in benchmarkingifvscase.