2. Types
Name types
rather than defining anonymous types, especially in definitions modules:
DeckIndex: TYPE = [0..52); -- YES
CardDeck: TYPE = ARRAY DeckIndex OF Card;
CardDeck: TYPE = ARRAY [0..52) OF Card; -- NO
Use closed lower bounds and open upper bounds
for interval types:
[0..upperLimit) -- preferred form
FOR i IN [0..n) DO IF a[i]=a[n] THEN . . . ENDLOOP;
Use positional notation for single-component argument lists,
record constructors, and extractors. Positional notation is also acceptable if there are multiple components, all of different types:
ViewerTools.SetSelection[handle.fact.input]; -- force the selection
execStream.PutF["Your user name backwards is: %g\n", IO.rope[backwordsName]];
Use the keyword form
for argument lists, record constructors, and extractors when there are two or more constituents, especially if any have equivalent types. It is also preferred when most components are being defaulted and only a few given values:
context.DrawBox[
Box[xmin: 0, ymin: 0, xmax: data.value, ymax: self.ch]];
Menus.AppendMenuEntry
[
menu: my.outer.menu, -- the outer container's menu (already defaulted)
name: "MyMenuEntry", -- name of the command
proc: MyMenuProc, -- proc associated with command
fork: TRUE, -- causes a new process to be forked & detached on invocation
copy: TRUE ]; -- make this a new menu (i.e. don't modify the default menu)
[in: in, out: out] ← IO.CreateTTYStreams[title];
R.Compare[s1: rope, s2: NARROW[r2, ROPE], case: TRUE]
NEW[ViewerClasses.ViewerClassRec ← [paint: PaintGraph]];
Name procedure parameters
in the definitions of procedure types in
DEFINITIONS modules:
CommandProc: TYPE = PROC [exec: ExecHandle, clientData: REF ANY ← NIL] RETURNS[ok: BOOLEAN ← TRUE];
Name procedure result fields,
especially if there is more than one:
CreateBarGraph:
PROC [x, y, w, h:
INT, parent: ViewerClasses.Viewer, fullScale:
REAL]
RETURNS [barGraph: ViewerClasses.Viewer]
FindItem: PROC[k: Key] RETURNS [v: Value, found: BOOLEAN];