Examples of Refactoring in Python

This repository contains examples in python that demonstrate over 30 different refactoring mechanisms. Each module contains code and comments illustrating the condition of the code before and after the refactoring.

Refactoring can be categorized into several broad themes [1]:

  • Extraction
  • Renaming
  • Encapsulation
  • Moving
  • Organizing Data
  • Simplifying Conditional Logic
  • Refactoring APIs
  • Dealing with Inheritance

The best way to use this material is to read through the code and comments for each module and run the code using python3 <module_name>.py. As these are pure refactorings, the output from both the approaches (starting code and refactored code) will be identical.

Requirements

  • python version >=3.7

References

[1] Fowler, Martin. Refactoring: improving the design of existing code. Addison-Wesley Professional, 2018.

More About the Author

More information on the author Varun Nayak can be found on his personal webpage.