Book Reviews


C Elements of Style

reviewed by Dwayne Phillips


Dwayne Phillips works as a computer and electronics engineer with the U.S. Department of Defense. He has a PhD in Electrical and Computer Engineering from Louisiana State University. His interests include computer vision, artificial intelligence, software engineering, and programming languages.

Steve Oualline's C Elements of Style is, as its name suggests, a style manual for C programming. Qualline outlines a method for writing clear and decipherable C code. He emphasizes a simple and straightforward style. In his words, this book is for programmers "who want their programs to be easily read and maintained by others."

Audience

You do not need to be a C wizard to understand this book. In fact, wizards who write statements like:

*destination++ = *source++
may not like this book. Oualline advocates the more accessible:

*destination = *source; destination++; source++;
If you write programs that must be corrected or augmented by others, this book will be of interest to you. It is especially appropriate for relatively new programmers who are struggling to learn good C programming habits.

Contents

C Elements of Style has nine chapters, a compact style manual, two appendices, and an index. The chapters cover the topics expected in this type of book. They include (1) style and program organization, (2) file basics, comments, and program headings, (3) variable names, (4) statement formatting, (5) statement details, (6) the preprocessor, (7) C++ style, (8) directory organization and makefile style, and (9) user-friendly programming.

For much of the book, Oualline gives examples of good and bad code, and summarizes with style rules. His rules are simple and easy to apply. Some of the rules apply to the process of writing code. For example, "Comment your code as you write it." (This rule, he says, saves time in the long run.) Other rules address the code itself: "Constant names are all upper case" and "Follow every variable declaration with a comment that defines it." Some rules just make a programmer's life easier: "Assume that *,/, and % come before + and -. Put parentheses around everything else."

The style rules are aimed at making programs readable and reliable. Good variable and subroutine names, which help make the code explain itself, and good comments, which help reveal the organization of programs, enhance readability. Oualline strives to increase reliability by limiting the code to safe subsets of the C language. (Not allowing shortcuts such as *destination++ = *source++ reduces the risk of problematic side effects.) Oualline describes this emphasis on readable and reliable code as "defensive programming" or "doing a lot of thinking so I don't have to do a lot of thinking."

The chapter on makefile style is outstanding and to my knowledge unique. Makefiles are essential to C programming and are often the hardest part of a project to understand. Other books describe how to use makefiles, but this is the only book I have seen that tells how to organize and format them. Oualline formally defines many makefile items that have become pseudo standards over the years. This material may seem trivial to an experienced UNIX C programmer, but many C programmers today have never worked on a UNIX system (believe it or not).

The chapter on user-friendly programming is a bit out of place, but useful. It discusses how to write programs that users will actually use. (It includes the Law of Least Astonishment: The program should act in a way that least astonishes the user.)

The style manual is 35 pages of rules without all the discussion. For those short on time, this book-within-a-book provides a concise summary of the text.

The first appendix shows three complete code examples (two in C, one in C++) that employ the style rules given in the body of the book. These examples show Oualline's recommendations in practice. The second appendix lists all the rules given in the chapters. (Take these seven pages and pin them on the wall next to the coffee machine.)

Other Style Manuals

For years, the only programming style manual available was Kernighan and Plauger's thin little classic The Elements of Programming Style [1]. Fortunately, several style manuals have appeared in the recent past — each written with a different point of view.

Plum's C Programming Guidelines [2] and C++ Programming Guidelines [3] (reviewed in The C Users Journal, January 1993) are comprehensive references on programming standards and style. They are written in a compact, subroutine-like style that is appropriate for reference books. These books are technical and not for the novice.

Steve McConnel's Code Complete [4] is a comprehensive handbook for programmers. As such, it includes plenty of good advice on programming style, but its style sections are scattered throughout the 800-plus pages.

While Oualline favors code that anyone can understand, Ranade and Nash's The Elements of C Programming Style [5] (reviewed in The C Users Journal, July 1993) encourages programmers to master and exploit the notation of the C language. (Ranade and Nash's do recommend backing away from terse C style if the audience includes programmers who have a limited knowledge of C.)

Conclusion

Oualline's book is the closest to Kernighan and Plauger's and it serves as a worthy update to that earlier classic. It is a short, yet complete treatment of C style.

C Elements of Style is a deserving addition to the desk top — not just the bookshelf.

References

[1] The Elements of Programming Style, second edition, Brian W. Kernighan, P.J. Plauger, McGraw Hill, New York, New York, 1978, ISBN 0-07-034207-5.

[2] C Programming Guidelines, second edition, Thomas Plum, Plum Hall Inc., ISBN 0-911537-07-4.

[3] C++ Programming Guidelines, Thomas Plum and Dan Saks, Plum Hall Inc., ISBN 0-911537-10-4.

[4] Code Complete, A Practical Handbook of Software Construction, Steve McConnel, Microsoft Press, One Microsoft Way, Redmond, Wash. 98052-6399, ISBN 1-55615-484-4.

[5] The Elements of C Programming Style, Jay Ranade and Alan Nash, McGraw Hill, New York, New York, 1993, ISBN 007-051278-7.

Title: C Elements of Style The Programmer's Style Manual for Elegant C and C++ Programs
Author: Steve Oualline
Publisher: M&T Books
411 Borel Ave, Suite 100
San Mateo, CA 94402
1992
Price: $21.95
Pages: 265
ISBN: 1-55851-291-8