Skip to content

Latest commit

 

History

History
155 lines (134 loc) · 7.59 KB

refactoring-cheatsheet.md

File metadata and controls

155 lines (134 loc) · 7.59 KB

Refactorings List

The below will be my notes on each of the sections in Refactoring Improving the Design of Existing Code


Composing Methods

Extract Method

https://refactoring.com/catalog/extractMethod.html

  • Helps provide clarity of purpose for the enclosed statements
  • Removes the need for comments when using expressive method names
  • Consider using even over short pieces of code that are semantically dense
  • Consider using if inside the method the statements deal with different layers of abstraction
  • Don't use if the extracted piece of code cannot be given a better name than the current method's

Inline Method

https://refactoring.com/catalog/inlineMethod.html

  • Helps with needless indirection
  • Consider using as an intermediate step to cleaning up badly factored, smaller methods
  • Don't use if a subclass overrides the method

Inline Temp

https://refactoring.com/catalog/inlineTemp.html

  • Only use when assigning the value of a method call
  • Consider using as an intermediate step when refactoring to a Replace Temp with Query

Replace Temp with Query

https://refactoring.com/catalog/replaceTempWithQuery.html

  • Helps reduce method size by extracting a commonly used query
  • Consider using if a given expression is used only once within the method
  • Consider using if the temp variable is only assigned to once within the method

Introduce Explaining Variable

https://refactoring.com/catalog/extractVariable.html

  • Helps with expressiveness of conditional statements
  • Consider using if many conditionals are being considered
  • Consider using Extract Method for the conditional statement if it is a recurring check instead
  • Often leads to Replace Temp With Query

Split Temporary Variable

https://refactoring.com/catalog/splitTemporaryVariable.html

  • Consider using if you are re-using a temp variable
  • Helps refactor code to follow Single Responsibility Principle
  • Can likely be identified if the name of the variable does not express what it's used for in a meaningful way (temp, foo, etc.)

Remove Assignments to Parameters

https://refactoring.com/catalog/removeAssignmentsToParameters.html

  • Avoid assigning value to parameters, instead use a temp variable
  • Helps avoid clarity issues between pass by reference and pass by value
  • Output parameters are an exception but should be used in very limited circumstances

Replace Method with Method Object

https://refactoring.com/catalog/replaceMethodWithMethodObject.html

  • Introduces the Command Pattern
  • Reduces duplication in common work by encapsulating that functionality in an object

Substitute Algorithm

https://refactoring.com/catalog/substituteAlgorithm.html

  • Given two algorithms, select the one that is either
    • More performant
    • More succinct
    • More expressive
  • Have tests that assert the result of the original algorithm and run them after replacing to insure the new algorithm is a proper substitute

Moving Features Between Objects

Move Method

https://refactoring.com/catalog/moveMethod.html