97 Things Every Programmer Should Know

(Chris Devlin) #1

(^140) 97 Things Every Programmer Should Know


Read Code ...................................


Karianne Berg


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.

Free download pdf