Page 161 -
P. 161

7     public static final int  STANDARD = 0;
                          8     public static final int  BUDGET = 1;
                          9     public static final int  PREMIUM = 2;
                          10     public static final int  PREMIUM_PLUS = 3;
                          11
                          12     float interestEarned( ) {
                          13         return ( principal * (float) Math.exp( rate * (daysActive / 365.25 ) ) )
                          14                 - principal;
                          15     }
                          16
                          17     public boolean isPremium( ) {
                          18         if (accountType == Account.PREMIUM || accountType == Account.PREMIUM_PLUS)
                          19             return true;
                          20         else
                          21             return false;
                          22     }
                          23 }
                          24
                          25 float calculateFee(Account accounts[]) {
                          26     float totalFee = 0;
                          27     Account account;
                          28     for (int i = 0; i < accounts.length; i++) {
                          29         account = accounts[i];
                          30         if ( account.isPremium( ) )
                          31             totalFee += BROKER_FEE_PERCENT * account.interestEarned( );
                          32     }
                          33     return totalFee;
                          34 }
                          35
                          36 static final double BROKER_FEE_PERCENT = 0.0125;
                          The last problem found during the inspection involved the interestEarned( ) method that
                          they had extracted. It was a confusing calculation, with several intermediate steps
                          crammed into a single line. When that behavior was buried inside the larger function, the
                          problem wasn’t as glaring, but now that it had its own discrete function, they could get a
                          clearer look at it.
                          The first problem was that it wasn’t exactly clear why there was a division by 365.25 in
                          line 13. The programmer explained that in the Account class, daysActive represented the
                          number of days that the account was active, but the rate was an annual interest rate, so

                          they had to divide daysActive by 365.25 to convert it to years. Another programmer asked
                          why principal was being subtracted at the end of the interest calculation. The explanation
                          was that this was done because the fee calculation was based only on the interest earned,
                          regardless of the principal that initially was put into the account.
                          The refactoring Introduce Explaining Variable was used to introduce two intermediate vari-
                          ables, years on line 13 and compoundInterest on line 14, to clarify the code:
                          1 class Account {
                          2     float principal;
                          3     float rate;
                          4     int daysActive;
                          5     int accountType;
                          6

                                                                                  DESIGN AND PROGRAMMING  153
   156   157   158   159   160   161   162   163   164   165   166