For even more transparency we could apply Extract Method again to leapyear by

# For even more transparency we could apply extract

• 517

This preview shows page 367 - 369 out of 517 pages.

details well encapsulated. For even more transparency, we could apply Extract Method again to leap_year? by extracting methods every_400_years? and every_4_years_except_centuries? . Note: Line 7 reflects the bug fix described in Screencast 9.3.1 . The conditional is also nested two-deep, making it hard to understand and increasing convert ’s ABC score. The Decompose Conditional refactoring also breaks up the complex condition by replacing each arm of the conditional with an extracted method. Notice, though, that the two arms of the conditional correspond to lines 4 and 6 of the pseudocode in Figure 9.14 , both of which have the side effects of changing the values of d and y (hence our use of ! in the names of the extracted methods). In order for those side effects to be visible to convert , we must turn the local variables into class variables throughout TimeSetter , giving them more descriptive names @@year and @@days_remaining while we’re at it. Finally, since @@year is now a class variable, we no longer need to pass it as an explicit argument to leap_year? . Figure 9.18 shows the result. 1 # NOTE: line 7 fixes bug in original version 2 class TimeSetter 3 ORIGIN_YEAR = 1980 4 def self.calculate_current_year(days_since_origin) 5 @@year = ORIGIN_YEAR 6 @@days_remaining = days_since_origin 7 while (@@days_remaining > 365) do 8 if leap_year? 9 peel_off_leap_year! 10 else 11 peel_off_regular_year! 12 end 13 end 14 return @@year 15 end 16 private 17 def self.peel_off_leap_year! 18 if (@@days_remaining >= 366) 19 @@days_remaining -= 366 ; @@year += 1 20 end 21 end 22 def self.peel_off_regular_year! 23 @@days_remaining -= 365 ; @@year += 1 24 end 25 def self.leap_year? 26 @@year % 400 == 0 ||
27 (@@year % 4 == 0 && @@year % 100 != 0) 28 end 29 end Figure 9.18: We decompose the conditional in line 7 by replacing each branch with an extracted method. Note that while the total number of lines of code has increased, convert itself has become S horter, and its steps now correspond closely to the pseudocode in Figure 9.14 , sticking to a single level of A bstraction while delegating details to the extracted helper methods. As long as we’re cleaning up, the code in Figure 9.18 also fixes two minor code smells. The first is uncommunicative variable names: convert doesn’t describe very well what this method does, and the parameter name d is not useful. The other is the use of “magic number” literal constants such as 1980 in line 4; we apply Replace Magic Number with Symbolic Constant (Fowler chapter 8) to replace it with the more descriptive constant name STARTING_YEAR . What about the other constants such as 365 and 366? In this example, they’re probably familiar enough to most programmers to leave as-is, but if you saw 351 rather than 365, and if line 26 (in leap_year? ) used the constant 19 rather than 4, you might not recognize the leap year calculation for the Hebrew calendar . Remember that refactoring only improves the code for human readers; the computer doesn’t care. So in such cases use your judgment as to how much refactoring is enough.

#### You've reached the end of your free preview.

Want to read all 517 pages?

• Spring '19
• Dr.Marcos

### What students are saying

• 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.

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

• 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.

Dana University of Pennsylvania ‘17, Course Hero Intern

• 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.

Jill Tulane University ‘16, Course Hero Intern