Roles of Variables Summary |
Variables in programs can be categorized by how they are used inside the program. We call these usage categories the roles of variables. For example, a variable whose value is never changed after initialization is considered a fixed value. Be careful not to confuse the roles of variables with the types of variables (integer, floating point, etc.)---they are different concepts.
Almost all variables used in programs can be identified as being in one of the following eleven role categories (green for all variables, blue for data structure use). The most commonly occurring roles are: fixed value, stepper and most-recent holder. Typically about 70% of variables in a program fall into one of these three categories. (On the other hand, some variables don't fit into any of the role categories below; this is uncommon, but not rare.)
Role | Example | Informal definition |
---|---|---|
Fixed value | MAX_STRING_LENGTH | A data item whose value is not changed in run-time after initialization. |
Stepper | count | A data item stepping through a succession of values in some systematic way. |
Most-recent holder | given_reply | A data item whose value is the latest value processed of a certain group or simply the latest input value. |
Most-wanted holder | smallest | A data item holding the "best" or otherwise the most-wanted value out of the values processed so far. |
Gatherer | sum | A data item that accumulates all the values processed so far. |
Follower | prev | A data item that always gets the old value of another known variable as its new value. |
One-way flag | error_detected | A Boolean variable that, once changed, is never reset to its original value. |
Temporary | temp | A data item holding some value for a very short period and then is not needed again. |
Organizer | list | A data structure storing elements that can be rearranged. |
Container | printer_queue | A data structure storing elements that can be added and removed. |
Walker | current_item | A data item used to traverse a data structure. |
Based on an item created by and courtesy of jorma.sajaniemi@uef.fi (3/31/2006)