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.
More recent articles
- Visualizing local election results with Datasette, Observable and MapLibre GL - 9th November 2024
- Project: VERDAD - tracking misinformation in radio broadcasts using Gemini 1.5 - 7th November 2024
- Claude 3.5 Haiku - 4th November 2024