CodeSOD: We Know How This Works


One of the selling points of a language like Java is that it comes with a rich standard library of useful classes. This allows developers to completely ignore those useful features, and instead reinvent the wheel badly. Once this novel square wheel has come off the assembly line, it becomes the defacto standard for the organization.

Take, for example, Caiwan’s office. They have a… special date-handling library.

public class DateUtil {

        private static final String DOT = ".";

        private static final String DATE_REGEX = "^("
                        + "((\\d{4})\\.(\\d{2})\\.(\\d{2})\\.?)" + "|"
                        + "((\\d{4})(\\d{2})(\\d{2}))"
                        + ")?$";
        private static final Pattern DATE_PATTERN = Pattern.compile(DATE_REGEX);

        //                   lengths for months:    0th Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
        private static final int[] MONTH_LENGTHS = {31, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

        private DateUtil() {}

        public static boolean isValid(String input) {
                if (StringUtils.isEmpty(input)) {
                        return true;
                }
                Matcher matcher = DATE_PATTERN.matcher(input);
                if (!matcher.matches()) {
                        return false;
                }
                if (matcher.group(2) != null) {
                        return isValidDate(matcher.group(3), matcher.group(4), matcher.group(5));
                } else if (matcher.group(6) != null) {
                        return isValidDate(matcher.group(7), matcher.group(8), matcher.group(9));
                }
                return false;
        }

        public static String getSimpleValue(String input) {
                if (!StringUtils.isEmpty(input)) {
                        Matcher matcher = DATE_PATTERN.matcher(input);
                        if (matcher.matches()) {
                                if (matcher.group(2) != null) {
                                        return matcher.group(3) + matcher.group(4) + matcher.group(5);
                                } else if (matcher.group(6) != null) {
                                        return matcher.group(7) + matcher.group(8) + matcher.group(9);
                                }
                        }
                }
                return input;
        }

        public static String getFormattedValue(String input) {
                if (!StringUtils.isEmpty(input)) {
                        Matcher matcher = DATE_PATTERN.matcher(input);
                        if (matcher.matches()) {
                                if (matcher.group(2) != null) {
                                        return matcher.group(3) + DOT + matcher.group(4) + DOT + matcher.group(5) + DOT;
                                } else if (matcher.group(6) != null) {
                                        return matcher.group(7) + DOT + matcher.group(8) + DOT + matcher.group(9) + DOT;
                                }
                        }
                }
                return input;
        }

        public static boolean isValidSzulIdo(String input) {
                if (StringUtils.isEmpty(input)) {
                        return true;
                }
                if (!isValid(input)) {
                        return false;
                }
                Matcher matcher = DATE_PATTERN.matcher(input);
                if (!matcher.matches()) {
                        return false;
                }
                String yearStr = "";
                if (matcher.group(2) != null) {
                        yearStr = matcher.group(3);
                } else if (matcher.group(6) != null) {
                        yearStr = matcher.group(7);
                }
                int year = new Integer(yearStr);
                return (1900 <= year && year < 2100);
        }

        @SuppressWarnings("unused")
        private static boolean isValidSzulIdo(Date input) {
                if (input == null) {
                        return true;
                }
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(input);
                int year = calendar.get(Calendar.YEAR);
                return (1900 <= year && year < 2100);
        }

        /*
         * preconditions:
         *     year matches "\\d{4}"
         *     month matches "\\d{2}"
         *     day matches "\\d{2}"
         */
        private static boolean isValidDate(String yearStr, String monthStr, String dayStr) {

                int year = Integer.valueOf(yearStr).intValue();
                int month = Integer.valueOf(monthStr).intValue();
                int day = Integer.valueOf(dayStr).intValue();

                if (month > 12) {
                        return false;
                }

                if (day > MONTH_LENGTHS[month]) {
                        return false;
                }

                // check February 29
                if (year > 0 && month == 2 && day > 28 && !isLeapYear(year)) {
                        return false;
                }

                return true;
        }

        private static boolean isLeapYear(int year) {
                return
                        (year % 4 != 0) ? false :
                                (year % 100 != 0) ? true :
                                        (year % 400 != 0) ? false :
                                                true;
        }
}

Caiwan asked the obvious question: WHHHHHHYYYYYYYYYYYYY?

The senior developer responsible explained:

Well, for starters, java.util is a big black box, nobody understands how it works. By doing it from scratch, we know exactly how it works. And testing? Testing is easy- wire it up to a UI, feed it inputs, check the outputs, and you know it works. And we know it works, because I copied a lot of this code off StackOverflow. Once it works, it’ll always work, if the code doesn’t change. That means we don’t need unit tests.

[Advertisement] Otter, ProGet, BuildMaster – robust, powerful, scalable, and reliable additions to your existing DevOps toolchain.

http://ift.tt/2y9Tldu

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s