Tips to Read Other People’s Code
Reading code is a basic skill that working programmers need to acquire in order to work on development teams. It is really hard to expect that you will work in isolation of other people’s code. Therefore, reading code is a skill that can make you much more productive.
Although it not easy to learn this quickly, a few tips can help with getting the most of any piece of code. Some of these ideas have helped me to understand large projects over the years.
- Understand the program first: it is pretty difficult to understand code for a program if you don’t know yet what it does in the first place. You should try to get used to how the program works before digging into its code.
- Read with a goal in mind: the same way it is easier to understand a book if you are trying to answer a concrete question, a similar state of mind will work for reading code. Start with a specific goal and try to answer it. For example, a question such as “how is the screen repainted” can lead you to understand better what you are reading.
- Understand the conventions of the platform/language: the way code is organized depends heavily in the language used, and on the requirements of the platform. For example, the way C++ code for Windows is organized is different from C code for XWindows. If we talk about web applications, this is even more dependent on the framework used.
- Use a debugger to acquire dynamic information: a debugger can be of great help in understanding code paths that are difficult to grasp (especially in OO languages). Just put a breakpoint in part of the code you want to study. When the breakpoint is hit, check the call stack. This will show you how a piece of code was called and how objects communicate.
The idea of literate programming was promoted by Knuth in his book Literate Programming.
The Linux kernel is a classic example of code that has been read for its own qualities. Understanding the Linux Kernel is one of the many books that explore this fact.