📜 ⬆️ ⬇️

One of the easiest ways to improve your programming skills is to read someone else's code.

Note: This article was originally written for the site Fuel Your Coding back in May 2010. Unfortunately, this site is not working now, so I am publishing an article here to save it for posterity. I was going to update it, given the latest trends, but decided to leave it as it was written. Those parts that are tired, may seem a little ridiculous, but oh well. Enjoy…

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

If you want to dramatically increase your programming skills, you must ... read code written by other programmers.
')
You can believe it, you can not believe it. Your right. But if you are ready to risk, then I am sure you will be rewarded for your time.

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

What to read


This is an important decision, and one in which it is difficult to advise. I would not like to just point you to some code that I think you should read, because in fact you have to consider what you are doing. However, I will give some guidelines to assist you in choosing programs to read.

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, which you find useful;
• jQuery plugin to which you keep coming back.

All of them - the first candidates for the study. You already know their public interfaces, so the barrier to understanding their internal work is below. In addition, you - as a user of this program - have the opportunity to add documentation, implement a new function, or even contribute to this project in some form.

Read programs that have impressed you.


I remember that when I first viewed the presentation creation service 280 Slides , I thought: “Yes! Cool!". I quickly found out that the program running 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 made a strong impression on you lately? Is this program open source? If so, then it is an excellent choice for reading: the code will most likely impress you just as the application itself.

Read programs written by those you respect.


image

Programmers, worthy of respect

If you’ve been involved in open source programming for some time, then you’ve probably already seen programmers who deserve your respect. I could just go and name a few developers whose programs just make me “white envy”.

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

Read programs you can really understand deep enough


If you're inclined to take risks, you can consider immersing yourself in a big project, like Ruby on Rails, Drupal, or jQuery. But I would suggest that you do not use such projects so far, unless of course you are an experienced program reader.

Large projects have extremely many interacting parts, and in the end, you will spend a lot of time and energy on mastering common ideas in order to learn something concrete. The entanglement of the subject matter of study discourages, and large projects are more likely to lead to your disappointment in reading. The advantage of choosing a small project for reading is that you can keep all the logic of the program in your head entirely. This allows you to work only with the details to learn some lessons.

How to read


Now that the code for reading 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.

Examine the big picture


image

Directory structure twitter gem

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

After proper clarity has been added, 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 take only a little longer.

First of all, fix the file structure for yourself. This step is easier to do 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 main part of the code is located, which 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 go, 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 small hierarchical comments that you left for yourself, and perhaps write more meaningful and authoritative comments that could be passed back to the project.

Use tests, Luke


(Approx. Translator: the author remembered "Use force, Luke" from "Star Wars")

Hopefully, the project you choose has a test suite. 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 needs to do. Some tests are more informative than others, but no matter how well written they are, it is often easier to find the programmer’s intentions in tests than in implementation. When reading, try to get a successful result when you run the entire test suite. This will ensure that your development environment is configured correctly and makes you more confident when making changes.

Change the code, compile


Who said reading the code should be passive? You will begin to really understand the code only after breaking everything and putting it together again. Remember the tests passed? Make sure they fail, add something, or try changing the implementation so that they go well. Try adding a 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 a private adventure than reading a detective novel. And this is exactly what we need!

Wash off and repeat


(Approx. Translator: from a joke about an IT person washing with shampoo strictly according to the instructions - infinitely)

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

Where to start


The only, most important source for reading the code is GitHub for me. This site makes it so easy to find new projects and really great programmers that you act to your detriment if you don’t use it. I propose to start on GitHub and read the code directly on the site until you find a project from which you think you can learn some lessons. Then incline it and read!

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

Source: https://habr.com/ru/post/319864/


All Articles