Minimize accesses to variables
From CSSEMediaWiki
(Difference between revisions)
RobertLechte (Talk | contribs) (New page: 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. J...) |
|||
Line 3: | Line 3: | ||
Johnson and Foote recommend getters as a way to increase this separation/generality. In their words: | Johnson and Foote recommend getters as a way to increase this separation/generality. In their words: | ||
− | + | <blockquote>''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.''</blockquote> | |
Note that "message" here simply refers to a method call, and consequently an "accessing message" is a getter method. | 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. | Cliffnotes version: ACCESS INSTANCE VARIABLES THROUGH GETTERS, ALWAYS. The only method that should deal directly with a variable is the getter for that variable. |
Revision as of 03:56, 6 October 2008
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.