0

I have a Enum in which I want to add a logic to calculate value. this method is getting long. Want to know whether this is a good design or bad? Please see code snippet below:

public enum TestEnum{

    Value01(
            null,
            true,
            "Value",
            Utils.SMALLER_OR_EQUAL_TO_ZERO),

    Value02(
            null,
            false,
            "value2",
            Utils.BIGGER_THAN_ZERO),

    Value03(
            null,
            false,
            "value4",
            Utils.BIGGER_THAN_ZERO);

}
//Long method
public static TestEnum getValues(.....){

        if (Condition 1) {
           // some line of code
        }
        if (Condition 2) {
            // some lines of code
        } else {
            // // some lines of code
        }
        return values;
    }
Nicholas K
  • 15,148
  • 7
  • 31
  • 57
  • 4
    Long methods in general are generally bad. – Sweeper Sep 06 '19 at 06:42
  • 1
    Are your conditions based on enum values? If that's the case you may be able to move the content to each enum value declaration. You should give a bit more context. – assylias Sep 06 '19 at 06:47

1 Answers1

0

Long methods are actually bad design no matter where there are placed. Think about extracting some of this logic to another classes or at least to another, smaller, methods (take a look at this article)

Enums in Java are basically classes in Java, and it's popular to use them to create singleton implementations (Implementing Singleton with an Enum (in Java)). I believe there is no reason to not put such methods there

m.antkowicz
  • 13,268
  • 18
  • 37