Magic Strings and Magic Numbers
This past week a very old (last time I did work for him was in 2007) client of mine contacted me because their program suddenly started exhibiting a problem. It seems that if a user enters a date anytime in 2015, the program displays an error message indicating that they need to enter a date greater than today and less than two years from today.
When I went to replicate the error in my debugger, I discovered this bit of code:
if (Year < 100)
{
if (Year < 15)
{
Year += 2000;
}
else
{
Year += 1900;
}
}
So that, if you enter any date as 15, it assumes the date is 1915 instead of 2015.
Now, we are talking about code here that has been around since the DOS days, and I’m pretty sure this particular routine has been around since then too. I’m sure when it was coded, the year 2015 seemed so distant, that no one considered that the program might actually still be running in some form.
But, that’s just the problem with Magic Strings and Magic Numbers. T
Comments