Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refactor rapidly evolving code?

I have some research code that's a real rat's nest, with code duplication everywhere, and clearly needs to be refactored. However, the code base is evolving as I come up with new variations on the theme and fit them into the codebase. The reason I've put off refactoring so long is because I feel like the minute I spend a few days coming up with good abstractions, seeing what design patterns fit where, etc., I'll want to try out some new unforeseen idea that makes my abstractions completely inadequate. In other words, because of the rate at which the code is evolving, I really have no idea where abstraction lines belong, even though there is no shortage of (approximate) duplication and the general messiness of the code makes adding stuff to it a real pain. What are some general best practices for coping with this kind of situation?

like image 398
dsimcha Avatar asked Feb 26 '09 05:02

dsimcha


People also ask

Does refactoring make code faster?

Allows you to write programs faster Ultimately, it all comes down to one thing: refactoring helps speed up the development of a code. Better designs, better readability, fewer errors – all this means quality. It makes programs develop faster because it keeps the composition of the system from decaying.

What refactoring can be used when a method is too long has duplicated code?

If a duplicate code is present in: The same method, create the same Local Variable and reuse it. The same class, create common Method refactoring.

Why is refactoring so hard?

Refactoring is difficult because many, if not most, programmers do not know what the end result should be. They have really never seen well written code and have been taught since writing their first line of code that if it compiles and runs, that is enough.


3 Answers

Don't spend so long refactoring!

When you're about make a change in a piece of code, consider refactoring it to make the change easier.

After making the change, refactor again to clean up the damage done by that change.

In both cases, make the refactorings small and do them quickly, and move on.

You don't have to keep your code pristine at all times, but remember that it's easier to go fast if you have well-factored code to work in (and if you have good unit tests, of course).

like image 191
Jay Bazuzi Avatar answered Sep 19 '22 23:09

Jay Bazuzi


Test Driven Development:

Red, Green, Refactor. Rinse, repeat.

Since it's one of the steps in every single cycle, you'll notice that's a LOT of usually minor refactoring taking place. That's the way it should be.

like image 44
Epaga Avatar answered Sep 20 '22 23:09

Epaga


Your situation is pretty familiar to me. While doing investigative coding often you have no idea what the "right" abstraction will be, and as you say it can change with every new idea.
Other posters have suggested:

  • Continuous small refactoring, which helps to avoid getting into the rats-nest situation
  • Test-Driven Development, which helps to find good, re-usable abstractions. It's important to note that TDD is less about testing than about doing good designs!

However, for investigative research code there is another strategy: the prototype. This seems to be what you are currently doing: coding as quickly as possible to prove a concept. There's nothing wrong with that, but a prototype should always be throw-away. Tweak it until you have all the necessary input and knowledge, then throw away the code and start over with TDD and continuous refactoring, and all your other "doing the things right" strategies.

Don't keep any of the code. Don't copy-paste anything. Don't refer back to it. Just start over with your new knowledge.

like image 45
Joris Timmermans Avatar answered Sep 17 '22 23:09

Joris Timmermans