While this might appear as a post that is “doing things different to do things different”. This article has a more subversive intent, which is to subtly promote functional design over imperative design.
This is just one in a series of articles where I try not to overload readers with complicated laws of functional programming. Instead I try to suggest techniques they can readily use in their code today.
There are fundamental differences between using an if statement when compared to a ternary expression.
This is just a very small part of a larger journey into functional design. I have outlined some differences between the if and ternary here:
The if statement though is much more readable for complex stuff.
One thing I have noticed about if statements, is that it allows unnecessary complexity to creep into your code. If you cannot fit your logic into a ternary and require an if statement, that would suggest the code needs to be broken down further.
One this code is properly broken down, it will look like a natural fit for a ternary.
Eliminating if statements will force you to write better code.
I would also recommend this great article by Charles Scalfani
Cheers!
