GoogleMethodName

Since Checkstyle 13.1.0

Description

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

This check enforces Google's specific method naming requirements:

  • Method names must start with a lowercase letter and use uppercase letters for word boundaries.
  • Underscores may appear in JUnit test method names to separate logical components.
  • Underscores may be used to separate adjacent numbers (e.g., version numbers like guava33_4_5), but NOT between letters and digits.

Examples

To configure the check:


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

Code Example:


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

  public void Foo() {}
  // violation above, 'Method name 'Foo' must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  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_fails() {}

  @Test
  public void test_foo1_1() {}

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

  @Test
  public void f$bar() {}
  // violation above, 'Test method name 'f\$bar' is not valid. Each segment must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'

  @Test
  public void test_Foo() {}
  // violation above, 'Test method name 'test_Foo' is not valid. Each segment must start with a lowercase letter, min 2 chars, avoid single lowercase letter followed by uppercase, and contain only letters, digits, or underscores.'
}

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.

Package

com.puppycrawl.tools.checkstyle.checks.naming

Parent Module

TreeWalker