Sunday, August 16, 2015

Programming Style

Languages features exist to provide support for programming styles. Please don’t look at an individual
language feature as a solution, but as one building brick from a varied set which can be combined
to express solutions.
The general ideals for design and programming can be expressed simply:
• Express ideas directly in code.
• Express independent ideas independently in code.
• Represent relationships among ideas directly in code.
• Combine ideas expressed in code freely – where and only where combinations make sense.
• Express simple ideas simply.
These are ideals shared by many people, but languages designed to support them can differ dramatically.

The C++ language features most directly support four programming styles:
• Procedural programming
• Data abstraction
• Object-oriented programming
• Generic programming
However, the emphasis is on the support of effective combinations of those. The best (most maintainable,
most readable, smallest, fastest, etc.) solution to most nontrivial problems tends to be one
that combines aspects of these styles.
My ideal is language facilities that can be used elegantly in combination to support a continuum
of programming styles and a wide variety of programming techniques.
• Procedural programming: This is programming focused on processing and the design of
suitable data structures. It is what C was designed to support (and Algol, and Fortran, as
well as many other languages). C++’s support comes in the form of the built-in types, operators,
statements, functions, structs, unions, etc. With minor exceptions, C is a subset of
C++. Compared to C, C++ provides further support for procedural programming in the
form of many additional language constructs and a stricter, more flexible, and more supportiv
e type system.
• Data abstraction: This is programming focused on the design of interfaces, hiding implementation
details in general and representations in particular. C++ supports concrete and
abstract classes. The facilities for defining classes with private implementation details, constructors
and destructors, and associated operations directly support this. The notion of an
abstract class provides direct support for complete data hiding.
• Object-oriented programming: This is programming focused on the design, implementation,
and use of class hierarchies. In addition to allowing the definition lattices of classes, C++
provides a variety of features for navigating class lattices and for simplifying the definition
of a class out of existing ones. Class hierarchies provide run-time polymorphism (§20.3.2,
§21.2) and encapsulation (§20.4, §20.5).
• Generic programming: This is programming focused on the design, implementation, and use
of general algorithms. Here, ‘‘general’’ means that an algorithm can be designed to accept a
wide variety of types as long as they meet the algorithm’s requirements on its arguments.
The template is C++’s main support for generic programming. Templates provide (compiletime)
parametric polymorphism.
Just about anything that increases the flexibility or efficiency of classes improves the support of all
of those styles. Thus, C++ could be (and has been) called class oriented.
Each of these styles of design and programming has contributed to the synthesis that is C++.
Focusing exclusively on one of these styles is a mistake: except for toy examples, doing so leads to
wasted development effort and suboptimal (inflexible, verbose, poorly performing, unmaintainable,
etc.) code.

No comments:

Post a Comment