There is a website (called the Anti-if campaign) that talks about how to replace big nested if-statements with something more maintainable (through the use of a ‘bond class’ see example). I understand the concept, but I don’t know how to implement it (even with the example).
Question:
May I get a concrete example of how to implement this (ie, the bond class)? – it has been bugging me for some time now. Java is my preference, but any examples are welcome.
note
I have found one more article on this in stack overflow here
This special technique to avoid if-else-if constructs like shown in the example on that campaigns website can be found in in Clean Code by Robert C. Martin. If you follow his basic advices, you don’t need to join the campaign 😉
The example at the referenced page looks dramatic mainly because the code is poorly formatted. This is how it should look like:
The recommendation is to replace the condition with polymorphism, which isn’t even the best ideas. A better design is to use composition:
Here we got rid of the conditions and delegated the calculation to a new type class.