Dec 102009
 

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?

[sourcecode language="csharp"]
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.
[/sourcecode]