Refactoring old code vs Rewriting everything for long-term?

twitter logo github logo Updated on ・1 min read

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 working on a feature for a vue app and I realized that in order to implement it, I need to refactor a big and important part of the code.

I'm prone to a lot of frustrating debugging hours If I choose to refactor this way.

So I came with the following idea: rewrite everything with TypeScript and also write useful tests. Even though it would take more than just refactoring, I strongly believe that this idea will help me long-term.

What would you advise me to do?

Thank you for your time.

Thank you a lot for your awesome piece of advice!

twitter logo DISCUSS (17)
markdown guide
 

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.

 

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

 

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.

 

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.

 

Thanks for the reply!

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

Your advice really helped me.

Thanks!

 

Selling the idea of changing a technology only because it will be more maintainable is sometimes really hard depending on your client. To obtain your seal of appeoval I strongly suggest you find flaws in the current code that would not be present with TypeScript.

Let'a say you use AngularJS 1.6

It is known to be vulnerable to two major XSS attacks, which is a good point to talk about to your client.

Otherwise, I'm in the rewrite camp, but I might be biased, I just really love Typescript

 

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

 

The flow is mainly why I use Typescript. It's just so easier to remember.

Good luck in your further projects 😁

 

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...

 

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!

 

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

 

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!

 

As with most things in the tech business, time is money and/or risk and a full rewrite means a lot of both, possibly more than your organization may tolerate. Rather than an either or, I would re-approach your binary proposal, coming up w/ multiple options or a phased approach with time AND honest risk assessments. That will give your management a way to plan for it over time rather than having to swallow it whole - the either or is a bit like blackmail, which will not win you any friends whether you are right or not.

 

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

 

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

 

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

Classic DEV Post from Jan 13

The Best Books for New Devs + Bonus Twitter Thread

Andrei Gatej profile image
An 18-year-old aspiring web developer.

Sore eyes?

dev.to now has dark mode.

Go to the "misc" section of your settings and select night theme ❤️