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

Nyall Dawson nyall.dawson at gmail.com
Thu Dec 15 18:55:08 PST 2016


On 10 December 2016 at 00:13, Matthias Kuhn <matthias at opengis.ch> wrote:
> 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?

Yes please! I've often wondered about this protected-by-default
behaviour and what the rationale behind it is.

Another reason why it should be avoided is that protected member
variables cannot be exposed to the python bindings. So if it's
important that they can be accessed/modified by subclasses, just
making them a protected member isn't sufficient. Instead they require
protected getter/setter functions.

Nyall

>
> Thanks
> Matthias
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> List info: http://lists.osgeo.org/mailman/listinfo/qgis-developer
> Unsubscribe: http://lists.osgeo.org/mailman/listinfo/qgis-developer


More information about the Qgis-developer mailing list