Book Review(s): Writing Quality Code

Summary: Clean Code is a clear guide to writing better software which can be applied to data mining and SQL queries. The Pragmatic Programmer is a more detailed but slightly more antiquated book. Code Complete is a massive tome full of specific guidance.

Clean Code and The Pragmatic Programmer Book Covers

Recently, I’ve been writing a lot more R code (at work and in competitions) and I noticed that I was having trouble keeping up with changes and repeating myself across data exploration and model development. So I took my local library and picked up what Amazon recommended!

The three books discussed below have a heavy focus object-oriented programming but there were many chapters that applied to functions, comments, style, testing, and just how to start writing.

Clean Code – Robert C. Martin

This was the last book I read (Amazon) and I thought it was a good encapsulation of the other two books in a friendlier format. It was also written in 2009 (which is 5 years more recent than Code Complete and 10 years more recent than Pragmatic Programmer).

The book is written in a clear and friendly way. Code examples are all in Java but the principles are explained clearly after each code snippet so you at least know what was changed and why it was changed.

Since it is a more recent book, it has the benefit of hindsight and draws upon dozens of other books written before it. So Clean Code is partially a collection and distillation of wisdom from many sources and not just one man’s opinions.

  • Write tests to make sure everything is working.
  • Keep code simple and free from duplication.
  • Write expressive functions and variable names and avoid writing comments that don’t add anything.
  • Write dirty code first and then clean it (but your tests should confirm that it still works.

One unique aspect of this book is that Martin spends 91 pages (chapters 14, 15, and 16) on showing you how to take dirty code and clean it up (in Java).

The final chapter is a quick rundown of tips for keeping Comments, Functions, Java, and General programming aspects clean.

The Pragmatic Programmer – Andrew Hunt & David Thomas

The Pragmatic Programmer includes tips on improving the code you write but it also provides soft skill tips.

For example, it asserts that writing prototypes for your programs are throwaway code and should never be amended to become production code. This lets the programmer just make a minimum viable piece of software rather than focusing on design standards.

This book also gives clear definition for the types of test you might run on your system (unit testing is for things like functions or one class, integration tests look at multiple subsystems, stress tests include activities like seeing what happens when memory fills up).

However, this is the oldest of the three books (published 1999). It doesn’t show its age (i.e. by referencing programming languages like Ada), but it could benefit from a second edition.

Code Complete – Steve McConnell

Clean Code, Code Complete, and The Pragmatic Programmer are Recommended together by Amazon

At over 900 pages, you will find something new in Code Complete. This was the first book I picked up and it essentially covered everything the other two would.

The biggest thing I took away from the book was McConnell’s pseudocode writing process.

  • Define the problem you’re solving
  • What’s going in and what’s going out.
  • How will you test it?
  • Write pseudocode.
  • Turn pseudocode into code line by line.
  • Mentally check the code
  • Compile.

It’s such a simple step to take but one that I often ignore as I just start writing a piece of code or query. It reminds me of how, at least in business, most people do not start off with a hypothesis to confirm or refute. At least writing some pseudocode can help you work out tough spots before you’re halfway through writing your program.

Code Complete is a great reference for the experienced programmer and it provides examples in a few languages like Visual Basic, Java, and C++.

Bottom Line: I would recommend reading Clean Code first and, if you have time, reading Code Complete as it is predominantly a superset of the other two books.