Lecture 9
Programming Methodology and Style Contents
- Some general dos and don’ts of program-
ming.
- Some specific Prolog advice
- What is top-down design?
(See Clocksin & Mellish Chapter 8)
Be organised
- Think and plan before you start writing the actual
program.
- For a medium or large-size program, try top-down design
(see later).
- Aim for simplicity and clarity, rather than obscure
- efficiency. (If efficiency becomes important later, that is
the time to worry about it.)
- Consider maintainability: what will it be like to make
sense of the program after a few weeks/months/years? what will it be like to try to amend it after such a period? what if it were someone else doing this? (After only a few weeks, you will feel like someone else).
“Introduction to Artificial Intelligence Programming”, School of Informatics 2
Layout and Comments
- Make your text easy to read.
- Not too much on each line.
- Indentation to show nesting or levels.
- Appropriate commenting.
“Introduction to Artificial Intelligence Programming”, School of Informatics 3
An example of commenting
Put a header at the top, optionally some commentary between clauses or at the right.
% count_atoms/2(+List, -CountOfAtoms) % % Succeeds where CountOfAtoms is % instantiated to a list containing the % count of each of the atoms in List, % in the following form: % % [a=2, b=3, c=4] % % Add an accumulator. count_atoms(List, CountList) :- count_atoms(List, [], CountList). ETC ...
Notation for parameters: if parameter must be instantiated on call: + if parameter is not instantiated on call: - if parameter may or may not be instantiated on call: ?
“Introduction to Artificial Intelligence Programming”, School of Informatics 4