Minimize accesses to variables
From CSSEMediaWiki
Revision as of 01:19, 14 August 2008 by RobertLechte (Talk | contribs)
The more we can keep our code separate from the underlying data respresentation, the more "abstract" the class is. "Abstract" in this sense means more general, and hence more reusable.
Johnson and Foote recommend getters as a way to increase this separation/generality. In their words:
"One way this can be done is to access all variables by sending messages. The data representation can be changed by redefining the accessing messages."
Note that "message" here simply refers to a method call, and consequently an "accessing message" is a getter method.
Cliffnotes version: ACCESS INSTANCE VARIABLES THROUGH GETTERS, ALWAYS. The only method that should deal directly with a variable is the getter for that variable.