Benefits of studying your own code

Lest night, I was reading an interview by Ken Thompson, the inventor
of UNIX, on the book “Coders at Work“. At one point of the
interview, he mentions that he considered himself a better programmer
before he was 35. The main reason according to him is that he knew
every line of code that he had ever written, from the moment he
started programming until that age. The way Ken Thompson achieved this was by studying his own code. Ken
would take with him print outs of the code written during the day and
study them at night, so he would remember exactly what each line
meant.

While it is impressive that Ken was willing to do this during his
early career, the question remains if this was just a helpful
practice of if it was instrumental for him to produce so much good
work. Also, it is interesting to understand the true advantages of
studying the code that we have written ourselves. After reflecting a
little on his procedure, here are some benefits that I believe we can
have by doing the same.

Finding bugs: this is the most obvious reason for reviewing code. If
you can check the details of your code without the pressures of
daytime work, it may become much easier to spot obvious flaws that
wouldn’t be so obvious in the heat of the compilations cycles.
Powerful results can be achieved when we focus on a particular part of
our work without the pressure of the normal schedule.

Finding opportunities for refactoring is another benefit that you can
achieve by studying and reviewing your programs. It is incredible how
much boilerplate code we are required to write in order to comply with
badly designed languages and libraries. If we do it in a mindless way,
all the magic of creating software is lost and we start to be just a
little piece in a big framework. Refactoring helps with reducing the
amount of repeated code we have to produce. By refactoring code
regularly, one can reshape code into something reusable, which is a
win for everyone.

Still another advantage of reviewing your own code is the possibility
of keeping the code longer in your head. Every software engineer knows
that the ability to keep code in your head is a big step into entering
“the flow” and consequently solving your programming problems more
easily. Reviewing what you have written is a good way to maintain
better focus, which subsequently can result in increased productivity.

Can we do it in practice?

After considering the advantages of careful and regular code
reviewing, the question becomes not just if it is a good practice, but
if we can really do it. The day to day of many software engineers is
so hectic that it is sometimes difficult to find time for the required
tasks, let alone something that is not essential.

One of the reasons it might be difficult to review code regularly is
that the programs we write these days are truly large. While in the
past standard applications could be stored in just a few KB, currently
there is no complete application that can be written in less than
several thousand lines. This is daunting. Reviewing this amount of
code, especially when it is printed, can even be bad for the
environment — can you imagine all the poor trees that would have to
die just for you to read a bad program?

One possible answer is that most of the code programmers need to write
these days is completely boilerplate. From verbose languages to badly
designed libraries, all of this seems to inflate the number of lines
necessary to run the simplest of the programs.

However, a software engineer doesn’t need to read all of this in order
to review the important parts of a program. It is important for
software engineers to spend at least some time doing a thorough review
of the main parts of the software they write. Even if it is not
possible to print the whole thing, is at least interesting to review
one or two major parts of the system. Going over such important piece
of code can make you confident that the whole thing is going to work,
specially if the details left out are really inconsequential.

The final question that can be asked about this technique is: is it
efficient? Of course it would be nice to write 100% bug free, optimal
code — but it is not possible. The reality is that we need to use
whatever tools we have to improve the quality of the programs that we
create. If such a review strategy can reduce the debugging needs by at
least some small amount, it is still a good thing. Remember, it is not
the number of lines you write that matter, but the number of bug-free
features you can deliver.

Debugging is the most time consuming part of creating software, and
everything we can do to avoid it, should be done. Maybe it is due time
to start reviewing more of the code we write. It can help in reducing
bugs. And it worked so well for Ken Thompson, why wouldn’t it work for
us?

Similar Posts:

About the Author

Carlos Oliveira holds a PhD in Systems Engineering and Optimization from University of Florida. He works as a software engineer, with more than 10 years of experience in developing high performance, commercial and scientific applications in C++, Java, and Objective-C. His most Recent Book is Practical C++ Financial Programming.

Sorry, comments for this entry are closed at this time.