DOC PREVIEW
Penn CIT 597 - Refactoring III

This preview shows page 1-2-20-21 out of 21 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 21 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 21 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 21 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 21 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 21 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Refactoring IIIGeneral philosophyDuplicated codeMaking code fasterLong methodsTemporary variablesReplace Temp With QueryIntroduce Parameter ObjectPreserve Whole ObjectReplace Method With Method ObjectLong parameter listLarge classExtract ClassExtract SubclassExtract InterfaceFeature envyPrimitive ObsessionMore bad smells, IMore bad smells, IITesting for nullThat’s all for now...Jan 13, 2019Refactoring IIIGeneral philosophyA refactoring is just a way of rearranging codeRefactorings are used to solve problemsIf there’s no problem, you shouldn’t refactorThe notion of “bad smells” is a way of helping us recognize when we have a problemFamiliarity with bad smells also helps us avoid them in the first placeRefactorings are mostly pretty obviousMost of the value in discussing them is just to bring them into our “conscious toolbox”Refactorings have names in order to crystallize the idea and help us remember itDuplicated codeMartin Fowler refers to duplicated code as “Number one in the stink parade”The usual solution is to apply Extract Method: Create a single method from the repeated code, and use it wherever neededWe’ve discussed some of the details of this (adding a parameter list, etc.)This adds the overhead of method calls, thus the code gets a bit slowerIs this a problem?Making code faster“We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.” -- Tony HoareRules of Optimization: Rule 1: Don't do it. Rule 2 (for experts only): Don't do it yet. -- M.A. Jackson“More computing sins are committed in the name of efficiency (without necessarily achieving it) than for any other single reason--including blind stupidity.” -- W.A. WulfVery few programs actually need to be fasterThe only two examples I can think of are game graphics and BlackboardDonald E. Knuth pointed out (in 1970) that it is essentially impossible to predict where the bottlenecks are in a program--you need to use a profiler to actually measure what the code is doingLong methodsAnother “bad smell” is the overly long methodAlmost always, you can fix long methods by applying Extract MethodFind parts of the method that seem to perform a single task, and make them into a new methodPotential problem: You may end up with lots of parameters and temporary variablesTemporaries: Consider Replace Temp With QueryParameters: Try Introduce Parameter Object and Preserve Whole ObjectIf all else fails, use Replace Method With Method ObjectTemporary variablesAccording to Fowler, temporary variables “tend to encourage longer methods, because that’s the only way you can get at the temp.”If the code fragment you want to extract into a method contains references to temporary variables whose scope overlaps the fragment, you have to somehow provide access to that temporary variableSolution: Use the Replace Temp With Query refactoringReplace Temp With QueryCreate a method to compute or access the temporary variableExample (from Fowler):Replace: double basePrice = quantity * itemPrice; if (basePrice > 1000) return basePrice * 0.95; else return basePrice * 0.98;with: if (basePrice () > 1000) return basePrice() * 0.95; else return basePrice() * 0.98; ... double basePrice() { return quantity * itemPrice; }Introduce Parameter Object Problem: You have a method that requires a long parameter listYou may have a group of parameters that go naturally togetherIf so, make them into an objectExample: Replace public void marry(String name, int age, boolean gender, String name2, int age2, boolean gender2) {...}with public void marry(Person person1, Person person2) {...}Preserve Whole ObjectProblem: You have a method that requires a long parameter listIf you are passing in multiple values from the same object, pass the object in insteadExample: Replace sendBill(customer.name, customer.address, customer.order, amount);with sendBill(customer, amount);Replace Method With Method Object If: you have a long method that uses local variables, and you can’t use Extract Method,Then: turn the method itself into a objectCreate a new class, named after the methodGive the new class the following fields:A final field to hold the object that the method originally came fromA field for each temporary variable and parameter in the methodWrite a constructor that takes as parameters the original object and the parameters of the original methodCopy the original method into the new classIf you need anything from the original object, you have a reference to itAll the old local variables are now fields of the objectThis makes it easy to decompose the long method into as many methods as you wantLong parameter listLong parameter lists are difficult to understand, difficult to remember, and make it harder to format your code nicelyWe’ve already discussed some solutions, such as Introduce Parameter ObjectAnother solution, which we won’t go into in detail, is called Replace Parameter With MethodThe idea is that you shouldn’t pass a parameter into a method if the method has enough information to compute the parameter for itselfLarge classClasses can get overly largeToo many instance variablesMore than a couple dozen methodsSeemingly unrelated methods in the same classPossible refactorings are Extract Class and Extract SubclassA related refactoring, Extract Interface, can be helpful in determining how to break up a large classExtract ClassExtract Class is used when you decide to break one class into two classesClasses tend to grow, and get more and more dataGood signs:A subset of the data and a subset of the methods seem to go togetherA subset of the data seems to be interdependentUseful tests:If you removed a method or a particular piece of data, what other fields and methods would become nonsense?How is the class subtyped?The actual refactoring technique involves creating a new (empty) class, and repeatedly moving fields and methods into it, compiling and testing after each moveExtract SubclassExtract Subclass is used when some behaviors of a class are used for certain instances, but not othersIf you have a “type code” that is used to distinguish between types of objects, this suggests that a subclass may be


View Full Document

Penn CIT 597 - Refactoring III

Documents in this Course
DOM

DOM

21 pages

More DOM

More DOM

11 pages

Rails

Rails

33 pages

DOM

DOM

21 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

RELAX NG

RELAX NG

31 pages

Rake

Rake

12 pages

Ruby

Ruby

58 pages

DOM

DOM

21 pages

Tomcat

Tomcat

16 pages

DOM

DOM

21 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Html

Html

27 pages

DOM

DOM

22 pages

RELAX NG

RELAX NG

30 pages

Servlets

Servlets

28 pages

XHTML

XHTML

13 pages

DOM

DOM

21 pages

DOM

DOM

21 pages

Servlets

Servlets

26 pages

More CSS

More CSS

18 pages

Servlets

Servlets

29 pages

Logging

Logging

17 pages

Load more
Download Refactoring III
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 III 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 III 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?