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?
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:
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.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With