Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ideal way to organize Java constants

We have a huge projects based on an old jdk 1.4. We have migrated the web app to JDK 1.6 but still lot of inefficient practices and bad design exist in the code.

On of the major pain point huge java classes 2500+ lines of code in single java file. So many files like these.

In an attempt to refactor the classes I had started off by removing constants and putting constants in different Constants.java file. but since there are so many constants through-out the application, the constants file has the risk of growing to humongous proportions.

I would appreciate feedback on what strategy are developers adopting to keep code clean and maintainable.

like image 504
Reg Mem Avatar asked Feb 28 '12 16:02

Reg Mem


1 Answers

Keep your constants in the class they are related to, don't feel obligated to extract them. It may clean up the code of the class, but mixing unrelated constants in a file is not an improvement.

Keep things related together.

And also you can convert them to Enums when possible/useful (but that can require some refactoring).

like image 180
Matthieu Napoli Avatar answered Nov 13 '22 05:11

Matthieu Napoli