The Value of a Code Review

I cannot remind myself often enough of just how valuable a simple thing like a code review is.  Just today, I was scratching my head over a bit of code that wasn't quite behaving as I expected.  It turns out, I was the victim of a basic cut-and-paste error.

I was using some code similar to the following:

for(i = 0; i < count; i++){    sz = (pObjectBuffer+count)->sz;}
What I was seeing was that I was encountering a NULL every time through the loop...  I attached the debugger to the application, stepped through the function and watched it behave as I expected until I got to the code above. 

I asked one of the kind folks here to look over my shoulder and see if I was doing anything silly.  We stepped through the function again and spotted the error (I'm sure you've caught the error already)... I was not actually incrementing my pointer at all...  I was actually checking beyond the bounds of my buffer.

The code should have been doing the following:

for(i = 0; i < count; i++){    sz = (pObjectBuffer+i)->sz;}
Silly mistake, easy to do, I was suitably embarrassed.

How could I have found the problem myself?  Asserting that the pointer was not NULL,  executing the code from my loop earlier in the function (right after the original data was acquired), and cleaning my glasses / monitor screens may have helped too. :)

Once again, a simple, easy mistake was found by showing the code to someone else and explaining the intended behavior.  I cannot stress enough how beneficial it is to review code with another developer, even one who is not familiar with the technology.  Leveraging an extra pair of eyes can sometimes save a great deal of time and almost always results in better code.

Take care,
-- DK

Disclaimer(s):
This posting is provided "AS IS" with no warranties, and confers no rights.