Hacker News new | ask | show | jobs
by djm_ 3398 days ago
My first thought when I read this was that it sounds like you're struggling against the language because you're trying to use it in an imperative fashion. I'm well aware this may not be correct but it's what I'm going to assume for the rest of this comment, so please forgive me if it's incorrect.

Elixir is a functional language, and while it's not pure, nested if statements deliberately do not belong.

The if statement itself is actually just a macro to a case/switch, the only reason it is there is to reduce the verbosity in cases where only one conditional branch is needed, José felt the practicality of this outweighed the fact it may get abused.

To use Elixir properly is to try and move away from conditional constructs as much as is realistically possible. This means instead using pattern matching, guard classes and multiple clause functions (both named and anonymous).

Once you take advantage of these you'll find your code stays flatter while keeping the individual branches of your conditional logic tied up into small independently testable functions.

If anyone would like to read more, I found these [1] [2] to be helpful.

[1] http://culttt.com/2016/05/30/branching-conditionals-elixir/

[2] http://blog.lucidsimple.com/2016/01/24/pattern-matching-help...

1 comments

Agreed, the fact that you're saying you can't nest if statements is a sign that you still don't get the grasp of functional programming.

cases, conds, with statements, recursion, pattern matching...