Monday, April 28, 2008

Hardcoded Years

A bug was opened at work recently reporting that the date selection for a number of reports would not allow you to select a year greater than 2008.
I decided to look into it and found the following code in the page:

<% selectValue = (new Integer(today.getYear() + 1900)).toString(); selectName = "endYear"; %>
<%@ include file="year_selectbox.jsp" %>

Ok, so off to this year_selectbox.jsp:

<select name="<%=selectName%>" class="formPadding">
<option value="1998"
<% if(selectValue.equals("1998")){out.print("selected");} %>>
1998
</option>
<option value="1999"
<% if(selectValue.equals("1999")){out.print("selected");} %>>
1999
</option>
......
<option value="2008"
<% if(selectValue.equals("2008")){out.print("selected");} %>>
2008
</option>
With this repeated for 1998-2008!


Hard coded years! Awesome! I checked the history, and this file has had no changes since it was first committed.
I considered replacing this with something more appropriate, but no one could tell me what behaviour it should have. It could:

Allow all years from 1998 to the current year + 1, + 2?
Allow all years from current year +/- 5?

So, I was told just to add a few more years to the code and leave it until we decide on something later. I just hope no one comes along 5 years from now, looks at my name in the version control history, and says, "What was this idiot thinking, perpetuating this horrible code rather than fixing it!"

UPDATE: In the end I decided to go with [1998, the current year +2]. I just couldn't bring myself to leave it as was. That being said, I'm sure 10 years from now and bug report will come in saying that the select field has gotten too big.

No comments:

Post a Comment