Last Updated: February 25, 2016
·
857
· ewernli

Anti-if programming

Polymorphism is at the heart of object-orientation. Objects communicate by sending messages and can respond to a given message in various ways.

More than once, if statements can be avoided by leveraging polymorphism.

A proliferation of statements indicates ususally that some abstractions are lacking in the code.
To refactor, identify missing abstractions and turn them into objects.

You might for instance introduce objects that abstract over context, strategies, or nullity; or compose behavior dynamically with decarators, filters, or chain of responsability.

If statements can differ in nature and not all if statements are bad, though. Certain piece of code (i.e. algorithm) are best implemented in a procedural way with conditionals.

But most of if statements are bad. Being suspicous about them is a good habit.