Knowing how to read code is one of the founding pillars of programming. While writing and practice are intrinsic to learning any programming language, much can be gained from reading the code written by other, better programmers. Just as a reading of Tolstoy, Twain and Austen is essential to the education of any serious writer (with even a book devoted entirely to this subject), every competent programmer should devote at least some time every day to reading high quality code.
Almost every programming book pays lip service to reading code, but few actually teach you how to read code. The peculiarities of a language and a coder’s personal idiosyncrasies can make a piece of code frustratingly difficult to read. You can’t really print out some open-source code, grab a cup of coffee and thumb through it leisurely as you would with a novel; there needs to be active engagement with the code and a continuous attempt to analyze its conceptual basis.
According to a research paper by the University of North Carolina’s CS department, we do not actually “read” code but, trained by habit, scan it for any recognizable mistakes, going through the function headers, then the function body, and finally resting our eyes where errors mostly reside, the loops. Effectively, what we call ‘reading’ actually amounts to actively scanning and comprehending code.
Learning how to read code efficiently is a skill that often sets apart excellent coders from the merely good. But it’s a skill that can be easily learned, especially by picking through examples of high quality code. SourceForge, Google Code and GitHub are three excellent resources for finding well written open-source code in a variety of languages.
Reading these five books with their heavy emphasis on writing and reading code will also take you a long way in acquiring this skill:
One of the few books to tackle the subject of reading code head on, this should be the starting point for anyone trying to learn how to read, and eventually, write good code.
This book works on the example method: the author, Diomidis Spinellis, a PhD in Computer Science from Imperial College, London, digs through over 600 real world examples to analyze how they work, how to read, and how to understand them.
A sister book to the above entry, ‘Code Quality’ uses real-life examples from open source projects to teach you how to write quality code. While not a particularly easy read, it is nevertheless a good book to buy for the budding programmer. Most of the best programmers in the world learn the foundational basis of writing quality code through years of experience; this book simplifies the process by providing helpful examples of well written code culled from hundreds of open source projects.
The purpose of the book might be oriented towards non-functional aspects of programming such as structure and problem solving, but the advice is equally useful for coders who want to learn how to read code.
This book extols the virtues of clean code (even though the author, Robert C. Martin, maintains throughout the book that writing clean code is a ‘lot of hard work’) and approaches the process of writing code almost as an artist would approach an easel. Software engineering is compared to craftsmanship; the end result, therefore, must be suitably clean and sufficiently alterable by others.
Refactoring is the process of altering the internal structure of a software program without affecting its actual functions. The end process leads to better designed, more readable, and more ‘coherent’ code that’s much easier to manage and modify.
Much can be learned about reading (and writing) code from the process of refactoring, namely, how to spot errors in code design. Learning about refactoring (a useful skill by itself) helps you read code better, as you will already be thinking about what changes should be made and how they should be made.
Steve McConnell’s masterpiece, Code Complete, can be usually found proudly displayed on the shelves of programmers all around the world. Indeed, this seminal volume in programming literature has enjoyed tremendous success over the years, and is often dubbed the “coders’ Bible.”
Code Complete focuses on the principles of good programming, teaching with examples from academia as well as real life.
Going through the well-written code samples (and McConnell’s insightful commentary) is an education in itself, and will teach you not just how to read code, but also how to think like a top notch programmer.