Skip to content

Adopt naming conventions for constant fields#5554

Open
mpkorstanje wants to merge 2 commits intomainfrom
rien/constant-conventions
Open

Adopt naming conventions for constant fields#5554
mpkorstanje wants to merge 2 commits intomainfrom
rien/constant-conventions

Conversation

@mpkorstanje
Copy link
Copy Markdown
Contributor

@mpkorstanje mpkorstanje commented Mar 25, 2026

A constant field is a static final field whose value is immutable. If a static final field has a primitive type or an immutable reference type it is a constant field. Constants should be named with uppercase words separated by underscores. Non-constant static final fields should be named with camel case.

For example a public static final String KEY is immutable and a constant, while private static final Logger logger holds a reference to the logging system, is mutable and not a constant.

While I've not checked all occurrences, we generally follow this convention in production code. However when discussing #5424 we noticed a drift towards LOGGER. Specifically:

  • all public constants are upper case.
  • we have 67 lowercase private constants, of which 34 are named logger.
  • we have 150 uppercase private constants, of which 5 are named LOGGER.

To ensure this drift doesn't persist:

  • we should explicitly adopt a naming convention for constants.
  • enforce that all non-private static final fields are named using the constant naming conventions.
  • enforce that loggers are never named following the constant naming conventions.

Out of scope: enforce that non-private static final fields are actually constants.


I hereby agree to the terms of the JUnit Contributor License Agreement.


Definition of Done

@mpkorstanje mpkorstanje changed the title Configure defacto constant name conventions in checkstyle Configure defacto constant naming conventions in checkstyle Mar 25, 2026
* public, protected and package-private constants must be upper case
* private constants may have any case
  * except for logger which must be lowercase.
@mpkorstanje mpkorstanje force-pushed the rien/constant-conventions branch from 95a69c9 to e4961fd Compare March 25, 2026 13:30
@mpkorstanje mpkorstanje marked this pull request as ready for review March 25, 2026 13:31
@testlens-app

This comment has been minimized.

@jbduncan
Copy link
Copy Markdown
Contributor

jbduncan commented Mar 25, 2026

Making loggers lowercase sounds reasonable to me.

The rationale I've seen elsewhere for making loggers lowercase is that they're not really constants so much as side-effecting "do-ers" that just so happen to be private static final because it stops extra logger instances from being created.

On the other hand, uppercase naming for private static final fields is near-universal across the ecosystem, regardless if they're constants or not, so I'd be equally happy with making everything uppercase.

I haven't searched the code for any of the other private static final fields, so I'm afraid I can't comment on them.

@mpkorstanje
Copy link
Copy Markdown
Contributor Author

From Joshua Blogs, Effective Java v2, p238

The sole exception to the previous rule concerns “constant fields,” whose names should consist of one or more uppercase words separated by the underscore character, for example, VALUES or NEGATIVE_INFINITY. A constant field is a static final field whose value is immutable. If a static final field has a primitive type or an immutable reference type (Item 15), then it is a constant field. For example, enum constants are constant fields. If a static final field has a mutable reference type, it can still be a constant field if the referenced object is immutable.

The mutability is important here and I think we can consider loggers to be mutable. They're holding a reference to the logging system which hold state somewhere.

And combined with the advice to minimize accessibility and mutability for public parts of the API, I think we could adopt the convention that public/protected/package-private static final fields must be immutable and follow upcase naming convention. With private static final fields allowed to be mutable and named with either upper or lowercase. Except for logger, which is mutable.

@mpkorstanje mpkorstanje changed the title Configure defacto constant naming conventions in checkstyle Adopt naming conventions for constant fields Mar 25, 2026
@testlens-app
Copy link
Copy Markdown

testlens-app bot commented Mar 25, 2026

✅ All tests passed ✅

🏷️ Commit: 956b129
▶️ Tests: 11193 executed
⚪️ Checks: 15/15 completed


Learn more about TestLens at testlens.app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants