SDA SE Wiki

Software Engineering for Smart Data Analytics & Smart Data Analytics for Software Engineering

User Tools

Site Tools


Predicate names, formal:

  • Lower case letters with words delimited by underscore ('_').
  • Like this: ast_node, ast_node_type
  • Don't use: astNode, ast_Node, ASTnode, AST_node
  • Why: Consistency with standard (SWI-) Prolog notation.

Predicate names, semantic:

  • Choose attributes for predicate names. yellow(B), connected(A, B)
  • Verbs in singular indicative mode are also ok. follows(A, B), knows(Person, Theory). Use plural only if the predicate says something about more than one thing, e.g. about a list. include(List, Sublist) but includes(List, Element)
  • Nouns that characterize relations may also be acceptable. Be aware that a noun generally sounds “symmetric” while the most predicates are not symmetric. dependency(A, B)
  • Don't use: Imperative forms. (We want to think declarative!)

Predicates intended for "private" use only:

  • Name ends with one underscore ('_').
  • Like this: ast_node_, ast_node_type_
  • Don't use: ast_node_private
  • Why: Easier understanding of listings.

Dynamic Predicates

  • Always declare dynamic predictes
  • Like this: :- dynamic dyn_pred/1.
  • Why: Undefined predicates lead to exceptions at runtime. If you query a not declared dynamic predicate and no clause/fact was added you will receive an exception instead of a silent fail.

Predicates processing lists / sets of elements:

  • Name ends with 's' (or 's_' if private).
  • Like this: check_statements(Pred, Statements)
  • Don't use: check_statement_List(Pred, StatementList)
  • Why: Says the same as the '_list' suffix but is easier (shorter) to write and read.
  • Idiom: Often use a recursive predicate to process lists, which calls a predicate with the same name except for the trailing 's' to process list elements:
              check_statements(Pred, []).
              check_statements(Pred, [Head|Tail]) :-
                  check_statement(Pred, Head),
                  check_statements(Pred, Tail).
    
              check_statement(Pred, Head) :-         ...
  • Note: If possible predicate names should sound like predicates and not like commands. So maybe statements_fulfill is more appropriate than check_statments.

Variables:

  • Use Java-style but start with underscore or capital letter.
  • Like this: _aVeryLongVarName, AnotherVeryLongName
  • Don't use: a_very_long_var_name
  • Why: Shorter and easier to write than the underscore variant. The leading underscore or capital letter are required by Prolog to identify variables.

Comments:

  • Use javadoc comments for things to include in Prologdoc and
  • or % … comments for commenting out code or adding implementation comments.

Line breaking:

  • One predicate per line. If argument list too long, put one argument in every line with matching brackets in same column.
  • Like this:
            mortal(X) :-
               alive(X).
    
            example_with_long_parameter_list :-
               findall( X,
                        (long(Z, X), conjunction(X), of(X, Y), predicates(X)),
                        Result
                       ),
               next_predicate(Result).
  • Don't use:
            mortal(X):-alive(X).
    
            example_with_long_parameter_list :-
               findall( X, (long(Z,X), conjunction(X), of(X,Y),            
    	   predicates(X)),Result),
               next_predicate(Result).

Spaces:

  • A “,”, “;”, “.”, “)” is followed by a “ ”, despite on of these characters follows.
  • Extra space after “(” is allowed.
  • Don't use tabs. Use spaces.
teaching/labs/xp/2004b/codingconventionsprolog.txt · Last modified: 2018/05/09 01:59 (external edit)

SEWiki, © 2020