Refactoring/Rewrites is the MOST rewarding task that can be achieved with both legacy and new code. I do it from the start of my career (when I don't even know the name of it).<p>And that include, regulary, rewriting to different langs (like, for example FoxPro -> C#, C# -> Python -> F# -> Swift (aborted) -> Rust (noob) -> Rust (now I know rust, almost!) <-- yes, the same project, years on it.<p>Exits a lot of FUD against this. Is risky? Is hard? Can be messed up? Everything is staked against you? Yep LIKE ANY OTHER CODE ACTIVITY.<p>----<p>Things that help in make this a success (assuming, of course, an average, half-decent developer. But I have done it when I not even know anything, so who knows if it work?):<p>CRITICAL:<p>- Have source control<p>- Have a task manager (mine: pivotal)<p>- Split stuff in small tasks (couple hours max)<p>- Priorize to know the data(schemas), the business REAL priorities (because all the time, everything will be declared of imperative importance). The code? Less so. If the code is good then you are not considering this at all.<p>- This can be done in the small or in the large. So, a full rewrite can be totally done in hours. If all is so tangled this is not true, then the code is beyond salvation, REWRITE.<p>Nice:<p>- If possible, deployable code in one go (of the old code)<p>- The database (or data in general) is kind of easy to grasp (if not, all is a mess, REWRITE). With a good database, rewrite is far more profitable than refactoring messy logic.<p>- Have good test that can be used to validate it? Great. I rarely have that luxury.<p>To become good:<p>- Rewrite/Refactor is a <i>muscle</i>. The more you do it, the better you become.<p>- Do <i>A LOT</i> of MICRO side-projects or experiments. Trash them, rewrite them, explore them. Make your mind used to it (note: this is not "ruin my life, only code yo!", is to start a major idea with a micro side project before commit bigly)<p>Everyone know the second time your code is better. Imagine if you do 5 rewrite in successions. That code will be celestial!<p>- Be confident in being able to read code. Read code. And read more. Much more if is a new lang.<p>- Ask question, and ask the same question, differently.<p>- Pick a project that you can do in your sleep (mine: Pseudo micro ORMS) and start with that when new lang or framework. In a side project. Must be of small size (few files, at most)<p>- If change lang, framework, be aware that some are BETTER than others. Learn to know why and when start using it.<p>- Dedicate a few minutes learning useful stuff.<p>- Do the HARDEST stuff first, at least, a significant piece of it. If the hard is done, the rest is easier!<p>- Have a solid selection of companion tools that you RARELY need to change (mine: Sqlite, Postgres, nginx, ubuntu (deploy))<p>The MORE messed up, the BIGGER is the payoff:<p>- Not have tests or are all useless? Start writing test and all is tangled? Rewrite<p>- Like a stupid idiot you are 4 months into a refactoring (doing stuff properly and all) and all paths lead to doom? Rewrite (you probably feel it 3 months ago. Idiot me)<p>- You testing (manually, auto, whatever) take X times than write code? Rewrite<p>- The customer is on fire, nothing work, all things are delayed? Rewrite<p>- Everyone in the path of requirements are even more lost than <i>you</i>? Rewrite<p>---<p>Surely exist some stuff that is conveniently ignored in this list: Have a customer/team that accept the rewrite.<p>Look, if the customers/boss/team is THAT bad, then NOT MATTER what you do, you ARE SCREWD.<p>But I'm lucky, and have worked in situations with serious trouble (but decent humans around, even if can't help much and I must figure a lot on my own) have tell me that people are more accepting of rewrites than some could think. Even delayed projects can be tolerated if exist a good flow of improvements.<p>P.D: This is my experience in my sector, and the few I have done in a mid-size startup...