Link to home
Start Free TrialLog in
Avatar of krazykoder
krazykoder

asked on

Enum standards / naming conventions

Hello,

Have couple questions about enums
First off I tend to nest my enums in my classes like...

ie.
public class Address
{
    // Enums
    public enum AddressType { Home, Billing, Alternative };

    // Member Variables
    private AddressType m_Type;

    // Properties
    public AddressType Type
    {
        get { return m_Type; }
        set { m_Type = value; }
    }
}

Is this good, bad, or just comes down to preference?
Where is the most logical place for it if it's not nested, same namespace as class?

My biggest issue seems to be that I always seem to want to name my Property the same as my Enum but can't cause of name collision.  For example in the above code sample i really wanted to name the property AddressType instead of Type.  It just seems natural to me that the enum should be named AddressType, but at the same time i also think the property should be named AddressType.

I checked MSDN naming conventions but the info didnt seem to help me.
Maybe my problem is just the fact that i should not be nesting my enums to begin with.
What would the correct standard be? Or at least what works well for you?

If you don't nest them what file do you put them in, all enums in same file or would you put it in the same file as the class it pertains to, If it doesnt pertain to more than 1 class that is.  So in my example above, lets say my AddressType enum will only be used in Address class, should i just put in that file, just not nested in the Address class?

What about an enum that is used in multiple classes, where best place for it to live?

Sorry for so many questions, I may be splitting hairs here but just a little confused on the best way to structure things, or at least the most commonly used way, since there may not be a "best" way.

Thanks!

Avatar of JimBrandley
JimBrandley
Flag of United States of America image

I think it's natural to place an enum that's associated with one class inside that class. In this case, you could:
public enum TYPE { Home, Billing, Alternative };

Then external references can be Address.TYPE.Home. The capitalization is unfortunate, but you might have trouble with Type. Then, your property can be:
    // Properties
    public TYPE AddressType
    {
        get { return m_Type; }
        set { m_Type = value; }
    }

Jim
Avatar of krazykoder
krazykoder

ASKER

i appreciate your feedback JimBrandley.

However I would rather stick with what i have then deal with that uppercase TYPE, I understand Type is probabaly a keyword, but at any rate that is just flip flopping what i already have.

I was kind of looking for a more global solution since i seem to run into this same exact scenario many times.  I can always solve it one way or another by giving the enum and property a different name, but was tryign to change my coding practices to prevent from running into this same scenario over and over.

At the same time don't want to break any of the conventions that are the norm.

i.e.
Enum names should be PascalCase
Property names should be PascalCase

I'm almost tempted to suffix my nested enums with Enum to prevent the name collision.
i.e.
enum AddressTypeEnum
public AddressTypeEnum AddressType

only thing is the naming convention on MSDN says not to do that :(
ASKER CERTIFIED SOLUTION
Avatar of JimBrandley
JimBrandley
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I use to name enumeration names starting with E, like EType or EAddrType
Not sure why there is an "Accepted Solution" that only offers up sympathy to the problem.  The specific questions are:

Is it a bad convention to use the same name for everything?  AddressType enum, AddressType property - the point is, it COMPILES - so what no one is actually saying is "yes, do it that way", and why not?  None of the enum naming guidelines suggest anything to distinguish the enum from the property.

Second - where to put enums?  In the class, in the namespace?  Seems like scope may be part of the answer, as in, if it is only used internally to the class, put the enum in the class, but if it is needed by external objects, put it in the namespace.  There is also the question of whether to put the enum in the same file as the class, or to have a separate file containing all of your enums.

I would hope that an expert would adress all these concerns and provide reasoning and justification for these choices, or perhaps point out that it is a matter of personal preference and/or style that will drive these decisions, as there does not seem to be any guideline that I can find.