The Twilight Zone (code snippet)

Every once in a while, I come across something at work that’s so mindblowing, it makes me exclaim out loud, “WTF am I reading?!”. My reaction isn’t as bad these days thanks to the anger management ring, but it’s bad enough that I have to share this.

I was tasked with cleaning up an old blog app and came across the archive URL parsing code. The guy who wrote this isn’t working for us any more (despite the copious commenting) and I can kinda see why.

DateTime start;
DateTime end = DateTime.UtcNow;
bool leap = false;

// Only posts from this year or before down to 2000
if (year > 2000 && year <= end.Year)
{
	// Check if the requested year is a leap year.
	// Used mainly for getting the last day for February
	leap = DateTime.IsLeapYear(year);

	// Only posts from a valid month or before if requesting this year
	if (month > 0 && month <= 12)
	{
		// If this year was requested, the month has to be on or before this month
		if (year == end.Year && month <= end.Month)
		{
			// Only posts from this day or before
			if (day > 0 && day <= EndDayForMonth(month, leap))
			{
				start = new DateTime(year, month, day);
			}
			else // Invalid day, start from the first of the requested month
			{
				start = new DateTime(year, month, EndDayForMonth(month, leap));
			}
		}
		else
		{
			start = new DateTime(year, end.Month, end.Day);
		}
	}
	else // Invalid month, start from this month instead
	{
		start = new DateTime(year, end.Month, EndDayForMonth(end.Month, leap));
	}
}
else
{ // Invalid year
	start = new DateTime(2000, 1, 1); // From the first start date
}

What am I seeing?!

 

Did I mention that he likes to comment? My head hurts!

Here’s the stupid-free version…

DateTime start;
DateTime end = DateTime.UtcNow;

// Fix invalid input ranges (only posts made from the year 2000 to today)
year = (year > end.Year || year < 2000) ? end.Year : year;

// Month range exceeded? reset to January
month = (month < 1 || month > 12) ? 1 : month;

// Day range exceeds day count for this month? Reset it to the 1st
day = (day < 1 || day > EndDayForMonth(month, DateTime.IsLeapYear(year))) ? 1 : day;

// If this year was selected, set maximums
if (year == end.Year)
{
	// Only posts made on or before this month
	month = (month > end.Month)? end.Month : month;
	if (month == end.Month)
	{
		// Only posts made on or before today
		day = (day > end.Day) ? end.Day : day;
	}
}

start = new DateTime(year, month, day);

BTW… Here’s the EndDayForMonth helper function.

private int EndDayForMonth(int month, bool leap)
{
	int end;

	switch (month)
	{
		case 2:
			end = (leap) ? 29 : 28;
			break;
		case 4:
		case 6:
		case 9:
		case 11:
			end = 30;
			break;
		default:
			end = 31;
			break;
	}
	return end;
}

Man, I can’t wait for Sunday!

4 thoughts on “The Twilight Zone (code snippet)

    • Anger Management Ring saves the day again :D

      90% Of all programming problems (and all problems for that matter) can be solved by simplification and a bit of common sense.

      After all, no need to use tongs to open a door knob.

      • May I quote you on that????? We were just talking about something along those lines regarding physics. Tongs and doorknobs! Wonderful…..

      • Go right ahead :D

        Speaking of physics; I had a friend in college who was a massive germaphobe and would use a paper towel that he carried around to open doors. That phrase popped into my head a few times while we were in the lab.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s