Partager via


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.

Comments

  • Anonymous
    January 24, 2006
    Good point - I've often found just explaining the problem to someone else is enough. If my co-workers wouldn't get scared, I could probably just talk to an "invisible friend" and get the same results.

  • Anonymous
    September 07, 2006
    I have worked with Dan for several years now (we both joined the .NET Compact Framework team at almost...

  • Anonymous
    December 28, 2006
    I started this series last year and thought I would continue the tradition with my best of 2006 collection.

  • Anonymous
    July 30, 2007
    One of the most common questions I hear towards the end of a product cycle is "are we ready to ship?".

  • Anonymous
    July 30, 2007
    One of the most common questions I hear towards the end of a product cycle is &quot;are we ready to ship

  • Anonymous
    January 03, 2008
    PingBack from http://actors.247blogging.info/?p=989