Tuesday, 18 April 2006

Student Java gotchas

We've been doing a lot of marking lately, it being the holidays, which is always a revealing experience. The results have been pleasing, but having to go through Java code caused me to remember how many small idioms students wrongly replicate. OOP so often ends up being all about style and detail and much of that can elude a beginner, because there's so much to learn about programming when you start.

My two favourite annoyances are unnecessary setters and getters and the following use of boolean expressions:

while / if(x == true) { ... }

The latter just says that the student doesn't really understand how expressions are evaluated, which is pretty fundamental, although I've seen the same "mistake" in production code too.

The first thing about setters and getters is slightly more subtle. A field should be declared private if either it is or should never be used by another object or if it has some class or object invariant associated with it. A field with no associated invariants can be declared public. In fact, this is probably preferable, as declaring a field to be public tells anyone using your API that that particular field definately has no invariants associated with it that they need to think about.

Sunday, 9 April 2006

Pygame

This year, with our intro programming class, James and I used Pygame as an example API in our weeks on using-external-libraries. There was some debate amongst the class as to whether Pygame is such a good choice (as oppose to, say, using a full-blown games engine from a popular game). I think our approach is very defensible: Pygame is a fast API to write with (in terms of development time) it's powerful enough to enable very creative games to be written, and so on. However, Jay Barnson, of Rampant Games has written about his week-long game hack which -- not surprisingly -- used Pygame. He puts the case very eloquently:


Lesson 4: Python rules!

I can't believe how quickly many features came together using Python as opposed to, say, C++ or even Java. Things like typeless variables, dictionaries, and extremely easy-to-declare lists (allowing a mixing and matching of object types) made it very easy to implement content lists, attribute handling, spell effects, and so forth. I was already a fan of the language, but now the prospect of using Python, tied into a high-level 3D engine, has become extremely appealing to me.