Figure 96 violates at least the first and last of these and exhibits other

Figure 96 violates at least the first and last of

This preview shows page 362 - 364 out of 517 pages.

Figure 9.6 violates at least the first and last of these, and exhibits other smells as well, as we can see by running reek on it: 1 time_setter.rb -- 5 warnings: 2 TimeSetter#self.convert calls (y + 1) twice (Duplication) 3 TimeSetter#self.convert has approx 6 statements (LongMethod) 4 TimeSetter#self.convert has the parameter name ’d’ (UncommunicativeName) 5 TimeSetter#self.convert has the variable name ’d’ (UncommunicativeName) 6 TimeSetter#self.convert has the variable name ’y’ (UncommunicativeName) Not DRY (line 2). Admittedly this is only a minor duplication, but as with any smell, it’s worth asking ourselves why the code turned out that way. Uncommunicative names (lines 4–6). Variable y appears to be an integer (lines 6, 7, 10, 14) and is related to another variable d —what could those be? For that matter, what does the class TimeSetter set the time to, and what is being converted to what in convert ? Four decades ago, memory was precious and so variable names were kept short to allow more space for code. Today, there’s no excuse for poor variable names; Figure 9.13 provides suggestions.
Image of page 362
What Guideline Example Variable or class name Noun phrase PopularMovie , top_movies Method with side effects Verb phrase pay_for_order , charge_credit_card! Method that returns a value Noun phrase movie.producers , actor_list Boolean variable or method Adjective phrase already_rated? , @is_oscar_winner Figure 9.13: variable-naming guidelines based on simple English, excerpted from Green and Ledgard 2011 . Given that disk space is free and modern editors have auto-completion that saves you retyping the full name, your colleagues will thank you for writing @is_oscar_winner instead of OsWin . Too long (line 3). More lines of code per method means more places for bugs to hide, more paths to test, and more mocking and stubbing during testing. However, excessive length is really a symptom that emerges from more specific problems—in this case, failure to stick to a single level of A bstraction. As Figure 9.14 shows, convert really consists of a small number of high-level steps, each of which could be divided into sub-steps. But in the code, there is no way to tell where the boundaries of steps or sub- steps would be, making the method harder to understand. Indeed, the nested conditional in lines 6–8 makes it hard for a programmer to mentally “walk through” the code, and complicates testing since you have to select sets of test cases that exercise each possible code path. The ancient wisdom that a method shouldn’t exceed one screenful of code was based on text-only terminals with 24 lines of 80 characters. A modern 22-inch monitor shows 10 times that much, so guidelines like SOFA are more reliable today. 1 start with Year = 1980 2 while (days remaining > 365) 3 if Year is a leap year 4 then if possible, peel off 366 days and advance Year by 1 5 else 6 peel off 365 days and advance Year by 1 7 return Year 8 9 Figure 9.14: The computation of the current year given the number of days since the beginning of a start year (1980) is much more clear when written in pseudocode. Notice that
Image of page 363
Image of page 364

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