What did I learn about refactoring ?

Link : Refactoring Python

  • Repeatedly reorganizing and rewriting code until it is obvious to a new reader
  • When do you refactor ?
    • In advance - If you are know you are about to change code, it
    • For testing - All code should be easy to test
    • DRY
    • Brittleness - If every time you add something, if your code breaks, then the code is so closely coupled. Hence it is time to refactor
    • Complex - Make it easy to understand
  • Difference between good and great programmers

Brett Slatkin - Good and Great Programmer

  • Great programmers spend considerable amount of refactoring and spend time on style
    • HALF of the time is spent on refactoring and style
    • Refactoring is always a good investment of time
  • How do you refactor ?
    • Identify bad code
    • Improve it
    • Run tests
    • Fix and improve tests
    • Repeat
  • How do you refactor in practice ?
    • Rename, split and move
    • Simplify
    • Redraw boundaries
  • Strategies for refactoring
    • Before you refactor you have to have thorough tests
    • Quick tests
    • Source control - You can back it out
    • Willing to make mistakes
  • THREE main strategies
    • Extract variable, functions and classes
      • Extract variables
      • Extract in to functions
      • Cache variables - extract in to variables and extract to functions
      • Extract a class if the function gets complicated
        • Caches the result of the computation
      • Is refactoring helping you to test ? Extract to improve testability
      • Extract variables and functions to improve readability
      • Extract variables in to classes to improve testability
      • Use __bool__ to indicate a class is a paper trail
    • Refactor classes
      • Keeping track of pet has more functionalities
      • Helper methods @property to act on the attributes
    • How do you redraw boundaries ?
      • Add an improved interface
        • Backward compatibility
        • Issue warnings for old usage
      • Migrate old usage to new usage
        • Run tests to verify correctness
        • Fix and improve broken tests
      • Remove code for old interface
      • All warnings can be made in to errors - Helps you to find old ways so that you can replace them
      • To remember
        • Split clsases using optional arguments
        • Use @property to move methods and fields between classes
        • Issue warnings in old code paths to find their occurrences
      • Move Field Gotchas
        • Keep in mind things when you move fields in the definition across classes
        • Use @property.setter to move fields that can be assigned
        • Defend against muscle memory with tombstone @property
  • Learnt about the book Effective Python
  • Stop Writing Classes
  • Beyond PEP 8

Overall, the biggest takeaway is that I am going to spend the next 15 days refactoring my code so that the code is beautiful, clean and is something I can be proud of.

Now, I do have a nice visual image about great programmers spending half of the time refactoring. So, that gives me added nudge to spend the rest of my time refactoring. This also means that I have to write tests first