Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I explicitly define the values my enumerated constants

I've heard before that I should simply let the compiler choose which values to assign for enumerated constants if I'm not doing something clever like using the values as bitmasks. If I'm just using enumeration values for more explicit code documentation, are there any gotchas that could creep in if I don't explicitly define all the values? I believe that values are assigned in ascending order. Should I define the 1st value to ensure the same values for each successive compilation?

like image 751
Joel B Avatar asked Jul 09 '12 15:07

Joel B


People also ask

How do you declare an enumeration constant?

The syntax for enumerated constants is to write the keyword enum, followed by the type name, an open brace, each of the legal values separated by a comma, and finally, a closing brace and a semicolon. Here's an example: enum COLOR { RED, BLUE, GREEN, WHITE, BLACK };

Can we define constants in enum?

A Java Enum is a Java type-class used to define collections of constants for your software according to your own needs. Each item in a Java enum is called a constant, an immutable variable — a value that cannot be changed.

Should I use enum for constants?

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 the proper use of an enumeration?

An enumeration, or Enum , is a symbolic name for a set of values. Enumerations are treated as data types, and you can use them to create sets of constants for use with variables and properties.


2 Answers

From C99, Section 6.7.2.2p3:

The identifiers in an enumerator list are declared as constants that have type int and may appear wherever such are permitted. An enumerator with = defines its enumeration constant as the value of the constant expression. If the first enumerator has no =, the value of its enumeration constant is 0. Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. (The use of enumerators with = may produce enumeration constants with values that duplicate other values in the same enumeration.)

The only time you need to assign a value to the first enumerator is if you want its value to be different from 0. Usually, you wouldn't do that, since the enumerators are often used to index an array.

Below is a simple example of "adding 1 to the value of the previous".

enum {
  A,
  B,
  C = 0,
  D,
  E
};

int main ()
{
    printf("%d\n", A);
    printf("%d\n", B);
    printf("%d\n", C);
    printf("%d\n", D);
    printf("%d\n", E);
    return 0;
}

The output for the above program is:

0
1
0
1
2

The only "gotcha" that I can think of is that if you want multiple enum names to represent the same value, you should remember to either stick them right next to the item you alias, or at the bottom of the enum list. I prefer the latter so I can count the enum lines to predict what the enum values will be.

enum {
    A,
    B,
    E,

    /* aliases below */
    C = A,
    D = B
};

Otherwise, an enumerator is just like an int literal.

like image 84
jxh Avatar answered Oct 15 '22 09:10

jxh


I think it will depend on your use of the type.

Advantages to not providing enum initializers:

  • Easier to add and remove values as you develop.
  • You won't accidentally define two identifiers with the same value.

Advantages to providing enum initializers:

  • Safe to serialize and deserialize the data. If you need to put these values in a file, network socket, etc., I would go ahead and write the values. (?)
  • Easier to look the values up for debugging purposes.
like image 5
aschepler Avatar answered Oct 15 '22 11:10

aschepler