Coding Guidelines
Organizing your source files
Store your files in a directory with a simple but descriptive name. Avoid using a name already in use in the current Logtalk distribution. The directory may contain, besides your source files, three other files:
- A loader file for your source files. Usually named loader.lgt, this file should load both your source files and any other files (e.g. library files) needed for compiling and running your code. Loader files may also be used to set appropriated flags for compiling and loading your source files.
- An informative NOTES.txt file with a description of your code, authorship, copyright, and licensing information.
- A SCRIPT.txt file containing instructions on how to load your source files and sample queries.
Source file names
Logtalk source files use the .lgt file name extension. When a source file contains a single entity, use the entity name for the source file name. For parametric objects, append the number of parameters to the file name. For source files containing multiple entities, use a descriptive name. In this case, the name should be the same as the name of the directory holding your files.
Source code layout and indentation
Format your source file using tabs, not spaces. Use a tab setting equivalent to four spaces. Use one tab for indenting the code encapsulated inside an entity. Indent the body of predicates using one tab. Clauses for the same predicate should be separated by at most one blank line. Closely related predicates (e.g. a predicate followed by its auxiliary predicates) should be separated by one or two blank lines. Clauses of other predicates can be separated by two or three blank lines, depending on code complexity.
If-then-else constructs
An actual example of the recommended layout:
is_prime(N, Sqrt, Prime) :-
( N > Sqrt ->
true
; Prime mod N > 0,
N2 is N + 2,
is_prime(N2, Sqrt, Prime)
).
Use a tab between the opening parenthesis and the first goal. Also use a tab between the ; and the goal that follows. Align vertically the opening parenthesis, the ;, and the closing parenthesis.
Failure-driven loops
Indent with one tab the goals between the generator goal and the goal that forces backtracking. For example:
write_person_database :-
person(Name, Age),
write('Name: '), write(Name), nl,
write('Age: '), write(Age), nl, nl,
fail.
write_person_database.
Predicate arguments
Use a single space after commas that separate predicate arguments.
List elements
Use a single space after commas that separate elements. Also use a single space after the | separating the enumerated elements from the list with the remaining elements. I.e. write [First, Second| Rest], not [First,Second|Rest].
Clause rules and directives
Use a single space before the :- operator in clause rules. Use a single space after the :- operator in directives.
Predicate names
Use descriptive predicate names using underscores if necessary to link words. Do not use CamelCase notation for predicate names (e.g. write number_of_sides, not numberOfSides).
For dynamic predicates, Logtalk uses a convention of ending its names with an underscore (e.g. random_seed_).
Variable names
Use descriptive variable names in CamelCase notation. In the case of singleton variables, use either the anonymous variable, _, or a variable name starting with an underscore followed with an uppercase letter (e.g. _Width). Note that the interpretation of variables that start with an underscore as either singleton variables or anonymous variables depends on the used back-end compiler (see the Logtalk compiler flag underscore_variablesfor more information).
