Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Never use public nested enums?

I recently came across a coding standard claiming that you should never use public inner enums/classes in Java. This is the first time I've encountered this convention, and haven't been able to find a satisfactory explaniation as to why.

I understand why public inner classes should be avoided, but for what reasons would you never use public nested enums? Or, why is this a bad convention to follow?

like image 676
AEW Avatar asked Nov 10 '10 21:11

AEW


Video Answer


1 Answers

Disclaimer: The following are not hard and fast rules. These are just my opinions and personal preferences. I personally find that it makes my code easier to read and easier to maintain.

First a question. Where did you come across this advice? Did they provide any rationale?

In my experience, I've normally used private nested enums to enhance readability and maintainability of my code. This especially comes into play when you're performing an integration with another system and you have to send in specific strings or numbers. I find that using an enum makes things easier to read and maintain. In this specific case, the information conveyed by the enum is limited in scope to the class (i.e., it makes no sense outside the class and no one else should need it).

I can't think of a definitive reason that says why public inner enums are a bad practice. But here are reasons why I don't (normally) use them:

  • If the enum is public, it essentially means that it conveys information in a larger scope (i.e., makes sense outside the scope of its parent class and so it probably means that other things are using it). If this is the case, then it might make more sense to make it a standalone enum.
  • I find it ThirdPartyResponseCodes.Success easier on the eyes rather than ThirdPartyIntegrationInterface.ThirdPartyResponseCodes.Success. There is no reason why you cannot name the enum appropriately to provide context and thus make it a standalone enum.
  • I would imagine that the same reasons for not using public inner classes also apply to public inner enums. First of all, if you have an inner class, it may mean that your outer class might benefit from refactoring. Perhaps your outer class is trying to do too much? On the flip side though, there is the benefit of encapsulation and limiting scope, especially if you can make the argument that the inner class only makes sense in the context of the outer class (which should go without saying if it is private). If it is a public inner class, then that probably means that its scope extends past the outer class and thus it would need to be pulled out.
  • If you really do need to use a public nested enum then you should document exactly why you would need it (I haven't found a reason to do so yet) and why it is better to leave it as an public nested enum and not a public standalone enum.
like image 129
Vivin Paliath Avatar answered Oct 13 '22 21:10

Vivin Paliath