Notice that what the method does is quick to grasp even though each step would

Notice that what the method does is quick to grasp

This preview shows page 363 - 365 out of 517 pages.

when written in pseudocode. Notice that what the method does is quick to grasp, even though each step would have to be broken down into more detail when turned into code. We will refactor the Ruby code to match the clarity and conciseness of this pseudocode. As a result of these deficiencies, you probably had to work hard to figure out what this relatively simple method does. (You might blame this on a lack of comments in the code, but once the above smells are fixed, there will be hardly any need for them.) Astute readers usually note the constants 1980, 365, and 366, and infer that the method has something to do with leap years and that 1980 is special. In fact, convert calculates the current year given a starting year of 1980 and the number of days elapsed since January 1 of that year, as Figure 9.14 shows using simple pseudocode. In Section 9.5 , we will make the Ruby code as transparent as the pseudocode by refactoring it—applying transformations that improve
Image of page 363
its structure without changing its behavior. Summary Software metrics provide a quantitative measure of code quality. While opinion varies on which metrics are most useful and what their “normal” values should be (especially in dynamic languages such as Ruby), metrics such as cyclomatic complexity and ABC score can be used to guide your search toward code that is in particular need of attention, just as low C0 coverage identifies undertested code. Code smells provide qualitative but specific descriptions of problems that make code hard to read. Depending on which catalog you use, over 60 specific code smells have been identified. The acronym SOFA names four desirable properties of a method: it should be S hort, do O ne thing, have F ew arguments, and maintain a single level of A bstraction. Self-Check 9.5.1. Give an example of a dynamic language feature in Ruby that could distort metrics such as cyclomatic complexity or ABC score. Any metaprogramming mechanism could do this. A trivial example is s=”if (d>=366) [...] ”; eval s , since the evaluation of the string would cause a conditional to be executed even though there’s no conditional in the code itself, which contains only an assignment to a variable and a call to the eval method. A subtler example is a method such as before_filter (Section 5.1 ), which essentially adds a new method to a list of methods to be called before a controller action. Self-Check 9.5.2. Which SOFA guideline—be S hort, do O ne thing, have F ew arguments, stick to a single level of A bstraction—do you think is most important from a unit-testability point of view? F ew arguments implies fewer ways that code paths in the method can depend on the arguments, making testing more tractable. S hort methods are certainly easier to test, but this property usually follows when the other three are observed.
Image of page 364
Image of page 365

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