Help With Better Design
Steve Howell
showell30 at yahoo.com
Tue Jun 19 21:45:53 EDT 2007
--- apollonius2 at gmail.com wrote:
>
> ON = "ON"
> OFF = "OFF"
>
> class LightBulb:
> def __init__(self, initial_state):
> self.state = initial_state
>
> def TurnOn(self):
> if self.state == OFF:
> self.state = ON
> else:
> print "The Bulb Is Already ON!"
>
> def TurnOff(self):
> if self.state == ON:
> self.state = OFF
> else:
> print "The Bulb Is Aleady OFF!"
>
I've written code that looks a lot like that, and it's
a perfectly acceptable pattern IMHO. I don't bother
with the variables ON and OFF, though, as they add no
clarity to simply using 'ON' and 'OFF' for the states.
> [...]
> The test portion of the code is actually longer than
> the class
> itself :-)
That's usually a good thing! It means your code is
concise, and your tests are exhaustive. (But that
doesn't mean you can't also refactor your tests.)
____________________________________________________________________________________
Be a better Globetrotter. Get better travel answers from someone who knows. Yahoo! Answers - Check it out.
http://answers.yahoo.com/dir/?link=list&sid=396545469
More information about the Python-list
mailing list