The Elements of Programming Style by Brian W. Kernighan and P. J. Plauger is one of those books that makes some excellent points that every software engineer should know and bear in mind every day. The central message is simple: write clearly and don’t be too clever. Say what you mean, use the language’s good features, choose names that won’t be confused, and structure your code so that its intent is obvious. There is plenty here that still feels completely relevant, including modularisation, encapsulation, keeping modules focused on one thing, making coupling visible, defensive programming and, perhaps most importantly, not stopping with your first draft. The advice to revise and refactor rather than being satisfied with code that merely works is something that could be written today.
The chapters on efficiency and documentation particularly stood out to me. The authors make the point that if a program doesn’t work, it doesn’t matter how fast it runs, and that you should make it right before you make it faster. Fundamental performance improvements are more likely to come from choosing a better algorithm than from fiddling with the code, and you should measure before making assumptions about where the time is being spent. There is a similar pragmatism in the documentation chapter. The best documentation for a clear program is its structure, and comments are only useful when they add something that the code itself cannot communicate. A wrong comment is worse than no comment, so don’t just echo the code with comments. Make every comment count.
The biggest problem with the book is that all of the examples are in Fortran or PL/I, which makes some of it hard work to apply directly to modern software development. That is understandable given when the book was written, but it does make the examples feel rather dated. What is interesting, though, is how much of the underlying advice has survived the intervening decades. Avoid clever code. Keep things simple. Choose good abstractions. Test boundary values. Don’t patch bad code when rewriting it would make more sense. Measure rather than guess. These are not really Fortran lessons. They are lessons about writing software, and that is why The Elements of Programming Style is still worth reading.
The chapters on efficiency and documentation particularly stood out to me. The authors make the point that if a program doesn’t work, it doesn’t matter how fast it runs, and that you should make it right before you make it faster. Fundamental performance improvements are more likely to come from choosing a better algorithm than from fiddling with the code, and you should measure before making assumptions about where the time is being spent. There is a similar pragmatism in the documentation chapter. The best documentation for a clear program is its structure, and comments are only useful when they add something that the code itself cannot communicate. A wrong comment is worse than no comment, so don’t just echo the code with comments. Make every comment count.
The biggest problem with the book is that all of the examples are in Fortran or PL/I, which makes some of it hard work to apply directly to modern software development. That is understandable given when the book was written, but it does make the examples feel rather dated. What is interesting, though, is how much of the underlying advice has survived the intervening decades. Avoid clever code. Keep things simple. Choose good abstractions. Test boundary values. Don’t patch bad code when rewriting it would make more sense. Measure rather than guess. These are not really Fortran lessons. They are lessons about writing software, and that is why The Elements of Programming Style is still worth reading.
The Elements of Programming Style
by P.J. Kernighan, Brian W.; Plauger
ISBN-13: 978-0070341999
1978

Comments
Post a Comment