- 2 -
2 Programming Paradigms
Object-oriented programming is a technique for programming
–
a paradigm for writing ‘‘good’’ pro-
grams for a set of problems. If the term ‘‘object-oriented programming language’’ means anything it must
mean a programming language that provides mechanisms that support the object-oriented style of program-
ming well.
There is an important distinction here. A language is said to support a style of programming if it pro-
vides facilities that makes it convenient (reasonably easy, safe, and efficient) to use that style. A language
does not support a technique if it takes exceptional effort or exceptional skill to write such programs; it
merely enables the technique to be used. For example, you can write structured programs in Fortran, write
type-secure programs in C, and use data abstraction in Modula-2, but it is unnecessarily hard to do because
these languages do not support those techniques.
Support for a paradigm comes not only in the obvious form of language facilities that allow direct use
of the paradigm, but also in the more subtle form of compile-time and/or run-time checks against uninten-
tional deviation from the paradigm. Type checking is the most obvious example of this; ambiguity detec-
tion and run-time checks can be used to extend linguistic support for paradigms. Extra-linguistic facilities
such as standard libraries and programming environments can also provide significant support for para-
digms.
A language is not necessarily better than another because it possesses a feature the other does not.
There are many example to the contrary. The important issue is not so much what features a language pos-
sesses but that the features it does possess are sufficient to support the desired programming styles in the
desired application areas:
[1] All features must be cleanly and elegantly integrated into the language.
[2] It must be possible to use features in combination to achieve solutions that would otherwise have
required extra separate features.
[3] There should be as few spurious and ‘‘special purpose’’ features as possible.
[4] A feature should be such that its implementation does not impose significant overheads on programs
that do not require it.
[5] A user need only know about the subset of the language explicitly used to write a program.
The last two principles can be summarized as ‘‘what you don’t know won’t hurt you.’’ If there are any
doubts about the usefulness of a feature it is better left out. It is much easier to add a feature to a language
than to remove or modify one that has found its way into the compilers or the literature.
I will now present some programming styles and the key language mechanisms necessary for support-
ing them. The presentation of language features is not intended to be exhaustive.
2.1 Procedural Programming
The original (and probably still the most commonly used) programming paradigm is:
Decide which procedures you want;
use the best algorithms you can find.
The focus is on the design of the processing, the algorithm needed to perform the desired computation.
Languages support this paradigm by facilities for passing arguments to functions and returning values from
functions. The literature related to this way of thinking is filled with discussion of ways of passing argu-
ments, ways of distinguishing different kinds of arguments, different kinds of functions (procedures, rou-
tines, macros, ...), etc. Fortran is the original procedural language; Algol60, Algol68, C, and Pascal are
later inventions in the same tradition.
A typical example of ‘‘good style’’ is a square root function. It neatly produces a result given an argu-
ment. To do this, it performs a well understood mathematical computation:
double sqrt(double arg)
{
// the code for calculating a square root
}
评论0
最新资源