Tuesday, December 30, 2008

The problem with OO programming languages

In Object Oriented Programming we extend a class by specializing it with a subclass. Going from the General to the specific. This seems to be the excepted way things should be. But it seems backward from the way things work in the real world.

In the real world primitive man had the whole numbers. Eventually problems were encountered that the whole numbers could not solve, so the integers were introduces. Later integers were found to insufficient to solve some problems, so rational numbers were developed. Eventually it was necessary to extend the rational numbers with the real numbers. Much latter rational numbers were extended to the complex numbers. Each extension required creating a super class of the existing class, not a subclass. I am hard pressed to think of a single field of endeavor where a breakthrough was made by specializing an existing concept to make a subclass of an existing class, it always seems to be the other way around.

The other problem is that the subclasses must carry all the baggage of the super classes. Yet in the real world specialized subclasses are less complex than the general super class, not more complex. Consider the classic shapes example, we will consider the specializations of a quadrilateral.

To specify a quadrilateral, we must specify the lengths of all four sides and two of the angles.

We derive a parallelogram from the quadrilateral. Now all we need to specify are the lengths of two sides and one angle, only half as much information. But in most OO languages (Ocaml being the only exception I can think of), we need to carry around all the data needed by the super class, a big waist, but it gets worse.

If we now derive a rectangle (a parallelogram the right angles) and a rhombus (a parallelogram with equal sides) from the parallelogram. The parallelogram requires three data points, but the rectangle and the rhombus each require only two, the lengths of two sides in the case of the rectangle and the length of a side and one angle in the case of the rhombus.

A square is both a rectangle and a rhombus (the dreaded diamond), this is the definition. Only one data point is required to define a square, It is simpler than either the rectangle or the rhombus, so why should it carry the overhead of both?

It seems to me the the existing OO programming concept of going from general to specific is backward. We should be going from the specific to the general in most cases.

Labels:

0 Comments:

Post a Comment

<< Home