Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it good practice to use ordinal of enum?

I have an enum:

public enum Persons {

    CHILD,
    PARENT,
    GRANDPARENT;

}

Is there any problem with using ordinal() method to check "hierarchy" between enum members? I mean - is there any disadvantages when using it excluding verbosity, when somebody can change accidentally order in future.

Or is it better to do something like that:

public enum Persons {

    CHILD(0),
    PARENT(1),
    GRANDPARENT(2);

    private Integer hierarchy;

    private Persons(final Integer hierarchy) {
        this.hierarchy = hierarchy;
    }

    public Integer getHierarchy() {
        return hierarchy;
    }

}
like image 326
ByeBye Avatar asked Jun 20 '17 13:06

ByeBye


People also ask

What does the ordinal () method from enum do?

enum ordinal() The ordinal() method returns the order of an enum instance. It represents the sequence in the enum declaration, where the initial constant is assigned an ordinal of '0' . It is very much like array indexes.

Should enums be numbers?

Enums are always assigned numeric values when they are stored. The first value always takes the numeric value of 0, while the other values in the enum are incremented by 1.

Is it better to use enum or constant?

Enums are lists of constants. When you need a predefined list of values which do represent some kind of numeric or textual data, you should use an enum. You should always use enums when a variable (especially a method parameter) can only take one out of a small set of possible values.

What is enum constant ordinal?

name - - The name of this enum constant, which is the identifier used to declare it. ordinal - - The ordinal of this enumeration constant (its position in the enum declaration, where the initial constant is assigned an ordinal of zero).


2 Answers

TLDR: No, you should not!

If you refer to the javadoc for ordinal method in Enum.java:

Most programmers will have no use for this method. It is designed for use by sophisticated enum-based data structures, such as java.util.EnumSet and java.util.EnumMap.

Firstly - read the manual (javadoc in this case).

Secondly - don't write brittle code. The enum values may change in future and your second code example is much more clear and maintainable.

You definitely don't want to create problems for the future if a new enum value is (say) inserted between PARENT and GRANDPARENT.

like image 117
vikingsteve Avatar answered Oct 09 '22 12:10

vikingsteve


As suggested by Joshua Bloch in Effective Java, it's not a good idea to derive a value associated with an enum from its ordinal, because changes to the ordering of the enum values might break the logic you encoded.

The second approach you mention follows exactly what the author proposes, which is storing the value in a separate field.

I would say that the alternative you suggested is definitely better because it is more extendable and maintainable, as you are decoupling the ordering of the enum values and the notion of hierarchy.

like image 17
msf1013 Avatar answered Oct 09 '22 12:10

msf1013