June 5, 2014

Refactoring

Refactoring is a natural part of good development. Refactoring lends itself well to the Agile iterative approach to development.

The term ‘refactoring’ is used to mean changing code to either clean it up so that it is easier to work with and adhere to best practices or to change in order to make new functionality possible.

Why refactoring is necessary and a good thing

Refactoring is necessary because each new feature that is added changes the existing code base in some way. Refactoring is the process of changing that code and improving it based on new knowledge that has come to light since it was first written.

Often this improvement comes because of increased developer knowledge and experience in how to code better, but can come simply because knowledge of the product and emerging architecture is better understood.

What the experts say

Kent Beck the founder of Extreme Programming has the following to say on refactoring code:

Why Refactoring  Works

“Programs have two kinds of value: what they can do for you today and what they can do for you tomorrow. Most times when we are programming, we are focused on what we want the program to do today. Whether we are fixing a bug or adding a feature, we are making today’s program more valuable by making it more capable.

You can’t program long without realizing that what the system does today is only a part of the story. If you can get today’s work done today, but you do it in such a way that you can’t possibly get tomorrow’s work done tomorrow, then you lose. Notice, though, that you know what you need to do today, but you’re not quite sure about tomorrow. Maybe you’ll do this, maybe that, maybe something you haven’t imagined yet.

I know enough to do today’s work. I don’t know enough to do tomorrow’s. But if I only work for today, I won’t be able to work tomorrow at all.

Refactoring is one way out of the bind. When you find that yesterday’s decision doesn’t make sense today, you change the decision. Now you can do today’s work. Tomorrow, some of your understanding as of today will seem naive, so you’ll change that, too.

What is it that makes programs hard to work with? Four things I can think of as I am typing this are as follows:

  • Programs that are hard to read are hard to modify.
  • Programs that have duplicated logic are hard to modify.
  • Programs that require additional behaviour that requires you to change running code are hard to modify.
  • Programs with complex conditional logic are hard to modify.

So, we want programs that are easy to read, that have all logic specified in one and only one place, that do not allow changes to endanger existing behaviour, and that allow conditional logic to be expressed as simply as possible.

Refactoring is the process of taking a running program and adding to its value, not by changing its behaviour but by giving it more of these qualities that enable us to continue developing at speed. ” – Kent Beck

Refactoring patterns

Most development environments have built into them refactoring tools that allow you to do the most common refactoring. For .Net in Visual Studio you have resharper http://www.jetbrains.com/resharper/ and for Java you have the intelliJ environment http://www.jetbrains.com/idea/

For a more formal understanding of refactoring you can review the book by Martin Fowler Refactoring: Improving the Design of Existing Code (Object Technology Series)

http://www.amazon.co.uk/gp/product/0201485672/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=1634&creative=6738&creativeASIN=0201485672&linkCode=as2&tag=cityboysalary-21

It is also worth reading Robert C Martin’s book Clean code. http://www.amazon.co.uk/gp/product/0132350882/ref=as_li_qf_sp_asin_il_tl?ie=UTF8&camp=1634&creative=6738&creativeASIN=0132350882&linkCode=as2&tag=cityboysalary-21

Simon Powers

Simon Powers is an Agile Coach specialising in large scale transformations and agile adoption. He has a background in very large enterprise architecture which has led on to organisational design and agile process refinement. Simon is the founder of Adventures with Agile.