A Funny Java Flavoured Look at the World

Friday, June 09, 2006

Should you always refactor code?

I read this blog again today Things you Should Never do, Part 1

It reminded me of a chat I had with another developer the other day, he was discussing some legacy code and he came to the conclusion that we should really refactor all of these classes. I have to admit that I wasn't so keen. I enjoy refactoring, watching the code transform is quite enjoyable.

but.... (I know you saw that coming)

I wasn't so sure it was worth refactoring all this code because it did currently work. Although it would improve the code most of the code were a set of seperate actions and refactoring them wouldn't have much improvement on the other code. I think I basically felt that we could use our time more productivily doing other things. I was in favour of a bit like are new junit test writing policy, if you change the code, write a test. Now we also have if you change the code, refactor it.

Of course most of the code doesn't have unit tests for it either. This can make writing a unit test and refactoring the code a bit tricky.

I appreciate that we will probably have to refactor the code when day and it maybe whilst fixing a bug but I had the line of agile programmers

"do the simpliest thing that works"

I was thinking the simpliest thing is to leave the code until we are in there for some reason. On the other hand though I can feel the code will leap up and bite us on the ass at some point in the future but if it does then we fix it and if it keeps working then let it keep working.

It is also a bit over whelming when you have quite a few classes to refactor, it feels a bit like writing the unit test after you have written the code, it becomes too much of an effort, an effort it seems at the time for no reward.

In the end we didn't really have a decision to make because we were busy working on a couple of projects but it got me thinking, should you always refactor code? What code shouldn't you refactor.

I suppose this must be a popular discussion between developers and talking about refactoring is a tricky subject because by its nature the outward benefits of refactoring are making the code easier to maintain, extend and reuse but it should do eactly the same as the smelly code you have removed.

This article talks about the decision in a more detailed manner than me



Post a Comment

<< Home