[SOLVED] Purpose of Enumeration in a Real Program

x BlueRobot

Administrator
Staff member
Joined
May 7, 2013
Posts
10,400
I understand how enumerations work, but when would they be applied to a real-life program situation? I know the _KOBJECTS is a enumeration used by Windows to store the different types of a object.
 
You can get by without using them at all, they are only there to make the programmer/reader's life easier.

Any time you are working in a team environment enumerated types are often used to improve readability. (Or if your code may be modified/built on in the future)

In the case of _KOBJECTS it's to make the programmers life easier, that way you don't have to remember what each number maps to you can use the "English name".

You also see them used frequently to make loop indices easier.

I bet Ace will be able to give you a better answer based off his experiences.
 
Thanks, I was thinking it may just be added because of readability, but I wondered if it had a significant purpose too :smile9:
 
They improve readability but they are also defined as constant values, which is the other important thing. Who would remember all of the Win32 Error Codes? This is the reason why these are declared as macro's for instance. In a program where you have to define maybe a type of some kind of object, lets say a type of car for instance. Why store them as bulky strings to keep the readability? And why store them as integers where you have to refer to some kind of comment (if provided presumably), for which integer value represents which car? Keep in mind, strings are variable length, integers are typically 4 bytes per 32 bit integer. This would thus also improve the performance of your application slightly, and is much cleaner than dealing with strings. Enumerations also group identifiers together, as named integral values, which is the last but not least, important thing to note about them. With intellisense, it is easy to see all of what that enumeration details.
 
So essentially, a enumeration main purpose is readability and cleaner code with some performance benefits.
 
So essentially, a enumeration main purpose is readability and cleaner code with some performance benefits.

For the most part yes, honestly the compiler(if you're using a compiled language) is going to create just about the same code rather you use an enumerated type or you use their associated values.

Plus, if it is a performance gain to not use enumerated types a simple compiler flag will replace them all at compile time.
 

Has Sysnative Forums helped you? Please consider donating to help us support the site!

Back
Top