Thanks to visit codestin.com
Credit goes to github.com

Skip to content

False-negative: Method and Member names with underscores #17839

@mohitsatr

Description

@mohitsatr

From: https://google.github.io/styleguide/javaguide.html#s5.3-camel-case

In very rare circumstances (for example, multipart version numbers), you may need to use underscores to separate adjacent numbers, since numbers do not have upper and lower case variants.

Google style is not exact. On one hand it says _ can be used in test methods to combine sentences and on another hand in general CamelCase explanation, _ is allowed for numbering suffix in all names(except package and generic names).

Valid uses of underscore :

1. guava33_4_5
2. guavaVersion33_4_5 
3. guavaversion33_4_5 (same as 1.)

Invalid uses of underscore:

1. guava_33_4_5_ (underscore at the end)
2. guava_33_4_5   (underscore between digit and letter)
3. guava_version33_4_5 (underscore between lowercase character sequences)

Note: Underscores are not allowed in normal method names. Name has to have the numbering format part at the end. Following are all invalid

guava_version
guava_Version
Set_guavaVersion3345

Some false-negatives left to address from #17708

/** some javadoc. */
public class NamesWithUnderscores {
 
  void gradle_9_5_1() {} // Expected violation, _ between digit and letter

  void jdk_9_0_392() {} // Expected violation, _ between digit and letter

  void testing_01231(String str) {} // Expected violation, _ between digit and letter

  int jdk_8_90; // Expected violation, _ between digit and letter

  int guava_33_4_7; // Expected violation, _ between digit and letter
}
$ java -jar checkstyle-11.0.0-all.jar -c google_checks.xml NamesWithUnderscores.java 
Starting audit...
Audit done.

we probably need a better approach than Regex. Regex has already grown very complex.

<module name="MethodName">
<property name="format"
value="^(?![a-z]$)(?![a-z][A-Z])[a-z][a-z0-9]*(?:[A-Z][a-z0-9]*)*(?:_[0-9]+)*$"/>

<module name="MemberName">
<property name="format"
value="^(?![a-z]$)(?![a-z][A-Z])[a-z][a-zA-Z0-9]*(?:_[0-9]+)*$"/>

if special check is needed, it should first determine the type of name - regular or name with numbering and then it should validate the name against the appropriate regex format, which will differ for each type of naming

Keep in mind: unless it's a non-regular name i.e name that has number formating suffix at the end or method with @Test annotation, underscores are not allowed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions