Best Practices & Common Pitfalls

43.1 Best practices

ALGOL 60: put every scalar input parameter in the value list; specify every parameter; avoid side effects in function designators; never rely on the controlled variable after a for loop; initialize own variables explicitly; end every statement before 'end'-following code with a semicolon.

ALGOL 68: prefer identity declarations (INT n = ...) to variables when the value does not change; use whole/fixed for output; write LWB/UPB instead of assuming bounds start at 1; use HEAP for data that outlives a block; cast REF REF variables before comparing with NIL; keep collateral clauses free of interacting side effects; use unions plus conformity clauses rather than "magic" values.

Both: indent consistently, keep procedures short, comment the purpose of each block, and test with run-time checks enabled.

43.2 Common pitfalls

Missing ; after end Next statement silently treated as comment

Always follow end with ; when more statements follow Forgetting value

Slow code; unexpected re-evaluation; assignments to expressions fail List scalar inputs in value swap(i, a[i]) Wrong element swapped

Avoid call by name for in-out data with dependent subscripts Using loop variable after loop

Copy it inside the loop or use goto exit Changing until limit in the body Loop runs too long/short Copy limits to locals first if a then if b then... else... Wrap inner if in begin... end Expecting 7/2 = 3 Real result 3.5 Use ÷ (MARST %) / OVER Single parentheses in print(a, b) print((a, b)) Raw print(n) output +42 with padding top IS NIL with REF REF REF NODE (top) IS NIL Returning a routine using locals

Pass data explicitly; partial parametrisation (a68g) Storing a LOC reference globally Computes (-x) 2 Write -(x 2) LONG INT x:= 1

Mode error (no INT to LONG INT widening) LONG 1 or LENG 1 One-field STRUCT display Syntax/mode error Assign the field after declaration Labels before declarations

Put all declarations of a serial clause before the first label Order of collateral units Use ; when order matters

Every keyword is an "undeclared identifier" Match the implementation's regime ('begin' for MARST, BEGIN for a68g)