From Uh-Oh to OO

So I’ve been toiling away at PONGS. I made 26 variations on PONG and then thought I was finished with it and would polish and release by next week. But then I was talking to Rilla and we thought of a couple more variants that simply had to be part of the collection. And once there were two more there really needed to be ten more. You know, for the children. But this led to a bit of an issue, because the way I coded the first 26 was a massive pain in the ass…

You see, I watched this pretty cool talk by Jonathan Blow where he talks about how you really shouldn’t bother with making your code “nice” as you go. Instead of thinking up clever data structures and efficient algorithms you should just do what works there and then so you can keep surging forwards. He makes a pretty nice case for the whole thing and I certainly found myself more or less convinced by the time all was said and done.

The problem is I think I got a little too into the message of “surge ahead”, so I adopting a particularly crappy way of writing my PONG variants. In particular, I wrote a “standard” version of PONG, and then I cut and paste all the code into a new file and then hacked it into each variant. That is, each file bears no relationship whatsoever to any other. Want to change the fonts used? Change all 26 files. Want to change how fast the ball moves? Change all 26 files. And so on.

And of course programming has, for a rather long time, had a specific solution to exactly this ridiculous situation. It’s called inheritance, and it’s pretty goddamn fundamental to object oriented programming. I didn’t use it because I was momentarily blinded my what I can only assume is my misinterpretation of Blow’s talk. Of course you should make 26 variants on PONG by using inheritance from a standard PONG. Good god man!

And so today I went back through it all and did just that, streamlining all the code so it inherits from the standard PONG code and just makes the changes necessary. Files that were a few hundred lines long shrunk to 10% or less of their size. The code actually made sense to read. I could now change fonts whenever I damned well pleased. It was pretty magical. I’m actually glad I didn’t used inheritance in the beginning just so I could see explicitly how wonderful it is to use inheritance by changing over. Not to mention how nice it has been starting work on the “final” 10 variants this evening and adding them to the inheritance structure. My my.

In short, it’s totally cool to forge ahead and code “what works”, but that probably shouldn’t include absolutely fundamental programming concepts. Just a heads up.

5 April 2012
← next words previous words →