Day35 - Tips for refactoring

05/27/20191 Min Read — In Refactoring

Today I had a refactoring session with my mentor Wolfram ≲Refactorer≳ Kriesing 😎

What is refactoring? Uncle Bob defines refactoring as

… simple operations that change the structure of code without changing its behavior.

Here is my collection of refactoring tips:

  • Make sure that all tests pass before you start (otherwise you cannot ensure that your refactoring does not change behavior)
  • If there are no tests for the code you want to refactor, write them before!
  • When you write these tests, use code coverage tools to ensure that your test really tests the lines of code you are going to refactor
  • Refactoring is done in small steps
  • Make use of your IDE's built-in refactoring support
  • Learn the IDE shortcuts for these refactoring tasks
  • Create a shortcut in your IDE to run tests (this way you get feedback as quickly as possible)
  • Run your tests as often as possible
  • Try to stay green the whole time
  • Don't hesitate to undo changes when you have a red bar
  • Be aware that you are doing refactoring, not implementing new behavior
  • Do not mix refactoring and behavioral changes in the same commit
  • Refactoring is done for economic reasons, not because of code ethics