If you use an ‘if’ you deserve to suffer
How’s this for an extreme opinion:
If you use an ‘if’ you deserve to suffer at Mark Needham
I actually agree with some of his conclusions, although I am surprised that he does not suggest the simple approach of extracting a method to eliminate the potential confusion of multiple statements depending on an “if”. For example, replace:
class Whatever { public void thing() { if(thisHappens()) { doThis(); doThat(); } } }
with
class Whatever { private void thisAndThat() { doThis(); doThat(); } public void thing() { if (thisHappens()) thisAndThat(); } }
Seems less trouble than a forced polymorphism approach, at least until polymorphism and the strategy pattern is actually needed.
Post a Comment