[Tutor] Simple code (was: Dumb Subclassing question)
Kent Johnson
kent_johnson at skillsoft.com
Fri Aug 6 03:31:55 CEST 2004
>On a final note, it should be realised that OOP does not
>automatically imply writing less code. In fact for short programs
>it very often means more code, but hopefully the result is both
>more maintainable and more robust and flexible.
I think good programming practice often does imply writing less code, for
two reasons. One is the DRY principle - Don't Repeat Yourself. In other
words don't copy/paste identical sections of code. Don't duplicate data.
Look for the common bits and factor them out into shared functions or
classes or configuration files.
Second is YAGNI - You Aren't Gonna Need It -- which is closely related to
"Do the simplest thing that could possibly work." These two principles of
Extreme Programming are generally useful. The idea is, don't write code
that you don't need today. Don't complicate today's design to accommodate
tomorrow's anticipated needs, because when tomorrow comes there will often
be something else that is more important.
One of the great things about Python is that you can write short programs
without cluttering them up with needless OO complexity. A really simple
program might start out as straight-line code without even any functions.
As it gets more complicated you break it up into functions to keep it
readable or to reuse bits of code. When you have several functions that are
sharing common state variables, or when you need to duplicate some state
variables, you introduce a class or two and maybe break the program up into
multiple modules.
I'm all in favor of maintainable and robust code. I generally try to omit
the flexibility until I actually need it. Simple is good.
Kent
More information about the Tutor
mailing list