tags:

views:

223

answers:

8

When I'm building a class library I usually create a file Enums.cs to hold all the enums used in the assembly. Here is an example:

namespace MyNamespace
{
    public enum Colors
    {
        Red,
        Green,
        Blue
    }
    public enum Shapes
    {
        Circle,
        Square,
        Triangle
    }
}

This makes all my enums easy to find, well organized and easy to access in code.

I'm wondering if there is any reason why this would not be such a good idea?

+2  A: 

We typically have an entire project, assembly that houses all our Constants, Enums, and helper Methods.

So YES, I would recomend placing them in a central, easily accessable location.

This will allow you to easily access the assembly from other assemblies, trying to avoid circular references.

astander
You put all of your enums in a separate *project?*
MusiGenesis
Enums that will be used in various other projects, yes.
astander
Well that makes sense, but I read your answer as saying that *all* your enums go in a separate project (even project-specific ones).
MusiGenesis
I prefer the logical separation rather than having it all mixed in throughout the project. I don't think I'd put it into a separate project but I would likely put it in it's own namespace to keep things organized.
Frank Hale
+11  A: 

I personally think this is not a good idea. I prefer to keep enums in their own file (one file per defined enum), just like a class. This makes it more obvious (to me) where to go if I'm looking for a type, by name. I treat enums just like a class or struct, or any other namespace-scoped type, which for me means one type per file.

The main downside to your approach arises if you have many Enums defined in your project - that "one file" can get quite large.

However, there is no technical difference in the compiled IL. This really is more of a personal, organizational strategy.

Reed Copsey
You meant, one file for each enum?
kzen
@kzen: Yeah - separate file per.
Reed Copsey
Are these files mixed with other class files or do you create a subfolder to hold all the enum files?
kzen
Having them in separate file also eases multiple checkout conflict resolution and branch integration. All IDEs make the actual file location of the enum pretty irrelevant (ie. they find/jump to the definition location immediately) so there are no serious drawbacks in having multiple files.
Remus Rusanu
@Remus: Exactly. I see a few disadvantages, and no real advantages, to putting them all in one file...
Reed Copsey
@kzen: I don't make separate folders. I have one subfolder per namespace, with all of the namespace types in place. (This keeps my folder structure in line with the object browser..) If you can't find your type in a folder because you have that many types, it's time to start considering moving things into separate namespaces, IMO.
Reed Copsey
@Reed: Do you have a file naming convention that allows you to quickly detect these enums and/or group them?
kzen
@kzen: I give them a very clean, clear name (based on the .NET naming conventions). This tends to make them very obvious. Between this and Visual Studio, I've never had an issue...
Reed Copsey
@kzen: Why are you trying to "group" enums, btw? If an enum really needs grouping, consider moving it into the class in question, and not having it global in the namespace scope. Otherwise, if it's shared between multiple classes, it should be (in my opinion) a public type of the namespace, and as a result, deserves it's own file ;) Enums deserve the same rights as other types :-D
Reed Copsey
@Reed: I like to keep the file count down to reduce the clutter so creating a file for each enum is just against my basic instincts...
kzen
@kzen: Smaller files are better for source control - more files are "clutter" - it's a balance you need to choose for your project and means. Most large-scale, enterprise applications have a lot of files anyways, and having the better ingration with source control and better discoverability of multiple files more than makes up for having "extra file clutter"... Personally, as I mentioned, if you get too many files in one folder, it means your namespace is getting crowded, and may need to be split anyways...
Reed Copsey
+13  A: 

It's generally better to arrange definitions by modularity, rather than by kind.

Barry Kelly
A point well made, and concisely made. Nice one.
T.J. Crowder
yep, and when it's arranged by modularity then you can arrange by kind... depends on the complexity of your application
Sebastien Lorber
+4  A: 

I think it depends on what areas of the code they apply to. If I'm looking at a function in a particular class, I would expect the associated enums to be nearby.

JoelHess
What if multiple classes utilize the same enum?
kzen
If the enum is generic, then it should be in a generic place. But even if you look at the MS frameworks, stuff like Colors is still under System.Drawing, even though it's used from lots of other places.
JoelHess
+8  A: 

Well, you could apply the same reasoning to all your delegate types, all your classes, all your structs.

By grouping things according to their significance to the compiler, you are losing the opportunity to group them in ways that are meaningful to you. The compiler already knows how to treat them according to their meaning in the language.

Personally when I want to find the declarations relevant to the "Parser" as opposed to the "Configuration Dialog", I go to the place where the code for that feature is kept. I don't have to look in five different places depending on the language features I'm using.

Daniel Earwicker
+1 I was about to write an answer which would have been very similar to this.
Bryan Watts
+2  A: 

I go for more of a Namespace organization. Visual Studio makes it too easy to navigate to an object's declaration. It makes more sense to me to have an enum related to database/data access in my MyData namespace rather than some generic enum namespace, i could care less about the file name.

Mike_G
A: 

There are certain code-file names that immediately raise a red flag for me. Along with utils.cpp, helpers.vb (or suchlike) if I see an enums.cs I immediately question whether a piece of software is becoming over coupled to enumerations.

Enumerations should be used sparingly and declared near the leaf (least type coupled) types that they are associated with.

Too often I have seen large systems that become over coupled because of shared enumerations. Before too long someone will have the bright idea to enumeration all of the database primary keys.

dkackman
+1  A: 

I'm a java developper but i think i can answer here too...

For me it's not a good idea. Like Daniel Earwicker said you could do the same for classes... you could handle your whole application in one single file with many inner classes (i assume in C# like in Java you can have inner classes...), but you don't do it anyway...

And an enum can sometime be a more complex structure, it can have attributes...

A (very) simple exemple in Java:

public enum JobPriority {
    HIGH(5),
    MEDIUM_HIGH(4),
    MEDIUM(3),
    MEDIUM_LOW(2),
    LOW(1)
    ;
    private int level;
    JobPriority(int level) {
        this.level = level;
    }
    public int getLevel() {
        return level;
    }
}

I work on a very big french website and we have enum structures a lot more complex than that... if we had all our enums in one single file this file would make hundreds of lines...

So if your application is small and enum structure is not complex, you can put all of them in one file.

But you'd rather put complex enums in single files on a enum namespace (package in java), and make a enum namespace when needed for each functional part of your application. For simple enums, why not adding then in a common enum file for a given namespace (you can have multiple common enums files). Dunno if it's a good practice.

Does it really matter to you to have many enum files? If you need to find a enum, your IDE perhaps offer you fast class search for finding easily enums no? You can also suffix all your enums by xxxEnum.cs to distinguish them (and also find them all fastly by class name *Enum).

In the java webapp i'm working on (~2 million lines of code, hundreds of enums), we can easily find enums in our Eclipse IDE with shortcuts like ctrl+shift+R + *Enum. It's a lot faster than having common enum files :)

Sebastien Lorber