Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Naming convention for class of constants in C#: plural or singular?

The guidelines are clear for enumerations...

Do use a singular name for an enumeration, unless its values are bit fields.

(Source: http://msdn.microsoft.com/en-us/library/ms229040.aspx)

...but not so clear for a class of constants (or read-only static fields/propertes). For example, should the name of this class be singular or plural?

public static class Token // or Tokens?
{
    public const string Foo = "Foo";
    public const string Bar = "Bar";
    public const string Doo = "Doo";
    public const string Hicky = "Hicky";
}
like image 588
devuxer Avatar asked Nov 01 '11 00:11

devuxer


People also ask

What is the naming convention for constants?

Constants should be written in uppercase characters separated by underscores. Constant names may also contain digits if appropriate, but not as the first character.

What is naming convention for classes?

Class names should be nouns, in mixed case with the first letter of each internal word capitalized. Try to keep your class names simple and descriptive. Use whole words-avoid acronyms and abbreviations (unless the abbreviation is much more widely used than the long form, such as URL or HTML).

What is the naming convention for variables in C?

Rules for naming a variableA variable name can only have letters (both uppercase and lowercase letters), digits and underscore. The first letter of a variable should be either a letter or an underscore. There is no rule on how long a variable name (identifier) can be.

Does C use CamelCase or Snakecase?

Classic C doesn't use camel-case; I've written code in camel-case in C, and it looks weird (so I don't do it like that any more). That said, it isn't wrong - and consistency is more important than which convention is used.


3 Answers

I would use the plural: Tokens. This implies that the static class is serving as a collection of items of some sort (whose runtime types are not that of the class).

On the other hand, an enumeration's fields are instances of the enumeration type. For example, TypeCode.String is a TypeCode. It would be weird to say that TypeCodes.String is a TypeCodes.

However, in your Tokens example, using the singular gives us Token.Foo, which is a token, but it is not a Token (it is a string).

(Or, if you use the plural class name, Tokens.Foo is a string, not a Tokens. Ack!)

like image 52
phoog Avatar answered Sep 18 '22 17:09

phoog


Since both are used essentially the same way, and are conceptually the same thing, I'd recommend just following the enum guidelines.

like image 36
kprobst Avatar answered Sep 19 '22 17:09

kprobst


I don't have any official naming standard to link to, but I can tell you what I would do.

I would use the plural name: Tokens

like image 31
Dylan Smith Avatar answered Sep 17 '22 17:09

Dylan Smith