Note: Role lists suitable for novice use can be found in the page describing how roles can be utilized in teaching.
This document describes version 2 of the role set; the old role set can be found here. Differences between role sets are described here.
Procedural programming: Roles apply to variables, and parameters.
Object-oriented programming: Roles apply to variables, parameters, atttributes, and objects that encapsulate a single conceptual attribute.
Functional programming: Roles apply to the recursive behavior of parameters and return values
Notes that are in italics describe special cases and can be skipped on first reading. Technical definitions and properties provide more exact information about the roles, and they are not expected to be used in, e.g., teaching programming to novices.
Technical definitions may be needed in, e.g., role analysis. They are meant for human classifiers who are able to use their understanding of a program to capture the data flow through a data entity and identify main phases of this flow. For example, a one-way flag may be reset to its initial value at the beginning of the main loop (and changed within a nested loop) yielding the one-way behavior within the main loop even though the flag goes both ways during the entire program execution.
Properties are descriptions of actual use in novice-level programs or consequences of the technical definitions. They can be used in, e.g., devising pictorial representations for roles.
Roles can be grouped according to the way they relate to data flow:
Fixed value and organizer contain the same data throughout the program; only the order of data elements may be changed. Most-recent holder and stepper record data flow sources; either coming from outside or generated internally. The net effect of all items in a data flow is represented by a one-way flag, most-wanted holder, or gatherer; while a manipulation of a single element is recorded in a follower or temporary. Data may be stored in a container which can be traversed with a walker. Finally, a data entity not covered by any of the previous roles is considered to have the role other.
The roles are described in the following order:
The role images are examples only. In actual use, they should be replaced by images better suiting the cultural backgrounds of users.
Fixed value
A data entity whose value does not change after initialization. |
Procedural example:
program fuelRate; var start, finish: integer; fuel: real; begin write('Enter fuel amount: '); readln(fuel); write('Enter odometer at start: '); readln(start); write('Enter odometer at finish: '); readln(finish); while finish <= start do begin write('Odometer cannot be smaller at finish. Re-enter: '); readln(finish) end; writeln('Average rate of fuel consumed was ', fuel / (finish - start) ) end.
(All roles in the program: start - fixed value | finish - most-recent holder | fuel - fixed value)
Object-oriented example:
public class Dog { String name; int age; public Dog (String n) { name = n; age = 0; } public void birthday () { age++; } }
(All roles in the class: name - fixed value | age - stepper | n - fixed value)
Examples of use:
Notes:
write('Enter fuel amount: '); readln(fuel); if fuel < 0 then begin fuel := abs(fuel); writeln('Using ', fuel, ' instead.') end; ...
Technical definition:
Properties:
|
Organizer
A data structure which is only used for rearranging its elements after initialization. |
Procedural example:
program bubbleSort; var i, j, temp: integer; a: array [1..10] of integer; begin for i := 1 to 10 do begin write('Enter number: '); readln(a[i]); end; for i := 1 to 9 do for j := i+1 to 10 do if a[i] > a[j] then begin temp := a[i]; a[i] := a[j]; a[j] := temp end; for i := 1 to 10 do writeln(a[i]) end.
(All roles in the program: i - stepper | j - stepper | temp - temporary | a - organizer)
Object-oriented example:
public class ReversableString { private char[] characters; public ReversableString (String s) { characters = s.toCharArray(); } public String get() { return new String(characters); } public void reverse() { int length = characters.length; char tmp; for (int i = 0; i<length/2; i++) { tmp = characters[i]; characters[i] = characters[length-i-1]; characters[length-i-1] = tmp; } } }
(All roles in the class: characters - organizer | s - fixed value | length - fixed value | tmp - temporary | i - stepper)
Examples of use:
Notes:
Technical definition:
Properties:
|
Stepper
A data entity stepping through a succession of values that can be predicted as soon as the succession starts. |
Procedural example:
program monthlySales; var month, largest: integer; sales: array [1..12] of integer; begin for month := 1 to 12 do begin write('Enter sales of month ', month, ': '); readln(sales[month]) end; largest := sales[1]; for month := 2 to 12 do if largest < sales[month] then largest := sales[month]; for month := 1 to 12 do begin writeln('Month ', month, ' gave ', largest-sales[month], ' less than the best month.') end end.
(All roles in the program: month - stepper | largest - most-wanted holder | sales - fixed value)
Object-oriented example:
public class Dog { String name; int age; public Dog (String n) { name = n; age = 0; } public void birthday () { age++; } }
(All roles in the class: name - fixed value | age - stepper | n - fixed value)
Examples of use:
count := 0; while ... do begin count := count + 1; ... end
size := numberOfItems; while size >= 1 do begin size := size / 2; ... end
Notes:
for density := 10 to 1 do begin scanPoint := density; while scanPoint < areaBorder do begin ... scanPoint := scanPoint + density end end
(In this example, density is also a stepper.)
Technical definition:
Properties:
|
Most-recent holder
A data entity holding the latest value encountered in going through a succession of values. |
Procedural example:
program fuelRate; var start, finish: integer; fuel: real; begin write('Enter fuel amount: '); readln(fuel); write('Enter odometer at start: '); readln(start); write('Enter odometer at finish: '); readln(finish); while finish <= start do begin write('Odometer cannot be smaller at finish. Re-enter: '); readln(finish) end; writeln('Average rate of fuel consumed was ', fuel / (finish - start) ) end.
(All roles in the program: start - fixed value | finish - most-recent holder | fuel - fixed value)
Object-oriented example:
public class BankAccount { private String accountNumber; private float interest, balance=0; public BankAccount (String nr, float percent) { accountNumber = nr; interest = percent; } public String getAccountNumber () { return accountNumber; } public float getBalance () { return balance; } public float getInterest () { return interest; } public void setInterest (float pc) { interest = pc; } public void transaction (float amount) { balance += amount; } }
(All roles in the class: accountNumber - fixed value | interest - most-recent holder | balance - gatherer | nr - fixed value | percent - fixed value | pc - fixed value | amount - fixed value)
Examples of use:
Notes:
while not eof(timesInSeconds) do begin read(timesInSeconds, time); time := time * 1000; (* convert to milliseconds *) ... end
Technical definition:
Properties:
|
One-way flag
A two-valued data entity that cannot get its initial value once its value has been changed. |
Procedural example:
program dateValidation; var day, month, year: integer; error: Boolean; begin write('Enter day: '); readln(day); error := (day < 1) or (day > 31); write('Enter month: '); readln(month); error := error or (month < 1) or (month > 12); write('Enter year: '); readln(year); case month of 1,3,5,7,8,10,12: (* ok *) ; 4,6,9,11: error := error or (day > 30); 2: if (year mod 400 = 0) or ((year mod 4 = 0) and (year mod 100 <> 0)) then error := error or (day > 29) else error := error or (day > 28) end; if error then writeln('Date incorrect.') else writeln('Date correct.') end.
(All roles in the program: day - fixed value | month - fixed value | year - fixed value | error - one-way flag)
Object-oriented example:
public class Thermometer { private int reading=-999, previous=-999, coldest=9999; private boolean frozen=false; public Thermometer () { } public int getReading () { return reading; } public void setReading (int r) { if (r < coldest) coldest = r; if (r < 0) frozen = true; previous = reading; reading = r; } public int getPrevious () {return previous; } public int getColdest () {return coldest; } public boolean getFrozen () {return frozen; } }
(All roles in the class: reading - most-recent holder | previous - follower | coldest - most-wanted holder | frozen - one-way flag | r - fixed value)
Examples of use:
Notes:
Technical definition:
Properties:
|
Most-wanted holder
A data entity holding the best value encountered so far in going through a succession of values. |
Procedural example:
program monthlySales; var month, largest: integer; sales: array [1..12] of integer; begin for month := 1 to 12 do begin write('Enter sales of month ', month, ': '); readln(sales[month]) end; largest := sales[1]; for month := 2 to 12 do if largest < sales[month] then largest := sales[month]; for month := 1 to 12 do begin writeln('Month ', month, ' gave ', largest-sales[month], ' less than the best month.') end end.
(All roles in the program: month - stepper | largest - most-wanted holder | sales - fixed value)
Object-oriented example:
public class Thermometer { private int reading=-999, previous=-999, coldest=9999; private boolean frozen=false; public Thermometer () { } public int getReading () { return reading; } public void setReading (int r) { if (r < coldest) coldest = r; if (r < 0) frozen = true; previous = reading; reading = r; } public int getPrevious () {return previous; } public int getColdest () {return coldest; } public boolean getFrozen () {return frozen; } }
(All roles in the class: reading - most-recent holder | previous - follower | coldest - most-wanted holder | frozen - one-way flag | r - fixed value)
Examples of use:
closest := 1; for i := 2 to 100 do if (a[i] - target) < (a[closest] - target) then closest := i
Notes:
Technical definition:
Properties:
|
Gatherer
A data entity accumulating the effect of individual values in going through a succession of values. |
Procedural example:
program sales; var month, monthSales, totalSales: integer; begin totalSales := 0; for month := 1 to 12 do begin write('Enter sales of month ', month, ': '); readln(monthSales); totalSales := totalSales + monthSales end; writeln('Total sales were ', totalSales) end.
(All roles in the program: month - stepper | monthSales - most-recent holder | totalSales - gatherer)
Object-oriented example:
public class BankAccount { private String accountNumber; private float interest, balance=0; public BankAccount (String nr, float percent) { accountNumber = nr; interest = percent; } public String getAccountNumber () { return accountNumber; } public float getBalance () { return balance; } public float getInterest () { return interest; } public void setInterest (float pc) { interest = pc; } public void transaction (float amount) { balance += amount; } }
(All roles in the class: accountNumber - fixed value | interest - most-recent holder | balance - gatherer | nr - fixed value | percent - fixed value | pc - fixed value | amount - fixed value)
Examples of use:
cardsInHand := 0; while ... do begin ... if taking_new_cards then begin cardsInHand := cardsInHand + numberOfNewCards end ... if putting_away_cards then begin cardsInHand := cardsInHand - numberOfDiscardedCards end ... end
value := 0; read(c); while (c >= '0') and (c <= '9') do begin value := value * 10 + ord(c) - ord('0'); read(c) end
Notes:
Technical definition:
Properties:
|
Container
A data structure where elements can be added and removed. |
Procedural example:
program Stack(input,output); const SIZE = 10; var table: array [1..SIZE] of integer; top, element: integer; command: char; begin top := 0; repeat write('Enter command: '); readln(command); case command of 'n': if top < SIZE then begin write('Enter element: '); readln(element); top := top + 1; table[top] := element end; 'r': if top > 0 then begin writeln(table[top], ' removed.'); top := top - 1 end end until command = 'q' end.
(All roles in the program: table - container | top - walker | element - most-recent holder | command - most-recent holder)
Object-oriented example:
public class Stack { private int contents; private Stack next; public Stack() { this.next = null; } public void add (int element) { Stack node; node = new Stack(); node.contents = element; node.next = this.next; this.next = node; } public int remove () { int removed = 0; if (this.next != null) { removed = this.next.contents; this.next = this.next.next; } return removed; } } class StackTest { Stack myStack = new Stack(); ... }
(All roles in the classes: contents - fixed value | next of the object myStack - most-recent holder | next of all other objects - fixed value | element - fixed value | node - temporary | removed - temporary)
Examples of use:
Notes:
Technical definition:
Properties:
|
Walker
A data entity that traverses a data structure. |
Procedural example:
program List(input,output); type nodePtr = ^node; node = record contents: integer; link: nodePtr end; var head, p, t: nodePtr; element: integer; command: char; begin new(head); head^.link := nil; repeat write('Enter command: '); readln(command); case command of 'n': begin write('Enter element: '); readln(element); p := head; while p^.link <> nil do p := p^.link; new(t); t^.contents := element; t^.link :=nil; p^.link := t; end; 'p': begin p := head^.link; while p <> nil do begin writeln(p^.contents); p := p^.link end end end until command = 'q' end.
(All roles in the program: contents - fixed value | link - fixed value | head - fixed value | p - walker | element - most-recent holder | command - most-recent holder)
Object-oriented example:
public class Stack { private int contents; private Stack next; public Stack() { this.next = null; } public void add (int element) { ... } public int remove () { ... } public int size () { int size = 0; Stack p = this.next; while (p != null) { size++; p = p.next; } return size; } }
(All roles in the method size: size - stepper | p - walker)
Examples of use:
Notes:
Technical definition:
Properties:
|
Follower
A data entity that gets its values by following another data entity. |
Procedural example:
program distance; var month, current, previous, largestDifference: integer; begin write('Enter 1. value: '); readln(previous); write('Enter 2. value: '); readln(current); largestDifference := current - previous; for month := 3 to 12 do begin previous := current; write('Enter ', month, '. value: '); readln(current); if current - previous > largestDifference then largestDifference := current - previous end; writeln('Largest difference was ', largestDifference) end.
(All roles in the program: month - stepper | current - most-recent holder | previous - follower | largestDifference - most-wanted holder)
Object-oriented example:
public class Thermometer { private int reading=-999, previous=-999, coldest=9999; private boolean frozen=false; public Thermometer () { } public int getReading () { return reading; } public void setReading (int r) { if (r < coldest) coldest = r; if (r < 0) frozen = true; previous = reading; reading = r; } public int getPrevious () {return previous; } public int getColdest () {return coldest; } public boolean getFrozen () {return frozen; } }
(All roles in the class: reading - most-recent holder | previous - follower | coldest - most-wanted holder | frozen - one-way flag | r - fixed value)
Examples of use:
Notes:
Technical definition:
Properties:
|
Temporary
A data entity holding some value for a very short time only. |
Procedural example:
program bubbleSort; var i, j, temp: integer; a: array [1..10] of integer; begin for i := 1 to 10 do begin write('Enter number: '); readln(a[i]); end; for i := 1 to 9 do for j := i+1 to 10 do if a[i] > a[j] then begin temp := a[i]; a[i] := a[j]; a[j] := temp end; for i := 1 to 10 do writeln(a[i]) end.
(All roles in the program: i - stepper | j - stepper | temp - temporary | a - organizer)
Object-oriented example:
public class Item { float taxfreePrice, priceWithTax; public Item (float price) { taxfreePrice = price; } public float setTax (float taxRate) { float tax; tax = taxRate * taxfreePrice / 100; priceWithTax = taxfreePrice + tax; return tax; } }
(All roles in the class: taxfreePrice - fixed value | priceWithTax - most-recent holder | price - fixed value | taxRate - fixed value | tax - temporary)
Examples of use:
Notes:
Technical definition:
Properties:
|
Other
Any other data entity. |
Procedural example:
not provided
Object-oriented example:
not provided
Examples of use:
Notes:
Technical definition:
Properties:
|
Last updated: February 8, 2006