Java
Coding Standards
Logger
Variable Naming
Software Development

Should a static final Logger be declared in UPPER-CASE?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

In Java, the convention around the naming of constants is to use upper-case letters with underscores to separate words. According to the Java Language Specification, constants are typically static final fields whose values are immutable once assigned. Given this, there seems to be an ongoing debate on whether a static final Logger should be named using the conventional UPPER_CASE format or not.

Understanding Constants in Java

Constants in Java are defined as static final variables, where:

  • static means that the variable belongs to the class, rather than instances of it.
  • final means the value assigned to the variable cannot change once it's initialized.

The convention is to write these in all upper case with underscores separating words, for example, MAX_HEIGHT or DEFAULT_VALUE. This naming convention helps in quickly distinguishing constants from other variables in the codebase, which increases code readability.

Is Logger a Constant?

To determine whether the Logger instance should be considered a constant, we must consider if it truly is immutable. Logger in Java, typically from the java.util.logging package or log frameworks like Log4J or SLF4J, is not genuinely immutable. While the reference (meaning the variable pointing to the Logger) does not change once assigned, the state of the Logger itself can change since it might involve different levels of logging (INFO, DEBUG, ERROR) or different appenders.

Thus, loggers do not strictly fit into the category of constants as their internal state can change. This is a crucial distinction because the immutability of true constants lies in both their references and their entirely unchangeable state.

Naming Convention of Loggers

Given that Logger instances are not true immutes, naming them in uppercase can be misleading and might imply characteristics (like complete immutability) that do not hold. Therefore, it is generally accepted that Logger variables are declared in camel case, for example, logger instead of LOGGER. This approach aligns with typical naming conventions for non-constant fields and helps to differentiate them from true constants within the code.

Technical Example

java
1import org.slf4j.Logger;
2import org.slf4j.LoggerFactory;
3
4public class SampleClass {
5    // Logger declaration using camel case
6    private static final Logger logger = LoggerFactory.getLogger(SampleClass.class);
7    
8    public void doAction() {
9        logger.info("Action performed.");
10    }
11}

This piece of code illustrates the common practice of defining a Logger instance in a Java class using camel case rather than UPPER_CASE.

Implications of Using UPPER-CASE

Using UPPER-CASE for loggers could go against common Java conventions and potentially lead to confusion. In Java, UPPER_CASE is reserved for constants that are completely immutable in every aspect. Since use of a logger can be modified (such as by setting a different logging level at runtime), it doesn’t strictly qualify as a constant.

Summary Table

AspectUPPER_CASECamel Case
ImmutabilityImplies full immutabilityN/A
ConventionFor true constantsTypical for mutable fields
Example of UsageMAX_VALUE, PIlogger, applicationContext
Implications for LoggerMisleadingAppropriate

Conclusion

While naming conventions might appear trivial at first glance, they carry significant implications for code clarity and maintenance. In the specific case of loggers, adhering to camel case naming, such as logger, is both conventional and functional, helping maintain clarity about the nature of the field within Java projects.


Course illustration
Course illustration

All Rights Reserved.