Chuck Allison is a regular columnist with CUJ and a software architect for the Family History Department of the Church of Jesus Christ of Latter Day Saints Church Headquarters in Salt Lake City. He has a B.S. and M.S. in mathematics, has been programming since 1975, and has been teaching and developing in C since 1984. His current interest is object-oriented technology and education. He is a member of X3Jl6, the ANSI C++ Standards Committee. Chuck can be reached on the Internet at allison@decus.org, or at (801)240-4510.
The Elements of C Programming Style gives advice to C programmers on how to create good programs. It contains tips, rules of thumb and sample programs that illustrate principles and techniques that take years to master. The authors hope to provide readers a short-cut on their journey to programming maturity and productivity.
Audience
The authors tried to put something for just about everyone into this book. There are specific rules and conventions for the novice. The intermediate programmer will likely gain a deeper understanding of the philosophy of C, and may have occasion to unlearn some bad habits. Experts will have an opportunity to compare their opinions with the authors', and maybe alter their own style just a little. The authors assume familiarity with basic C syntax and program structure.
Content
The book contains five main parts:
Each part has several chapters and each chapter contains a set of rules or principles to program by. Each rule is stated concisely, then followed by a discussion. With rare exception, each rule and its discussion occupy only one or two pages, making for easy reference.
- Part I The Project Cycle
- Part II General Programming Advice
- Part II C Usage
- Part IV C Review
- Part V Miscellaneous
Part I discusses the concepts of design, testing, debugging, and optimizing that apply to any programming language. Part II covers visual organization, commenting, data objects, numeric considerations, and input and output. Part III explores the particulars of C usage, including expressions and statements, functions, arrays and pointers, and the preprocessor. Part IV examines each operator in the language and points out key features of the Standard C Library. Part V has an unusual chapter "Coding for non-C Programmers." This chapter doesn't quite fit the book's motif. It attempts to gently introduce non-C programmers to the language by soft-pedal-ling the "hard-core" use of C that the rest of the book encourages. The very next chapter swings the pendulum to the other extreme with idioms that are unique to C.
Commentary
There is much good advice in this book. I especially liked the fact that the authors do not apologize for C. (It has a bad rap in many quarters: terse, dangerous, unmaintainable, etc.) In their own words:"The C language, like many other notations (mathematical, musical, etc.), is difficult for a beginner or outsider to understand. Once mastered, however, it is easier to read than equivalent text. Readability should therefore be measured not by how similar the program is to text, but rather how effectively the notation is used to express the operations clearly.
"It is a key concept of this book that this notation (the C language) should be mastered and exploited."
The authors demonstrate that although C has its quirks, it is a language that works.
I have very few nits to pick with this book. In one case (section 17.8), the text mentions that a signal handler should only write to variables of type volatile sig_atomic_t, but the sample code leaves off the volatile keyword. The authors also seem to think that wide characters and multibyte characters are the same thing (which they are not). Two nits in 340 pages ain't bad.
Since this is book on style, it treads on subjective ground. What is elegant or aesthetically-pleasing to one may be unclear or offensive to another. For example, they prefer this
while (condition_0) { statement_1; statement_2; if (!condition_1) continue; statement_3; statement_4; if (!condition_2) continue; statement_5; statement_6; }to this
while (condition_0) { statement_1; statement_2; if (condition_1) { statement_3; statement_4; if (condition_2) { statement_5; statement_6; } } }They also think
for (i=1, e=t=1.0; i<=n; t/=++i) e += t;is clear. However, many find
e = t = 1.0; for (i = 0; i <= n; ++i) { e += t; t /= (i + 1); }is clearer.But this brings us to the most important point of all. There is no need for a "one-true-style" approach to coding. What is important is consistency. I like the way the authors say it:
"While some disagreement is inevitable with a book of this sort, we hope that the expert will not condemn this book if only a few rules conflict with his or her opinions...If you disagree with a rule, read the discussion. If you still disagree with the rule, do not use it. Every programmer has personal preferences...Rules should be applied consistently, but not slavishly. A good example is the structured programming approach. Strict structured programming...prohibits the use of goto. Strict adherence to structured programming results, in general, in good code. In some cases, however, very ugly code can be generated by blind adherence to some of its rules. (For example, not allowing more than one exit point per function can create complicated situations). Consistency is very important. Blind obedience is not."
While the claim on the back cover, that this is "the first book offering clear and direct rules for good C programming style," is stretching things a bit (I think Tom Plum beat them to it in the eighties with C Programming Guidelines as well as every other Plum-Hall publication), it is true that not many books on C programming style have been written. The authors identify their book as being in the spirit of Strunk and White's The Elements of Style, and The Elements of Programming Style by Kernighan and Plauger. (It wants to be sequel to the latter.) Since both of those books are about one-fifth the size of this book, they have produced a work of a different genre (should I say style?). But what they lose in length they make up in completeness. There is a place for The Elements of C Programming Style on the C practitioner's bookshelf.
Title: The Elements of C Programming Style
Author: Jay Ranade and Alan Nash
Publisher: McGraw-Hill,
11 West 19th Street,
New York, NY 10011,
1-800-822-8158,
FAX 212-337-4092
Price: $29.95
ISBN: 0-07-051278-7