Since the beginning of our programming career, whether that takes root in self-taught evenings and weekends in dusty basements or messy atticsNaturally everybody reading here, or in boring school/university lessons, we have all started coding from the same few basic steps… and the if-else conditional statement is one of them
We all know its basic structure as well as its meaning
And we know that the else part can be used like this or it can be chained with an if to make else if (condition). Unfortunately both uses open up more traps than benefits, and I’ll spend the next few minutes trying to show you what I mean
Let’s imagine to have, somehwere hidden in our code, a function like this one:
Pretty messy isn’t it? And this is just the tip of the iceberg, think about how would it look if we had checks regarding other parameters and cross conditions with them… pure hell
That is why in my projects I got rid of the if-else satement, for both clarity of mind and of code (not only for myself but for whomever will get to read my code in the future)
And how would it look this structure without any else? here it is
Removing the else from our first method we achieved a cleared structure, smaller methods with only one responsibility, easier to test and decoupled :)
Leave a Comment