Chapter 70. Read Code

Karianne Berg

image with no caption

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 ...

Get 97 Things Every Programmer Should Know now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.