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.

Friday, 24 March 2006

Emacs-fu

Emacs just rules. Lately, I've been trying to figure out how abbrev mode can be used nicely with skeletons. The following is a skeleton to produce a Java "main" method and is due toJorgen Shaefer:

(add-hook 'java-mode-hook 'fc-java-setup)


(defun fc-java-setup ()
(c-set-style "k&r")
(setq c-basic-offset 4)
(c-set-offset 'case-label '+)
(define-abbrev java-mode-abbrev-table "main" "" 'java-main-skeleton)
(define-skeleton java-main-skeleton
"Insert a Java main function."
> "public static void main (String[] args) {\n"
_ "\n"
"}" >)
)

technorati tags: , , , ,

Tuesday, 28 February 2006

Tail Call Optimization as a Python Decorator

Until today the coolest uses I've seen for Python decorators were implementing design by contract and currying. However, Crutcher Dunnavant has implemented a decorator which performs tail call optimization by messing with the runtime stack. Even cooler, it's only 14LOC. Go check it out!

Monday, 30 January 2006

Colour transforms

I found this *really* useful comment in the Python source for the Python Image Library. Guess I should have remembered this from Uni, but never mind!

When translating a colour image to black and white (mode "L"), the library uses the ITU-R 601-2 luma transform:

L = R * 299/1000 + G * 587/1000 + B * 114/1000

technorati tags: , , , , , , ,

Wednesday, 25 January 2006

Pong for the Symbian60


I've spent an evening and a bit writng Pong in Python for my Nokia 7610. Seems weird to have a hobby after all the time we've spent working for the ARC launch, but Pong was fun and it was nice to see how incredibly simple the PySymbian stuff really is. Yay.

technorati tags: , , , , , , , ,

Monday, 23 January 2006

Camera modes for Symbian60 camera phones

I've been messing about with the Python SDKs for Symbian60 phones. If you want to write anything using the camera, it helps to know what modes are available on your phone. So, here's a short Python script to do that and print the relevant information to the interpreter window:




"""
Prints all available information about available cameras:

* Number of cameras
* Maximum zoom
* Available image modes
* Available white balance modes
* Available image sizes
* Available flash modes
* Available exposure modes
"""

from camera import *

__author__ = 'Sarah Mount '

print 'This phone has', cameras_available(), 'camera(s).'
print 'Max zoom is', max_zoom(), '.'

for mode in image_modes():
print 'img_mode:', mode
for mode in white_balance_modes():
print 'wb_mode:', mode
for size in image_sizes():
print 'img_size:', size
for mode in flash_modes():
print 'flash_mode:', mode
for mode in exposure_modes():
print 'exp_mode:', mode