Out of the corner of my eye

Exploring HumanStateSpace

Out of the corner of my eye header image 2

Lazy programmers - why is ‘protected’ the default?

December 1st, 2007 · No Comments

Lots of programmers seem to use protected as the default access level, and I think it’s lazy.

I can kind of see the reasoning: you don’t want to expose your variables and private methods because the user of your class probably won’t know what they should be changing and what should be left alone, but you assume if someone is deriving from the class, then they can be trusted to work out how everything works, and it’s safe to give them free reign over everything. Besides, they can just change the base’s members to protected themselves, so why bother trying to stop them?

Personally I think that’s exactly what should happen. Unless you specifically intend something to be used by derived classes, members should be marked as private, and the programmer deriving the class should be responsible for opening up the parts of the base that they require access to. That way, the strongest level of encapsulation is maintained, and hopefully the programmer will be more motivated to learn about how the class works.

Conversely, this may put off people from deriving classes when it makes sense to do so. How high a fence should you really build around communal stuff?

Tags: C++ dev

0 responses so far ↓

  • There are no comments yet...Kick things off by filling out the form below.

Leave a Comment