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

5

32

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";
}
Lillis answered 1/11, 2011 at 0:33 Comment(4)
Consider this, if you were creating a class to hold extension methods, would you give it a singular or plural name? Also think about the other methods that simply holds references to specific values/instances. e.g., Colors.Aesop
For what it's worth, Windows itself seems to use the plural: System.Windows.Media.Brushes, System.Windows.Media.Colors.Starchy
@RaymondChen not always though; for example System.Drawing.Color takes the opposite approach. -- it seems sometimes the WPF classes do weird things with naming.Hellkite
@RaymondChen And also consider System.Windows.Media.DashStyle vs. System.Windows.Media.DashStyles. Here, the latter is a predefined set of the former.Putrescible
J
37

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!)

Jackson answered 1/11, 2011 at 4:54 Comment(1)
+1. I like the same common-sensical approach to the situation. When naming anything, I make sure that it is pronouncable and usable in conversation, since there is a good chance that at one point or another, it might need to be communicated to another developer. While there are "standards" in place in how we name things, there are always exceptional circumstances that may dictate a departure from the norm.Osteitis
T
6

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

Tica answered 1/11, 2011 at 0:38 Comment(1)
+1 for the singular name. IMHO plural makes sense when writing the static class (this is a container with Tokens Foo, Bar, etc), whereas singular makes sense when using the static class (Do something with Token Foo). Because the 2nd case (using a token) should far outweigh the 1st case (defining tokens), singular is more appropriate.Thurman
H
0

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

Hudnut answered 1/11, 2011 at 0:37 Comment(1)
I should note that I often break the naming guideline for enums also and name them plural. It just feels more natural to me.Hudnut
C
0

I would use the plural name: Tokens

However you may consider creating a Token class for holding the const value.

This would be similar to System.Windows.Media.Colors where e.g. Colors.Blue returns a System.Windows.Media.Color instance.

public class Token
{
    public Token(string value)
    {
        Value = value;
    }

    public string Value { get; private set; }

    public static implicit operator string(Token token)
    {
        return token == null ? null : token.Value;
    }

    public bool Equals(string value)
    {
        return String.Equals(Value, value);
    }

    public override bool Equals(object obj)
    {
        var other = obj as Token;
        if (other == null)
        {
            return false;
        }

        return Equals(other.Value);
    }

    public override int GetHashCode()
    {
        return Value.GetHashCode();
    }

    public override string ToString()
    {
        return Value;
    }
}

public static class Tokens
{
    public static readonly Token Foo = new Token("Foo");
}

class Program
{
    static void Main(string[] args)
    {
        // You can use it as if they were string constants.
        string token = Tokens.Foo;
        bool areEquals = String.Equals(token, Tokens.Foo);
    }
}
Cini answered 15/4, 2016 at 11:50 Comment(0)
M
0

The question is quite old but still actual. I like to use plurals and not just because it feels more natural to me. If you create a nested enumeration in the class, you can't create the property with the same name. i.e.:

public class Token
{
    public TokenType TokenType { get; set; }

    public enum TokenType
    {
        Foo,
        Bar
    }
}

If you try to compile - you will get the compilation error "Error CS0102 The type 'Token' already contains a definition for 'TokenType'".

If you use singular for enum names - you need to come up with a unique name for the property, which is often inconvenient and unnatural.

But if you change the nested enumeration name to plural 'TokenTypes', there is no issue anymore. For consistency, I use the plural for each enumeration name, regardless of whether they are nested or not.

Methane answered 22/7 at 17:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.