A Conversation for Python - the Programming Language
Wal Started conversation Mar 7, 2002
It is not actually Object-Oriented Programming that reduces code duplication as such, though it can help. The main thing is that it is harder to get stuck into O-O coding without doing some sort of design before hand.
In my (many years) experience of software development I have found (and observed) that it is good design that maximises code reuse, regardless of the language chosen to implement the design.
There is nothing forcing a programmer to derive the "Apple" class from the "Fruit" (abstract) class when using an O-O language, but a half-decent designer will see the obvious benefits immediately and do so.
The other big benefit which an O-O design encourages is encapsulation.
This means that you could write code to make use of the properties of my "Apple" object (colour, sweetness, size, how long before it molds, etc.) without knowing how the details are stored or processed internally. Once developers (or designers at least) get past this concept, their designs are much better, and the subsequest programs are *far* easier to maintain and support!
Niten Posted Mar 7, 2002
Hi Wal --
In principle, I agree with your comments, however I assert that while OOP doesn't reduce code duplication, necessarily, it does provide the programmer with built-in tools for maximizing reusability which will (with discipline) minimize duplication.
The most obvious of these, as you mention, is encapsulation. Good encapsulation leads to better interfaces leads to greater reusability. Good encapsulation is not a consequence of OOP; it requires effort and design. An OO language, like Python, provides apparati for expressing that design in a way that is more obvious and less cumbersome than a typical procedural or functional language.
Joe aka Arnia, Muse, Keeper, MathEd, Guru and Zen Cook (business is booming) Posted Jul 10, 2002
There are other strategies to improve code quality. The most important of these is functional programming as represented by the beautifully elegent Haskell.
The following (to use a famous example) is a quicksort on any orderable values in a list.
qsort  = 
qsort (x:xs) = qsort lt_x ++ [x] ++ qsort gt_x
where lt_x = [y | y <- xs, y < x]
gt_x = [y | y <- xs, y >= x]
Key: Complain about this post