An old friend from TAMS sent me this url today. It links to a particularly … horrid example of programming practice. Our conversation follows.
(15:40:34) Matt: must… not… look…
(15:40:52) Ammon: huh?
(15:41:00) Matt: (look ;-))
(15:41:29) Ammon: …
(15:41:34) Ammon: _wow_
(15:42:00) Matt: hooray for exceptions
(15:42:05) Ammon: that’s just obscene
(15:43:13) Ammon: … it
(15:43:17) Ammon: it hurts my soul
(15:43:27) Matt: deep down, where the pie goes?
(15:43:28) Matt: yeah
(15:43:32) Ammon: yeah
The code in question (for those who don’t want to follow the link)?
int idx = 0;
catch (IndexOutOfBoundException ex)
I don’t know if I really want to explain what’s wrong with this for those non-programmers who might be reading… No. I don’t. But I will anyway.
Since time immemorial, the most common way to iterate over an list of values has been to use an index variable that is incremented until it reaches the size of the list. And then you stop. And you don’t continue to look at things. If you try to look beyond the end of the list, strange and unpredictable things can happen – exactly what happens depending on the system involved.
This program … does an infinite loop, increments its counter, and then crashes when it runs past the end of the list of product id’s to be displayed. But, it plans on crashing. So it knows to just ignore the disturbance in the Force that is generated and continue running despite the millions of souls crying in agony before being silenced by the idiot programmer who is being paid to murder them in their sleep.
This morning, a job posting went across the MUD-Dev meta list. Well, two postings, really, but the one that interested me was an entry-level game programmer position with Three Rings – the Puzzle Pirates people. They are an open-source house that develops in Java under Debian. They use Ant and have a pretty cool open networking library (matchmaking, etc…) called Game Gardens.
Part of the job posting mentioned that short-listed applicants would be asked to develop a small game in the library. So, I’ve been poking around with it today and think I have a pretty decent grasp on how they’re doing things.
In preparation for playing with the library further, I am planning on writing a networked version of the old snake game. I figure since it was the first graphical program I ever wrote (Turbo Pascal, back in the summer of ’93 or so), it’s a good candidate.
My first playable (single-player) draft of the basic game took about 2 hours to write up since I kept getting distracted by Penny 😉 I’ve got a jar file available for download here. It is pretty ugly at present and doesn’t keep score (other than growing your snake). Every time you crash, it will ask if you want to play again – until you click no, at which point the program will just sit there until you close it.
It doesn’t use Game Gardens yet, that’ll be the project for tomorrow. I have plans for allowing for various types of power-ups and a population of up to 4 snakes on the field (one starting from each cardinal direction on the map). We will require the players to collect assorted eggs until a key appears. Grabbing the key will open the exit, go through exit to next map. Maps will have various silly little patterns of walls on them. If I get to keep playing with the thing, we will probably add different sorts of walls – moving obstacles, etc… I also have ideas for actual sprites to use, but that’s not a major priority – the game runs just fine in blocks of colour.