Recent Posts
Recent Comments
Link
관리 메뉴

NaggingMachine

What a fool! 본문

TechnoBabbler

What a fool!

naggingmachine 2006. 9. 6. 10:27
About a week ago, I debug my code for 1 hour. Because it was very simple code, I couldn't understand why the result was wrong. It drove me crazy. I thought me as a debug machine, but it turned out that I'm not such a debug guru. The code that I debug is the following code.

if ( $count < 10 )

  if ( $count == 0 )

      $count = 1;

else

  $count = 10;
Can you find what the problem is in seconds? Now, of course, I can. It is like a coding style problem. The code was not mine. I mean I didn't code it. The value of $count was 6 and the value that I got after the execute the previous code was 10. I thought the result should be 6 because 6 is less than 10 and not equal 0. It should remain as it was. Why was the value of $count changed to 10? You can get the idea if the code was like the following.



if ( $count < 10 )

  if ( $count == 0 )

      $count = 1;

  else

      $count = 10;
Now, 6 is less than 10 and not equal 0, so the value is 10. Isn't it clear? Then why couldn't I debug it? Because I'm a human being, not a machine, I debug it by using my eyes and brain. From the information that my eyes gave me, I thought the else statement is releated with the first if statement.



I think you happed to meet this situation any time. So my guidlines are like followings.



1. Re-organize the code. There are many plug-ins and macros integrated with IDE that help you to make your code more readable.

2. Use explicitly brackets. Don't save lines. Even if the if statement always have just one line, use brackets. The if statment can have more lines later.

3. Use only your brain, not the information your eyes give you.


'TechnoBabbler' 카테고리의 다른 글

WPF Killer Application, TimesReader!  (0) 2006.09.26
Install the lastest Vista, Build #5728  (0) 2006.09.26
좋은 개발 툴 소개  (0) 2006.09.04
Microsoft 모래성 출시하다.  (0) 2006.08.31
Windows GUI 변천사  (0) 2006.08.21