Sunday, April 26, 2009

OOP Gotchas...

I was reading a book, "Learning Python", from Mark Lutz, and although a different topic from usual projects at work, I found one particular comment pretty interesting, and serve all languages in general, IMHO:

I once worked in a C++ shop with thousands of classes (some machine-generated), and up to 15 levels of inheritance. Deciphering method calls in a such complex system was often a monumental task: multiple classes had to be consulted for even the most basic operations. In fact, the logic of the system was so deeply wrapped that understanding a piece of code in some cases required days of wading through related files.

The most general rule of thumb of Python programming applies here, too: don't make things complicated unless they truly must be. Wrapping your code in multiples layers of classes to the point of incomprehensibility is always a bad idea. Abstraction is the basis of polymorphism and encapsulation, and it can be a very effective tool when used well. However, you'll simplify debugging and aid maintainability if you make you class interfaces intuitive, avoid making your code overly abstract, and keep your class hierarchies short and flat unless there is a good reason to do otherwise.
Nice words to remind us of the KISS principle. http://lh4.ggpht.com/_HO2u4R9azX8/Sdiirg5JhAI/AAAAAAAAAik/CuhUaBPD3T4/LOL+Animated.gif

0 comments: