[Qgis-developer] Use of protected/private in API

Matthias Kuhn matthias at opengis.ch
Fri Dec 9 06:13:38 PST 2016


Hi,

Throughout the codebase, there are two different styles used when
declaring member variables and helper methods. Some classes are exposing
them as private (with just a few protected ones), others use protected
by default.

I assume that protected is coming from a pythonic background where no
private context exists. There is the __prefix in python to tag "private"
variables as such, which advises devs to be careful about a variable but
still gives anyone full access.

The main advantage I see is, that once in a while you run into a
situation where you wish to be able to modify some internals of an
instance because for whatever reason it doesn't behave the way you
expect/want it to behave.

The disadvantages I see are that instead of improving the API, one can
easily just "work around" it. Exploiting undocumented behavior which may
suddenly change in the future. There is also no __prefix in our API, so
without consulting the documentation there is no way to tell if
something should be considered public or not (e.g. the python console
will happily autocomplete it for you). In the python world, there's also
the @property annotation which can trigger side-effects when setting a
variable, something that doesn't exist in C++. I think it also makes the
binaries bigger and compilation slower.

>From my point of view, we should use C++ the way it was designed to be
used (public/protected/private as appropriate).

Is there something I missed?
And do you think we can be consistent with this?

Thanks
Matthias


More information about the Qgis-developer mailing list