Improving variable names in the Easter program
Just with a few changes in names the algorithm starts making sense
A few posts ago I showed you a program that calculates the date of Easter. The program closely followed Gauss’s Easter algorithm.
Did you understand the program clearly? No.
Did you have a clue what’s going on? No.
In the end of this post I’m showing you that very same program with only two changes:
I’ve renamed most of the variables.
I’ve moved some variable definitions so that they’re nearer to where they’re being used.
Let’s consider the same two questions:
Do you understand the program clearly? Still no.
Do you have a clue what’s going on? Yes!
Just by renaming a few variables you now have a clue what’s going on without needing to study Gauss’s paper.
Of course it’s less concise than what it was, but it’s our time and brain power that are in scarcity rather than ink and paper, much less storage bytes.
However, it still needs much improvement. Anyway, here is its current form:
const DAYS_IN_12_MOONS = 12 * 29.5;
const DIFF_BETWEEN_12_MOONS_AND_ONE_YEAR = 365 - DAYS_IN_12_MOONS;
function getEasterDate(year) {
let month;
let dayOfMonth;
const yearNumberInMetonicCycle = year % 19;
const century = Math.floor(year / 100);
const p = Math.floor((13 + 8 * century) / 25);
const quadricentennial = Math.floor(century / 4);
const M = (15 - p + century - quadricentennial) % 30;
const daysFromEquinoxToFullMoon = (
(
(30 - DIFF_BETWEEN_12_MOONS_AND_ONE_YEAR)
* yearNumberInMetonicCycle + M
) % 30
);
const b = year % 4;
const c = year % 7;
const N = (4 + century - quadricentennial) % 7;
const daysFromFullMoonToSunday = (
2 * b + 4 * c + 6 * daysFromEquinoxToFullMoon + N
) % 7;
const exception1 = (
daysFromEquinoxToFullMoon === 29
&& daysFromFullMoonToSunday === 6
);
const exception2 = (
daysFromEquinoxToFullMoon === 28
&& daysFromFullMoonToSunday === 6
&& (11 * M + 11) % 30 < 19
);
if (exception1) {
month = 4;
dayOfMonth = 19;
} else if (exception2) {
month = 4;
dayOfMonth = 18;
} else {
month = 3;
dayOfMonth = (
22 + daysFromEquinoxToFullMoon + daysFromFullMoonToSunday
);
if (dayOfMonth > 31) {
month = 4;
dayOfMonth -= 31;
}
}
return [month, dayOfMonth];
}