SlideShare a Scribd company logo
1 of 95
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
REFACTORING TEMPLATE
KEY BENEFITS OF REFACTORING
KEY ADVANTAGES OF REFACTORING
CODE REVIEW – BEST PRACTICES
Questions/Discussions

More Related Content

What's hot

Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet classRuchi Maurya
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design PatternsGodfrey Nolan
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceAbishek Purushothaman
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in javaTalha Saleem
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScriptT11 Sessions
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesVMware Tanzu
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Modelchomas kandar
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom eventBunlong Van
 

What's hot (20)

Spring Data JPA
Spring Data JPASpring Data JPA
Spring Data JPA
 
Java- Datagram Socket class & Datagram Packet class
Java- Datagram Socket class  & Datagram Packet classJava- Datagram Socket class  & Datagram Packet class
Java- Datagram Socket class & Datagram Packet class
 
concept of oops
concept of oopsconcept of oops
concept of oops
 
Android Design Patterns
Android Design PatternsAndroid Design Patterns
Android Design Patterns
 
PHP
PHPPHP
PHP
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
Multiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritanceMultiple choice questions for Java io,files and inheritance
Multiple choice questions for Java io,files and inheritance
 
Java Applet and Graphics
Java Applet and GraphicsJava Applet and Graphics
Java Applet and Graphics
 
Templates in c++
Templates in c++Templates in c++
Templates in c++
 
CSS
CSSCSS
CSS
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
Introduction to web programming with JavaScript
Introduction to web programming with JavaScriptIntroduction to web programming with JavaScript
Introduction to web programming with JavaScript
 
Dynamic method dispatch
Dynamic method dispatchDynamic method dispatch
Dynamic method dispatch
 
Wpf Introduction
Wpf IntroductionWpf Introduction
Wpf Introduction
 
C# Lab Programs.pdf
C# Lab Programs.pdfC# Lab Programs.pdf
C# Lab Programs.pdf
 
Spring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutesSpring Data JPA from 0-100 in 60 minutes
Spring Data JPA from 0-100 in 60 minutes
 
6. static keyword
6. static keyword6. static keyword
6. static keyword
 
Document Object Model
Document Object ModelDocument Object Model
Document Object Model
 
Javascript dom event
Javascript dom eventJavascript dom event
Javascript dom event
 
css.ppt
css.pptcss.ppt
css.ppt
 

Similar to Code Refactoring - 3.0

Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Shaer Hassan
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeValerio Maggio
 
Speeding up web_application
Speeding up web_applicationSpeeding up web_application
Speeding up web_applicationAchintya Kumar
 
Code Refactoring using rails
Code Refactoring using railsCode Refactoring using rails
Code Refactoring using railsAchintya Kumar
 
Bade Smells in Code
Bade Smells in CodeBade Smells in Code
Bade Smells in CodeWill Shen
 
Refactoring: Code it Clean
Refactoring: Code it CleanRefactoring: Code it Clean
Refactoring: Code it CleanAliGermiyanoglu
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문Sangcheol Hwang
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: RefactoringMarcus Denker
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design PatternProdigyView
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4dplunkett
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerIgor Crvenov
 

Similar to Code Refactoring - 3.0 (20)

Code Refactoring Cheatsheet
Code Refactoring CheatsheetCode Refactoring Cheatsheet
Code Refactoring Cheatsheet
 
Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1Bad Smell in Codes - Part 1
Bad Smell in Codes - Part 1
 
Bad Smell In Codes 1
Bad Smell In Codes 1Bad Smell In Codes 1
Bad Smell In Codes 1
 
Refactoring: Improve the design of existing code
Refactoring: Improve the design of existing codeRefactoring: Improve the design of existing code
Refactoring: Improve the design of existing code
 
Speeding up web_application
Speeding up web_applicationSpeeding up web_application
Speeding up web_application
 
Code Refactoring using rails
Code Refactoring using railsCode Refactoring using rails
Code Refactoring using rails
 
Bade Smells in Code
Bade Smells in CodeBade Smells in Code
Bade Smells in Code
 
Java defining classes
Java defining classes Java defining classes
Java defining classes
 
Refactoring: Code it Clean
Refactoring: Code it CleanRefactoring: Code it Clean
Refactoring: Code it Clean
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Refactoring Chapter 6,7.pptx
Refactoring Chapter 6,7.pptxRefactoring Chapter 6,7.pptx
Refactoring Chapter 6,7.pptx
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Agile korea 2013 유석문
Agile korea 2013 유석문Agile korea 2013 유석문
Agile korea 2013 유석문
 
Lecture: Refactoring
Lecture: RefactoringLecture: Refactoring
Lecture: Refactoring
 
L5
L5L5
L5
 
Implementing the Adapter Design Pattern
Implementing the Adapter Design PatternImplementing the Adapter Design Pattern
Implementing the Adapter Design Pattern
 
Ap Power Point Chpt4
Ap Power Point Chpt4Ap Power Point Chpt4
Ap Power Point Chpt4
 
31 days Refactoring
31 days Refactoring31 days Refactoring
31 days Refactoring
 
Refactoring Chapter11
Refactoring Chapter11Refactoring Chapter11
Refactoring Chapter11
 
Refactoring Tips by Martin Fowler
Refactoring Tips by Martin FowlerRefactoring Tips by Martin Fowler
Refactoring Tips by Martin Fowler
 

Code Refactoring - 3.0

Editor's Notes

  1. Conclusion to course, lecture, et al.
  2. Conclusion to course, lecture, et al.
  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.
  8. 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. An opportunity for questions and discussions.