Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How often should you refactor?

Tags:

refactoring

I had a discussion a few weeks back with some co-workers on refactoring, and I seem to be in a minority that believes "Refactor early, refactor often" is a good approach that keeps code from getting messy and unmaintainable. A number of other people thought that it just belongs in the maintenance phases of a project.

If you have an opinion, please defend it.

like image 832
Bob King Avatar asked Sep 26 '08 16:09

Bob King


People also ask

When should you refactor?

The best time to consider refactoring is before adding any updates or new features to existing code. Going back and cleaning up the current code before adding in new programming will not only improve the quality of the product itself, it will make it easier for future developers to build on the original code.

When should I stop refactoring?

One should not start refactoring unless he has a clear purpose in mind. Once the purpose has been accomplished, one is done. There is probably not an explicit 10 point check list to tell you when you are done, but most people can determine if they are being productive or just playing.

When should you refactor and rewrite?

Rewrites differ from refactors because refactors take the existing code and improve it, while rewrites throw away the old code before starting on the new. Instead of rewriting code, learn better refactoring techniques.

When should refactoring be done in agile?

Refactoring is a mandatory skill for Agile Teams and is a critical component of the Team and Technical Agility competency of the Lean Enterprise. Refactors should routinely appear on the Team Backlog and be included—along with in-line refactoring—in story estimates.


2 Answers

Just like you said: refactor early, refactor often.

Refactoring early means the necessary changes are still fresh on my mind. Refactoring often means the changes tend to be smaller.

Delaying refactoring only ends up making a big mess which further makes it harder to refactor. Cleaning up as soon as I notice the mess prevents it from building up and becoming a problem later.

like image 89
jop Avatar answered Sep 23 '22 00:09

jop


I refactor code as soon as it's functional (all the tests pass). This way I clean it up while it's still fresh in my mind, and before anyone else sees how ugly the first version was.

After the initial check-in I typically refactor every time I touch a piece of code. Refactoring isn't something you should set aside separate time for. It should be something you just do as you go.

like image 31
2 revs Avatar answered Sep 25 '22 00:09

2 revs