Sunday, December 16, 2007

Bad Code

It happens so , that besides my primary job, I currently have other projects on a side.
One is WTL/C++ , couple more with C#.NET and today I also had to fix some legacy code written with ASP/VBscript that I inherited (very poorly written code).
Yes, that's right VBScript. It would take much longer for my clients to contact the original developers of that code, than me fixing the bug.

After seeing such "masterpieces" from time to time I think I might start to collect them and publish on my blog.


Here is what I found today:
todaysDate = Now()
resultDate = Right(Left(todaysDate,(InStr(1, todaysDate, "/"))-1),3)+1 & "/1/20" & Right(Left(todaysDate,(InStr(1, todaysDate, " "))-1),2)

Isn't that beautiful ?
Try to see what happens with today's date 12/16/2007 ......
.....
bingo 13/1/2007 .... and there is no 13th month.

Whoever wrote the code was relying on particular format of the date, parsing it's string representation, in such a way that makes sane developers cry.


One of the ways to fix it and to get to the same result in this case, would be to do something like:
tempDate = DateAdd("m",1,Date())
resultDate = Month(tempDate) & "/1/" & Year(tempDate)

... and think twice before shopping for cheapest software engineers :)