CodeSOD: Exactly a Date

This post was originally published on this site

The Daily WTF

Alexandar sends us some C# date handling code. The best thing one can say is that they didn’t reinvent any wheels, but that might be worse, because they used the existing wheels to drive right off a cliff.

try { var date = DateTime.ParseExact(member.PubDate.ToString(), “M/d/yyyy h:mm:ss tt”, null); objCustomResult.PublishedDate = date; } catch (Exception datEx) { }

member.PubDate is a Nullable<DateTime>. So its ToString will return one of two things. If there is a value there, it’ll return the DateTimes value. If it’s null, it’ll just return an empty string. Attempting to parse the empty string will throw an exception, which we helpfully swallow, do nothing about, and leave objCustomResult.PublishedDate in whatever state it was in- I’m going to guess null, but I have no idea.

Part of this WTF is that they break the advantages of using nullable types- the entire point is to be able to handle

To read the full article click on the 'post' link at the top.