Skip to content

re: To IF Or Not To IF—That’s The Conditional Statement VIEW POST


I really like this take, but I also really disagree with it.

Surely we use if-statements in real life all the time?

If your grade is too low, you will fail the class.
If you see something, say something.
If you want to go to Disney world, you should pick up the trash and clean up your room.
If you want to go to the movies, the new Avengers flick runs at eight.

Well, maybe that last one has its own linguistical/logical pitfalls. But in these cases, where there is a boolean condition and a command to execute, the most appropriate flow control is just the humble if-statement, right?

I just did a search on if ( and switch ( in the codebase of my current project, and we use 4500 if-statements and 300 switch-statements in 70k lines of code. Maybe I'm just afraid of the consequences if I accept this gospel as truth.


This gospel was never meant to be accepted as a holy truth :)

I explicitly stated in the preamble and repeated this in the post scriptum: this is not a golden rule, but I use it as a golden rule to prevent producing spaghetti when there is a better abstraction.

Also, all your examples are perfectly valid and I probably should have mentioned it explicitly, the evil is to mentally accept “branched ifs,” postfix conditions like “this if that” are indeed mostly unavoidable, but they are also clean and maintainable.

The main goal of this writing was to enforce those who is engaged to think about possible better abstractions before spitting the nested if out. The latter always seems easier, but usually, it has terrific consequences in maintenance/support.

code of conduct - report abuse