Property Getters and Setters **

class MyRectangle {

...

private int mHeight;

...

public int getHeight() {

return mHeight;

}

public void setHeight(int newHeight) {

if(newHeight > 0) {

mHeight = newHeight;

mArea = mHeight * mWidth;

} else {

error(“Invalid Height.”);

}
}
...
}

 
 

… object classes consist of PROPERTIES that define its current state; objects are manipulated by other objects through PROCEDURES and FUNCTIONS; access to information in the object depends on the information’s AVAILABILITY (public, private, or protected).

* * *

            Classes are full of properties that other objects want to access at some level.  However, these properties must not only be easily accessible, but must also be limited to ensure that a program is always in a consistent state.

            In programming, setting a variable often requires no more than explicitly assigning a new value to the variable.  However, sometimes certain things must occur when a variable changes value or when a variable is accessed.  For instance, to maintain consistency and accuracy, the area of a rectangle must be updated each time the height or width is changed.  Or a program may wish to keep track of how many times a variable is requested.  More often, concurrency issues require that a function obtain a lock on a variable before changing its value.  Thus, each time the variable is accessed to learn or change its value, surrounding code must be present to provide the appropriate and necessary functionality.

In traditional, non-object oriented programming, programmers learn to decompose their functions to provide short, coherent methods.  When global variables are used to maintain state, and certain things must happen whenever the variable is accessed or set, programmers know to put this common code in a single function and always access that function when dealing with the variable.  Without such functions, code is messy, full of redundancy, and prone to errors.

Object-oriented programming also requires decomposition, and provides a framework and rationale for such decomposition.  Objects, or classes, should have control over their state.  They should be responsible for handling any internal manipulation when a variable is accessed.  If other objects should have access to a variable, this variable is referred to as a property.  Code will compile when a variable representing a property is public and other objects can directly set the value.  However, it is more appropriate to keep the variable private and use a public getter and setter for other objects to access the variable.  Even when the actions surrounding a “property access” vary somewhat during the course of the program (between only a few standard actions), conditional or multiple getter/setter functions are used to avoid complication and confusion.  Code that is written in this form:

°         Ensures that the object itself handles all other manipulations that must accompany the accessing of a property, and thus adheres to the principles of object-oriented programming.

°         Allows for easily extending the code when other manipulations are deemed necessary later in the coding process.

°         Reduces the chance of error from forgetting to include necessary surrounding code.

°         Makes the code cleaner and easier to understand.

Therefore:

Make it standard practice to create private variables and public getter/setter functions for accessing the variables.  Centralize all necessary manipulations in these functions to avoid errors and adhere to OOP principles.  Include conditional statements or use multiple getter/setter functions if the actions surrounding a “property access” vary.

 

* * *

Publish public properties and their getter/setter functions through a PROPERTY INSPECTOR; Use getter/setter functions to handle CONCURRENCY calls, and to keep a CONSISTENT INTERNAL STATE;  use DEBUGGING FUNCTIONS in the getter and setter functions to determine access-related errors during the debugging stage, and to easily remove the code for release…