Concept Flow - Enum vs constants decision
Start
Need named values?
Yes
Are values related and fixed set?
No→Use constants
Yes
Use enum
Use in code
End
Decide if you need a fixed set of related named values (enum) or just separate constant values.
enum Color { Red, Green, Blue } const int MaxValue = 100; Color c = Color.Green; int max = MaxValue;
| Step | Action | Evaluation | Result |
|---|---|---|---|
| 1 | Define enum Color with Red=0, Green=1, Blue=2 | N/A | Enum Color created |
| 2 | Define constant MaxValue = 100 | N/A | Constant MaxValue created |
| 3 | Assign c = Color.Green | Color.Green value is 1 | c = 1 |
| 4 | Assign max = MaxValue | MaxValue is 100 | max = 100 |
| 5 | Use c and max in code | c is enum value 1, max is 100 | Values ready for use |
| Variable | Start | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|
| c | undefined | 1 (Color.Green) | 1 | 1 |
| max | undefined | undefined | 100 | 100 |
Enum vs Constants Decision: - Use enum for fixed sets of related named values. - Use constants for single fixed values. - Enums improve code clarity and safety. - Constants are simple but less structured. - Choose based on grouping and usage needs.