Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should constants be defined in their own files?

I've noticed some projects like to store constants in their own file, i.e constants used globally and in the main program loop might clutter the main file so perhaps they look to place them elsewhere and then reference/import file/class.

I understand that when writing an OOP class that you'd want to keep all constants at the header of the class file so they can be referenced statically as such:

myCar.setColour(Colour.RED);

Where RED is a colour constant in the Colour class.

What is good practice for having a large amount of constants, should they just be at the top of your main file or is it in any way wise to have maybe a ProgramConstants class that is purely static, public and available to read?

like image 398
insidesin Avatar asked Jul 17 '15 10:07

insidesin


People also ask

Should constants be in their own file?

Even if constants are related, they should not be put into a single file. The wrapper classes in Java is a good example of related constants being defined in their own class files rather than in a WrapperConstants file.

Where should you define constants?

If the constants are strongly tied to an existing class or interface, you should add them to the class or interface. For example, all of the boxed numerical primitive classes, such as Integer and Double, export MIN_VALUE and MAX_VALUE constants.

When should a constant be used in programming?

Because of this, and since readability is also important, you should strive to use an explicit constant whenever possible and leave variables for things that can actually change. As to why constants are used instead of literal numbers: 1) It makes code more readable.

Where do you put constants files?

Putting it in a constant file just spams the file with unnecessary stuff. The less the constant has the character of a constant but a variable (like version number) the more you can put it outside. The less variable the constant is, so the more constant it is, the more it should placed inside it's scope.


2 Answers

What is good practice for having a large amount of constants, should they just be at the top of your main file or is it in any way wise to have maybe a ProgramConstants class

The decision about where to place constants should depend on the type of constant.

The Integer class in the JDK has a constant called MIN_VALUE that defines the minimum value of an int. The Character class defines a constant named MIN_VALUE as well which defines the minimum value of a char.

Compare the above approach with the approach of defining a global WrapperConstants class/enum with two constants namely CHAR_MIN_VALUE and INT_MIN_VALUE. You will soon be adding more constants to this file for other data types.. ( LONG_MIN_VALUE, FLOAT_MIN_VALUE and so on...)

What happens when you also want to define MAX_VALUE? See how quickly your class can explode? What about readability. Is WrapperConstants.CHAR_MIN_VALUE more readable than Character.MIN_VALUE? Not really.

Defining constants in classes they relate to is the way to go IMO. That said, not all constants belong to Java classes/interfaces/enums. Some constants (error messages for example) are better off being placed in message bundles/property files.

like image 98
Chetan Kinger Avatar answered Oct 15 '22 13:10

Chetan Kinger


I prefer putting constants into classes where they logically belongs.

Don't put not-related constants into class like ProgramConstants, because you can create a bunch of messy constants, which will become hard to maintain.

like image 45
Gondy Avatar answered Oct 15 '22 13:10

Gondy