SlideShare ist ein Scribd-Unternehmen logo
1 von 95
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt? (20)

Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
C# Delegates
C# DelegatesC# Delegates
C# Delegates
 
Code Smells
Code SmellsCode Smells
Code Smells
 
Intro Programación Orientada a Objetos
Intro Programación Orientada a ObjetosIntro Programación Orientada a Objetos
Intro Programación Orientada a Objetos
 
Jsp with mvc
Jsp with mvcJsp with mvc
Jsp with mvc
 
Clases abstractas e interfaces en java
Clases abstractas e interfaces en javaClases abstractas e interfaces en java
Clases abstractas e interfaces en java
 
Adjustment of inheritance
Adjustment of inheritanceAdjustment of inheritance
Adjustment of inheritance
 
6. tda arrayu generico
6. tda arrayu generico6. tda arrayu generico
6. tda arrayu generico
 
Modelo Entidad Relacion Hospital
Modelo Entidad Relacion HospitalModelo Entidad Relacion Hospital
Modelo Entidad Relacion Hospital
 
Java programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- InheritanceJava programming -Object-Oriented Thinking- Inheritance
Java programming -Object-Oriented Thinking- Inheritance
 
Tipo de datos_para_c++
Tipo de datos_para_c++Tipo de datos_para_c++
Tipo de datos_para_c++
 
카카오i 오픈빌더로 피자챗봇 만들기
카카오i 오픈빌더로 피자챗봇 만들기카카오i 오픈빌더로 피자챗봇 만들기
카카오i 오픈빌더로 피자챗봇 만들기
 
Delegates and events in C#
Delegates and events in C#Delegates and events in C#
Delegates and events in C#
 
Pilares de las poo
Pilares de las pooPilares de las poo
Pilares de las poo
 
1 Paradigma Objetos
1 Paradigma Objetos1 Paradigma Objetos
1 Paradigma Objetos
 
struct and class deferences
 struct and class deferences struct and class deferences
struct and class deferences
 
Abstract factory
Abstract factoryAbstract factory
Abstract factory
 
Greenfoot 8
Greenfoot 8Greenfoot 8
Greenfoot 8
 
Paradigma Orientado a Objetos
Paradigma Orientado a ObjetosParadigma Orientado a Objetos
Paradigma Orientado a Objetos
 
Ejemplo de modelo relacional
Ejemplo de modelo relacionalEjemplo de modelo relacional
Ejemplo de modelo relacional
 

Ähnlich wie 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
 
Polymorphism
PolymorphismPolymorphism
PolymorphismKumar
 

Ähnlich wie Code Refactoring - 3.0 (20)

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
 
Polymorphism
PolymorphismPolymorphism
Polymorphism
 

Code Refactoring - 3.0

Hinweis der Redaktion

  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.