Live data from Hacker News

Refactor vs. Rewrite

remesh.blog

21–30 of 123 posts

Re: Refactor vs. Rewrite

#21
post #17

Earlier quoted context omitted.

No good plan survives first contact with the enemy... Less pithily, the good architecture will almost certainly end up with arbitrary patches and madness after 2 years of active use, if anyone cares about it at all... And then somebody will join and say 'This is a mess, we should refactor and/or burn it to the ground.'

I don't doubt that this could accurately reflect your experience in the industry, but it is not a universal truth. I've worked on sustaining engineering of systems that were years old, the stewardship of which involved refactoring only, and which are still in production today. There are lots of systems that are more or less right in version 1.

> There are lots of systems that are more or less right in version 1.

While true, in my experience this usually happened when at least one - if not all three - of these conditions were true:

1. The problem domain is relatively static, unmoving, and self contained

2. The systems in question is relatively small

3. The developers in question have written substantially similar systems before, or ground locally, or used version 0.x version numbers first (in any of these cases, is it really "version 1" per se?)

...game development is admittedly weak in all 3 categories, and high on time pressure. Oh sure, there's some solid code that will probably last decades in many codebases, but even the best programmers will occasionally make a system that - while good in isolation - needs reworking when combined with other systems - to say nothing of the content and rendering pipelines that vary wildly by decade.

Re: Refactor vs. Rewrite

#22
post #12

IME whether you rewrite or refactor, the lesson is the same: You have to grind your way into the good architecture. It doesn't become good because the code is fresh, but because you have battle scars to show. And a success story in that case comes from having a complete learning loop. In a lot of orgs the learning itself is argued against for one reason or another - development proceeds with as little feedback on qua…

I feel like you have denied the existence of people who are capable of writing down a good architecture on the first try, without all the grinding.

I have yet to know any such people :-)

Re: Refactor vs. Rewrite

#23
post #12

Earlier quoted context omitted.

I feel like you have denied the existence of people who are capable of writing down a good architecture on the first try, without all the grinding.

I have yet to know any such people :-)

Agreed. Every new design is informed by past mistakes. Programming is not an a priori field.

Re: Refactor vs. Rewrite

#24
post #17

Earlier quoted context omitted.

No good plan survives first contact with the enemy... Less pithily, the good architecture will almost certainly end up with arbitrary patches and madness after 2 years of active use, if anyone cares about it at all... And then somebody will join and say 'This is a mess, we should refactor and/or burn it to the ground.'

I don't doubt that this could accurately reflect your experience in the industry, but it is not a universal truth. I've worked on sustaining engineering of systems that were years old, the stewardship of which involved refactoring only, and which are still in production today. There are lots of systems that are more or less right in version 1.

> the stewardship of which involved refactoring only

I've known several such systems, and the reason they stuck with the original architecture is they wrote it in C, and it's very hard to iterate architecture in C programs. The programmers will just keep bashing the C code so it works well enough.

Yes, I know this is a provocative statement.

The reason behind it is that C's ability to encapsulate behavior is just not very good. Even simple things, like "is it an array or a linked list" leak out all over the place.

Even simpler, "is it a value or a reference type" means one has to swap "." and "->" everywhere. C++ half-fixed it by introducing reference types, D fixes it all the way by noting that "." can be unambiguously used for both operations.

Re: Refactor vs. Rewrite

#26
Maybe controversial but I think original authors should be able to rewrite, i.e. they can learn from previous mistakes and incorporate those lessons into the rewrite.

Re: Refactor vs. Rewrite

#27

Earlier quoted context omitted.

God bless whoever wrote tests before you. Whether it be a rewrite or working on the current code base, those who have the time and resources and don't write tests are inexcusable and bad stewards

Sometimes you can produce tests (not unit, but integration and such) from the existing system by treating it as an oracle. That's what I did once, it worked really well and in the end of the differences between it and mine (where mine technically failed the tests), we found out the old system had dozens of critical bugs and mine hand a handful (old system gave many false negatives, which was a major no-no, mine gave…

I'm the person that you're replying to responded to.

This is exactly what I went through during that four month period. The existing tests weren't very useful and I was completely changing the structure of how the program was architected, but the input v output could be measured. I, like you, also discovered critical bugs in the existing implementation because I needed to cover the entire combinatorial set or possible inputs whilst the original implementers presumably went through them as they came up.

Re: Refactor vs. Rewrite

#28
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).

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

----

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?):

CRITICAL:

- Have source control

- Have a task manager (mine: pivotal)

- Split stuff in small tasks (couple hours max)

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

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

Nice:

- If possible, deployable code in one go (of the old code)

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

- Have good test that can be used to validate it? Great. I rarely have that luxury.

To become good:

- Rewrite/Refactor is a muscle. The more you do it, the better you become.

- Do A LOT 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)

Everyone know the second time your code is better. Imagine if you do 5 rewrite in successions. That code will be celestial!

- Be confident in being able to read code. Read code. And read more. Much more if is a new lang.

- Ask question, and ask the same question, differently.

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

- If change lang, framework, be aware that some are BETTER than others. Learn to know why and when start using it.

- Dedicate a few minutes learning useful stuff.

- Do the HARDEST stuff first, at least, a significant piece of it. If the hard is done, the rest is easier!

- Have a solid selection of companion tools that you RARELY need to change (mine: Sqlite, Postgres, nginx, ubuntu (deploy))

The MORE messed up, the BIGGER is the payoff:

- Not have tests or are all useless? Start writing test and all is tangled? Rewrite

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

- You testing (manually, auto, whatever) take X times than write code? Rewrite

- The customer is on fire, nothing work, all things are delayed? Rewrite

- Everyone in the path of requirements are even more lost than you? Rewrite

---

Surely exist some stuff that is conveniently ignored in this list: Have a customer/team that accept the rewrite.

Look, if the customers/boss/team is THAT bad, then NOT MATTER what you do, you ARE SCREWD.

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.D: This is my experience in my sector, and the few I have done in a mid-size startup...

Re: Refactor vs. Rewrite

#29
post #17

Earlier quoted context omitted.

I don't doubt that this could accurately reflect your experience in the industry, but it is not a universal truth. I've worked on sustaining engineering of systems that were years old, the stewardship of which involved refactoring only, and which are still in production today. There are lots of systems that are more or less right in version 1.

> There are lots of systems that are more or less right in version 1. While true, in my experience this usually happened when at least one - if not all three - of these conditions were true: 1. The problem domain is relatively static, unmoving, and self contained 2. The systems in question is relatively small 3. The developers in question have written substantially similar systems before, or ground locally, or used v…

4. A system that never needs to scale much beyond its original projected load.

Re: Refactor vs. Rewrite

#30

I constantly refactor my code, even the ancient stuff. Here's my most recent one: https://github.com/dlang/dmd/pull/11202 which changes a data structure from a linked list to an array.

I was about to comment something sarcastic, then I googled you and instead I'll just say thanks!
Post reply on HN