The source code

Learning from reading source code

One of the best ways to get better at programming is to read other people’s code. Fortunately, with our day and age, that’s easy to do. There’s a proliferation of open source code and it’s very easy to find high-quality examples to study.

There are three things one can learn about when reading source code, especially of the libraries and frameworks one uses in day to day work:

  • learning about language idioms,
  • learning about novel approaches to solving problems,
  • learning the internals of libraries and frameworks

Idioms

Each language has it’s own way to express common solutions. Usually, they are in a form of small snippets of code. Those are immediately recognizable. They form a higher level language with which it’s easier to express the solutions.

Those idioms could be a shorter way of expressing a solution or the one in which it will be more efficient. Generally, those form a part of the culture around the language.

Knowledge of those idioms not only helps with coding quicker and with less chance of errors but also makes it easier for others to read the code. That other person maybe you in couple months.

Learning those idioms is part of learning the language’s culture and the best way to acquire them quickly is to go through the high-quality code.

Problem-solving

Programming is all about solving problems. The wider array of the approaches one have the better it is to find an approach which will be the best fit.

Existing code, which survived long term usage and scrutiny of many people will contain a lot of battle-tested solutions. Those solutions may be extracted and generalized. And even if they are very specific to the given piece of code one can learn how to tackle such problem. It’s gaining experience without the struggle to arrive at the solution. A shortcut to becoming a better programmer.

We are lucky that we can study how the best of us build great code. Studying craft of other programmers is what helps to improve.

Most of the time we can even look into the past, follow how given a piece of the code come to be, how it evolved and become what it is now. Maybe bugs were fixed, maybe new requirements showed up. Usually, that can be traced, thanks to the source control systems.

Internals

Another benefit of reading source code, especially for libraries one uses in day to day work is learning about their internals.

In the perfect world, one wouldn’t have to look under the hood to know how things work. The documentation would explain everything required for efficient library usage and also describe how it works. It would be in sufficient level of details so one would be equipped to make the best use of it.

Unfortunately, that’s rarely the case and to master given piece of technology, it’s often required to reach to the source.

Even for well-documented libraries, there are usually pieces not yet documented. Also there you can witness exactly how the library works internally which may help with optimizing the code around that knowledge.

The danger of doing that is too tight coupling to the implementation details. The internal structure of a library may change with the future versions and the optimization may no longer work as expected. Too tight coupling should be avoided when possible.

Summary

Reading good quality source code has many benefits and one should find time to do it regularly. It can only help to become better at the craft of programming.