Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where should we put our enums? [closed]

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++)?

like image 231
Zonata Avatar asked Dec 26 '22 21:12

Zonata


1 Answers

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

like image 171
Attila Avatar answered Jan 05 '23 18:01

Attila