I think it's worse than that. I think the core problem is that the world is a messy complicated place and any time you take a simple, straightforward set of rules and require strict obedience to them in the real world, you're going to end up with situations that produce absurd results.
It's like trying to fit a finite-term polynomial to a fractal - there's literally no way to make things match up everywhere. [1]
People complain because law/finance/whatever seem to be too complicated, so they try simpler, more "obviously correct" sets of rules that perform even worse in practice than what we already had because the world is still a fractal, and simpler rules just mean you're trying to use fewer points to fit that infinitely complicated curve. It's easy to look at existing law/finance/whatever from the outside and say "it's too complicated, we should refactor everything from first principles" but there's good reason to believe that is always a mistake [2].
Simple, obvious rules like "Transactions should be immutable once they are completed" seem like a good idea, but then they encounter incredibly common real world situations like "what if somebody made a mistake when they specified the amount of the transaction or the account number in the 'to' field?" Real world finance has ways to handle this - if nothing else, you can take it to court and ask a judge "does this seem reasonable?". "Code-is-law", like Procrustes "perfect bed", only has admonitions to be completely perfect yourself or suffer the consequences.
So IMO it's not just that most code is not well written, it's that the very idea that any finite amount of code, no matter how "perfect", can be a good fit for all the intricate needs of the actual lives of billions of people is fundamentally flawed.
And also, most of the code is not very well written :)
[1] as an alternate analogy, it's like trying to fix Godelian incompleteness in a formal system by adding a finite number of axioms.
[2] https://www.joelonsoftware.com/2000/04/06/things-you-should-...