Multiple return statements

Some programmers find that, occasionally, some methods become clearer if multiple return statements are used, instead of the usual single point of exit.

This technique can be easily abused, and should be used with care. It's mainly used to replace nested if structures.

Multiple return statements seem to work well for "guard code" at the beginning of a method, in which the main body of the method is executed only if certain conditions are satisfied.


Here, equals has multiple return statements, since a successful test implies that further comparison is redundant. The alternative is to use multiple if statements, which some would find less legible.

import java.util.*;

public final class Auto {

  @Override public boolean equals(Object aThat) {
    if (this == aThat) return true;
    if (!(aThat instanceof Auto)) return false;

    Auto that = (Auto)aThat;
      EqualsUtil.areEqual(, &&
      EqualsUtil.areEqual(this.numDoors, that.numDoors) &&
      EqualsUtil.areEqual(this.options, that.options) &&
      EqualsUtil.areEqual(this.gasMileage, that.gasMileage) &&
      EqualsUtil.areEqual(this.color, that.color) &&
      Arrays.equals(this.maintenanceChecks, that.maintenanceChecks)



  private String name;
  private int numDoors;
  private List<String> options;
  private double gasMileage;
  private String color;
  private Date[] maintenanceChecks;

See Also :
Implementing equals
Conventional name for return value
Avoid basic style errors