Return Early

public String returnStuff(SomeObject argument1, SomeObject argument2){
      if (!argument1.isValid()) {
            throw new Exception();
      }
 
      if (!argument2.isValid()) {
            throw new Exception();
      }
 
      SomeObject otherVal1 = doSomeStuff(argument1, argument2);
 
      if (!otherVal1.isValid()) {
            throw new Exception();
      }
 
      SomeObject otherVal2 = doAnotherStuff(otherVal1);
 
      if (!otherVal2.isValid()) {
            throw new Exception();
      }
 
      return "Stuff";
}

  • Fail fast
  • Single Entry, Single Exit (SESE)

Disadvantage

  • Unable to clean resources