Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

At what point does refactoring become not worth it?

Say you have a program that currently functions the way it is supposed to. The application has very poor code behind it, eats up a lot of memory, is unscalable and would take major rewriting to implement any changes in functionality.

At what point does refactoring become less logical then a total rebuild?

like image 490
JD Isaacks Avatar asked Mar 05 '09 16:03

JD Isaacks


People also ask

When should refactoring not be done?

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.

What are the disadvantages of refactoring code?

Maintainability: After refactoring, the code is fresher, easier to understand or read, less complex and easier to maintain. Disadvantages of Code Refactoring: Time Consuming: You may have no idea how much time it may take to complete the process. It may also land you into a situation where you have no idea where to go.

What would not be considered refactoring?

Fixing any bugs that you find along the way is not refactoring. Optimization is not refactoring. Tightening up error handling and adding defensive code is not refactoring. Making the code more testable is not refactoring – although this may happen as the result of refactoring.

How much time should be spent on refactoring?

In practice, most times you look at your code and you're happy with it or there's a small refactor, like extracting a method, which takes a minute or two of your time. On rare occasions, a significant refactoring becomes apparent and takes up 20 – 30 minutes of your time.


9 Answers

One benefit of refactoring over rebuilding is that IF you can do refactoring step by step, i.e. in increments, you can test the increments in the context of the whole system, making development and debugging faster.

Old and deployed code, even when ugly and slow, has the benefit of having been tested thoroughly, and this benefit is lost if you start from scratch.

An incremental refactoring approach also has helps to ensure that there is always a product available which can be shipped (and it's improving constantly).

There is a nice article on the web about how Netscape 6 was written from scratch and it was business-wise a bad idea.

like image 57
Antti Huima Avatar answered Oct 12 '22 03:10

Antti Huima


Joel wrote a nice essay about this very topic:

Things You Should Never Do, Part 1

The key lesson I got from this is that although the old code is horrible, hurts your eyes and your aesthetic sense, there's a pretty good chance that a lot of that code is patching undocumented errors and problems. Ie., it has a lot of domain knowledge embedded in it and it will be difficult or impossible for you to replicate it. You'll constantly be hitting against bugs-of-omission.

A book I found immensely useful is Working Effectively With Legacy Code by Michael C. Feathers. It offers strategies and methods for approaching even truly ugly legacy code.

like image 39
Dana Avatar answered Oct 12 '22 02:10

Dana


Robert L. Glass suggests that

Modification of reused code is particularly error-prone. If more than 20 to 25 percent of a component is to be revised, it is more efficient and effective to write it from scratch.

like image 21
SilentGhost Avatar answered Oct 12 '22 04:10

SilentGhost


Well, the simplest answer is if it will take longer to refactor than it will to rebuild, then you should just rebuild.

If it's a personal project then you might want to rebuild it anyway as you will probably learn more from building from scratch than you would from refactoring, and that's one big objective of personal projects.

However, in a professional time-limited environment, you should always go with whatever costs the company the least amount of money (for the same payoff) in the long run, which means choosing whichever takes less time.

Of course, it can be a little more complicated than that. If other people can be working on features while the refactoring is being done, then that might be a better choice over having everyone wait for a completely new version to be built. In that case rebuilding might take less time than just the refactoring would have taken, but you need to take the entire project and all contributors of the project in to account.

like image 23
Allie the Icon Avatar answered Oct 12 '22 04:10

Allie the Icon


When you spend more time refactoring than actually writing code.

like image 41
TheTXI Avatar answered Oct 12 '22 03:10

TheTXI


At the point where the software doesn't do what it's supposed to do. Refactoring (changing the code without changing the functionality) makes sense if and only if the functionality is "as intended".

like image 28
Mike Burton Avatar answered Oct 12 '22 04:10

Mike Burton


If you can afford the time to completely rebuild the app, don't need to improve functionality incrementally, and don't wish to retain any of the existing code then rewriting is certainly a viable alternative. You can, on the other hand, use refactoring to do an incremental rewrite by slowly replacing the existing functions with equivalent functions that are better written and more efficient.

like image 44
tvanfosson Avatar answered Oct 12 '22 02:10

tvanfosson


If the application is very small, then you can rewrite it from scratch. If the application is big, never do it. Rewrite it progressively, one step at a time validating you didn't break anything.

The application is the specification. If your rewrite it from scratch you will most likely run into a lots of insidious bugs because "no one knew that the call to this function was supposed to return 3 in that very specific case" (undocumented behaviour...).

It's always more fun to rewrite from scratch so your brain might trick you into thinking it's the right choice. Be careful, it's most likely not.

like image 23
Edouard A. Avatar answered Oct 12 '22 02:10

Edouard A.


I've worked with such applications in the past. The best approach I've found is a gradual one: When you are working on the code, find things that are done multiple times, group them together in functions. Keep a notebook (you know, a real one, with paper, and a pencil or pen) so that you can mark your progress. Use that in combination with your VCS, not instead of it. The notebook can be used to provide an overview of the new functions you've created as part of the refactoring, and the VCS of course fills in the blanks for the details.

Over time, you will have consolidated a lot of code into more appropriate places. Code duplication during this period of time is going to be next to impossible, so just do it as best as you can until you've reached a point where you can really start the refactoring process, auditing the entire code base and working on it as a whole.

If you've not enough time for that process (which will take a very long time), then rewriting from scratch using a test-first approach is probably better.

like image 34
Michael Trausch Avatar answered Oct 12 '22 04:10

Michael Trausch