Hello there. The codebase I have been thrust into contains many an interesting snippet. Today I shall present to you GetDayBeginning, or as sane people say, DateTime.Now.Date. And yes, I've checked, the Date property has been there since .NET 1.1.
public static DateTime GetDayBeginning(this DateTime pValue)
{
DateTime result = DateTime.MinValue;
try
{
result = result.AddYears(DateTime.Now.Year - 1);
result = result.AddMonths(DateTime.Now.Month - 1);
result = result.AddDays(DateTime.Now.Day - 1);
}
catch (Exception) { }
return result;
}
Marvel not only at the complete insanity of reinventing the wheel, but also doing it in the shape of a pentagram. Ask yourself this: Which exceptions could possibly be thrown here? If by some miracle an exception is thrown, why does the developer prefer a wrong result to propagating this exception? Would the developer be less crazy had he returned new DateTime(pValue.Year, pValue.Month, pValue.Day)
? Why is the parameter using Hungarian notation anyway? Will calling this method awaken dead Cthulhu from his slumber of aeons?
As luck would have it, the method was completely unused and was quickly cast back into the dark abyss it once came from.