Visit the leap exercise on Exercism to read the full instructions and download the exercise files.
Dig Deeper
Boolean chain
Chain of Boolean expressions
public static bool IsLeapYear(int year)
{
return year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
}
The first Boolean expression uses the remainder operator to check if the year is evenly divided by 4.
- If the year is not evenly divisible by
4, then the chain will “short circuit” due to the next operator being a logical AND (&&), and will return false.
- If the year is evenly divisible by
4, then the year is checked to not be evenly divisible by 100.
- If the year is not evenly divisible by
100, then the expression is true and the chain will “short-circuit” to return true,
since the next operator is a logical OR (||).
- If the year is evenly divisible by
100, then the expression is false, and the returned value from the chain will be if the year is evenly divisible by 400.
| year | year % 4 == 0 | year % 100 != 0 | year % 400 == 0 | is leap year |
|---|
| 2020 | true | true | not evaluated | true |
| 2019 | false | not evaluated | not evaluated | false |
| 2000 | true | false | true | true |
| 1900 | true | false | false | false |
The chain of Boolean expressions is efficient, as it proceeds from testing the most likely to least likely conditions.
Shortening
When the body of a function is a single expression, the function can be implemented as an expression-bodied member, like so
public static bool IsLeapYear(int year) =>
year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
or
public static bool IsLeapYear(int year) => year % 4 == 0 && (year % 100 != 0 || year % 400 == 0);
Ternary operator
Ternary operator
public static bool IsLeapYear(int year)
{
return year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
}
A conditional operator, also known as a “ternary conditional operator”, or just “ternary operator”,
uses a maximum of two checks to determine if a year is a leap year.
It starts by testing the outlier condition of the year being evenly divisible by 100.
It does this by using the remainder operator.
If the year is evenly divisible by 100, then the expression is true, and the ternary operator returns if the year is evenly divisible by 400.
If the year is not evenly divisible by 100, then the expression is false, and the ternary operator returns if the year is evenly divisible by 4.
| year | year % 100 == 0 | year % 400 == 0 | year % 4 == 0 | is leap year |
|---|
| 2020 | false | not evaluated | true | true |
| 2019 | false | not evaluated | false | false |
| 2000 | true | true | not evaluated | true |
| 1900 | true | false | not evaluated | false |
Although it uses a maximum of only two checks, the ternary operator tests an outlier condition first,
making it less efficient than another approach that would first test if the year is evenly divisible by 4,
which is more likely than the year being evenly divisible by 100.
Shortening
When the body of a function is a single expression, the function can be implemented as an expression-bodied member, like so
public static bool IsLeapYear(int year) =>
year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
or
public static bool IsLeapYear(int year) => year % 100 == 0 ? year % 400 == 0 : year % 4 == 0;
switch on a tuple
switch on a tuple
public static class Leap
{
public static bool IsLeapYear(int year)
{
return (year % 4, year % 100, year % 400) switch
{
(_, _, 0) => true,
(_, 0, _) => false,
(0, _, _) => true,
_ => false,
};
}
}
A tuple is made from the conditions for the year being evenly divisible by 4, 100 and 400.
The tuple is tested in a switch expression.
It checks the values of the expressions in the tuple, and uses the _ discard pattern to disregard a value.
The default arm of the switch returns false when none of the previous arms match.
| year | year % 4 | year % 100 | year % 400 | is leap year |
|---|
| 2020 | 0 | 20 | 20 | true |
| 2019 | 3 | 19 | 19 | false |
| 2000 | 0 | 0 | 0 | true |
| 1900 | 0 | 0 | 300 | false |
Although some may consider it to be a more “functional” approach, the switch on a tuple approach is somewhat more verbose than other approaches,
and may also be considered less readable.
Source: Exercism csharp/leap