Now listen here you little shit
ITT: AI induced dunning-kruger. Everybody can write maintenable code, just somehow it happens that nobody does.
Most of the unmaintainable code I’ve seen is because businesses don’t appreciate the need to occasionally refactor/rewrite or do anything to maintain code. They only appreciate piling more on. They’d do away with bug fixing too if they could.

Many opensource projects are in same state, I know for sure my projects become spaghetti if I work more than a year on them.

Besides, I’d argue that if you need to rewrite (part of) it is because it wasn’t maintainable in the first place.

I disagree. Rewriting is a core component of maintaining a code base. It’s the evolution of code. Not rewriting and hooking in some janky way is much worse. No one can see all the possible needs of code the first time they write it. Or even the tenth. Updating the code by rewriting sections is the healthier way to use everything you learned since the first time you wrote it to keep it clean and improve it.

Well, if rewriting is maintaining, everybody can write maintenable code.

Did it become a mess? Rewrite time!

For me the art is writing it so you don’t need to rewrite and you don’t need a janky temporary permanent workaround if requirements change. Clean interfaces, SOLID, plug-ins, etc. Can’t do it myself, but the legendary 10x devs usually do.

All code is maintainable with enough time and money.

But yes, well structured code where those rewrites are minimal is the goal.

There probably is a threshold where the amount you have to rewrite becomes too high. But with each rewrite, hopefully the next time you have a section you need to redo its smaller that before. Eventually going from rewriting a couple thousands of lines to just a hundred or so on the 5th iteration.

I don’t believe in code that never needs a rewrite, but scalable code should be compartmentalized and future-proofed to the point that the next rewrite can be pushed as far into the future as possible. Me personally, I tend to discover what these best practices are during those rewrites.

The most maintainable code is built to be replaced with minimal impact.

How much of the program will must be replaced if you remove one module? If you need to replace the entire program, then your program is not maintainable. Too much is heavily dependent on this module.