Gramps coding guidelines

OOP allows for encapsulation of implementation details such as hiding the name of variables used to store an object state. This is associated with accessors like get_xxx() and set_xxx(value) methods.

When such accessors are present, all accesses, except in the owning class, should proceed through the accessors. Python offers a way to enforce this usage by prefixing the xxx-associated variable with two underscores __ (or one depending on context).

However this convention is not consistently observed in Gramps.

Taking the example of PrivacyBase class, it store the privacy state into a boolean variable privacy and defines get_privacy(self) and set_privacy(self,value) accessors. But these accessors are not used at all in any gramps/gen/lib/ module (which could be tolerated because all classes here derive in some way of PrivacyBase) nor in gramps/gui/.

There are get_privacy(self) and set_privacy(self,value) access methods defined gramps/plugins/importer/importprogen.py and used in various locations in gramps/plugins/importer/ modules. But when referring to the privacy flag of primary objects, private is referenced directly.

I kind of remember that in the GUI part, accessors are also defined but code use “indifferently” accessors and variable name.

What is the recommended practice?

The guidelines say nothing about it.

I can’t speak for the core developers but I should think the recommended practice would be to follow the best practice of using accessors. I’m guilty myself of using direct references in some things I’ve worked on and need to revisit them.

It might be overly ambitious but some of your observations suggest that maybe a refactoring of the core classes should be undertaken. If such a thing was done it might also be an opportunity to review and perhaps enhance the data model.

I hope you continue to share your thoughts as you review things.

One thing I’m missing here because I have a very low Python programming experience is the performance difference between accessors and direct access.

More generally, having a quality approach is a requirement in a 625k lines app (with comments, not counting the 1.8M lines in po/). Since I LXR’ed Gramps, I can very quickly search and locate such accesses and see the inconsistencies. But performance concern should also be kept in mind.

I guess based on discussion here and other places not using getters/setters is the more Pythonic approach.

4 Likes

Excellent reference, thank you.