DOC PREVIEW
CU-Boulder CSCI 6448 - Refactoring

This preview shows page 1-2-3-4-5 out of 16 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 16 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Lecture 19: Refactoring (part 2)Kenneth M. AndersonObject-Oriented Analysis and DesignCSCI 4448/6448 - Spring Semester, 20051March 15, 2005 © University of Colorado, Boulder, 20052IntroductionCredit where Credit is DueSome of the material for this lecture is taken from “Refactoring: Improving the Design of Existing Code” by Martin Fowler; as such some material is copyright © Addison Wesley, 1999Last LectureRefactoringIntroduced core ideasImprove design without changing functionalityWatch out for “bad smells” in codeCovered several examplesGoals for this LecturePresent a longer, more detailed exampleMarch 15, 2005 © University of Colorado, Boulder, 20053TutorialA simple program for a video storeMovieRentalCustomercustomer object can print a statement (in ASCII)We’d like to modify the code to also print a statement in HTML and have discovered that none of the existing code can be reused!See example code (available on class website)Added a test case! We will test our code after each refactoringMarch 15, 2005 © University of Colorado, Boulder, 20054Does this code need refactoring?For such a simple systemprobably notbut imagine that these three classes are part of a larger systemthen the refactorings we do during the tutorial can indeed be usefulthe point is to imagine following this process on a daily basis in a larger system projectrefactoring needs to be incremental, systematic, and safeMarch 15, 2005 © University of Colorado, Boulder, 20055Initial Class Diagramstatement() works by looping through all rentals;for each rental, it retrieves its movie and the number of days it was rented; it also retrieves the price code of the movieit then calculates the price for each movie rental and the number of frequent renter points and returns the generated statement as a string; (see next slide)MoviepriceCode: intRentaldaysRented: intCustomerstatement()1*1*March 15, 2005 © University of Colorado, Boulder, 2005Initial statement() algorithm6:Customer*[for all rentals]:RentalgetMovie():MoviegetPriceCode()getDaysRented()March 15, 2005 © University of Colorado, Boulder, 20057Step 1: refactor statement()Why? It’s a “long method” which is one of the “bad smells” presented in the last lectureBesides, our purpose is to add a new method to generate an HTML statement and refactoring statement() may lead to code that can be reused by the new functionThis matches one of Fowler’s conditions for refactoring: cleaning up the code to make it possible to add a new functionMarch 15, 2005 © University of Colorado, Boulder, 20058How to start?We want to decompose the statement() method into smaller piecesWe’ll start with “Extract Method”and target the switch statement firstlook for local variables: each and thisAmounteach is not modified, thisAmount isnon-modified variables can be passed as parameters to the new method (if required)modified variables require more care; since there is only one, we can make it the return value of the new methodPitfallsbe careful about return types; in the original statement, thisAmount is a double, but it would be easy to make the mistake of having the new method return an int; if you do, your test will fail because the rounding of ints to doubles would cause some of your amounts to change; try it and see with the Customer class in the step1 directoryMarch 15, 2005 © University of Colorado, Boulder, 20059Step 2: rename variablesThe variable names in the new amountFor method don’t make sense now that they have been moved out of the statement() method“Any fool can write code that a computer can understand. Good programmers write code that humans can understand”Lets rename them and run our testso far so good!March 15, 2005 © University of Colorado, Boulder, 200510Step 3: move methodamountFor() uses information from the Rental classIt does NOT use any information from the Customer classMethods should be located close to the data they use, so lets move amountFor() to the Rental classWe get rid of a parameter this wayLets also rename the method to getCharge() to clarify what it is doingAs a result, back in Customer, we must delete the old method and change the call to amountFor(each) to each.getCharge()Then we need to compile and test; all good!March 15, 2005 © University of Colorado, Boulder, 200511New class diagramNo major changes; however Customer is now a smallerclass and an operation has been moved to the class thathas the data it needs to do its job;Definitely making progress!MoviepriceCode: intRentaldaysRented: intgetCharge()Customerstatement()1*1*March 15, 2005 © University of Colorado, Boulder, 200512Step 4: Replace Temp with QueryIn the statement() method, thisAmount is now redundant. It is set once with the call to each.getCharge() and is not changed afterward; lets get rid of it.Don’t forget to run your test!Removing temp variables is a good thing, because they often cause the need for parameters where none are required and can also cause problems in long methods;of course the charge is now calculated twice through the loop, but we can optimize the calculation later (but only if we determine that it is slowing us down)March 15, 2005 © University of Colorado, Boulder, 200513Step 5: frequent renter pointsLets do the same thing with the logic to calculate frequent renter pointsStep 5a: extract methodeach can be a parameter, as in step1frequentRenterPoints has a value before the method is invoked, but the new method does not read it; we simply need to use appending assignment outside the methodStep 5b: move methodAgain, we are only using information from Rental, not customer, so lets move getFrequentRenterPoints to the Rental classBe sure to run your test case after each step March 15, 2005 © University of Colorado, Boulder, 200514New class diagramCustomer continues to get smaller, Rental continues to getlarger; but Rental now has operations that change it frombeing a “data holder” to a useful objectOur sequence diagram has changed (see next slide); statement()used to call the movie class to get the price code for each movie.Now rental takes care of that. And statement() now calls methodsthat have names that mean something rather than presentinglots of code whose purpose may not be clearMoviepriceCode: intRentaldaysRented: intgetCharge()getFrequentRenterPoints()Customerstatement()1*1*March 15, 2005 © University of Colorado, Boulder, 2005new statement() algorithm15:Customer*[for all


View Full Document

CU-Boulder CSCI 6448 - Refactoring

Documents in this Course
Struts

Struts

12 pages

Adapter

Adapter

23 pages

Prototype

Prototype

16 pages

Weka

Weka

15 pages

qooxdoo

qooxdoo

16 pages

Django

Django

12 pages

Overview

Overview

22 pages

XNA

XNA

5 pages

Load more
Download Refactoring
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Refactoring and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Refactoring 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?