Heart containing Coding Chica Java 101

Verb’s the Word! Java method names

TIP: References Quick List

Table of Contents

  1. Table of Contents
  2. Introduction
  3. Format
  4. JUnit Test Method Format
  5. Summary

Introduction

In Java, a method is generally an action that we want taken. Therefore, the convention is to make method names verbs or verb phrases. Generally speaking, parameter and variable names should be ASCII letters. ASCII digits are also permitted by Java, although their use breaks the verb or verb phrase approach. Here, I am again describing the Google Style Guide’s Naming Conventions, as that is what I am using for the Java 101 project.

Format

Typically, a Java method is going to follow lowerCamelCase format, where:

  • The first letter is lowercase,
  • Words are pushed together without spaces, and
  • Subsequent words start with a capital letter.
public void setName(final String newName)...
public void getName()...

JUnit Test Method Format

In a unit test, sections of a method name may also be separated by underscores. For example, if using the Given-When-Then test naming approach from Cucumber / Gherkin / Behavior Driven Development (BDD), then a test method name might look like:

public void testNameField_givenSetterInvoked_whenGetterInvoked_thenNewNameReturned(...

Summary

Java method names are typically verbs or verb phrases, describing the action that should be taken during the method. Runtime methods don’t generally include underscores, but unit test method names can use underscores to separate parts of the method name, such as given/when/then naming in BDD’s given/when/then format.

Verb’s the Word! Java method names

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.