Technocracy-Calendar

One of the more difficult things for beginning programmers to pick up is computer-minded thinking. Sure, if you're reading this, it's probably easy for you to look at a system and plot out how to get the outputs you want in one area out of the information you have in another. For someone who's been programming for years, it's practically second nature. When mentoring interns or teaching beginners, however, it can readily become apparent just how strange this mindset can be to newcomers.

We don't know this date-parsing code was written by a newbie ... but we have our suspicions:


/**
 *
 * @param date
 * @return
 * @throws Exception
 */
public static Date getPreviousSaturDay(Date date) throws Exception {
        Calendar calendar = Calendar.getInstance();
        Date saturday = null;
        if (date != null) {
                calendar.setTime(date);
                int day = calendar.get(Calendar.DAY_OF_WEEK);
                if (day == Calendar.SATURDAY) {
                        calendar.add(Calendar.DATE, 0);
                } else if (day == Calendar.SUNDAY) {
                        calendar.add(Calendar.DATE, -1);
                } else if (day == Calendar.MONDAY) {
                        calendar.add(Calendar.DATE, -2);
                } else if (day == Calendar.TUESDAY) {
                        calendar.add(Calendar.DATE, -3);
                } else if (day == Calendar.WEDNESDAY) {
                        calendar.add(Calendar.DATE, -4);
                } else if (day == Calendar.THURSDAY) {
                        calendar.add(Calendar.DATE, -5);
                } else if (day == Calendar.FRIDAY) {
                        calendar.add(Calendar.DATE, -6);
                }
                SimpleDateFormat simpledateformat = new SimpleDateFormat(
                                DateUtil.MM_DD_YYYY_DATE_PATTERN);
                saturday = simpledateformat.parse(simpledateformat.format(calendar
                                                .getTime()));
        }
        return saturday;
}

No javadoc other than a placeholder the IDE probably generated? Bizzarely-cased method name? Throwing a generic Exception? These are all signs of someone without a firm grasp on the language, but possibly not a total newcomer to programming. After all, someone who's only done hobby work in loosely typed languages could make all the above mistakes. The real hallmark is the strange, twisty path the method takes to get from point A to point B.

Starting with a Date, the method aims to return a Date that represents the previous Saturday. With JodaTime (or Java 8's new Time library, which is essentially the same), this is a simple task: date.withDayOfWeek(DateTimeConstants.SATURDAY) will get you the nearest Saturday, and from there it's a simple comparison to see if you need to subtract a week. But this uses the famously terrible Calendar library that has plagued Java for decades, which means you have to have a firm grasp on what you're doing in order to work with it successfully.

Still, the mistakes made aren't related to the date library, but to a fundamental lack of understanding of the task at hand. Stating with a Date, they turn it into a Calendar and get the day of the week the input represented. They then use an if-ladder to manipulate the Calendar, subtracting the right number of days. Then, at a loss for how to turn the Calendar back into a Date, they extract the Date from the Calendar, run it through a formatter to turn it into a string, then run that string through a parser to get a Date.

What?

Unfortunately, this wasn't a school project or some other learning exercise. This was found in production code.

[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!