Should a "static final Logger" be declared in UPPER-CASE?
For Java static final Logger instances, best practice leans towards using lowercase logger
, not UPPER_CASE LOGGER
. This distinction makes it clear that Logger is not a true constant, but a mutable tool used across your application.
So, let's keep it logger
for readability and consistency with broader development practices.
Uppercase or Lowercase? That is the question.
In Java, uppercase labels are used for immutable constants. These are static final fields with a constant, unchanging value known at compile-time.
But a logger instance is mutable. It changes state during the application lifecycle, which is a stark contrast to our unchanging, mountain-like constant.
Understanding mutable vs. immutable
Static final fields can be split into two families: the immutable objects and the mutable folks.
- Immutable family: Members like String and primitives live here. They enjoy
UPPER_CASE
as they never change.
- Mutable folks: Logger instances belong here. Despite being popular, they maintain a low profile with
lowercase
.
Tools and standards adherence
Automated code tools like PMD or Checkstyle endorse naming conventions that improve readability and maintainability. They aren't fond of uppercase for mutable static final fields -- they think it's a big no-no.
Consider your logging framework
Different logging frameworks might go against the grain. SLF4J can be flexible (LOGGER
or logger
), whereas Log4J or Java's Util Logging really prefer to see logger
.
Balancing your framework convention with practices for mutable and immutable static final fields will yield a beautiful, readable code your team will treasure.
Navigating the seas of consistency
Continuity across your project is a lighthouse in a storm. Stick with LOGGER
if that's the norm around you, or logger
if that floats your boat. Uniform code is a treat for new contributors or late-night bug fixes.
When you swim into a legacy codebase's waters, where LOGGER
and logger
fight for dominance, ensure you document your choice and guide future coders.
Finding style guide treasure maps
Style guides are a goldmine of wisdom curated by experienced developers. They show the lands of best practices and code clarity.
Visualizing "Static Final Logger Naming Convention":
Think of your codebase as a toolbox:
Naming Convention Insight:
The Logger is a purpose-specific tool. Traditional Java Naming:
Pick readability and standardization over inflexible dogma. 📘👁️
Was this article helpful?