CodeSOD: Classic WTF: Dimensioning the Dimension
by Alex Papadimoulis from The Daily WTF on (#55DN0)
It was a holiday weekend in the US, so we're taking a little break. Yes, I know that most people took Friday off, but as this article demonstrates, dates remain hard. Original -- Remy
It's not too uncommon to see a Java programmer write a method to get the name of a month based on the month number. Sure, month name formatting is built in via SimpleDateFormat, but the documentation can often be hard to read. And since there's really no other place to find the answer, it's excusable that a programmer will just write a quick method to do this.
I have to say though, Robert Cooper's colleague came up with a very interesting way of doing this: adding an[other] index to an array ...
public class DateHelper{ private static final String[][] months = { { "0", "January" }, { "1", "February" }, { "2", "March" }, { "3", "April" }, { "4", "May" }, { "5", "June" }, { "6", "July" }, { "7", "August" }, { "8", "September" }, { "9", "October" }, { "10", "November" }, { "11", "December" } }; public static String getMonthDescription(int month) { for (int i = 0; i < months.length; i++) { if (Integer.parseInt(months[i][0]) == month) { return months[i][1]; } } return null; }}
If you enjoyed friday's post (A Pop-up Potpourii), make sure to check out the replies. There were some great error messages posted.
[Advertisement] Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!