Return from functions as early as possible

As a best practice, Guidewire recommends that functions return as early as possible to avoid unnecessary processing.

The following sample Gosu code is inefficient. The function uses a variable unnecessarily, and it does not return a result as soon as it detects the affirmative condition.

public function foundThree() : boolean {
  var threeFound = 0               // A numeric variable is unnecessary to return a boolean result.

   for (x in 5) {
    if (x == 3) {
      threeFound = threeFound + 1  // The loop keeps iterating after third one is found.
    }

   }

   return threeFound >= 1           // The function returns long after third one is found.
}

The following modified sample code is more efficient. The function returns a result as soon as it detects the affirmative condition.

public function foundThree() : boolean {
  for (x in 5) {
    if (x == 3) {
      return true                  // The function returns as soon as the third one is found.
    }

   }

   return false
}