Roles of variables framework

Jorma Sajaniemi가 제안하고 Programmer’s brain에서 소개한 개념.

Sajaniemi argues that with just 11 roles, you can describe almost all variables:

Roles:

  • Fixed value: A variable whose value does not change after initialization plays the role of a fixed value. This can be a constant value if the programming language you are using allows for values to be fixed, or it can be a variable that is initialized once and afterward is not changed. Examples of fixed-value variables include mathematical constants like pi, or data read from a file or database.
  • Stepper: When iterating in a loop, there is always a variable stepping through a list of values. That is the role of the stepper, whose value can be predicted as soon as the succession starts. This can be an integer, like the canonical i iterating in a for-loop, but more complicated steppers are also possible, like size = size / 2 in a binary search, where the size of the array to be searched is cut in half on every iteration.
  • Flag: A variable used to indicate that something has happened or is the case. Typical examples are is_set, is_available, or is_error. Flags are often Booleans, but they can be integers or even strings.
  • Walker: A walker traverses a data structure, similar to a stepper. The difference lies in the way the data structure is traversed. A stepper always iterates over a list of values that are known beforehand, like in a for-loop in Python: for i in range(0, n). A walker, on the other hand, is a variable that traverses a data structure in a way that is unknown before the loop starts. Depending on the programming language, walkers can be pointers or integer indices. Walkers can traverse lists, for example in binary search, but more often traverse data structures like a stack or a tree. Examples of a walker are a variable that is traversing a linked list to find the position where a new element should be added or a search index in a binary tree.
  • Most recent holder: A variable that holds the latest value encountered in going through a series of values is a most recent holder. For example, it might store the latest line read from a file (line = file.readline()), or a copy of the array element last referenced by a stepper (element = list[i]).
  • Most wanted holder: Often when you are iterating over a list of values, you are doing that to search for a certain value. The variable that holds that value, or the best value found so far, is what we call a most wanted holder. Canonical examples of a most wanted holder are a variable that stores a minimum value, a maximum value, or the first value meeting a certain condition.
  • Gatherer: A gatherer is a variable that collects data and aggregates it into one value…. Its value can, however, also be calculated directly in functional languages or languages that encompass certain functional aspects: functional_total = sum(list).
  • Container: A container is any data structure that holds multiple elements that can be added and removed. Examples of containers are lists, arrays, stacks, and trees.
  • Follower: Some algorithms require you to keep track of a previous or subsequent value. A variable in this role is called a follower and is always coupled to another variable. Examples of follower variables are a pointer that points to a previous element in a linked list when traversing the list, or the lower index in a binary search.
  • Organizer: Sometimes a variable has to be transformed in some way for further processing. For example, in some languages, you cannot access individual characters in a string without converting the string to a character array first, or you may want to store a sorted version of a given list. These are examples of organizers, which are variables that are only used for rearranging or storing values differently. Often, they are temporary variables.
  • Temporary: Temporary variables are variables that are used only briefly and are often called temp or t. These variables may be used to swap data or to store the result of a computation that is used multiple times in a method or function.

2024 © ak