I did miss
lastchance's beautiful ninja:
1 2 3
|
if ( Year != compare.Year ) return Year < compare.Year ;
if ( Month != compare.Month) return Month < compare.Month;
return Day < compare.Day;
|
|
What can we learn?
1. IF RETURN does not need ELSE
When you have
if ( cond ) statementA; else statementB;
only one fo the two statements is executed.
IF the cond is true, THEN the else is skipped entirely.
When you
return
from function, you do skip the rest too; the
else
does not make a difference.
2. The condition is
Year != compare.Year
. That is same as
not (Year == compare.Year)
Essentially:
1 2
|
if ( Year != compare.Year ) // the year decides
else // must look at month (and day?)
|
Which could be written:
1 2
|
if ( Year == compare.Year ) // must look at month (and day?)
else // the year decides
|
Now we seem to have only
two cases: either year is same, or it is not.
However, the "year is not same" contains exactly two cases:
* A is before B
* A is after B
Since A cannot be B when A!=B, we can get a solution with A<B
3. We return the value of
expression:
return Year < compare.Year ;
You could expand the
if ( Year != compare.Year ) return Year < compare.Year ;
into:
1 2 3 4 5 6
|
if ( Year != compare.Year )
{
bool smaller = Year < compare.Year ;
if ( smaller == true ) return true;
else return false;
}
|
That, however, is awfully redundant. Too much to type without benefit. The more you type, the more errors can lurk in.