Saturday, June 26, 2004

Why Refactor

Refactoring is changing the structure of existing code without changing the behavior of that code.

I often get asked why we (developers) need to refactor. Here are some reasons.

The ability to refactor your code takes the pressure off the design phase of software development. Refactoring gives you the ability to change the design of the code at a later stage. This means that you don’t have to get the design absolutely right before you write any code. You can get a rough design worked out, code it up and then if (when) you spot a better design you can refactor your code towards the better design. This allows you to get started on the coding earlier than you may have done otherwise and therefore get feedback on the progress from other coders and even customers at an earlier time. This is valuable because this feedback may also force you to rethink the original design and the earlier you can do this the higher your chances are of getting the final result that the customer wants.

It is very rare for any piece of software to be developed ‘right the first time’. I should qualify that by adding that every programmer I have ever spoken to has said they if given a second opportunity to code a solution they would do it differently from the way they did it the first time. I have met a few arrogant individuals who claim they are so smart they always code the correct solution first time. Personally I don’t believe they can always be right and even if they are, they are in a very small minority. Most of us (programmers) need to use all the help we can get to ensure we get a good piece of software written. Refactoring is some of that help; it allows us to admit that we are less than perfect and provides us with a tool to enable us to work towards perfection over time.

The changes are made to the code in order to improve the design, with the aim of making the code easier to read, simpler to understand and cheaper to change.

No comments: