Zunes bug

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
 
J

Jean-Marc Bourguet

Spiros Bousbouras said:
static int IsLeapYear(int Year) {
if (Year % 400 == 0 || ( Year%4 == 0 && Year%100 != 0 ))
return 1 ;
else
return 0 ;
}

That looks like religious anti-one return fundamentalism ;-) What
about:

static int IsLeapYear(int Year)
{
return Year % 4 == 0 && (Year % 100 != 0 || Year % 400 == 0);
}

Yours,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top