DEV Community

Refactoring old code vs Rewriting everything for long-term?

Andrei Gatej on May 11, 2019

Hi devs! I'm finding myself in a tricky situation and I'd really appreciate if you could express your opinion on my approach to this. So I'm work...
Collapse
 
johannestegner profile image
Johannes

I would personally say: Start with writing the tests. That way you can make sure that the current code is working as intended. After that, take a long look at the "logic" and see if it would actually be worth to rewrite, if it's a short process to write a new version, branch out and do it! If it would take a long time, it depends much on the time you actually have to do it. If you are allowed to rewrite it even if it takes time, I'd go on with a rewrite, but else, leave it.

It's hard to give much advice as we don't know much about your position, if you have an annoying lead or boss standing behind your back yelling that they want that new feature a week ago, you might just want to patch it.

Collapse
 
johannestegner profile image
Johannes

Even if you don't rewrite it, create the tests, testing is great, testing is awesome, you should always write tests! ;D

Collapse
 
anduser96 profile image
Andrei Gatej

Thank you! I will definitely follow your advice.

I’ve heard a lot about testing and its practices, but I’ve never actually written a single test.

Now, because the project I’m working on became pretty complex, I can see how writing tests would make my life easier.

Collapse
 
logicmason profile image
Mark • Edited

It's impossible to say for sure, since every project is different, but I'd shy away from doing a full rewrite unless you have a lot of time and/or people devoted to the project.

It might help you to write some high-level tests first. That will clarify how the app currently works. Tests also make it possible to refactor a bit more fearlessly since you'll get feedback as soon as a change breaks things. After you've made the changes necessary for your feature, you can assess what other parts of the app look dangerous to change and write tests for those.

If you want to migrate the existing project to TypeScript, I'd start from the bottom level components and work up. E.g. a sub-component can be written in TypeScript even if the modules using it are all plain JS.

Collapse
 
anduser96 profile image
Andrei Gatej

Thanks for the reply!

On second thoughts, a complete rewrite would indeed take a lot of time.

Your advice really helped me.

Thanks!

Collapse
 
stephencweiss profile image
Stephen Charles Weiss

Having just spent several days (not a huge investment in the grand scheme of things) refactoring a significant portion of my app, I'm nonetheless reminded of a fantastic essay by Joel Spolksy -- joelonsoftware.com/2000/04/06/thin...

Collapse
 
anduser96 profile image
Andrei Gatej

It is indeed fantastic.
Even though I decided not to rewrite everything, now I see things differently.
The article you provided has taught me important lessons. I couldn’t agree more with what he states.

Thank you!

Collapse
 
cubiclebuddha profile image
Cubicle Buddha

It’s better to finish than to be perfect. So I’d recommend refactoring it file by file into TypeScript. And yes as other people have mentioned, having some tests created first will help you to refactor with confidence.

One of my favorite programming guides shared the wisdom that’s almost never appropriate to do a full rewrite. Now he’s mostly referring to professional applications, but consider watching the video. True to it’s name, it’s fun! youtu.be/XcUUY8ziTuk

Collapse
 
anduser96 profile image
Andrei Gatej

It will be a true challenge now that it’s been a month since last I worked on the project.

But yes, using TS and writing tests is the way to go.

Thanks a lot for sharing!

Collapse
 
anduser96 profile image
Andrei Gatej

Thank you for sharing your thoughts!

Now I reached a point in my app where I feel types are really needed.

Especially when I've been away from the project for a few days, I find it quite difficult to remember the flow of my app.
If there was an interface that I could inspect in order to remind myself what a part of code does... I think it would a lot more easier.

Tests would help here as well. :D

Collapse
 
jacoahmad profile image
Jaco Ahmad

Hi Andrei, thanks for posting this, I currently in the same situation and decided to move the current repository to the new one (with new configs), and leave the old code as legacy code

Collapse
 
anduser96 profile image
Andrei Gatej

I didn’t think about this alternative. I have already started diving into testing though.
Thanks anyway!

Collapse
 
anduser96 profile image
Andrei Gatej

Definitely something that's worth being considered.
Thank you for sharing wisdom!

 
anduser96 profile image
Andrei Gatej

Thanks! You too!