Representative Line: Tern Down a Date

This post was originally published on this site

The Daily WTF

Today’s anonymous submitter has managed to find a way to do date formatting wrong that I don’t think I’ve seen yet. That’s always remarkable. Like most such bad code, it checks string lengths and then adds a leading zero, if needed. It’s not surprising, but again, it’s all in the details:

// convert date string to yyyy/MM/DD return dtmValue.Year + “-” + ((dtmValue.Month.ToString().Length == 1)? (“0” + dtmValue.Month.ToString()): dtmValue.Month.ToString()) + “-” + ((dtmValue.Day.ToString().Length == 1)? (“0” + dtmValue.Day.ToString()): dtmValue.Day.ToString());

This is only one line, but it has it all, doesn’t it. First, we’ve got good ol’ Hungarian notation, which conveys no useful information here. We’ve got a comment which tells us the code outputs /es, but the code actually outputs -. We’ve got ternaries that are definitely not helping readability here, plus repeated calls to ToString() instead of maybe just storing the result in a variable.

And, for the

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