Simon Willison’s Weblog

Subscribe

Casting out getters and setters

3rd December 2004

Python Is Not Java by Phillip J. Eby (via Ned) is the most useful article on programming I’ve read in ages. If you have any interest at all in either language, go and read it. It’s all good, but the part that really struck a nerve for me was this:

Getters and setters are evil. Evil, evil, I say! Python objects are not Java beans. Do not write getters and setters. This is what the ’property’ built-in is for. And do not take that to mean that you should write getters and setters, and then wrap them in ’property’. That means that until you prove that you need anything more than a simple attribute access, don’t write getters and setters. They are a waste of CPU time, but more important, they are a waste of programmer time. Not just for the people writing the code and tests, but for the people who have to read and understand them as well.

Writing getters and setters in Java has always bugged me, but I’ve never quite been able to articulate the reason until now. Python’s property syntax (and Ruby’s similar attr_reader and attr_writer methods) are so much more elegant that writing those things by hand, or even auto-generating them with Eclipse, leaves a nasty taste in my mouth.

This is Casting out getters and setters by Simon Willison, posted on 3rd December 2004.

Next: Google Print

Previous: Blogmarks on del.icio.us

Previously hosted at http://simon.incutio.com/archive/2004/12/03/getters