Introduce Variable
To increase readability and sometimes to increase performance, introducing temporary variables into a method is necessary. For example, consider the code in Listing 7.4.
Listing 7.4. Calculate Vacation Days
public int getVacationDays( ) { int X; X = (int)(10 * java.lang.Math.random()); if (X + 2 < 5 ) { return X + 2; } if (X + 2 > 5 ) { return X + 2 - (X + 2) / 7; } if (X + 2 == 5) { return 5; } return X; } |
In Listing 7.4, notice the number of times that X + 2 is used. Every time that expression is evaluated, a calculation takes place. Calculations are typically one of the more CPU-intensive operations.
By using Introduce Variable, a temporary variable can be created to replace the expression X + 2. To display the Introduce ...
Get Borland® JBuilder™ Developer’s Guide now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.