Remove constant variables and expressions from loops

As a performance best practice, Guidewire recommends that you do not include unnecessary variables, especially ones that hold objects and entity instances, within loops.

The following sample Gosu code suffers a performance problem. The loop includes an object access expression, period.Active, which remains unchanged throughout all iterations of the loop.

var period : PolicyPeriod

 for (x in 5) {
if (x == 3 and period.Active) {  // Evaulate a constant expression redunantly within a loop. 
    print("x == 3 on active period")
  }

 }

In the preceding example, Gosu evaluates the expression period.Active at least twice unnecessarily. The following modified sample code improves performance.

var period : PolicyPeriod

 if (period.Active) {            // Evaluate a constant expression only once before a loop.
   for (x in 5) {
     if (x == 3) {
       print("x == 3 on active period")
     }
  }
}