Read the code

We, programmers, are strange creatures. We love writing code. But when it comes to reading code, we usually shy away from it. After all, writing code is much more exciting, and reading code is difficult—sometimes almost impossible. Especially hard is reading code written by others. Not always because it is poorly written, but because another person thinks and solves problems differently than you do. Have you ever thought that reading someone else’s code can help improve your own code?

The next time you read some code, stop and think. Is it hard to read or easy? If it’s hard, why? Is it poorly formatted? Is the naming system inconsistent or illogical? Are multiple tasks mixed in one piece of code? Perhaps the chosen language makes the code difficult to read. Try to learn from others’ mistakes so you don’t repeat them in your own code. You might be in for surprises. For example, dependency breaking techniques can be useful for introducing loose coupling, but they can also make the code harder to read. And code that some consider elegant, others might call unreadable.

If the code is easy to read, pay attention to it and see if you can learn something from it. Perhaps it uses a design pattern that you are not familiar with or one that you have tried to implement before. Or it contains methods that are shorter and more accurately named than yours. In some open-source projects, you can find plenty of examples of excellent, understandable code, while in others, you will encounter the exact opposite! Download some code from the repositories of such projects and study it.

Reading your own old code from a project you no longer work on can also be an enlightening experience. Start with some of the oldest code and gradually move to the code you write today. You may find that reading old code is not as easy as it was when you wrote it. Your early code might cause you some embarrassment, similar to the feeling you get when someone recounts what you said the night before while drinking at a pub. Observe how your skills have grown over the years; this can be a very encouraging discovery. Identify which parts of the code are hard to read and consider whether you still write code in the same style today.

So, the next time you feel the need to improve your programming skills, don’t pick up books. Read code.