Refactoring really is quite the skill and something that I hope I will get better at over time. One of my stories over the past couple of weeks was to go through the VideoStore refactoring kata introduced in the first chapter of Refactoring by Martin Fowler. It has been really useful, more to get in the habit of making small changes and running tests more than anything else.

Here are some of the main learnings I picked up going over chapter 1.

  1. Start by finding out if there are any local variables in local scope and see if they can be passed in as a parameter to an extracted method that uses them.
  2. Always try to refactor methods where it makes sense to do so and makes the code easier to read and always see if you can rename methods after you refactor.
  3. Check what information the method you have extracted is using. If it is not using much from the class it is in, chances are it is in the wrong place and you should try and move it to a class whose methods it does use.
  4. Check if the variables are really necessary or if you can use method calls instead were they are needed (usually if there are two or less).
  5. Try to remove temp variables and use query methods if possible (These make the code easier to use, within HTML templates for example).
  6. If a calculation method is used, try and find a place where it will have the least “ripple effect.” ie. If it changes, it will effect the least code.
  7. Use polymorphism where you can, and try to replace conditional statements with it.

One of the biggest take aways I got was to push to git regularly, after every little change or refactor, which actually makes thinking of an accurate and short commit message much easier.

Another aspect I found interesting was going through Uncle Bob’s video on the same VideoStore refactor and seeing the solution he came up with, which is quite different. It was useful to see that even though their final code was different the approaches they had were similar. Small refactorings, running the tests regularly and naming well. I am going to make sure I do the same going ahead.