Skip to content

Instantly share code, notes, and snippets.

@cjolowicz
Last active July 30, 2021 13:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cjolowicz/659cd69f6b7b0b44e153552990019a78 to your computer and use it in GitHub Desktop.
Save cjolowicz/659cd69f6b7b0b44e153552990019a78 to your computer and use it in GitHub Desktop.

Refactoring

These are abbreviated notes taken from Martin Fowler's Refactoring.

Replace Inline Code with Function Call

(This is an atomic refactoring.)

Rename Variable

(This is an atomic refactoring.)

Separate Query from Modifier

  1. Copy the function and rename as query
  2. Remove side effects from query
  3. Replace each call using return value with call to query plus call to modifier
  4. Remove return value from original function

Substitute Algorithm

  1. Extract Function on code to be replaced
  2. Add tests for the new function
  3. Replace the function body

Extract Function

  1. Create the function
  2. Copy the extracted code into the body
  3. Add parameters for non-local references
  4. Replace Inline Code with Function Call on the source function

Inline Function

  1. Replace call with function body (repeating for each call)
  2. Remove function definition

Change Function Declaration (Rename Function)

  1. Extract Function on body (i.e. delegate from source function)
  2. Change the signature of the extracted function
  3. Inline Function to remove source function
  4. Rename Function to replace source function (this refactoring, simple mechanics)

Move Function

  1. Copy function to target context
  2. Replace Inline Code with Function Call to delegate from source function
  3. Inline Function to remove source function

Move statement into function

  1. Extract Function on first caller
  2. Replace Inline Code with Function Call on remaining callers
  3. Inline Function to remove source function
  4. Rename Function to replace source function

Move statement to callers

  1. Extract Function on body without the statement
  2. Inline Function on source function
  3. Rename Function to replace source function

Preserve Whole Object

  1. Create an empty function with the desired signature
  2. Delegate to the source function
  3. Replace each call to the source function with a call to the target function
  4. Inline Function to remove source function
  5. Rename Function to replace source function

Replace Constructor with Factory Function

  • This is a form of Extract Function
  1. Create an empty function with the desired signature
  2. Delegate to the constructor
  3. Replace each call to the constructor with a call to the factory function

Encapsulate Variable

  1. Create functions to read and update the variable
  2. Replace Inline Code with Function Call to replace each variable reference
  3. Restrict the visibility of the variable

Encapsulate Record

  1. Encapsulate Variable on the record variable
  2. Create a class wrapping the record, with a record accessor
  3. Use the object in the variable (use the record accessor in the encapsulating functions)
  4. Extract Function to create an object accessor
  5. Inline Function to use the object accessor in client code
  6. Extract Function to create accessors for fields from client code
  7. Remove Function on the record accessor
  8. Remove Function on the encapsulating functions

Combine Functions into Class

  1. Encapsulate Record to create the class
  2. Move Function on each function
  3. Extract Function on data manipulation code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment