3 ms·
While I agree switch on true might be an 'abuse' of the switch statement, I'm not sure about the last example. In Elixir, I find myself using: if x do <
by mercer 7y ago
While I agree switch on true might be an 'abuse' of the switch statement, I'm not sure about the last example.
In Elixir, I find myself using:
if x do
<blah>
else
<bleh>
end
only in situations where I'm almost certain I won't end up having more cases, and in practice I tend to limit myself even to conditionals that are short, where I can do:
if x, do: y, else: z
But in many, many situations I go for:
case x do
<condition 1> -> <stuff>
<condition 2> -> <other stuff>
end
because it looks cleaner, and because I often end up adding another condition.
Furthermore, there's also 'cond', which looks similar. using 'case' and 'cond' often just seems clearer to me over having a quite different syntax in the one situation where there's only two options ('if').
(not having to 'break' makes a big difference though)