Code That Works The First Time

Scary, isn’t it? How is it that this new block of code that you just wrote is performing perfectly the very first time you ran it? I’ve done it a few times in my career in non-trivial blocks of code.

Ok, maybe you are just *that good*. Maybe you thought through your problem and improved your odds of solving it from the get-go. Or maybe your code is bypassing all the hard stuff and because it’s not crashing it just looks like it’s working.

What next? How confident are you in it? You wrote tests of course, and they all pass too. Here are some things to do if you’re not ready to move onto the next problem:

1. Have a colleague look at it. Make sure you’re not missing something obvious. Are you making any assumptions that you should not?
2. Step-debug the code anyway. Inspect variables as it runs.
3. Examine your tests. Are they too superficial or too general? Is there overlap between what the tests consider the ‘solution space’ to be and failure?
4. Comment your code more. Describe the intent so the next maintainer will have some insight into what you were trying to accomplish.
5. Consider writing or performing a more exhaustive test. Try your best to break it.
6. Consider load or stress tests. (Load and stress tests are different!)
7. Look for code that could cause exceptions or infinite loops. Build escape hatches for ‘do’ and ‘while’ loops if you need.
8. Remove dead code, dead comments, commented-out code, unused code, or anything else that looks sloppy.

If you’ve done all of these and everything still works, then congratulations! Next problem!

Leave a Reply

Your email address will not be published. Required fields are marked *