Questions about app design - OOP with python classes

Steven D'Aprano steve at REMOVEME.cybersource.com.au
Thu Mar 1 23:14:11 EST 2007


On Thu, 01 Mar 2007 21:45:55 +0100, Bruno Desthuilliers wrote:

> As a side note : hungarian notation is usually considered bad form here. 
> Look here for usual naming conventions:
> http://www.python.org/dev/peps/pep-0008/

Which Hungarian notation do you mean?

If you mean the Windows "Systems Hungarian", where the prefixes on
variable names is just an underpowered, manual and redundant type system,
then you're right.

But if you mean the original "Apps Hungarian", where prefixes are used to
indicate semantic KINDS of data (e.g. distances in inches versus distances
in millimetres) then I think Joel Spolsky makes a good defence of Apps
Hungarian.

http://www.joelonsoftware.com/articles/Wrong.html

Some years ago, an expensive Mars lander crashed into the planet because
somebody mistakenly compared inches to millimetres. They had done
something conceptually like this:


# we use Systems Hungarian, or a type system that knows about floats
# get the distance to the surface of the planet in millimeters
fpCurrentHeight = radar_unit.get_height_above_planet()  # a float
# 
# ... many lines of code ...
# 
# get the critical height in inches at which we need to engage
# the retro-rockets
fpCriticalHeight = calculate_height(speed, fuel, mass) # also a float
#
# ... many more lines of code ...
#
if fpCurrentHeight <= fpCriticalHeight:
    # safe because both objects are floats
    engage_the_retro_rockets()


A type system doesn't help. So what if they're both floats? The test
is still bogus, your code will still wait too long to engage the
retro-rockets, and the billion dollar space craft will still be travelling
at hundreds of miles an hour when it reaches the surface of Mars.

Oops.

But if you used Apps Hungarian, and saw this line of code:

if hmmCurrentHeight <= hinCriticalHeight:

then you should instantly recognise that there's a problem. Comparing
a height in millimetres to a height in inches is not a good thing to do,
no matter that they're both floats.



-- 
Steven D'Aprano 




More information about the Python-list mailing list