Skip to content

Instantly share code, notes, and snippets.

@itolosa
Last active June 22, 2023 19:58
Show Gist options
  • Select an option

  • Save itolosa/dceb1a41b81c83434d4d05b7805bb019 to your computer and use it in GitHub Desktop.

Select an option

Save itolosa/dceb1a41b81c83434d4d05b7805bb019 to your computer and use it in GitHub Desktop.

How to refactor code

Refactoring: Change the code's structure without changing its external behavior

  • Low coupling: Separate all that's possible to separate without making a mess
  • High cohesion: Minimize the mess ~ Minimize the inter-group dependencies and maximize the intra-group ones

Invariants:

  • Behavior must be the same at all times - i.e Testing
  • Optionally format at all times
  • Optionally 1 class per file

Procedure:

  1. Divide / Decompose
  2. Group / Abstract
  3. Join / Compose

Incremental version (OOP)

  • Minimal piece of abstraction = Method

  • Group of methods = Class

  • Group of classes = Module

  • Use testing at all times

Method level:

  1. Extract all possible methods
  2. Group methods into other methods
  3. Compose methods

Method-Class level:

  1. Divide methods
  2. Group methods into classes
  3. Compose classes

Class level:

  1. Extract classes
  2. Group classes into other classes
  3. Compose classes

Class-Module level:

  1. Divide classes
  2. Group classes into modules
  3. Compose modules
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment