Enumerated Data Types

Introduction

This article talks about Enumerated Data Types and set of guidelines in using them.

Background

Definition:

An Enumerated Type is a user defined data type that maps set of names to numeric values called Enumerators. Enums are generally used when we know all the possible values of a variable and want to express them in our own meaningful words.

Syntax:

enum identifier
{
enumerator-list
}

Here identifier is often called as tag of the enumeration specified by the list.

Notice that it does not include any fundamental data type in the declaration. We can say it creates a whole new data type from scratch without basing it on any other existing type.

If we don't give any values to the enumeartors, by default here, 0 stands for Colour_White, 1 stands for Colour_Yellow etc.

Characteristics:

An enumeration set can contain duplicate constant values. For example, you could associate the value 0 with two different identifiers in the same set.

The identifiers in the enumeration list must be distinct from other identifiers in the same scope with the same visibility, including ordinary variable names and identifiers in other enumeration lists.

Enumeration tags follow the normal scoping rules. They must be distinct from other enumeration, structure, and union tags with the same visibility.

As shown in the above program it is better to call with Enum as below:

a.compare(A::colour_green, A::colour_peach);

3. Use Enum Types for Reliability

An Enum type lets the compiler perform more thorough type checking than it can do with integers and named constants.

For Example:

If we have a named constant like:

const int Wednesday = 3;

And if we want to perform an assignement like:

colour = Wednesday;

This will not be a problem!!

But if we have Colour as an Enum type and then declaring it's variable as colour. Now compiler will allow the colour variable to be assigned with only defined colours in the Enum. Otherwise it will throw an compiler error.

4. Use Enum types for Maintainability

Enum types for better to maintain the program. If our program does not use Enum and down the line if we find there is a flaw giving as - 0 stands for colour_green, 1 stands for colour_white and 2 stands for colour_yellow as values, then we have to go through our code and change where all 0s, 1s, 2s are used to whatever the value we want to change it.

Instead, it is much easier now, since our program uses Enum, as it just needs the modification to the definition of Enum only. So we can change it to whatever the values we want and also we can do add some other colours to the enumerator list etc and just do re-compile it.

5. Use Enum Types as an Alternative to bool type variables

Many times, Boolean variable is not enough to express the meanings it needs to.

For Example:

We have a method, that returns True if it performs it's task successfully and it returns False otherwise.

Down the line, if we want this method to Return 2 different types for False like:

First type means that the task failed and it's effects are limited to method itself.

Second type means the task failed and caused a fatal error and that needs to be propagated to rest of the Application.

In this case, below Enum will be more meaningful and useful than having bool data type with True and False as return type of a method here.

Enum Status
{
Status_Success,
Status_Warning,
Status_FatalError
}

6. Check for invalid values

While testing an Enum type in an If or Case statement, check for invalid values.

Here, attepmting to loop through Colour, would interate through invalid values like 2,3,4 and 6

9. Reserve the first entry in an Enum defintion as an Invalid entry

Since many compilers by default assign the first entry of an Enum type to the value 0. So declaring it as an Invalid helps us to catch variables that were not initialised properly as they are more likely to be 0 than any other invalid value.

Having used the guidelines 7 & 9 in our Application for defining enumerations which helped us for better redability and to catch errors. But it has to be used consistently used whereever needed in the application and at the same time it has to be documented in an application specific coding standards, for the new comers to the project, to understand the same.