Wait, nothing’s wrong?
I recently read a tweet that really set me to thinking:
Nothing is quite so shocking as writing code which runs fine the first time.
— cliffski (@cliffski) January 8, 2013
I’ve had this same thought many, many times throughout my career, but I can’t help but notice that it’s been in entirely different contexts.
Phase One: Sweet! No Syntax Errors!
When first learning how to code, or later on when learning new languages, it can be pretty shocking when you hit the compile button and your console doesn’t light up like a christmas tree. You may not have typed a lot of code, but you didn’t have to Google any function signatures or anything, and it compiles and runs. Mostly. I mean, it doesn’t do what you want to do at all, but it still compiled, right?
Modern IDEs mostly mitigate this stage, due to real-time syntax checking. I don’t think I’ve had more than a syntax error per week since switching to a competent IDE (Sublime Text 2, if you’re wondering).
Phase Two: My function works!
Eventually, you get comfortable enough with a language to be able to write a solid chunk of code – perhaps a whole, medium-sized function – run it, and have it run as you expect. This is always a delight when it happens, as you’ve demonstrated the ability to hold the current problem space in your mind long enough to solve it and commit it to code. I imagine this is roughly how a juggler feels once they get the basic three-ball rotation down.
Phase Three: I AM A GOD.
It’s been maybe a half-hour, perhaps even as long as an hour since last you hit the compile button. You’ve been in multiple files, up and down the class structure, at all levels of project complexity. You’ve changed dozens, close to hundreds of lines of code, maybe even rewritten a whole class or low-level algorithm. You’re just… in the zone. Eventually you realize how long it’s been since you’ve tried out your changes, and ever-so hesitantly run them. Then, before your very eyes, everything works as intended.
The only possible answer? You are a tiny god, completely in control of your environs.
Phase Four: Oh no, where’s the bug?
Exactly the same as above, except you are old enough and wise enough to know that the fact that it didn’t break on run means that the bug is subtle, hidden, and dangerous.
Time to write more tests.