TICS Coding Standard Viewer 
TIOBE Software Quality Framework
Print-friendly version
©TIOBE Software www.tiobe.com
C++ Coding Standard

Rule:  TT#012

Synopsis:Public Data Members
Severity Level:2
Category:Class Interface


The encapsulation principles dictate that data members should not be public. If you need to provide full access to your data members the use of inline getters/setters is preferred. Rationale: getters/setters decouple the implementation from the public interface. Besides they make it easier to subclass and extend the behavior through polymorphism (e.g. for mocking), and to add debug statements or asserts when certain data members are accessed.

In some simple cases it is allowed to have public data members, but only when the following rules are met:

  • These members have no dependencies between them.
  • The state of the data member is always valid.
  • There are no side effects related to the access to the members.
  • The type is used in a fairly local area of the code (e.g. within a single component). Rationale: if we need to add non-boilerplate getters and setters afterwards, it will be a manageable task.

Literature References:
TomTom Guidelines