Getter and setter policy

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
(New page: What rules do we think should guide our use of getters & setters? Should fields always be private? Should an object call its own getters?)
 
(Well there is a starting policy, now we get to decide if it is right.)
Line 1: Line 1:
 +
Objects should store all their data in private variables. Getter and setter methods should be produced for any variable you might have been tempted to declare public. Classes should ideally call their own getter and setters when accessing their internal data. This prevents the object getting into a invalid state.
  
What rules do we think should guide our use of getters & setters?  Should fields always be private?  Should an object call its own getters?
+
Some complications may be encountered when classes contain collections that require non-transactional access.

Revision as of 06:24, 27 July 2009

Objects should store all their data in private variables. Getter and setter methods should be produced for any variable you might have been tempted to declare public. Classes should ideally call their own getter and setters when accessing their internal data. This prevents the object getting into a invalid state.

Some complications may be encountered when classes contain collections that require non-transactional access.

Personal tools