4

A short question that always bother me when I develop in Java. I actually use a lot of different enums and I am never sure of where I should put them. Usually, I create a special package named enumeration which I am quite sure is not the best practice. Should I put my enums directly in the same package than the group of class it most belong to?

Also, would it be the same for another language (C# or C++)?

assylias
  • 321,522
  • 82
  • 660
  • 783
Zonata
  • 471
  • 2
  • 6
  • 20

5 Answers5

7

The best descision depends on how the enumeration is used.

E.g.

  • if the enumeration is only used in one class, you can make it an inner class to that class
  • if the enumeration is used by one class (e.g. A) more than others (e.g. the others call methods on A that have parameters of type of the enumeration; you might want to put it in the same package/namespace as A is in.

In general, find the best package/namespace based on where its used most

Attila
  • 28,265
  • 3
  • 46
  • 55
1

When my application grew large enough, we actually migrated all of our enums to a single C# csproj/DLL file. The file was very small, but because it was completely detached from all applications, it meant that:

  • People always knew where to look to find an enum
  • Reduced likelihood of "recreating" the same enum twice
  • It could be used in both server applications and client applications directly to ensure that both were using identical definitions

I don't claim this is the best solution for everyone, but it really reduced our hassle level.

Ted Spence
  • 2,598
  • 1
  • 21
  • 21
1

The declaration and placement of enums follow the same rules as the declaration and placement of other variables regarding visibility and block-hierarchy. That means if you put the code of an enum inside a class A then that enum will belong to class A.

I am also using enums extensively and what I try to do in order to create a clean and correct model is to put the enum where it closely belongs because most of the time I will use it at this place - no matter if it's a namespace, a clas or a nested class. When I am using the enum from anywhere else I will have to explicitly need to use the parent scope of the enum which again models the semantics - if coded correctly - best.

So it's primarily a question of object-related real-world mapping, semantic consistency and code-reusability I would say.

marc wellman
  • 5,808
  • 5
  • 32
  • 59
1

I don't know about Java or C#, but in C++, I always put in C++ class specification if its strongly related to class. If it's used across different classes, I keep it in a separate header file ( in a separate namespace where system specifics enums and constants are kept).

asit_dhal
  • 1,239
  • 19
  • 35
0

Use nested namespaces (see other thread here: Fuller information on this). The main thing is it reduces dependencies outside the subsystem in question.

So in the enum header file you get:

// MyEnumHeader.h
// Consolidated enum header file for this dll,lib,subsystem whatever.
namespace MyApp
{
  namespace MyEnums
  {
    enum SomeEnum { EnumVal0, EnumVal1, EnumVal2 };
  };
};

And then in the class header file you get:

// MyInterfaceHeader.h
// Class interfaces for the subsystem with all the expected dependencies.

#include "MyEnumHeader.h"

namespace MyApp
{
  class MyInterface
  {
  public:
    virtual void DoSomethingWithEnumParam (MyEnums::SomeEnum enumParam) = 0;
  };
};

Then in client code use "using" syntax:

using namespace MyApp::MyEnums;
Community
  • 1
  • 1
charlest
  • 31
  • 4