One easy way to improve your programming skills is to read someone else’s code.

Original author: Jerod Santo
  • Transfer
Note: This article was originally written for the Fuel Your Coding back site in May 2010. Unfortunately, this site is currently down, so I am posting an article here to save it for posterity. I was going to update it, taking into account the latest trends, but decided to leave it as it was written. Those parts that are outdated may seem a little ridiculous, but oh well. Have fun ...

The most obvious way to improve the quality of your programming is to write more programs. Everyone knows that. However, another way that I'm sure will improve your programming is the exact opposite. I will state it as clearly as I can.

If you want to dramatically increase your ability to program, you must ... read code written by other programmers.

You can believe it, you can not believe it. Your right. But if you are willing to take a chance, then I am sure you will be rewarded for the time spent.

In this article, I would like to help you in choosing what to read, and give practical advice on such reading. If you already read other programs, then maybe you will find something here that will allow you to get more out of your efforts. If you do not read the codes of other developers, then you simply must do this.

What to read

This is an important decision, and one in which it is difficult to advise. I would not want to just point you to some code that I think you should read, because you really need to consider what you are doing. However, I will give some guidelines to help you choose your reading programs.

Read related programs

A great place to start are any plugins or libraries that you already use.

• WordPress plugin that you really like;
• Ruby gem that you find useful;
• The jQuery plugin to which you keep coming back.

All of them are the first candidates for study. You already know their public interfaces, so the barrier to understanding their internal work is lower. In addition, you, as a user of this program, have the opportunity to add documentation, introduce a new function, or even make some contribution to this project.

Read the programs that impressed you

I remember that when I first looked at the 280 Slides presentation creation service , I thought: “Yes! Cool!". I quickly found out that the program managing this site is an open source Cappuccino project. This knowledge went deep into my mind, and when I somehow stumbled upon another impressive application that worked on Cappuccino , I already knew that I could learn a lot from this project. What has made a strong impression on you lately? Is this program open source? If so, then it is a great choice to read: the code is likely to impress you as much as the application itself.

Read programs written by those you respect


Respectable programmers

If you've been doing open source programming for a while, you probably already have programmers who deserve your respect. I could immediately name a few developers whose programs simply make me “white envy”.

If you do not have such a developer yet, then finding it is easy. He (a) is probably the author of a program in one of the previous two sections (programs related to you, or programs that impressed you).

Read programs that you can really understand deep enough

If you are risk averse, you might consider diving into a large project, such as Ruby on Rails, Drupal, or jQuery. But I would suggest that you do not use such projects for now, unless, of course, you are an experienced reader of programs.

Large projects have an extremely large number of interacting parts, and you will ultimately spend a lot of time and effort mastering the general concepts in order to learn something specific. The intricacy of the subject under study dampens, and large projects are more likely to lead to your disappointment when reading. The advantage of choosing a small project to read is that you can keep the entire logic of the program in your head. This allows you to work only with details in order to learn some lessons.

How to read

Now that the code to read is selected, what is the best way to read it? I have read many programs today and can offer several ways to maximize your efficiency.

Learn the big picture


Twitter gem directory structure

I assume that you at least know at the macro level what the code you are reading is doing. If not, I suggest reading the project website, tutorials, documentation, and everything else that you can get in addition to the code.

Once proper clarity has been introduced, your first step should be to review the project structure. The volume of this work depends on the size of the selected source code base, but even if it takes more than one file, it will only require a little more time.

First of all, fix the file structure for yourself. This step is easier to complete with an editor that looks like a folder hierarchy, such as TextMate. Here, as an example, shows a great view of Twitter Ruby gem.

The purpose of this step is simply to familiarize yourself with the source. Understand which files include / call / load other files, where the bulk of the code is located, what namespaces are used (if any), and so on. Having a general idea, you can dive into the details.

Document your results

Reading the code should not be some kind of passive action. I recommend adding comments as you progress, documenting your assumptions and your conclusions when you begin to understand the progress of the program. When you start for the first time, your comments will probably look like this:

# Полагаю, что вызов этой функции происходит после "initialize"
# Что же делает эта задача?
# Уверен, эта переменная теряет свою область действия после строки 17

As you understand the processes, you can remove the small hierarchical comments that you left for yourself, and possibly write more meaningful and authoritative comments that could be passed back to the project.

Use the tests, Luke

(Note of the translator: the author recalled “Use your power, Luke” from “Star Wars”)

Let's hope that the project you have selected has a set of tests. If not, you can skip this section altogether (or find a project that has such a set).

Tests are a great place to start reading someone else's code, because they document what the program should do. Some tests are more informative than others, but no matter how well written, it is often easier to find a programmer’s intent in tests than in implementation. When reading, try to get a successful result while running the entire test suite. This will ensure that your development environment is configured correctly and will make you more confident when making changes.

Change the code, compile

Who says code reading should be passive? You will really begin to understand the code only after you break everything and put it together again. Remember the tests passed? Make them fail, add something, or try to change the implementation so that they work fine. Try adding some small “feature” that seems “cool” to you, or set up registration for the entire project so that you can print the output at various stages of the program. Is it still reading? Absolutely, but this approach is more of a personal adventure than reading a detective novel. And this is exactly what you need!

Rinse and repeat

(Note of the translator: from the joke about the IT specialist washing himself with shampoo strictly according to the instructions - endlessly)

Having finished reading one code base, take another and start the process again. The more codes you read, the better this reading goes and the more you get from it in less time. I think you will find that your efficiency is growing quite quickly and that it is really a very nice way to learn.

Where to start

The one, most important source for reading code is GitHub for me . This site makes it so easy to find new projects and really great programmers that you act to your own detriment if you do not use it. I suggest starting on GitHub and reading the code directly on the site until you find a project from which you think you can learn some lessons. Then clone him and read!

Do you use code reading as a learning tool? What projects would you recommend to your colleagues? Have you read any good program lately?

Also popular now: