If you read the post title and thought “no shit, Sherlock”, then you can probably be safe in skipping this week’s post.
If you’d like to hear about another selling point of Elixir that isn’t often mentioned directly, then by all means read on.
This is probably a 2 minute read.
Object Oriented programmers are often preoccupied with avoiding conditional logic.
Take a look at this example:
I don’t think I’m the only one, but when I have to deal with even small amounts of code like this I find that my developer happiness temporarily takes a dive.
But why should it? The little chunk of pseudo code has a lot going for it:
And still, most of the developers I know wouldn’t call this “good” code. To be fair, they probably wouldn’t call it terrible either. But it would be the sort of thing that they would hope to minimize.
And that’s where a lot of brain cycles start to burn up: the conditions themselves are legitimate parts of your domain logic, and they may even be simple when considered one-at-a-time. But when expressed as a whole it somehow transforms itself into an unmaintainable morass.
If you’re clever, then you can mitigate the problem with polymorphism and other OO fare.
But those techniques exact a penalty, too: If you design things correctly, then you’ll pay with indirection. If you design things poorly, then you will still pay with indirection, but you also end up with the wrong abstractions.
As you might have guessed, this is all just a setup to demonstrate how a language with pattern matching (e.g Elixir) handles this problem:
The simplicity of this solution makes it the spiritual equivalent of this gif:
Note how pattern matching allows each condition to live on its own at the top level:
You can parse each case individually without the mental gymnastics needed to keep track of which conditional branch you’re in.
To be clear, this isn’t to say that you should avoid conditionals in Elixir. That would be absurd.
My main point is that the nastiness of nested conditionals often evaporates when using a language that supports pattern matching.
And that using Elixir often feels like bringing a gun to a knife fight.