I know what enum does and how it works but what to use it for is the question...
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
Any time you have a list of things that you would normally number. For example, I use it for types of things or states. Rather than saying something hard to read like:
if (CurrentState == 3)
{
SetState(5);
}
you can say...
if (CurrentState == STATE_HUNTING)
{
SetState(STATE_ATTACKING);
}
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
if (CurrentState == 3)
{
SetState(5);
}
you can say...
if (CurrentState == STATE_HUNTING)
{
SetState(STATE_ATTACKING);
}
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
well it''s basically a nice define replacement.
enum Game_States
{
Running,
Paused,
Main_Menu,
Config_Menu,
Done
};
or
enum Player_State
{
Running,
Jumping,
Walking,
Dead,
}
or
enum Enemy_State
{
Tracking,
Running,
Firing,
Follow_Path
}
and so on you get the general idea.
enum Game_States
{
Running,
Paused,
Main_Menu,
Config_Menu,
Done
};
or
enum Player_State
{
Running,
Jumping,
Walking,
Dead,
}
or
enum Enemy_State
{
Tracking,
Running,
Firing,
Follow_Path
}
and so on you get the general idea.
To help get rid of magic numbers for one:
etc....
_____________________________
And the Phoenix shall rise from the ashes...
--Thunder_Hawk -- ¦þ
______________________________
enum MODE {START, PLAY, NEW, RESET, PAUSE, GAMEOVER, QUIT, GOBACK};mode = PLAY;if (mode == PLAY) { DoGameLogic();}
etc....
_____________________________
And the Phoenix shall rise from the ashes...
--Thunder_Hawk -- ¦þ
______________________________
As people have said, it replaces a #define, but with another feature besides source code readability. The debugger knows about your enums. When you look at an enum variable, it will show a (hopefully) meaningful name rather than a number. Want to change the value of the variable in the debugger. Type in a new name from the enum, or a number, your choice.
1) Its easier to read the code.
2) Less bugs... Think of a calculator program. You have Operation "Add", "Multiply", "Divide" with the numbers 1,2,3.
How about if you want to add in "Subtract" as being #2 instead.
You'd have to look throughout the entire code where anything is checking the operation variable (switches or if...statements) and change them to use the new number values.
If you made them ENUM's, you would just change the ENUM's around and all the ifs and switch statements would all be converted for you.
It reduces the number of bugs in case you did change the scale. And it doesn't slow down the code at all (its figured out at compile-time, not during run-time)... #2 is the main reason to do enums... My work does enums ALL THE TIME. If there is ever is a scale of numbers to do operations its always enumed...
[edited by - JoeyBlow2 on January 28, 2003 5:11:03 PM]
2) Less bugs... Think of a calculator program. You have Operation "Add", "Multiply", "Divide" with the numbers 1,2,3.
switch(operation){ case 1: number+= value; case 2: number*= value; case 3: number/= value;}
How about if you want to add in "Subtract" as being #2 instead.
You'd have to look throughout the entire code where anything is checking the operation variable (switches or if...statements) and change them to use the new number values.
If you made them ENUM's, you would just change the ENUM's around and all the ifs and switch statements would all be converted for you.
enum{ ENUM_ADD, ENUM_SUBTRACT, ENUM_MULTIPLY, ENUM_DIVIDE};switch(operation){ case ENUM_ADD: number+= value; case ENUM_MULTIPLY: number*= value; case ENUM_DIVIDE: number/= value;}
It reduces the number of bugs in case you did change the scale. And it doesn't slow down the code at all (its figured out at compile-time, not during run-time)... #2 is the main reason to do enums... My work does enums ALL THE TIME. If there is ever is a scale of numbers to do operations its always enumed...
[edited by - JoeyBlow2 on January 28, 2003 5:11:03 PM]
So...why would you number like the states of a program?
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
tcache
Contact Me
-----------
AH! MY BRAIN IS GOING TO SELF-DETONATE! -- Yours Truly (Jan, 2003)
quote:Original post by Tazel
So...why would you number like the states of a program?
Uh... because you have to keep them separate somehow. How else would you be able to determine what state you are in?
Dave Mark - President and Lead Designer
Intrinsic Algorithm - "Reducing the world to mathematical equations!"
It's all about readability, maintenance, and the fact that some guy who's never seen your code before is going to be messing with it at some point.
Tell me, which would you rather see when you have to jump in and do some maintenance on code that isn't yours (or even code that IS yours but you haven't touched in a few weeks or months or years), 200 case statements numbered 1 to 200, or 200 case statements with things like
case STATE_WALKING:
?
Yes, you could just use a #define instead... but if you use an enum you don't have to go hunting to see if you already used that number for a #define earlier... and it's easier to control scope... and it's typed according to the enum...
-fel
[edited by - felisandria on January 28, 2003 6:01:20 PM]
Tell me, which would you rather see when you have to jump in and do some maintenance on code that isn't yours (or even code that IS yours but you haven't touched in a few weeks or months or years), 200 case statements numbered 1 to 200, or 200 case statements with things like
case STATE_WALKING:
?
Yes, you could just use a #define instead... but if you use an enum you don't have to go hunting to see if you already used that number for a #define earlier... and it's easier to control scope... and it's typed according to the enum...
-fel
[edited by - felisandria on January 28, 2003 6:01:20 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement