Greetings :)

Hi, I'm Louis!
I like to code, but I don't do enough of it.

This blog is about trying my best to keep up with the ever evolving stream of technology.

Friday, September 17, 2010

Refactoring


Steps to take while refactoring:
  1. One SMALL change at a time
  2. Run tests to ensure functionality is the same
  3. Go to step 1

Things to remember while refactoring:
  • Don't add any additional functionality
  • Make the solution easier to understand, and easier for future modification (separation of concerns, good variable naming practice etc).
Refactoring Tactics:
You can also find a lot of information on the refactoring.com website, or Canterbury's OO wiki.
  • Divide and Conquer / Piecemeal refactoring - break the problem into manageable chunks, make only a small change at a time.
  • Rejected Parameter - when extracting a method it is often useful to remove some of the parameters passed to the new method. This can be done by:
    • Inlining local variables into the code that is about to be extracted (so the variable is not declared prior to the method call).
    • Converting local variables to fields, so that the new method will be able to see the data (only do this if it makes sense to make the local variables fields).
  • Caller Swap - Instead of having something like "var2.getX().equals(var1.getX()), you extract the code into a new method isCompatibleWith(var1, var2), and then you move the isCompatibleWith method to the class of var1 or var2 to look like var1.isCompatibleWith(var2)"
  • Inline then extract - when you wish to move a method to another class, you can inline the contents of the method to the other class, and then extract the resulted inlined code into a new method.
  • Temporary static - when wishing to move a method to another class, there may be problems with moving it with refactoring tools. This problem doesn't occur if the method is static, so you can temporarily make the method static, fix any dependencies on local variables (e.g. by passing in a reference to an object as a parameter), and then move the method to a new class, and remove the static declaration.
  • Scaffolding - creating methods temporarily to help transition to a better design. Once the methods no longer have a use, the code in the methods can be inlined into their appropriate positions.


    No comments:

    Post a Comment