****SPAM(11.2)**** [Tutor] Larger program organization

Alan Gauld alan.gauld at freenet.co.uk
Sun Feb 13 10:06:13 CET 2005


> I am curious about Bob's "Whenever you find yourself writing an if
> statement ask whether this would be better handled by subclasses."
>
> Could you explain a bit more?

One of the basic purposes of OOP is to eliminate if/switch statements
that are conditional on the type of the object being handled.

So when writing OO code every time you see an if statement you should
ask is this really part of the logic or am I habdling a special type
of processing that should be in a separate subclass.

For example, if you were writing your Node class parsing method and
you started writing code like:


if dataType == 'Some literal value'
    # extract one format of data
else
    # extract another data format

Then maybe you should have two classes and rely on
polymorphism to do the two different types of extraction.

Thus
subType = DataType()
subType.extractData()

By eliminating the if/else you potentially eliminate an
ongoing maintenance headache and this is one of the big
wins of OOP.

HTH,

Alan G.




More information about the Tutor mailing list