W
Willem
Spiros Bousbouras wrote:
) 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);
) }
Ah. A classic case of 'only one return' religious fundamentalism.
) 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 ;
) }
It violates the religious 'only one return' dogma. ;-)
I'd do it the other way round though, for clarity:
if ( Year % 400 == 0 ) return 1;
if ( Year % 100 == 0 ) return 0;
if ( Year % 4 == 0 ) return 1;
return 0;
Or at least make the % 400 clause also be not-equals and return 0.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
) 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);
) }
Ah. A classic case of 'only one return' religious fundamentalism.
) 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 ;
) }
It violates the religious 'only one return' dogma. ;-)
I'd do it the other way round though, for clarity:
if ( Year % 400 == 0 ) return 1;
if ( Year % 100 == 0 ) return 0;
if ( Year % 4 == 0 ) return 1;
return 0;
Or at least make the % 400 clause also be not-equals and return 0.
SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT