DOC PREVIEW
UMD CMSC 433 - Refactoring

This preview shows page 1-2-3 out of 10 pages.

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

Unformatted text preview:

22CMSC 433 – Programming LanguageTechnologies and ParadigmsSpring 2004RefactoringMarch 6, 2004Lots of material taken from Fowler, Refactoring: Improving theDesign of Existing Code23Administrivia• Project deadline Saturday, March 13– Usual late rules apply• Minor (but important) project changes:– set_field methods should return the value of theirparameter• Helps with EncapsulateField y in x = y = 1– Can ignore +=, -=, *=, /=24Switch Statements• Usually not necessary in delegation-based OOprogramming• Replace Type Code with State/Strategy– Define a class hierarchy, a subclass for each type code• Replace Conditional with Polymorphism– Call method on state object to perform the check;switching is based on dynamic dispatch25Replace Type Code with State/Strategy26Replace Conditional with Polymorphismdouble getSpeed() { switch (kind) { case EUROPEAN: return getBaseSpeed(); case AFRICAN: return getBaseSpeed()-loadFactor()*numberOfCoconuts; case NORWEGIAN_BLUE: return (isNailed) ? 0 : getBaseSpeed(voltage); throw new RuntimeException(“Should be unreachable”);} }27Duplicated Code• The same expression used in different places inthe same class– Use Extract Method to pull it out into a method• The same expression in two subclasses sharing thesame superclass– Extract Method in each, then– PullUp method into parent• Duplicated code in two unrelated classes– Extract Class - Break a class that does many things intosmaller classes28Pull Up Method• Might do other refactorings if methods don’t quitematch• What if doesn’t appear in all subclasses?29Extract Class• How do we decide what goes in new class?• Do fields still need to be accessed in orig class?30Long Parameter List• Lots of parameters occlude understanding• Replace Parameter with Method– Remove method parameters and instead use some otherway to get the parameter value (e.g., method call)• Introduce Parameter Object– Group parameters that go together into a containerobject31Replace Parameter with Method• discountedPrice can call getDiscount() itselfdouble basePrice = num * price;double discount = getDiscount();double finalPrice = discountedPrice(basePrice, discount);double basePrice = num * price;double finalPrice = discountedPrice(basePrice);32Introduce Parameter Object33Divergent Change• One class is commonly changed in different waysfor different reasons– To add a new database, change these three methods– To add a new financial currency, change these four• Suggests maybe this shouldn’t be one object• Apply Extract Class to group together variations34Shotgun Surgery• Every time I make change X, I have to make lotsof little changes to different classes– Opposite of Divergent Change• Move Method• Move Field– Switch field from one class to another• Inline Class– A class isn’t doing very much, so inline its features intoits users (reverse of Extract Class)35Other Bad Smells• Data Clumps– Objects seem to be associated, but aren’t grouped together• Primitive Obsession– Reluctance to use objects instead of primitives• Parallel Inheritence Hierarchies– Similar to Shotgun Surgery; every time we add a subclassin one place, we need to add a corresponding subclass toanother36Other Bad Smells (cont’d)• Lazy Class– A class just isn’t useful any more• Speculative Generality– “Oh, I think we need the ability to do this kind of thingsomeday.”• Temporary Field– Instance variable only used in some cases. Confusingto figure out why it’s not being set everywhere.37Other Bad Smells (cont’d)• Message Chains– Long sequences of gets or temporaries; means client istied to deep relationships among other classes• Middle Man– Too much delegation. If a class delegates lots of itsfunctionality to another class, do you need it?• Inappropriate Intimacy– Classes rely on too many details of each other38Other Bad Smells (cont’d)• Alternative Classes with Different Interfaces– Methods do the same thing but have different interfaces• Incomplete Library Class– Library code doesn’t do everything you’d like• Data Class– Classes that act as “structs,” with no computation• Refused Bequest– Subclass doesn’t use features of superclass39Other Bad Smells (cont’d)• Comments!– If code is heavily commented, either• It’s very tricky code (e.g., a hard algorithm), or• The design is bad, and you’re trying to explain it– “When you feel the need to write a comment, first try torefactor the code so that any comment becomessuperfluous.”40Refactoring with Tools• Many refactorings can be performed automatically• This reduces the possibility of making a sillymistake• Eclipse provides support for refactoring in Java– http://www.eclipse.org41More information• Textbook: Refactoring by M. Fowler• Catalog of refactorings:– http://www.refactoring.com/catalog/index.html• Refactoring to patterns–


View Full Document

UMD CMSC 433 - Refactoring

Documents in this Course
Trace 1

Trace 1

62 pages

Reflection

Reflection

137 pages

Testing

Testing

25 pages

Paradigms

Paradigms

10 pages

Testing

Testing

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Java RMI

Java RMI

17 pages

Trace 1

Trace 1

46 pages

Jini

Jini

4 pages

Final

Final

15 pages

Java RMI

Java RMI

13 pages

Testing

Testing

16 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?