Skip to content

Clarification needed for 5.2.5 Non-constant field names coverage in Google style #17680

@VeniceWish

Description

@VeniceWish

I have read check documentation: https://checkstyle.org/checks/misc/indentation.html
I have downloaded the latest checkstyle from: https://checkstyle.org/cmdline.html#Download_and_Run
I have executed the cli and showed it below, as cli describes the problem better than 1,000 words

From: https://google.github.io/styleguide/javaguide.html#s5.2.5-non-constant-field-names

Non-constant field names (static or otherwise) are written in lowerCamelCase.
These names are typically nouns or noun phrases. For example, computedValues or index.

Code:

import java.util.List;

class Test {
  int Count; // Expected violation
  String User_Name; // Expected violation

  class Constants {
    // Constants
    static final int MAX = 10;
  }

  class NoConstants {
    // Not constants
    static final List<String> names = List.of("Ed", "Ann"); // Expected violation
  }
}

Cli:

$ java -jar checkstyle-10.26.1-all.jar -c google_checks.xml Test.java 
Starting audit...
[WARN] Test.java:4:7: Member name 'Count' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. [MemberName]
[WARN] Test.java:5:10: Member name 'User_Name' must match pattern '^[a-z][a-z0-9][a-zA-Z0-9]*$'. [MemberName]
Audit done.

Formatter:

$ java -jar google-java-format-1.28.0-all-deps.jar Test.java > FormattedCode.java
$ diff -Naru Test.java FormattedCode.java
None

According to the Checkstyle Google style page for 5.2.4 Constant names (https://checkstyle.sourceforge.io/google_style.html#a5.2.4):

Every constant is a static final field, but not all static final fields are constants — impossible to check.

5.2.4 is marked as “requirements are not possible to check by Checkstyle at all.”

For 5.2.5 Non-constant field names, however, the page says “Existing Check covers all requirements from Google” (via MemberName). If it is impossible to automatically distinguish true constants from static final fields that are not constants (as acknowledged in 5.2.4), then 5.2.5 may also be subject to the same limitation.

It would be clearer if the documentation stated that 5.2.5 is only partially supported, subject to the same constraint as 5.2.4.

As a side note, I’m curious how Checkstyle treats other naming-related guidance from the Google Style Guide, such as “These names are typically nouns or noun phrases. For example, computedValues or index.” Are such guidelines considered out of scope for Checkstyle checks?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions