19.13 On the Use of Programming Paradigms

With the inclusion of object oriented techniques in the repertoire, the programmer has the ability to produce code in a number of styles. It would be a good idea to ask when a given style of programming is appropriate, and when a different one should be used. Here are a few suggestions:

Monolithic style:
All the code is in a single compilation unit, and there are few or no procedures.

Advantages: There is less writing to do because there are few or no imports; the style is simple; and the program may link and run rather quickly without the overhead of libraries and procedure calls. All the code is available to the programmer for debugging.

Disadvantages: This method does not scale well even to medium sized programs, much less to large ones. Debugging time goes up dramatically with the length of the single program. All code has to be produced by the writer for each program.

When to use: Use for one-of-a-kind simple throw-away programs to do a calculation or simple data manipulation, where reusability is not a concern, and the program is less than a hundred lines.

Modular style:
Code is grouped into procedures and modules according to its type relationship and/or functionality.

Advantages: Modular design scales well even to very large programs. When used to hide data intelligently, static modules are a good tool to factor code into manageable packets that can be designed and debugged quickly and efficiently. This method can be used for the vast majority of programming needs. Existing libraries can be imported, and new ones written that may, if well designed, be reusable.

Disadvantages: Static modules in themselves do not well serve the need for dynamic data. Pointers used for dynamic allocation are notorious for being error prone, even when encapsulated into libraries. Some code has to be rewritten many times for just minor changes in data.

When to use: Use for programs of any size or complexity, from hundreds to millions of lines of code.

Generic style:
Some code is parameterized, particularly as to a data type.

Advantages: This method extends the idea of static modules to solve a particular kind of problem. Code for common structures such as lists or routines such as sorts can be written once, and then easily specialized for a particular type of data.

Disadvantages: This is still a static method. It does not address the problems associated with the safety of dynamic data.

When to use: Use in those cases where a data type such as a list has all its code common regardless of the type of data going into the structure, or when a routine such as a sort has common code regardless of what data is being sorted. Generic templates can be refined for the particular types with a minimum of effort.

Object Oriented style:
The core of the program's focus is on classes of data; actions are secondary to data.

Advantages: This method allows for a different view of problems that are data-centric. Like modular and generic techniques, it promotes the reusability of code by handing off responsibility for some actions to other data classes. If traced classes are used, many problems with pointers vanish, as memory does not need to be manually managed.

Disadvantages: Object oriented programming has a greater overhead in planning, and its code runs slower than conventional procedure calls. If care is not taken in the management of objects, the tangle of data relationships can produce a kind of spaghetti approach to problem solving that rivals that found in languages employing GOTO for transfer of control

When to use: Use in those cases where there are related classes of data that lend themselves naturally to a class hierarchy, and/or as a means to avoid the problems of dynamic data associated with pointers. Object oriented programming combines well with generic approaches, and the two are often used together.


Contents