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?