The author 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 interest include computer vision, artificial intelligence, software engineering, and programming languages.
C Programming Guidelines and C++ Programming Guidelines are comprehensive style manuals that list specific standards for all aspects of C and C++ programming. They are good books for any group of programmers trying to work together to produce consistently high-quality, portable software. C Programming Guidelines covers standard ANSI C while C++ Programming Guidelines covers general C++. C++ Programming Guidelines repeats large parts of C Programming Guidelines, but it covers C++ specifics and has excellent material concerning migrating from C to C++.
Purpose
These books list coding standards, that is, rules governing the details of how to write source code. C and C++ are flexible languages and programmers can write correct, working programs using a wide variety of "styles" (including the obfuscated style). A coding standard offers rules and guidelines that help different developers to produce programs in the same style. The authors give three primary reasons for using a coding standard: reliability, readability/maintainability, and portability. If programmers follow a sound coding standard, the result will be more reliable, readable, and portable software. Some programmers argue that while noble, these attributes ruin program speed and efficiency. Although a legitimate argument in some special situations, long-term performance and monetary costs support the authors' reasons for advocating a coding standard.
Audience
Most C and C++ programmers could use these books. For example, if you are starting a software company, these books could aid your quality control program. In a university research lab, these books could help standardize the work of the beginners. A government employee wanting to specify software standards to a contractor could simply hand these over. A programmer working alone could use these to minimize the tendency to drift into a personal programming style.The publisher offers machine-readable versions of both books. This enables you to place them online as ready references or fold them into your group's own coding standard.
Organization
Because both books are references, the they are organized along similar lines. The chapters contain one- to ten-page sections with the majority being two pages long. These sections resemble man pages on UNIX systems. Each section has subsections titled STANDARD or GUIDELINE, JUSTIFICATION, EXAMPLE, ALTERNATIVES, and REFERENCES. The authors use STANDARDs as rules that you must follow while GUIDELINEs are suggestions.The subsections contain short, direct paragraphs about the subject at hand that resemble cohesive subroutines. This style of writing is appropriate for such reference books. These are not the types of books you cuddle up with in front of the fire and read from cover to cover. Nevertheless, the authors do an excellent job of tying all the sections together around the three primary reasons for using a coding standard.
Sometimes the authors are too efficient. They often refer to other books published by Plum Hall for details. I prefer reading the information in the book at hand over holding my place with one finger while thumbing through a different book.
The programming guidelines in the books fall into one of two categories. The first category comprises syntax rules (use of tabs, spaces, lines per function, lines per file). The second category comprises style rules (functional cohesiveness, functions per file, program and problem structure). This may seem to be a lengthy statement of the subject since most books on programming devote only one chapter to programming style. The subject, however, is more complicated than most people realize and does require one book per language.
One criticism of the text is a shortage of code examples. The authors do give code examples, but not in every section. Programmers sometimes do not translate words into action well. They do, however, understand code and I wish the these books had more source code.
Highlights
There are several sections in each deserving special attention.
C Programming Guidelines
In C Programming Guidelines, the author discusses byte order and how this affects program portability. When you venture outside the PC world, you find the different machines store numbers with a different byte order. This is especially true with floating-point numbers. C Programming Guidelines suggests converting data to a canonical form before storing to disk and converting back immediately after reading from disk.One section discusses the different methods of placing braces ({}) in C programs. The author gives three different methods with many examples. This may sound trivial, but the author claims this is a highly-charged issue with most programmers.
Another section states how to use a local standard header for a project to improve portability. I have been the victim of this mistake. Someone once gave me a program that had 40 C files and each started with #include /usr/users/dave/all.h. When we moved the program to a new environment we had to replace each of these with #include all.h. This may sound trivial, but it can be very time-consuming.
Plum advocates the use of code reviews to ensure correctness and portability. Plum describes a "first-order correctness review" that produces a list of test cases for the software. You use these for first-order correctness testing and later for regression testing.
A final section to note covers the environment of Standard C. In this section Plum defines a "strictly portable" C program as producing "identical behavior in any Standard C environment." Strictly portable programs must be careful of word sizes and number representations. Plum lists several dozen rules that make your program strictly portable.
C Programming Guidelines concludes with a description of all the library functions in Standard C. This will not replace a vendor's reference manual. It is, however, a good, concise description of the standard library.
C++ Programming Guidelines
C++ Programming Guidelines repeats several dozen sections from C Programming Guidelines. Nevertheless, C++ Programming Guidelines contains significant additions about C++ items.C++ Programming Guidelines dedicates considerable attention to migrating from C to C++, a very important topic given that most of us C programmers will be using C++ in five years if not sooner. We would all like the luxury of not having to produce anything of value for six months while we learn C++, but that will not happen. We must remain productive while migrating.
The authors describe three levels in the migration: Typesafe C, Object-Based C++, and Full C++. Typesafe C is the common subset of Standard C and C++. You should use it when the target platform currently supports C and will support C++ by the time you field your system. Object-based C++ is a form of beginner's C++ using basic classes and other C++ extensions. It is not full C++ in that you do not use class derivation and virtual functions. The authors describe these levels amply and as a programmer and manager of programmers I endorse their slow but sure approach to this problem.
C++ Programming Guidelines does not dodge the issue of portable C++ programs. The ANSI C standard makes portable C possible. C++ does not yet share such a standard, so portable C++ programs are more difficult to realize. The authors discuss how to work around this throughout the text and recommend using Ellis and Stroustrup's text as a de facto standard (see reference below).
C++ Programming Guidelines has several sections that discuss code reuse and management of reusable code. One of the selling points of full C++, object-oriented programming is reusable code. You do not receive the full advantages of reusable code without following certain rules in coding and managing code. The authors do a commendable job of explaining this subject and sprinkling tips through the entire book.
C++ Programming Guidelines has one short but vital page on the importance of design in object-oriented, C++ programming. In C++ you create base classes from which you derive many other classes. If you change a base class, then this change can ripple through many derived classes and you have a quandary. The solution is design. You must spend a much larger proportion of time and effort when designing your base classes. You need more reviews and more outsiders asking questions about your design. The result is shorter overall development time and higher quality software.
The authors conclude C++ Programming Guidelines with two benchmark programs. These programs produce a table of execution times for basic operations in C and C++. They also provide you with a sense of execution times for basic operations so you can estimate program performance.
Conclusion
These are working books that spell out coding standards. A coding standard will help improve the quality and portability of your group's software by helping you to have consistent source code in all your projects. If your group does not have a coding standard, it needs one. If your group needs a coding standard, start with one of these books. You may not agree with everything in the books, but that's not necessary. Use the books as a foundation and cut, paste, add, and delete until you have a standard that will work for you. This will improve the quality of your software, and we all need to do that.
Reference
Ellis, Margaret A. and Bjarne Stroustrup. 1990. The Annotated C++ Reference Manual. Reading, MA: Addison-Wesley.Book Information
C Programming Guidelines, 2nd Edition
by Thomas Plum
Price: $30.00
ISBN: 0-911537-07-4C++ Programming Guideliness
by Thomas Plum and Dan Saks
Price: $34.95
ISBN: 0-911537-10-4
Publisher: Plum Hall Inc.
To order: (913) 841-1631 (books),
(808) 885-6663 (machine-readable text)