Instant Gratification: The Joys of Coding

I got up at 6 a.m. on Sunday morning to do some coding. I know some might think it sad, but it was a delightful hour.

I’m learning to program in Python through the Coursera course taught by a team of professors from Rice University. I know some Python, but I’m no programmer. The tasks are fairly easy but I find myself learning a lot: not just about the Python language, but also about online courses, how I learn, and the benefits of learning to program.

my stopwatch game

This morning I was finishing up a mini-project that was due at 8 a.m. It’s not a complicated program: a timer that you can turn on and off, with a game element of scoring points every time you stop it exactly on the second. Nothing Earth-shaking – I won’t be selling it as an app anytime soon! – but the process was very rewarding and illuminating.

I’d been working on the program for a couple of hours the other day, and had some frustrations. It kept blowing up on me, no matter what I did. I’d code something, run it and then the thing would wind up with a huge series of the same error message and lock up the system. I knew there was something going on with the timer in the program, but I couldn’t figure out what. I had started saving locally every version of the program, and having to open new windows (it’s a browser-based environment) and reload the code to work on it.

I then got more methodical in what I was doing and had more success without it blowing up. Eventually I got it about 75% working and shut down for the evening. This morning I got up and loaded up what I had saved, and the realization of what I had been doing wrong came to me clear as a bell: I’d been closing a window the program was drawing without resetting the program. That lock-up and series of error messages was the program shouting at me that I was doing things backwards. It’s amazing how a simple insight can come after walking away from a problem and clearing your mind. You come back fresh and look at things anew.

Now treating the interface properly, I was able to complete the other elements of the program, test them, and get everything running in proper order. The program runs, keeps score and works properly. (As long as you don’t close the pop-up window!) I submitted my assignment, feeling quite pleased.

Over my first cup of coffee of the morning, I reflected on it. I really do feel quite proud and happy about this simple little program. It’s quite amazing. The program itself is nothing to write about (you’re welcome to try it out) but I’m pleased as punch with it. Why?

I guess it’s that I faced difficulties and had to think my way through them. Something wouldn’t work right, and I’d have to figure it out. Apart from the interface blow-ups, there was the aspect of the timer counter that had to be tweaked (how do you get the minutes digit out of a counter that is in milliseconds?), figuring out how to ensure that the score wouldn’t be affected by mis-clicks, tweaking the visual interface, etc. Each time I encountered one of those I had to think it through: understand what was happening and what I had to do to change it to make it work correctly. I would change something, test it, change it again, test again – repeat until it was right.

And that process of code – test – tweak gives the same kind of feedback as you get from an addictive game: instant. You know when it’s right and you know when you’ve done something wrong. You’re rewarded with success for doing things right, and you get a failure when you do something wrong. No penalties – no bad grade – just instant feedback that it wasn’t right and you won’t get the reward until you do it correctly. Sure, it’s a bit of a Sysiphean task but so are all games – play Angry Birds for a while, why don’t you?

So I stuck at it until it worked right. And it does. I didn’t get a grade or even a score, but I got it right. And I did it myself. Can you tell I’m beaming as I write this?

Boy, this coffee tastes good this morning! OK, what’s next?

Follow

Get every new post delivered to your Inbox

Join other followers: