Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Enum class filenames be suffixed with 'Enum'?

When breaking out classes into files, I am wondering what the naming convention is for class files containing only enums. Should they match the enum name, like any other class, or should they be suffixed with 'Enum' so that the developer knows that the file contains only an enum?

For example:

namespace MyCompanyName.MyApplication
{
    public enum Colors
    {
        Red,
        Blue,
        Green,
        Yellow
    }
}

Would you say the file containing the above code should be named 'Colors.cs' or 'ColorsEnum.cs'? Alternatively, perhaps there is another accepted naming convention for such files?

like image 614
Luke Baulch Avatar asked Jun 21 '11 23:06

Luke Baulch


People also ask

Should enum class names be plural?

Use a singular name for most Enum types, but use a plural name for Enum types that are bit fields. With the note that bit fields should be pluralized.

How should enums be named?

Enums are types, so they should be named using UpperCamelCase like classes. The enum values are constants, so they should be named using lowerCamelCase like constants, or ALL_CAPS if your code uses that legacy naming style.

Should enum name be plural or singular?

Enums in Java (and probably enums in general) should be singular. The thinking is that you're not selecting multiple Protocols, but rather one Protocol of the possible choices in the list of values.

Can enum and class have same name Java?

The enum value BAD resides in the general identifier scope, while the class type BAR resides in the class identifier scope. That is the reason why you are allowed to have both an enum value and a class with the same name: both names do not collide.


1 Answers

First off, this is a complete personal/team preference thing.

That being said:

I personally would name it Colors.cs. This keeps the filename corresponding to the type name. When I'm working on the API, if "Colors" are a fixed set of things represented by an enum, I'd probably know that, and there would be no confusion.

like image 177
Reed Copsey Avatar answered Sep 21 '22 21:09

Reed Copsey