I've got some software that I wrote around 2 years ago and needs some features added to it. I've realised that it's in an awful mess, and I have the urge to move everything around, tidy up, etc. I've read the Joel on Software article about not starting again, so whats the best way forward?
You have three basic options:
If the app is very small and a real mess, starting again might actuall be your best bet.
Live with the mess and hack in the additional features.
Typically, option (2) is your best bet.
How much refactoring you actually do will depend on the resource you put in vs the value you get out. Questions to ask will include:
- What time / budget is available?
- How much modification do you anticipate in the future?
- Who else will see the code? (ie. will messy code damage your reputation?)
- Is anyone else expected to maintain the code?
- What refactoring tools are available to help you?
- What is your experience of refactoring?
- What experience will you gain from refactoring?
- What kinds of refactoring will give you the most benefits?
- What automated tests already exist? Need to be written?
- How much manual testing will be required?
- How will you feel if you leave the code as it is?
In my experience, it is very easy to get into proper muddle during a refactoring session. The most important lessons that I've learned are to:
- Do one thing at a time.
- Take small steps.
- Make good use of your source control (check in frequently + include comments).
- Make use of automated refactoring tools.
- Know the IDE.
Well, at least refactor enough so the new feature can be added safely. I.e. don't make it even worse. The rest depends on motivation, budget and time constraints - but be aware to fully cleaning up a mess can take longer than originaly creating it.
This time while fixing the things make sure to document it. Next time you will see the code it will be much easier to recall the things.
It depends, is it going to cost more time maintaining it because it's a mess, or to rewrite it so it's not a mess and easily maintained. I'm personally going through this right now, I'm converting an intranet site to ASP.Net MVC3 because the old code was a pile of crap (that I wrote) because it was supposed to be disposable (yes, I should know better). The old pile of crap is still here, and it's a headache adding features and fixing bugs. MVC is beautiful and makes working on it actually pleasurable, so it's getting a rewrite.