Search code examples
javaif-statementcoding-style

How can avoid if else statements and rewrite them into smaller and cleaner code?


I am supposed to insert an argument by the form of yyyy""mm""dd, while using " / ", " . " or " - " as seperators. The programm identifies which seperator was used, the other functions are not relevant. However there are way too many if else statements that I am sure can be rewritten or replaced with cleaner code. Any suggestions?

static String[] weekDays = new String[] {
        "Sun", "Mon", "Tues", "Wednes", "Thurs", "Fri", "Satur"
};

enum weekDays {SUN,MON,TUES,WEDNES,THURS,FRI,SATUR};

static void setCalendarDate(final Calendar cal, final String date, final String seperator) {
    final String[] splitDate = date.split(seperator);
    cal.set(parseInt(splitDate[0]), parseInt(splitDate[1]) - 1, parseInt(splitDate[2]));
}

public static void main(final String[] args) {

    final Calendar cal = Calendar.getInstance();
    if (args.length != 0) {
        if (args[0].contains("-")) {
            setCalendarDate(cal, args[0], "-");
        } else if (args[0].contains("/")) {
            setCalendarDate(cal, args[0], "/");
        } else if (args[0].contains(".")) {
            setCalendarDate(cal, args[0],".");
        }
    }
    final int weekDay = cal.get(Calendar.DAY_OF_WEEK);
    System.out.printf("%sday\n", weekDays[weekDay - 1]);
}

}


Solution

  • Personally I would store an array of valid separators like so:

    static final String[] validSeparators = new String[] {"-","/","."};
    

    You can move the separator check to the setCalendarDate method itself, checking if the string contains any of the appropriate characters. Then I would refactor the method to throw an exception on invalid separators, and remove the separator argument.

    static void setCalendarDate(final Calendar cal, final String date) throws ParseException{
        String separator = null;
        for(String s : validSeparators) {
            if(date.contains(s)) {
                separator = s;
                break;
            }
        }
        if(separator == null) {
            throw new ParseException("No valid separator found", 0);
        }
    
        final String[] splitDate = date.split(separator);
        cal.set(Integer.parseInt(splitDate[0]), Integer.parseInt(splitDate[1]) - 1, Integer.parseInt(splitDate[2]));
    }
    

    Another option would be to check a specific character of the date string:

    static void setCalendarDate(final Calendar cal, final String date) throws ParseException{       
        String separator = String.valueOf(date.charAt(4));
        if(Arrays.asList(validSeparators).contains(separator)) {
            final String[] splitDate = date.split(separator);
            cal.set(Integer.parseInt(splitDate[0]), Integer.parseInt(splitDate[1]) - 1, Integer.parseInt(splitDate[2]));
        } else {
            throw new ParseException("Valid separator not found", 0);
        }
    }
    

    If you wanted extra safety you could also check the 7th character and compare them before the split.

    Finally, your main would now look like this:

    final Calendar cal = Calendar.getInstance();
    if (args.length != 0) {
    
        try {
            setCalendarDate(cal, args[0]);
        } catch (ParseException ex) {
            ex.printStackTrace();
        }
    }
    final int weekDay = cal.get(Calendar.DAY_OF_WEEK);
    System.out.printf("%sday\n", weekDays[weekDay - 1]);