GoogleMethodName

Since Checkstyle 13.10.0

Description

Checks that method names conform to the Google Java Style Guide for method naming.

Regular method names must:

  • Be at least 2 characters long.
  • Start with a lowercase letter.
  • Not have a single lowercase letter followed by uppercase.
  • Not have a consecutive uppercase.
  • Contain only letters and digits and underscores.

Test method names must:

  • Be at least 2 characters long per segment.
  • Start each segment with a lowercase letter.
  • Not have a single lowercase letter followed by uppercase in any segment.
  • Not have a consecutive uppercase.
  • Allow underscores to separate segment and digits.
  • Not have leading, trailing, or consecutive underscores.

Notes

Methods annotated with @Override are ignored, as they must match the name defined in the parent class or interface.

Examples

To configure the check:


<module name="Checker">
  <module name="TreeWalker">
    <module name="GoogleMethodName"/>
  </module>
</module>

Code Example:


import org.junit.jupiter.api.Test;

class Example1 {
  public void fooBar() {}
  public void guava33_4_5() {}
  public void parseXml() {}

  public void Foo() {}
  // violation above """Method name 'Foo' must be more than a character,
  // start lowercase, and not have a single lowercase followed by uppercase,
  // or consecutive uppercase."""

  public void gradle_9_5_1() {}
  // violation above """Method name 'gradle_9_5_1' has invalid underscore usage,
  // underscores only allowed between adjacent digits."""

  @Test
  public void login() {}

  @Test
  public void jdk17_0_1() {}

  @Test
  public void testLogin_failsGracefully12() {}

  @Test
  public void transferMoney_deductsFromSource() {}

  @Test
  public void login_fails() {}

  @Test
  public void test_1() {}
  // violation above """Test method name 'test_1' has invalid underscore usage,
  // underscore only allowed between letters or between digits."""

  @Test
  public void test_Foo() {}
  // violation above """Test method name 'test_Foo' segment must be more than a
  // character, start lowercase, and not have a single lowercase followed
  // by uppercase, or consecutive uppercase."""
}

Example of Usage

Violation Messages

All messages can be customized if the default message doesn't suit you. Please see the documentation to learn how to.

Fully Qualified Name

com.puppycrawl.tools.checkstyle.checks.naming.GoogleMethodNameCheck

Use this fully qualified class name in configuration when an exact class reference is required.

Parent Module

TreeWalker