I hope this series becomes popular enough for bad developers to notice code like this. Because these code is really what’s out that! I’m serious! Next up, abuse of try-catch. This is how type validation is done. Wonderful, isn’t it? And look at the wonderful booleans associated with the try-catch logic? Doesn’t that just make you cringe?

DateTime newDateStart;
DateTime newDateEnd;
string errorMessage;
int index = 0;
bool isInputValid = true;
bool isSingleDayLeave = true;

try
{
    // dateStart is a string assigned from above this code snippet
    newDateStart = Convert.ToDateTime(dateStart);
}
catch
{
    index++;
    errorMessage += index + ". Date start should be given.<br />";
    isInputValid = false;
}

try
{
    // dateEnd is a string assigned from above this code snippet
    newDateEnd = Convert.ToDateTime(dateEnd);
    isSingleDayLeave = false;
}
catch
{
    isSingleDayLeave = true;
}
// ... more of similar code below.

Related posts:

  1. Code You Should Not Be Writing – Part 1
  2. Complex Properties with Inner Property Persistence persisting a Collection property in a User Custom Control using Code Beside Method
  3. Toronto Code Camp Interview
  4. StringControl
  5. ManWrap Library – Managed Code in Unmanaged C++!?!
  • http://blog.dk.sg DK

    Can’t see the logic leh.

  • http://www.justinlee.sg Justin Lee

    There is no logic. The logic is… if Convert cannot convert the string to the type, then it throws an exception. That’s how this code does type checking. In this case, it is converting strings to a DateTime without checking first whether it can be converted, thus the try-catch to do that logic.

  • Pingback: Code You Should Not Be Writing – Part 5 @ JustinLee.sg

   
© 2009 - 2011 JustinLee.sg Suffusion theme by Sayontan Sinha
Stop SOPA!

SOPA breaks our internet freedom!
Any site can be shut down whether or not we've done anything wrong.

Stop SOPA!