In our case re running flog on the refactored code in Figure 918 brings the ABC

In our case re running flog on the refactored code in

This preview shows page 368 - 370 out of 517 pages.

In our case, re-running flog on the refactored code in Figure 9.18 brings the ABC score for the newly- renamed calculate_current_year from 23.0 down to 6.6, which is well below the suggested NIST threshold of 10.0. Also, reek now reports only two smells. The first is “low cohesion” for the helper methods peel_off_leap_year and peel_off_regular_year ; this is a design smell, and we will discuss what it means in Chapter 11 . The second smell is declaration of class variables inside a method. When we applied Decompose Conditional and Extract Method, we turned local variables into class variables @@year and @@days_remaining so that the newly-extracted methods could successfully modify those variables’ values. Our solution is effective, but clumsier than Replace Method with Method Object (Fowler chapter 6). In that refactoring, the original method convert is turned into an object instance (rather than a class) whose instance variables capture the object’s state; the helper methods then operate on the instance variables. 1 # An example call would now be: 2 # year = TimeSetter.new(367).calculate_current_year 3 # rather than: 4 # year = TimeSetter.calculate_current_year(367) 5 class TimeSetter 6 ORIGIN_YEAR = 1980 7 def initialize(days_since_origin) 8 @year = ORIGIN_YEAR 9 @days_remaining = days_since_origin 10 end 11 def calculate_current_year 12 while (@days_remaining > 365) do 13 if leap_year? 14 peel_off_leap_year! 15 else
Image of page 368
16 peel_off_regular_year! 17 end 18 end 19 return @year 20 end 21 private 22 def peel_off_leap_year! 23 if (@days_remaining >= 366) 24 @days_remaining -= 366 ; @year += 1 25 end 26 end 27 def peel_off_regular_year! 28 @days_remaining -= 365 ; @year += 1 29 end 30 def leap_year? 31 @year % 400 == 0 || 32 (@year % 4 == 0 && @year % 100 != 0) 33 end 34 end Figure 9.19: If we use Fowler’s recommended refactoring, the code is cleaner because we now use instance variables rather than class variables to track side effects, but it changes the way calculate_current_year is called because it’s now an instance method. This would break existing code and tests, and so might be deferred until later in the refactoring process. Figure 9.19 shows the result of applying such a refactoring, but there is an important caveat. So far, none of our refactorings have caused our characterization specs to fail, since the specs were just calling TimeSetter.convert . But applying Replace Method With Method Object changes the calling interface to convert in a way that makes tests fail. If we were working with real legacy code, we would have to find every site that calls convert , change it to use the new calling interface, and change any failing tests accordingly. In a real project, we’d want to avoid changes that needlessly break the calling interface, so we’d need to consider carefully whether the readability gained by applying this refactoring would outweigh the risk of introducing this breaking change.
Image of page 369
Image of page 370

You've reached the end of your free preview.

Want to read all 517 pages?

  • Spring '19
  • Dr.Marcos

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors