Enumerated Data Types
This article talks about Enumerated Data Types and set of guidelines in using them.
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.
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.
Guidelines for using Enumerated Data Types
1. Use Enum types for better Readability
Instead of writing:
if (mycolour == 2);
We can better write it as:
if (mycolour == colour_yellow)
2. Use for Routine Paramters
Calling Routines will be better with Enum types as below:
Instead of calling a function :
As shown in the above program it is better to call with Enum as below:
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.
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.
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:
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.
6. Check for invalid values
While testing an Enum type in an If or Case statement, check for invalid values.
7. Define the first and last entries of an Enum to use them as loop limits
Defining the first and last entries of an Enum as colour_first and colour_last or day_first and day_last etc allows us to iterate all the elements in an Enum.
You can write some special methods using switch statement to print enum names as they are.
8. Beware of pitfalls of assigning explicit values to elements of an enumeartion
We can even assign specific values to enumeartions as below and in such case looping through the Enum would result iterating through 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.
Hope it helps!!
Re: Enumerated Data Types
Nomination for Article of the month - Feb 2010 started. Nominate this article.
|All times are GMT +5.5. The time now is 07:32.|