Refactoring-III

Refactoring-III - Refactoring (continued) Source:...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
Refactoring (continued) Source: "Refactoring: Improving the Design of Existing Code", Martin Fowler
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Effective Refactoring Knowing what refactorings are available Knowing when to apply them
Background image of page 2
Refactoring Catalog Example: Introduce Parameter Object
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Knowing When to Refactor “If it stinks, change it.” Grandma Beck, discussing child-rearing philosophy
Background image of page 4
Bad Smells in Code (Signs that you need to refactor) Duplicated code Long method Large class Long parameter list Divergent change Shotgun surgery Feature envy Data clumps Primitive obsession Switch statements Parallel inheritance hierarchies Lazy class Speculative generality Temporary field Message chains Middle man Inappropriate intimacy Alternative classes with different interfaces Incomplete library class Data class Refused bequest Comments
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Duplicated Code The same code structure is duplicated in multiple places Identical sections of code Similar sections of code (e.g., methods with similar structures) Hard to maintain, serious design problem Same Class => Extract Method Sibling Classes => Extract Method , Pull Up Method Similar Code In Sibling Classes => Form Template Method Unrelated Classes => Extract Class , all classes invoke the new class Unrelated Classes => Extract Method in one class, make other classes call its method
Background image of page 6
Temporary Field A class has one or more fields (i.e., variables) that are not used all the time Trying to understand why and when these fields aren't set is confusing Example: Some instances have a particular attribute, some don't E.g., Employee class with hourlyRate field that is used only for some employees Missing subclass. Use Extract Subclass to push conditional attributes into appropriate subclasses (e.g., HourlyEmployee) E.g., Within a class, rather than passing values between methods through parameter lists, values are temporarily stored in object variables. These variables have meaningful values only when a particular method is running (undesirable) Replace Method with Method Object
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Long Method Long methods are hard to understand and more prone to bugs Find parts of the method that naturally go together, and Extract Method Problem: How do the new sub-methods access the parameters and locals of the original method? Store the original method’s parameters and locals in instance variables so all sub-methods can access them? No. This would cause the “Temporary Field” problem (fields that are not used all of the time) Parameters & locals of original method could be passed as parameters into sub-methods Often works, but sometimes leads to long parameter lists on sub- methods Could Introduce Parameter Object to shorten parameter lists
Background image of page 8
Long Method (cont.) If the long method is really long , or if the parameter lists on extracted sub-methods are too long, you can Replace Method with Method
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2012 for the course C S 340 taught by Professor Woodfield,s during the Winter '08 term at BYU.

Page1 / 30

Refactoring-III - Refactoring (continued) Source:...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online