top of page

different styles when coding #6

every developer comes into contact with conditions very quickly. there is no way one can write an application without a single check if the given input is valid or not. i'm not going to talk about simple if statements or the ternary operator. what if a condition consists of multiple branches with or statements for example, how would you write such a block of code?


the first idea that comes in mind is maybe the classic if-else statement, which can be stringed together very easily. another option is to use switch-cases. with the introduction of the new switch expressions, it becomes more attractive for developers. there is another alternative using maps. it might sound weird, but it has also its benefits.


so lets have a look at the three styles.


style 1:

public static String getSeason1(final Month month) {
    return switch (month) {
        case JANUARY, FEBRUARY, MARCH -> "SEASON1";
        case APRIL, MAY, JUNE -> "SEASON2";
        case JULY, AUGUST, SEPTEMBER -> "SEASON3";
        case OCTOBER, NOVEMBER, DECEMBER -> "SEASON4";
    };
}

style 2:

public static String getSeason2(final Month month) {
    if (month == Month.JANUARY || month == Month.FEBRUARY || month == Month.MARCH) {
        return "SEASON1";
    } else if (month == Month.APRIL || month == Month.MAY || month == Month.JUNE) {
        return "SEASON2";
    } else if (month == Month.JULY || month == Month.AUGUST || month == Month.SEPTEMBER) {
        return "SEASON3";
    } else if (month == Month.OCTOBER || month == Month.NOVEMBER || month == Month.DECEMBER) {
        return "SEASON4";
    }
    throw new IllegalArgumentException();
}

style 3:

private static final Map<Month, String> SEASON_BY_MONTH = new EnumMap<>(Month.class);

static {
    SEASON_BY_MONTH.put(Month.JANUARY, "SEASON1");
    SEASON_BY_MONTH.put(Month.FEBRUARY, "SEASON1");
    SEASON_BY_MONTH.put(Month.MARCH, "SEASON1");
    SEASON_BY_MONTH.put(Month.APRIL, "SEASON2");
    SEASON_BY_MONTH.put(Month.MAY, "SEASON2");
    SEASON_BY_MONTH.put(Month.JUNE, "SEASON2");
    SEASON_BY_MONTH.put(Month.JULY, "SEASON3");
    SEASON_BY_MONTH.put(Month.AUGUST, "SEASON3");
    SEASON_BY_MONTH.put(Month.SEPTEMBER, "SEASON3");
    SEASON_BY_MONTH.put(Month.OCTOBER, "SEASON4");
    SEASON_BY_MONTH.put(Month.NOVEMBER, "SEASON4");
    SEASON_BY_MONTH.put(Month.DECEMBER, "SEASON4");
}

public static String getSeason3(final Month month) {
    return Optional.ofNullable(SEASON_BY_MONTH.get(month)).orElseThrow(IllegalArgumentException::new);
}

which style do you use the most and which is a no-go, tell me about it in the comments. i personally like the new switch expressions a lot. the map style can come in handy if you want to keep your method small, also you can move the initialization part to a configuration class, but the logic is a bit hidden from the developer.


you can download the source code on github: https://github.com/KlemensM/styles-6


Recent Posts

See All
bottom of page