views:

1816

answers:

6

I'm asking this question despite having read similar but not exactly what I want at http://stackoverflow.com/questions/495051/c-naming-convention-for-enum-and-matching-property

I found I have a tendency to name enums in plural and then 'use' them as singular, example:

public enum EntityTypes {
  Type1, Type2
}

public class SomeClass {
  /*
    some codes
  */

  public EntityTypes EntityType {get; set;}

}

Of course it works and this is my style, but can anyone find potential problem with such convention? I do have an "ugly" naming with the word "Status" though:

public enum OrderStatuses {
  Pending, Fulfilled, Error, Blah, Blah
}

public class SomeClass {
  /*
    some codes
  */

  public OrderStatuses OrderStatus {get; set;}

}

Additional Info: Maybe my question wasn't clear enough. I often have to think hard when naming the variables of the my defined enum types. I know the best practice, but it doesn't help to ease my job of naming those variables.

I can't possibly expose all my enum properties (say "Status") as "MyStatus".

My question: Can anyone find potential problem with my convention described above? It is NOT about best practice.

Question rephrase:

Well, I guess I should ask the question this way: Can someone come out a good generic way of naming the enum type such that when used, the naming of the enum 'instance' will be pretty straightforward?

+1  A: 

In general, the best practice reccommendation is singular, except for those enums that have the [Flags] attribute attached to them, (and which therefore can contain bit fields), which should be plural

After reading your edited question, I get teh feeling you may think the property name or variable name has to be different from the enum type name... It doesn't. The following is perfectly fine...

  public enum Status { New, Edited, Approved, Cancelled, Closed }

  public class Order
  {
      private Status stat;
      public Status Status
      { 
         get { return stat; }
         set { stat = value; }
      }
  }
Charles Bretana
True, I guess my method is a 'quick and lazy' way of avoiding the need to think of names when using the enums.
o.k.w
+19  A: 

Microsoft recommends using singular for Enums unless the Enum represents bit fields (use the FlagsAttribute as well). See Enumeration Type Naming Conventions (a subset of Microsoft's Naming Guidelines).

To respond to your clarification, I see nothing wrong with either of the following:

public enum OrderStatus { Pending, Fulfilled, Error };

public class SomeClass { 
    public OrderStatus OrderStatus { get; set; }
}

or

public enum OrderStatus { Pending, Fulfilled, Error };

public class SomeClass {
    public OrderStatus Status { get; set; }
}
Jason
Yes, this is a correct answer. This guidlines are used in the .Net Framework e.g. enum DayOfWeek and flags enum RegexOptions.
Shurup
Yes, this is the recommended practice, I welcome it. However it does not answer my question.
o.k.w
+6  A: 

I started out naming enums in the plural but have since changed to singular. Just seems to make more sense in the context of where they're used.

enum Status { Unknown = -1, Incomplete, Ready }

Status myStatus = Status.Ready;

Compare to:

Statuses myStatus = Statuses.Ready;

I find the singular form to sound more natural in context. We are in agreement that when declaring the enum, which happens in one place, we're thinking "this is a group of whatevers", but when using it, presumably in many places, that we're thinking "this is one whatever".

Bob Kaufman
+2  A: 

Best Practice - use singular. You have a list of items that make up an Enum. Using an item in the list sounds strange when you say Versions.1_0. It makes more sense to say Version.1_0 since there is only one 1_0 Version.

Jeremy Cron
+3  A: 

The situation never really applies to plural.

An enum shows an attribute of something or another. I'll give an example:

enum Humour
{
  Irony,
  Sarcasm,
  Slapstick,
  Nothing
}

You can have one type, but try think of it in the multiple, rather than plural:

Humour.Irony | Humour.Sarcasm

Rather than

Humours { Irony, Sarcasm }

You have a sense of humour, you don't have a sense of humours.

Kyle Rozendo
Haha, well, programmers are not always grammatically/politically correct. In your case, I probable use "HumourTypes". Bad habit I guess.
o.k.w
Yeah I love British humour.
Epaga
A: 

On the other thread http://stackoverflow.com/questions/495051/c-naming-convention-for-enum-and-matching-property someone pointed out what I think is a very good idea:

"I know my suggestion goes against the .NET Naming conventions, but I personally prefix enums with 'E' and enum flags with 'F' (similar to how we prefix Interfaces with 'I')."

RenniePet