CodeSOD: All the Rest Have 31
Horror movies, as of late, have gone to great lengths to solve the key obstacle to horror movies- cell phones. When we live in a world where help is a phone call away, it's hard to imagine the characters not doing that. So screenwriters put them in situations where this is impossible: in Midsommar they isolate them in rural Sweden, in Get Out calling the police is only going to put our protagonist in more danger. But what's possibly more common is making the film a period piece- like the X/Pearl/Maxxxine trilogy, Late Night with the Devil, or Netflix's continuing series of R.L. Stine adaptations.
I bring this up, because today's horror starts in 1993. A Norwegian software company launched its software product to mild acclaim. Like every company, it had its ups and downs, its successes and missteps. On the surface, it was a decent enough place to work.
Over the years, the company tried to stay up to date with technology. In 1993, the major languages one might use for launching a major software product, your options are largely C or Pascal. Languages like Python existed, but weren't widely used or even supported on most systems. But the company stayed in business and needed to update their technology as time passed, which meant the program gradually grew and migrated to new languages.
Which meant, by the time Niklas F joined the company, they were on C#. Even though they'd completely changed languages, the codebase still derived from the original C codebase. And that meant that the codebase had many secrets, dark corners, and places a developer should never look.
Like every good horror movie protagonist, Niklas heard the "don't go in there!" and immediately went in there. And lurking in those shadows was the thing every developer fears the most: homebrew date handling code.
/// <summary>/// /// </summary>/// <param name="dt"></param>/// <returns></returns>public static DateTime LastDayInMonth(DateTime dt){int day = 30;switch (dt.Month){case 1:day = 31;break;case 2:if (IsLeapYear(dt))day = 29;elseday = 28;break;case 3:day = 31;break;case 4:day = 30;break;case 5:day = 31;break;case 6:day = 30;break;case 7:day = 31;break;case 8:day = 31;break;case 9:day = 30;break;case 10:day = 31;break;case 11:day = 30;break;case 12:day = 31;break;}return new DateTime(dt.Year, dt.Month, day, 0, 0, 0);}/// <summary>/// /// </summary>/// <param name="dt"></param>/// <returns></returns>public static bool IsLeapYear(DateTime dt){bool ret = (((dt.Year % 4) == 0) && ((dt.Year % 100) != 0) || ((dt.Year % 400) == 0));return ret;}
For a nice change of pace, this code isn't incorrect. Even the leap year calculation is actually correct (though my preference would be to just return the expression instead of using a local variable). But that's what makes this horror all the more insidious: there are built-in functions to handle all of this, but this code works and will likely continue to work, just sitting there, like a demon that we've made a pact with. And suddenly we realize this isn't Midsommar but Ari Aster's other hit film, Hereditary, and we're trapped being in a lineage of monsters, and can't escape our inheritance.
[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!