Someone rip this idea apart:
When you write an
if
statement in one class that's using data from an object of a different class to make a decision, ask yourself:
- Would this code be more appropriate in the other object?
- Would it be better to introduce a new object, whose purpose it is to do this?
Thoughts appreciated.
Hey! Why don't you make your life easier and subscribe to the full post
or short blurb RSS feed? I'm so confident you'll love my smelly pasta plate
wisdom that I'm offering a no-strings-attached, lifetime money back guarantee!
Leave a comment
If OO design was so simple, languages would just remove the "if statement" e.g.
if (salary < 50000)
taxRate= 0;
else
taxRate = 20;
I can complicate the above "if" statement by adding different tax rates and tax percentages. I don't think OO design is just about removing if statements.
Posted by Vijay Nathani
on Feb 22, 2012 at 08:48 AM UTC - 6 hrs
No, of course there is more to it than if statements.
But would these two questions help lead to a better (as opposed to perfect) design?
Posted by
Sammy Larbi
on Feb 22, 2012 at 09:03 AM UTC - 6 hrs
I think the questions are good. I also use them:-)
Other questions or thoughts: SOLID principles and so on, all the nice ideas about software development...
Posted by Thomas Eichberger
on Feb 22, 2012 at 10:37 AM UTC - 6 hrs
I think its a good start of questions to get ideas flowing to the next logical steps...whatever that may be depending on the usage of the class. It could be trying to decide how the new object is introduced. For example, should I use inheritance or composition. OOP design seems confusing from overall perspective but asking simple questions help give a starting point.
Posted by
Keith H
on Feb 22, 2012 at 12:49 PM UTC - 6 hrs
The reek gem for ruby holds to this, as does The Anti If Campaign www.antiifcampaign.com, and it agrees with "tell, don't ask" (search for the article by the Pragmatic Programmers). People can't cope with too many nested if statements. All that is clear. So why is it so basic to human languages? I suppose the functional programming approach of template matching is pretty much all ifs, a collection of implicit case statements. So you do have to ask. The answer won't always yes to one or both. I suppose at some point browsing the class tree gets more difficult than browsing the code.
Posted by hgs
on Feb 22, 2012 at 12:57 PM UTC - 6 hrs
Leave a comment