Actually, it is not a "typo" bug at all.
If you are an experienced programmer, you have encountered code that
just does the right thing (and sometimes contains "typos"). But when
you look at this short bit of code, it smacks you right in the face as
being unnecessary complicated and therefore error prone. It uses a
programming style that I would call "asking for trouble". Of course it
is possible to make such code work correctly with enough effort and
enough testing, but a good programming style achieves the same with
much less effort.
Indeed. Here's another example of "asking for trouble" code again from
http://pastie.org/349916
static int IsLeapYear(int Year)
{
int Leap;
Leap = 0;
if ((Year % 4) == 0) {
Leap = 1;
if ((Year % 100) == 0) {
Leap = (Year%400) ? 0 : 1;
}
}
return (Leap);
}
Note the triple (counting the ? operator) nesting and the construct
Leap = (Year%400) ? 0 : 1 which practically invites you to get
the test the wrong way round.
What's wrong with writing
static int IsLeapYear(int Year) {
if ( Year % 4 != 0 ) return 0 ;
if ( Year % 100 != 0 ) return 1 ;
if ( Year % 400 == 0 ) return 1 ;
return 0 ;
}
Or perhaps
static int IsLeapYear(int Year) {
if (Year % 400 == 0 || ( Year%4 == 0 && Year%100 != 0 ))
return 1 ;
else
return 0 ;
}