Modula-2 style guide
last modified November 27, 2026 Modula-2
Modula-2 leaves much to the programmer: the compiler accepts a whole program on one line, any spelling of names and imports in any form. This guide collects the conventions that the tutorials in this series follow. Most of them go back to Wirth's own programs and to the ISO library; where the two differ, the guide names a choice and explains it. The rules are not law, but a project should pick one set and keep to it.
All examples compile with gm2 -fiso -Wall without warnings.
Naming
Keywords and standard identifiers in Modula-2 are upper case, and names are case-sensitive. The conventions for the other names follow from their kind.
MODULE naming;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
CONST
PricePerCup = 3;
TYPE
Size = (Small, Medium, Large);
Order = RECORD
size: Size;
cupCount: CARDINAL
END;
PROCEDURE PriceOf(order: Order): CARDINAL;
BEGIN
RETURN order.cupCount * PricePerCup * (ORD(order.size) + 1)
END PriceOf;
VAR
lunchOrder: Order;
BEGIN
lunchOrder.size := Large;
lunchOrder.cupCount := 2;
WriteString("price: "); WriteCard(PriceOf(lunchOrder), 0); WriteLn
END naming.
Modules, types, procedures, constants and enumeration values start with a
capital letter: Order, PriceOf,
PricePerCup, Large. Variables, record fields and
parameters start with a small letter: lunchOrder,
cupCount. Names of several words are written in camel case,
without underscores. A procedure name is a verb or a question
(Push, IsEmpty); a function can also be named
after the value it returns (PriceOf). The ISO library
writes enumeration values in lower case (opened,
allRight); this series capitalises them, and either is fine
when it is consistent. Two names that differ only in case, such as
count and Count, are legal but confusing, and
-Wall reports them.
$ gm2 -fiso -Wall naming.mod -o naming $ ./naming price: 18
Layout
Line breaks and spaces mean nothing to the compiler. The next program is correct, and hard to read.
MODULE crowded; FROM STextIO IMPORT WriteString,WriteLn; FROM SWholeIO IMPORT WriteCard;
VAR n,s,i:CARDINAL; BEGIN n:=10;s:=0;FOR i:=1 TO n DO IF ODD(i) THEN s:=s+i*i; END; END;
WriteString("sum: ");WriteCard(s,0);WriteLn; END crowded.
The same program laid out in the style of this series reads from top to bottom.
MODULE layout;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
CONST
Limit = 10;
VAR
i, sum: CARDINAL;
BEGIN
sum := 0;
FOR i := 1 TO Limit DO
IF ODD(i) THEN
sum := sum + i * i
END
END;
WriteString("sum: "); WriteCard(sum, 0); WriteLn
END layout.
Each declaration section (CONST, TYPE,
VAR) starts on its own line with its entries indented below
it. The body is indented by two spaces, and every structured statement
indents its contents again. An END stands below the start of
the statement it closes, so IF and its END line
up. There is one statement per line, except for short groups that belong
together, such as a label and its value in WriteString and
WriteCard. The semicolon separates statements; it is not
needed before END, ELSE or
UNTIL. A blank line separates the declarations, the
procedures and the body. The literal 10 became the constant
Limit: a named constant says what the number means and is
changed in one place.
$ gm2 -fiso -Wall crowded.mod -o crowded $ ./crowded sum: 165 $ gm2 -fiso -Wall layout.mod -o layout $ ./layout sum: 165
CARDINAL or INTEGER
CARDINAL is the natural type for counts, sizes and indexes,
and the standard procedures such as HIGH return it. Its danger
is subtraction: a result below zero wraps round to a huge number without
an error, unless the program is compiled with runtime checks.
MODULE countdown;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteInt, WriteCard;
PROCEDURE ShowBackwards(VAR a: ARRAY OF INTEGER; count: CARDINAL);
VAR
i: CARDINAL;
BEGIN
(* count - 1 would wrap round for count = 0, so i stays one above
the index *)
i := count;
WHILE i > 0 DO
DEC(i);
WriteInt(a[i], 4)
END;
WriteLn
END ShowBackwards;
VAR
data: ARRAY [0..3] OF INTEGER;
empty: CARDINAL;
BEGIN
data[0] := 3; data[1] := 1; data[2] := 4; data[3] := 1;
ShowBackwards(data, 4);
ShowBackwards(data, 0);
empty := 0;
WriteString("0 - 1 as a CARDINAL: "); WriteCard(empty - 1, 0); WriteLn
END countdown.
A backwards loop written as FOR i := count - 1 TO 0 BY -1
fails for an empty array, because count - 1 is 4294967295.
ShowBackwards keeps i one above the index and
decrements it before use, so it never goes below zero, and the second
call prints an empty line as it should. The comment
explains why the loop has this shape, which the code alone does not say; a
comment that repeats the code, such as (* decrement i *),
only adds lines. Use INTEGER for values that can be negative,
such as differences, and for indexes that may step below the start, as in
quicksort.
$ gm2 -fiso -Wall countdown.mod -o countdown $ ./countdown 1 4 1 3 0 - 1 as a CARDINAL: 4294967295
Qualified imports
FROM M IMPORT x brings x into the module under
its plain name. IMPORT M brings in the module, and its names
are written M.x. The second form is longer, but it shows
where each name comes from and avoids clashes.
MODULE qualified;
IMPORT STextIO, TextIO, SWholeIO, WholeIO, StreamFile;
FROM ChanConsts IMPORT write, old, OpenResults, opened;
CONST
Count = 5;
VAR
file: StreamFile.ChanId;
res: OpenResults;
i: INTEGER;
BEGIN
StreamFile.Open(file, "squares.txt", write + old, res);
IF res # opened THEN
STextIO.WriteString("cannot open squares.txt"); STextIO.WriteLn;
HALT(1)
END;
FOR i := 1 TO Count DO
WholeIO.WriteInt(file, i * i, 0);
TextIO.WriteLn(file)
END;
StreamFile.Close(file);
STextIO.WriteString("squares written: ");
SWholeIO.WriteInt(Count, 0); STextIO.WriteLn
END qualified.
STextIO and TextIO both export
WriteLn, and SWholeIO and WholeIO
both export WriteInt. With unqualified imports the program
could import only one of each; the qualified names keep them apart and say
which channel each call uses. A good rule: use FROM for a few
names used often, such as WriteString and
WriteLn, and qualified imports for everything else, in
particular for modules of your own project.
$ gm2 -fiso -Wall qualified.mod -o qualified $ ./qualified squares written: 5 $ cat squares.txt 1 4 9 16 25
Documenting a module
A definition module is the only file most programmers read before they use a module. It should explain what each procedure does, what it expects and what it guarantees, in comments next to the declarations.
DEFINITION MODULE Counters; (* Named event counters. A counter starts at zero and only grows; Reset sets it back to zero. *) TYPE Counter; PROCEDURE Create(): Counter; (* Returns a new counter with the value 0. *) PROCEDURE Increment(c: Counter; by: CARDINAL); (* Adds by to the counter. *) PROCEDURE Value(c: Counter): CARDINAL; (* Returns the current value. *) PROCEDURE Reset(c: Counter); (* Sets the counter back to 0. *) PROCEDURE Destroy(VAR c: Counter); (* Frees the counter and sets c to NIL; c must not be used again. *) END Counters.
The comment under the module heading describes the abstraction; each
procedure has a short comment below its heading. The opaque type
Counter keeps the representation out of the interface, and
one module holds one abstract data type, as in the
opaque types article.
IMPLEMENTATION MODULE Counters;
FROM Storage IMPORT ALLOCATE, DEALLOCATE;
TYPE
Counter = POINTER TO RECORD
value: CARDINAL
END;
PROCEDURE Create(): Counter;
VAR
c: Counter;
BEGIN
NEW(c);
c^.value := 0;
RETURN c
END Create;
PROCEDURE Increment(c: Counter; by: CARDINAL);
BEGIN
INC(c^.value, by)
END Increment;
PROCEDURE Value(c: Counter): CARDINAL;
BEGIN
RETURN c^.value
END Value;
PROCEDURE Reset(c: Counter);
BEGIN
c^.value := 0
END Reset;
PROCEDURE Destroy(VAR c: Counter);
BEGIN
DISPOSE(c)
END Destroy;
END Counters.
The implementation needs fewer comments: the reader already knows from the definition what each procedure does, and the code shows how.
MODULE visits;
FROM STextIO IMPORT WriteString, WriteLn;
FROM SWholeIO IMPORT WriteCard;
IMPORT Counters;
VAR
pages, errors: Counters.Counter;
BEGIN
pages := Counters.Create();
errors := Counters.Create();
Counters.Increment(pages, 3);
Counters.Increment(errors, 1);
Counters.Increment(pages, 2);
WriteString("pages: "); WriteCard(Counters.Value(pages), 0);
WriteString(", errors: "); WriteCard(Counters.Value(errors), 0); WriteLn;
Counters.Destroy(pages);
Counters.Destroy(errors)
END visits.
The client imports the module qualified, so each call reads as
Counters.Increment and the procedure names in the module can
stay short. Every Create has its Destroy.
$ gm2 -fiso -Wall -c Counters.mod $ gm2 -fiso -Wall visits.mod Counters.o -o visits $ ./visits pages: 5, errors: 1
Matching parameter names
The heading of a procedure appears twice: in the definition module and in the implementation module. The language requires the same parameter types; it does not require the same names.
DEFINITION MODULE Rects; PROCEDURE Area(width, height: CARDINAL): CARDINAL; END Rects.
IMPLEMENTATION MODULE Rects; PROCEDURE Area(w, h: CARDINAL): CARDINAL; BEGIN RETURN w * h END Area; END Rects.
The definition names the parameters width and
height, the implementation w and h.
The default build accepts this. With -Wall, gm2 15.2 reports
the mismatch, and as an error, not a warning: the module does not compile.
The names in the definition are what the clients read, so the
implementation should use the same descriptive names.
$ gm2 -fiso -c Rects.mod
$ gm2 -fiso -Wall -c Rects.mod
./Rects.def:3:16: error: In procedure ‘Area’: procedure ‘Area’ in the proper procedure differs from the definition procedure, 1st parameter name is inconsistent, named as ‘width’
3 | PROCEDURE Area(width, height: CARDINAL): CARDINAL;
| ^~~~~
./Rects.def:3:23: error: procedure ‘Area’ in the proper procedure differs from the definition procedure, 2nd parameter name is inconsistent, named as ‘height’
3 | PROCEDURE Area(width, height: CARDINAL): CARDINAL;
| ^~~~~~
Rects.mod:3:16: error: procedure ‘Area’ in the proper procedure differs from the definition procedure, 1st parameter name is inconsistent, named as ‘w’
3 | PROCEDURE Area(w, h: CARDINAL): CARDINAL;
| ^
Rects.mod:3:19: error: procedure ‘Area’ in the proper procedure differs from the definition procedure, 2nd parameter name is inconsistent, named as ‘h’
3 | PROCEDURE Area(w, h: CARDINAL): CARDINAL;
| ^
Isolating extensions
Code that should compile with other Modula-2 compilers must avoid the
extensions of gm2, or keep them in one place. HALT with an
exit status is such an extension; the next module wraps it.
DEFINITION MODULE Exits; (* The exit status of the program. The only module of the project that uses the gm2 extension HALT(n). *) CONST Success = 0; Failure = 1; BadUsage = 2; PROCEDURE Leave(status: CARDINAL); (* Ends the program with the given exit status. *) END Exits.
The definition module names the exit statuses as constants and says that the module depends on gm2. Porting the project to another compiler means rewriting this one implementation module.
IMPLEMENTATION MODULE Exits; PROCEDURE Leave(status: CARDINAL); BEGIN HALT(status) END Leave; END Exits.
MODULE usage;
FROM STextIO IMPORT WriteString, WriteLn;
FROM ProgramArgs IMPORT IsArgPresent, NextArg;
FROM Exits IMPORT Leave, BadUsage;
BEGIN
NextArg;
IF NOT IsArgPresent() THEN
WriteString("usage: usage FILE"); WriteLn;
Leave(BadUsage)
END;
WriteString("processing the file"); WriteLn
END usage.
The program uses Leave(BadUsage) instead of
HALT(2), which also explains the number. Other gm2 extensions
that this series avoids in portable code are functions returning arrays,
REM in the PIM dialect, the libc module and the
Environment module. gm2 accepts most of them without a
warning even in the PIM dialects, and -Wpedantic does not
report them, so the discipline is up to the programmer.
$ gm2 -fiso -Wall -c Exits.mod $ gm2 -fiso -Wall usage.mod Exits.o -o usage $ ./usage usage: usage FILE $ echo $? 2 $ ./usage notes.txt processing the file $ echo $? 0
A checklist
Before a module is finished, go through the following points.
- Names follow their kind: capitals for modules, types, procedures, constants and enumeration values, small letters for variables and fields.
- Bodies are indented by two spaces, one statement per line, with each
ENDbelow the start of its statement. - Numbers with a meaning are named constants.
CARDINALsubtraction cannot go below zero, and values that can be negative areINTEGER.- Imports are qualified, apart from a few common names.
- The definition module documents every exported procedure, and the implementation uses the same parameter names.
- One module holds one abstract data type, behind an opaque type.
- Comments explain why, not what.
- Compiler extensions are kept in one module.
- The module compiles without warnings with
-Walland passes its tests with-fsoft-check-all.
Source
Modula-2 reference at modula2.org
In this article we have named things by their kind, laid out a program,
replaced literals with constants, guarded CARDINAL
subtraction, qualified imports, documented a definition module, kept the
parameter names of both module parts equal and isolated a compiler
extension in its own module. The modules
and opaque types articles cover the
module structure behind these rules, and the
unit testing article shows how to
check a module once it is written.
Author
List all Modula-2 tutorials.