When do you need to define constants in your files?

I noticed some projects, such as storing constants in their own file, that is, constants used globally and in the main program loop can clutter up the main file, so maybe they want to place them in another place and then link / import file / class.

I understand that when writing an OOP class that you want to keep in a constant in the header of the class file so that they can refer statically as such:

myCar.setColour(Colour.RED); 

Where RED is the color constant in the Colour class.

What is good practice for having a large number of constants if they are simply at the top of the main file or is it reasonable to have the ProgramConstants class in any way that is purely static, public and readable?

+6
source share
4 answers

What is a good practice for having a large number of constants if they are simply at the top of the main file or it is wise to have the ProgramConstants class in any way

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

The JDK Integer class has a constant named MIN_VALUE that defines the minimum value of int. The Character class also defines a constant named MIN_VALUE , which defines the minimum value of a char.

Compare the above approach with the approach for defining a global WrapperConstants class / enumeration with two constants, namely CHAR_MIN_VALUE and INT_MIN_VALUE . Soon you will add more constants for other data types to this file .. ( LONG_MIN_VALUE , FLOAT_MIN_VALUE , etc.)

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

Defining constants in the classes to which they belong is the IMO path. However, not all constants belong to Java classes / interfaces / enums. Some constants (e.g. error messages) are better placed in message packages / property files.

+3
source

I prefer to put constants in classes where they logically belong.

Do not put ProgramConstants constants in a class, such as ProgramConstants , because you can create a bunch of messy constants, which will be difficult to maintain.

+1
source

No, you should not put all your constants at the top of your main class or in your class, they should go in any class with which they are logically connected.

The problem is that if the programmer sees a common place for placing something, for example, a file of constants, then they will put all the constants here to maintain the template, whereas they should put them in the right and logical place. The presence of one large file of constants makes it difficult to work with constants, since they are not effectively classified and break modularity. Your role as an architect is to avoid creating a system with such traps.

So, say, for example, you have system properties related to managing your Java application, which means there are some of these constants in your main class. If you end up too much in your main class, move them to the SystemProperties class in the same package. When a programmer needs several constants for their own use, say, for colors in your example, they must create their own class Colors, which is included in the package associated with this function that contains these constants.

+1
source

"Should all constants in one file be defined?"

Absolutely not. In all but the smallest programs, this will create a single point of contention / disorder. Very similar to the anti-pattern of a global variable.

"Should constants be in their files?"

Again, no. Generally speaking, there are three types of constants, and each of them must be grouped in its own way in a different way.

  • Constants that are part of an internal implementation detail relate to them using an implementation code.
  • Public API constants. These are the constants that form part of the public API public contract, they belong close to the API that is defined.
  • System configuration for the program. These constants belong to the boot code for the system.
+1
source

All Articles