****SPAM(7.4)**** Re: ****SPAM(11.2)**** [Tutor] Larger program organization

Bob Gailer bgailer at alum.rpi.edu
Sun Feb 13 16:13:19 CET 2005


At 03:21 PM 2/12/2005, Brian van den Broek wrote:
[snip]
 > I am curious about Bob's "Whenever you find yourself writing
 > an if statement ask whether this would be better handled by subclasses."

I start out writing a class like:

class A:
   def __init__(self, type):
     self.type = type
...
   def foo(self, ...):
     if self.type = 1:
       statements to process object of type 1
     else:
       statements to process object of type 2

The '"if statement" alerts me to consider creating subclasses for types 1 
and 2:

class A:
...
class A1(A);
   def foo(self, ...):
     statements to process object of type 1
class A2(A);
   def foo(self, ...):
     statements to process object of type 2

That takes less code. Eliminates the type property. I get greater 
visibility about the existence and distinction of the two (or more) 
sub-types. I now can much more easily extend each subtype.

Bob Gailer
mailto:bgailer at alum.rpi.edu
303 442 2625 home
720 938 2625 cell 



More information about the Tutor mailing list