Bad code is good for you

ctodx
08 October 2009

OK, admit it. You've got an RSS subscription to The Daily WTF. Part of The Daily WTF's self-applied remit is the CodeSOD section (Code Snippet of the Day) whose premise is simple: developers enjoy a jolly good laugh at seeing someone else's badly written code. We read it with our fingers crossed, fervently muttering under our breath that we hope that one of our colleagues didn't just send up something we wrote last month. Even though some of the posts there ring false to me (no one can be that dumb, surely?), there's just enough posts that ring true that you continue to read and laugh.

Of course we have all written bad code. The only person I'm sure hasn't is Donald Knuth, and that's only because he used to send out checks for $2.56 to everyone who discovered a bug. (I will note that the bug discoverers just used to frame said checks and hang them on the wall rather than cashing them. I certainly would have. Notice as well that I've never dared emulate him.) Bear with me when I say bad code is nonetheless good for you; I'll explain. 

Apart from being clichéd, the saying "you learn from your mistakes" holds a metric tonne of truth. Think back to when you wrote an appalling bit of code. Yes, I'll agree the deadline was tight, or you were meeting that rather pretty young lady (or gentleman) from Accounting for drinks that evening, or your kids needed taking to practice, but, no matter what the excuse, you laid down some rather nasty code. Remember what happened when the inevitable bug report was posted? You investigated and saw with some horror what you had managed to write in that moment of distraction. If you're like me, you'd have broken out into a sweat and uttered WTF, uncompressed. Perhaps you were able to cover it up in layers of updates to the version control system, but almost certainly there would remain a record in the bug tracking system and then you would have had to walk past people in the hallway who would fall silent from their conversations and watch as you shuffled by.

But I bet you never wrote code like that again.

Here's another aphorism you'll recognize: you learn by doing. You can read all the books and blogs about TDD (test-driven development) and understand that it can help you write better software, but you won't viscerally "get it" until you actually practice what it says. Until that point, it's merely an interesting thought experiment. But I wonder how many mistakes you'll endure before you speculate about whether a test suite that you maintain and run regularly could actually save your bacon. And once you have a unit test suite, you'll be adding tests to it all the time. Your code will get better because it's being tested constantly. You'll be able to refactor whenever you want with the knowledge that your tests will cover you. You'll avoid the mistakes you've made previously, possibly making other, different ones that you'll be able to fix more quickly. 

You see, we do have that need for bad code. It is only through bad code that we recognize that we need and should use better tools, algorithms, and methodologies. Bad software is everywhere; it permeates the applications we use every day, it irritates the heck out of us. But it forces us, as developers, to exert ourselves to write better code, better than that — time for another initialism, I think — POS software over there.

Most of all, though, it takes bad code to help us appreciate good code and gives us a good laugh into the bargain.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.