Xman Blog Inside

Monday, August 21, 2006

Does it do what you claim it does?

I was adding functionality to the Parallel Reversi. I came across this code:
numMove = list.GetNum() ;
for(i = 0 ; i <= numMove ; i++) { ... }
Oh oooooo ...... how could I make such a mistake? That will cause the accesses to the movelist array out of bound. But it's strange that I had been running the Parallel Reversi for many times without problems. Strange~~~~ I checked the move list implementation to confirm:
public int GetNum() {
return num ;
}
Mmmmm, seems like I made a mistake since long time ago. I then changed to
for(i = 0 ; i < numMove ; i++) { ... }
and declared I fix I bug that never give any complaints. Today I checked again and surprisingly find that the variable num range from 0 to Number_of_moves - 1. Oh nooooooo, I just fixed a correct implementation and introduced a bug. :S :'( ...... The variable names num, numMove quickly lead me to an understanding that they contain value number of moves. However, the move list implementation does not do what the variable names claim what they contain. It will be much better if I use GetMaxIndex() rather than GetNum(), or change num to index+1, any ways that make several components consistent. Phew ....... Now I declared the previous fix was a mistake, and now I fixed a mistaken fix.

Lesson learned:
  • Verify your program does what your variable, function names, or even comments claimed what it does. Otherwise, the consequences could be frustrating, dangering, destructive, ......
  • If I see such codes in a program, I'll definitely doubt the correctness of the entire program. I should have reviewed my codes for consistency. It's ashame to find this appears in my own programs.
  • Next question, should I update all calls to GetNum()? If this function is called everywhere, I guess it's not a good idea to anyhow update a reusable component, as my mass updates could be error prone. I shall just add in some CONSISTENT comments in GetNum() to clarify.

Labels: , ,

0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home