Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have seen C code where people used enum heavily. But all it does is confuse others. In many places plain integers can do the same thing with less ambiguity. What are the common misuses of enum?

share|improve this question
12  
Why would an int be less ambiguous? I would say that in general, an opaque integer is more ambiguous than an enum. Can you give an example? –  Dean Harding Dec 13 '10 at 9:52
    
@Dean Harding In my case, In some UI code the common practice is to make an enum for each menu. For each menu-item, there will be a enumeration. But their only purpose is to use instead of the index of the menu-item in the UI. Working there, I felt there should be plain integers, not the enums. –  Gulshan Dec 13 '10 at 15:03
1  
but without that enum, won't everything break if you add a new menu item to the top? –  Winston Ewert Dec 30 '10 at 1:29
    
I am not telling to avoid enum like we avoid goto. I am just asking for not misusing it. Like someone is replacing every possible integers with enums and if there are too much enum used in one c source file, it's confusing. –  Gulshan Dec 30 '10 at 3:18
1  
the problem is that we haven't seen a compelling example of where enum's are worse then plain integers. –  Winston Ewert Jan 4 '11 at 23:24
add comment

3 Answers

One case I can think of, though haven't seen in the wild yet, is to abuse enums as a collection of disjointed integer constants:

enum somestuff { days_in_week = 7,
                 months_in_year = 12,
                 number_of_planets = 8,
                 fingers_per_hand = 5 };

The most pathological, innocent looking example I can imagine would be

enum numbers { one, two, three, four, five, six, seven, eight, nine, ten };

because one==0, two==1 etc.

share|improve this answer
3  
An enum of numbers: Just in case mathematics is redefined you're probably OK! Just change the enum. –  Jonathan Hobbs Dec 13 '10 at 12:05
1  
I agree, but enum months { january = 1, feb, march, april, may, ... } is perfectly fine. –  Tim Post Dec 13 '10 at 12:23
7  
8 planets? In my heart, Pluto is still a planet. #TeamPluto :) –  splattne Dec 13 '10 at 13:49
2  
splattne: Sure, Pluto is a planet, but this is C, so counting is 0-based. ;-) –  user281377 Dec 13 '10 at 13:56
1  
BTW, I possess strong evidence suggesting Pluto's disqualification as a planet was actually a workaround for an off-by-one bug in IAU's planet management system (PMS). Disclosure on Wikileaks will happen the day after March 31st, 2011. –  user281377 Dec 13 '10 at 14:18
show 3 more comments

The biggest issue I've had with enumerations is them being defined twice.

EnumZ
{
    Yes,
    No,
    Maybeso
}

and someone else, with a very similar name, would have

EnumZee
{
    No,
    Maybeso,
    Yes
}

It's not a "quick catch" because that's the enum you made.. right? It's something you don't really see until you're stepping through the code and see that your 'No' is coming back as zero instead of one.

share|improve this answer
add comment

In C++ (would have posted as comment, but for the code this works better)

enum ELength
{
   mm = -3, 
   m = 0,
   km = 3,
   // ...
}


double operator*(double d, ELength scale)
{
   return d * pow(10, scale);
}

// allows:
double len = 2*mm;

Another applicaiton would be qualifiers:

 enum EConstraintMode
 {
    abs   = 1,
    rel   = 2,
    expand = 3,
 };


 struct Constraint
 {
    EConstraintMode mode;
    int value;
 }

 Constraint operator-(EConstraintMode mode, int value)
 {
    Constraint c = { mode, value };
    return c;
 }

 void SetConstraints(Constraint top, Constraint Left,....) { ... }

 SetConstraint(abs-12, rel-50, abs-12, rel-50);

I'm still undecided if this is wickedly cool, wickedly horrible, or both. (Wouldn't use that in production code, for that reason).

share|improve this answer
3  
oh my word that second example Terrifies me. –  Stephen Furlani Dec 13 '10 at 15:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.