Read Code
Writing Code Is Fun. Reading It? Not So Much.
Editor’s Note: Readable code is the key to creating applications that can be maintained and last beyond their creator. In order to create readable code, you have to know how to read code. In this piece from 97 Things Every Programmer Should Know, CTO, developer, and speaker Karianne Berg shares her thoughts on why reading code is so important to being a good developer.

WE PROGRAMMERS ARE WEIRD CREATURES. We love writing code. But when it comes to reading it, we usually shy away. After all, writing code is so much more fun, and reading code is hard — sometimes almost impossible. Reading other people’s code is particularly hard. Not necessarily because other people’s code is bad, but because they probably think and solve problems in a different way than you. But did you ever consider that reading someone else’s code could improve your own?
The next time you read some code, stop and think for a moment. Is the code easy or hard to read? If it is hard to read, why is that? Is the formatting poor? Is naming inconsistent or illogical? Are several concerns mixed together in the same piece of code? Perhaps the choice of language prohibits the code from being readable. Try to learn from other people’s mistakes, so that your code won’t contain the same ones. You may receive a few surprises. For example, dependency-breaking techniques may be good for low coupling, but they can sometimes also make code harder to read. And what some people call elegant code, others call unreadable.
If the code is easy to read, stop to see if there is something useful you can learn from it. Maybe there’s a design pattern in use that you don’t know about, or had previously struggled to implement. Perhaps the methods are shorter and their names more expressive than yours. Some open source projects are full of good examples of how to write brilliant, readable code — while others serve as examples of the exact opposite! Check out some of their code and take a look.
Reading your own old code, from a project you are not currently working on, can also be an enlightening experience. Start with some of your oldest code and work your way forward to the present. You will probably find that it is not at all as easy to read as when you wrote it. Your early code may also have a certain embarrassing entertainment value, kind of in the same way as being reminded of all the things you said when you were drinking in the pub last night. Look at how you have developed your skills over the years — it can be truly motivating. Observe what areas of the code are hard to read, and consider whether you are still writing code in the same way today.
So, the next time you feel the need to improve your programming skills, don’t read another book. Read code.
Learn faster. Dig deeper. See farther.
Join the O’Reilly online learning platform. Get a free trial today and find answers on the fly, or master something new and useful.
Karianne Berg is the CTO of Kaukus. She likes to contribute to making people better developers, and is co-organizer of the ROOTS and Smidig conferences, as well as the Oslo XP Meetup. She has also presented at several conferences, and was last seen at Smidig 2009. Karianne’s main fields of interest include agile development, patterns, and the Spring framework.