Tuesday, February 9, 2010

Code Inspection

Many of our tests exercise code. They might be unit tests, they might be system tests, they might be GUI tests, etc. Sometimes, though, it behoves us to stop exercising the code and start looking at it.

Code inspection can help identify potential problems, and it can help understand problems that you've already found. Code inspection really is about as simple as it sounds: you open it up and start looking.

Just randomly reading code, however, is not particularly useful. To work effectively, you need direction in your code inspection, just like you need direction and purpose in your tests.

There are several things you can do to make code inspection more useful:
  • Short times only. This kind of concentration is difficult, so don't try to do it for hours and hours. Spend an hour or two and then stop. Take a break and do something else for a while.
  • Pick a single purpose. Look for conformance to coding standards, or look for logic flow, or look for variable naming, or look for memory allocation/deallocation. It doesn't matter what purpose you pick, as long as you make it fairly small and fairly defined.
  • Explain it. As you're walking through the code, explain it to someone. That will help you not gloss over code and just figure it looks right. (It's hard to tell when your eyes are glazed over!)
Code inspection can be a very useful tool, but only if you take the time to use it effectively.

1 comment:

  1. Nice post catherine, the techniques you have eloborated for code inspection is really good and informative.

    Keep posting :)