Visual Prolog 7 TutorialsCode FormattingWritten by Thomas Linder Puls and Yuri Ilyin Last updated: 27-06-2008 This document describes coding standards for Visual Prolog programs, written as part of the Visual Prolog system itself. It is also the standards for examples in user documentation. As such, it represents Prolog Development Center's suggested coding standards for users as well. KeywordsKeywords are typed in lower-case letters. In documents keywords are typeset with a boldfaced sans serif font, for example Arial. Default color is dark yellow.
Semi-keywordsVisual Prolog uses a number of tokens for diverse syntactical structuring. These words are all written in lower-case (except the calling convention C, which is written C) normal faced sans serif font. These semi-keywords are colored in two different colors depending on their nature. If the word represents a choice then it is colored navy, whereas if it is a structuring word, it is colored dark yellow.
This example shows the colors and the font:
LiteralsLiterals are colored blue.
IdentifiersThe general format of an identifier can be described by the following EBNF grammar:
Prefixes and suffixes are used to signal the kind of identifier, and will be dealt with in connection with each kind of identifiers. The words are capitalized, except for those situations, where the first letter of the whole identifier has to be lower case. All variables start with an uppercase letter and everything else starts with a lower case letter. In documents everything except keywords are typeset in a serif font, for example Times New Roman ConstantsConstants have got no prefix or suffix. They start with a lowercase letter.
VariablesVariables have got no prefix or suffix. As mentioned (and demanded by Prolog) variables start with an uppercase letter. In documents variables are colored green.
PredicatesPredicates have got no prefix. However, "try" can be used to signal that a predicate is determ, and it is especially used to distinguish a determ version of a predicate from a corresponding procedure version, where the latter will raise an exception instead of failing. For example:
isXxxx and hasXxxx are good choices for test predicates (provided the name makes sense of course). Predicates have got no suffixes, unless it seems to be necessary in order to avoid confusion. In such cases the following suffixes should be preferred:
Notice, that normally multi predicates would be suffixed with _nd, but if circumstances demand it _multi can be used instead.
DomainsDomains have got no prefix. _list is used as suffix to list domains, in the cases, where the list domain does not have a (business-)domain name, i.e. a database record is a list of values, but record is a better domain name that value_list, so that is preferred. Notice that domains start with a lowercase letter. This also holds for build-in domains like string, integer, etc.
Classes and InterfacesClasses and interfaces have got no prefix.
COM interfaces traditionally start with an "I", this "I" is kept but converted to lowercase:
FormattingThis section considers formatting of program code. By formatting we mean line breaking, indentation and alignment. Indentation refers to the amount of white space at the beginning of the line, where as alignment refers to aligning constructs which are not the first on the line. Line breakingLine breaking follows the following rules:
IndentationBy indentation we mean the amount of white space at the beginning of the line. Indentation follows the following rules:
AlignmentNo alignment is used (alignment refers to aligning constructs which are not the first on the line, constructs which are the first on the line are "aligned" by indentation rules). Space characters
ConstructsIn this section we shall deal with constructs one by one. SectionsSection keywords are on a line by itself. A section keyword is not indented. The constructs of the section are indented one step. Sections must be separated by at least one empty line.
Classes, Interfaces, Implementations, etcThe class opening and class closing is on a line by itself. The closing always includes the class identifier, the section keywords inside the class are not indented.
The same goes for other constructs like class (i.e. interface, implement). Predicate declarationsPredicate declarations always have names for the arguments; these names are formatted as variables. Mode declaration is optional.
Notice the line break after the opening parenthesis and the ordinary increase in indentation. Either all arguments are on a single line or each of them is on a separate line. DomainsFunctors always have got names for their arguments; these names are formatted as variables. If a domain declaration is broken on several lines then it is first broken after the equal sign. Either all functors are on a single line or each of them is on a separate line. Either all arguments of a functor are on a single line or each of them is on a separate line.
ClausesThe clause head is on a line by itself. Each call in the clauses body is on a line by itself. If clause head have to be broken then the arguments are indented two steps more than the clause head, because otherwise it would be indented to the same position as the clause body.
Non-deterministitic LoopsA construction that is often used in Prolog is "looping" over the results of a non-deterministic call. For this construction we propose to indent the "loop body" one extra level:
Formatting Code in Microsoft WordThis section gives a few hints that make it easier to format code in Microsoft Word. I have constructed a paragraph style code and a number of character styles: keyword, parameter, var, literal, etc. The code paragraph style has the following properties:
The keyword character style changes the font to Arial and the font face to bold and the color to dark yellow, var changes the color to green and finally literal changes the color to blue. When you type in code you start by selecting the code paragraph style and the type your code using soft line breaks (shift+newline), and tab for indentation (reset the option insert and backspace set left indent on tools -> options … -> edit sheet). Timesaver: Then double-click one of the keywords and select the keyword character style. With that keyword still selected double-click the format painter (the paintbrush in the toolbar), and then single-click all your keywords one at the time. When all your keywords have been painted press escape or click the format painter again. |
|
|