SlideShare a Scribd company logo
CODE REFACTORING
CODE REFACTORING DEFINITION
WHY CODE REFACTORING?
WHEN TO DO CODE
REFACTORING
CODE REFACTORING - REQUIREMENTS
IDENTIFYING THE CODE THAT SMELLS
TOOLS FOR REFACTORING
COMPOSING METHODS
Extract Method
Turn the fragment into a method whose name explains the purpose of
the method.
Inline Method
Put the method’s body into the body of its callers and remove the method.
Inline Temp
Replace all references to that temp with the expression.
Replace Temp with Query
Extract the expression into a method. Replace all references to the temp
with the expression. The new method can then be used in other methods.
Introduce Explaining Variable
Extract the expression into a method. Replace all references to the temp
with the expression. The new method can then be used in other methods.
Split Temporary Variable
Make a separate temporary variable for each assignment.
Remove Assignments to Parameters
Use a temporary variable instead
Replace Method with Method Object
Turn the method into its own object so that all the local variables become
fields on that object. You can then decompose the method into other
methods on the same object
Substitute Algorithm
Replace the body of the method with the new algorithm
Substitute Algorithm
Replace the body of the method with the new algorithm
MOVING FEATURES BETWEEN OBJECTS
Move Method
Create a new method with a similar body in the class it uses most. Either
turn the old method into a simple delegation, or remove it altogether
Move Field
Create a new field in the target class, and change all its users
Extract Class
Create a new class and move the relevant fields and methods from the old
class into the new class.
Inline Class
Move all its features into another class and delete it.
Hide Delegate
Create methods on the server to hide the delegate.
Remove Middle Man
Get the client to call the delegate directly.
Introduce Foreign Method
Create a method in the client class with an instance of the server class as its
first argument.
Introduce Local Extension
Create a new class that contains these extra methods. Make this extension
class a subclass or a wrapper of the original.
ORGANIZING DATA
Self Encapsulate Field
Create getting and setting methods for the field and use only those to access
the field.
Replace Data Value with
Object
Turn the data item into an object.
Change Value to Reference
Turn the object into a reference object.
Change Reference to Value
Turn it into a value object
Replace Array with Object
Replace the array with an object that has a field for each element.
Duplicate Observed Data
Copy the data to a domain object. Set up an observer to synchronize the two pieces
of data.
Change Unidirectional Association to
Bidirectional
Add back pointers, and change modifiers to update both sets.
Change Bidirectional Association to
Unidirectional
Drop the unneeded end of the association.
Replace Magic Number with Symbolic
Constant
Create a constant, name it after the meaning, and replace the number with it.
Encapsulate Field
Make it private and provide accessors.
Encapsulate Collection
Make it return a read-only view and provide add/remove methods
Replace Record with Data
Class
Make a dumb data object for the record.
Replace Type Code with
Class
Replace the number with a new class.
Replace Type Code with Subclasses
Replace the type code with subclasses.
Replace Type Code with State/Strategy
Replace the type code with a state object.
Replace Subclass with
Fields
Change the methods to superclass fields and eliminate the subclasses.
SIMPLIFYING CONDITIONAL
EXPRESSIONS
Decompose Conditional
Extract methods from the condition, then part, and else parts.
Consolidate Conditional Expression
Combine them into a single conditional expression and extract it.
Consolidate Duplicate Conditional
Fragments
Move it outside of the expression.
Remove Control Flag
Use a break or return instead.
Replace Nested Conditional with Guard Clauses
Use guard clauses for all the special cases.
Replace Conditional with
Polymorphism
Move each leg of the conditional to an overriding method in a subclass. Make
the original method abstract.
Introduce Null Object
Replace the null value with a null object.
Introduce Assertion
Make the assumption explicit with an assertion.
MAKING METHOD CALLS SIMPLER
Rename Method
Change the name of the method.
Add Parameter
Add a parameter for an object that can pass on this information.
Remove Parameter
Remove it.
Separate Query from
Modifier
Create two methods, one for the query and one for the modification.
Parameterize Method
Create one method that uses a parameter for the different values.
Replace Parameter with Explicit
Methods
Create a separate method for each value of the parameter.
Preserve Whole Object
Send the whole object instead.
Replace Parameter with
Method
Remove the parameter and let the receiver invoke the method.
Introduce Parameter Object
Replace them with an object.
Remove Setting Method
Remove any setting method for that field.
Hide Method
Make the method private.
Replace Constructor with Factory
Method
Replace the constructor with a factory method.
Encapsulate Downcast
Move the downcast to within the method.
Replace Error Code with
Exception
Throw an exception instead.
Replace Exception with Test
Change the caller to make the test first.
DEALING WITH GENERALIZATION
Pull Up Field
Move the field to the superclass.
Pull Up Method
Move them to the superclass.
Pull Up Constructor Body
Create a superclass constructor; call this from the subclass methods.
Push Down Method
Move it to those subclasses.
Push Down Field
Move the field to those subclasses.
Extract Subclass
Create a subclass for that subset of features.
Extract Superclass
Create a superclass and move the common features to the superclass.
Extract Interface
Extract the subset into an interface.
Collapse Hierarchy
Merge them together.
Form Template Method
Get the steps into methods with the same signature, so that the original methods
become the same. Then you can pull them up.
Replace Inheritance with
Delegation
Create a field for the superclass, adjust methods to delegate to the superclass,
and remove the subclassing.
Replace Delegation with
Inheritance
Make the delegating class a subclass of the delegate.
OTHER BIG REFACTORINGS
Tease Apart Inheritance
Create two hierarchies and use delegation to invoke one from the other.
Convert Procedural Design to Objects
Turn the data records into objects, break up the behavior, and move the behavior
to the objects.
Separate Domain from
Presentation
Separate the domain logic into separate domain classes
Extract Hierarchy
Create a hierarchy of classes in which each subclass represents a special case.
CONVENTIONAL REFACTORING SOLUTION
CODE REFACTORING CYCLE
Ensure all tests
pass
Find code that
smells
Determine
Simplification
Make
Simplification
Ensure all tests
still pass
Code Refactoring - 3.0
REFACTORING TEMPLATE
KEY BENEFITS OF REFACTORING
KEY ADVANTAGES OF REFACTORING
CODE REVIEW – BEST PRACTICES
Questions/Discussions

More Related Content

PPTX
5.interface and packages
PPT
2. visualization in data mining
PPTX
component based development model
PDF
Waterfall model
PPTX
PCA and LDA in machine learning
PPTX
Advantages and disadvantages of Simulation
PPT
Software requirements and analysis
PPTX
Waterfall model ppt final
5.interface and packages
2. visualization in data mining
component based development model
Waterfall model
PCA and LDA in machine learning
Advantages and disadvantages of Simulation
Software requirements and analysis
Waterfall model ppt final

What's hot (17)

PPT
Formal Specification in Software Engineering SE9
PPTX
Ppt on this and super keyword
PDF
Revised-Blooms-Taxonomy-Action-Verbs.pdf
PPTX
OOP-Advanced_Programming.pptx
PPT
Validation and verification
PPTX
Slide 2 data models
PPTX
Principles and advantages of oop ppt
PPTX
Coding standard and coding guideline
PDF
[Android] Using Selection Widgets
PDF
Unsupervised Learning in Machine Learning
PDF
Exception handling in plsql
PPTX
Introduction to System, Simulation and Model
PPTX
Slide 6 er strong & weak entity
PPSX
Fixed point and floating-point numbers
PPTX
no free-lunch theorem
PPT
Domain object model
Formal Specification in Software Engineering SE9
Ppt on this and super keyword
Revised-Blooms-Taxonomy-Action-Verbs.pdf
OOP-Advanced_Programming.pptx
Validation and verification
Slide 2 data models
Principles and advantages of oop ppt
Coding standard and coding guideline
[Android] Using Selection Widgets
Unsupervised Learning in Machine Learning
Exception handling in plsql
Introduction to System, Simulation and Model
Slide 6 er strong & weak entity
Fixed point and floating-point numbers
no free-lunch theorem
Domain object model
Ad

Similar to Code Refactoring - 3.0 (20)

PDF
Code Refactoring Cheatsheet
PPT
Bad Smell in Codes - Part 1
PDF
Bad Smell In Codes 1
PDF
Refactoring: Improve the design of existing code
PPTX
Code Refactoring using rails
PPTX
Speeding up web_application
PDF
Bade Smells in Code
PDF
Java defining classes
PPTX
Refactoring: Code it Clean
PPT
Code Refactoring
PPTX
Refactoring Chapter 6,7.pptx
PPT
TDD And Refactoring
PPTX
Agile korea 2013 유석문
PPT
Lecture: Refactoring
PDF
L5
PPTX
Implementing the Adapter Design Pattern
PPT
Ap Power Point Chpt4
PPTX
31 days Refactoring
PPTX
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
PDF
Refactoring Chapter11
Code Refactoring Cheatsheet
Bad Smell in Codes - Part 1
Bad Smell In Codes 1
Refactoring: Improve the design of existing code
Code Refactoring using rails
Speeding up web_application
Bade Smells in Code
Java defining classes
Refactoring: Code it Clean
Code Refactoring
Refactoring Chapter 6,7.pptx
TDD And Refactoring
Agile korea 2013 유석문
Lecture: Refactoring
L5
Implementing the Adapter Design Pattern
Ap Power Point Chpt4
31 days Refactoring
JAVA METHOD AND FUNCTION DIVIDE AND SHORT.pptx
Refactoring Chapter11
Ad

Code Refactoring - 3.0

Editor's Notes

  • #3: Conclusion to course, lecture, et al.
  • #4: Conclusion to course, lecture, et al.
  • #5: Conclusion to course, lecture, et al.
  • #6: Conclusion to course, lecture, et al.
  • #7: Conclusion to course, lecture, et al.
  • #9: Conclusion to course, lecture, et al.
  • #10: Conclusion to course, lecture, et al.
  • #11: Conclusion to course, lecture, et al.
  • #12: Conclusion to course, lecture, et al.
  • #13: Conclusion to course, lecture, et al.
  • #14: Conclusion to course, lecture, et al.
  • #15: Conclusion to course, lecture, et al.
  • #16: Conclusion to course, lecture, et al.
  • #17: Conclusion to course, lecture, et al.
  • #18: Conclusion to course, lecture, et al.
  • #19: Conclusion to course, lecture, et al.
  • #20: Conclusion to course, lecture, et al.
  • #21: Conclusion to course, lecture, et al.
  • #22: Conclusion to course, lecture, et al.
  • #23: Conclusion to course, lecture, et al.
  • #24: Conclusion to course, lecture, et al.
  • #25: Conclusion to course, lecture, et al.
  • #26: Conclusion to course, lecture, et al.
  • #27: Conclusion to course, lecture, et al.
  • #28: Conclusion to course, lecture, et al.
  • #29: Conclusion to course, lecture, et al.
  • #30: Conclusion to course, lecture, et al.
  • #31: Conclusion to course, lecture, et al.
  • #32: Conclusion to course, lecture, et al.
  • #33: Conclusion to course, lecture, et al.
  • #34: Conclusion to course, lecture, et al.
  • #35: Conclusion to course, lecture, et al.
  • #36: Conclusion to course, lecture, et al.
  • #37: Conclusion to course, lecture, et al.
  • #38: Conclusion to course, lecture, et al.
  • #39: Conclusion to course, lecture, et al.
  • #40: Conclusion to course, lecture, et al.
  • #41: Conclusion to course, lecture, et al.
  • #42: Conclusion to course, lecture, et al.
  • #43: Conclusion to course, lecture, et al.
  • #44: Conclusion to course, lecture, et al.
  • #45: Conclusion to course, lecture, et al.
  • #46: Conclusion to course, lecture, et al.
  • #47: Conclusion to course, lecture, et al.
  • #48: Conclusion to course, lecture, et al.
  • #49: Conclusion to course, lecture, et al.
  • #50: Conclusion to course, lecture, et al.
  • #51: Conclusion to course, lecture, et al.
  • #52: Conclusion to course, lecture, et al.
  • #53: Conclusion to course, lecture, et al.
  • #54: Conclusion to course, lecture, et al.
  • #55: Conclusion to course, lecture, et al.
  • #56: Conclusion to course, lecture, et al.
  • #57: Conclusion to course, lecture, et al.
  • #58: Conclusion to course, lecture, et al.
  • #59: Conclusion to course, lecture, et al.
  • #60: Conclusion to course, lecture, et al.
  • #61: Conclusion to course, lecture, et al.
  • #62: Conclusion to course, lecture, et al.
  • #63: Conclusion to course, lecture, et al.
  • #64: Conclusion to course, lecture, et al.
  • #65: Conclusion to course, lecture, et al.
  • #66: Conclusion to course, lecture, et al.
  • #67: Conclusion to course, lecture, et al.
  • #68: Conclusion to course, lecture, et al.
  • #69: Conclusion to course, lecture, et al.
  • #70: Conclusion to course, lecture, et al.
  • #71: Conclusion to course, lecture, et al.
  • #72: Conclusion to course, lecture, et al.
  • #73: Conclusion to course, lecture, et al.
  • #74: Conclusion to course, lecture, et al.
  • #75: Conclusion to course, lecture, et al.
  • #76: Conclusion to course, lecture, et al.
  • #77: Conclusion to course, lecture, et al.
  • #78: Conclusion to course, lecture, et al.
  • #79: Conclusion to course, lecture, et al.
  • #80: Conclusion to course, lecture, et al.
  • #81: Conclusion to course, lecture, et al.
  • #82: Conclusion to course, lecture, et al.
  • #83: Conclusion to course, lecture, et al.
  • #84: Conclusion to course, lecture, et al.
  • #85: Conclusion to course, lecture, et al.
  • #86: Conclusion to course, lecture, et al.
  • #87: Conclusion to course, lecture, et al.
  • #88: Conclusion to course, lecture, et al.
  • #89: Conclusion to course, lecture, et al.
  • #90: Conclusion to course, lecture, et al.
  • #91: Conclusion to course, lecture, et al.
  • #92: Conclusion to course, lecture, et al.
  • #93: Conclusion to course, lecture, et al.
  • #94: Conclusion to course, lecture, et al.
  • #95: Conclusion to course, lecture, et al.
  • #96: An opportunity for questions and discussions.