Ever since I gained experience at a software house I have found it difficult to tolerate code not neatly structured, without any form of architecture or beauty or clarity whether or not it works i.e. does what its supposed to do.
I find I am always caught up in refactoring code like this sometimes "at my own expense" is where my comfort v productivity dilemma is.
What is the quickest way to deal with badly written code you inherit in your experience? Write yours or encapsulate / refactor what's there, time consumingly?
Sometimes the best answer is to leave it alone except at the borders, where it interfaces with higher-quality code. Once the interface is clean, you can always clean up the ugliness inside, or just replace it. But without those interface walls in place, any attempt at fixing part of the mess becomes a lot like pulling at a loose thread on a sweater and watching it all unravel.
I heard a podcast on Hanselminutes on this topic just the other day talking with Michael Feathers, who has a book on this: Working Effectively with Legacy Code. I encourage a read. I myself have dealt with a lot of bad legacy code in the past, and generally think that you should refactor/rewrite the bad parts gradually, being careful that you don't cause too much breakage, and be strategic about what to refactor/rewrite. I also encourage the this read from Joel: Things you Should Never Do, Part I
If it works, leave it alone. We're not paid to make code look beautiful, we're paid to make it functional. I'm not advocating writing ugly code, by all means make your own code as beautiful as you want (although beauty is subjective, of course). Just be aware that there is no business benefit in changing code that works, no matter how butt-ugly you think it is.
Show me a customer that cares about the beauty of the code in preference to its functionality and I'll show you a customer that will be out of business in five years.
If there's a bug in that code, you can consider refactoring but, even then, that should be secondary to fixing the bug.
Depending on how much time you have and on how crappy the code really is, the scenario ranges from throw it all away and rewrite everything from scratch to leave it as it is and take the pain from the uglyness.
Rewriting everything is most likely not an option, if the code actually works, even though it might be very ugly. However, to expand that code with new features might be impossible without refactoring. Take your time and do small changes. Compile and test often!
If the code is both buggy and ugly and you need to introduce new features as well. Then it might be really worth it to consider rewriting the whole thing. There can only be so much spaghetti in the code, until it becomes unmanagable.
Go with your guts. If it feels bad and messy when you're coding, because the code is ugly and nasty. Just start changing it piece by piece, soon the code "will be yours", you will know what the code actally does, you're making it your territory :)
Cheers !
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With