Skip to content

Coding conventions

David Legland edited this page Jun 22, 2018 · 6 revisions

Some coding conventions for the MatGeom project.

Table of Contents

File names

  • no limitation in the size of file names
  • extension is ".m".

Functions

  • Function names use lower camel case: start with a lower case, words are separated by upper case. Example: `myTinyFunction.m`
  • Function names may use a prefix to better identify a functional group. Example: `meshXXX` indicates a function operating on a mesh.
  • Function names should start by verbs. Examples: `drawPoint`, `clipPolygon`, `smoothMesh`... When the function consists in computing a property from a data structure, a name ccan be used. Example: `polygonArea`, `meshCentroid`

Classes

(not used for the moment, but in case of future use)

  • Class names use upper camel case: MyClassName.
  • Class methods are either verbs, or substattive, to avoid repetitive "getXXX" functions.

Test functions

When performing unit tests, the convention is to prefix the name of the function under test by the prefix `test_`. This allows to include the name of the original function without case modification.

Code

The code should be rather "light". Some general guidelines:

  • one instruction per line if possible
  • one space before and after affectation mark "="
  • one space before and after conditional marks: "==", "~=", ">", "<="...
  • parens may be avoided when possible
  • no space between function name and opening paren

Punctuations

  • semicolons are not necessary after "end" statements
  • semicolons are welcome after "return" statement (this is an instruction)

Structuration of code

It is a good practice to write "functional blocs", composed of several lines, that focus on a specific task, together with a small comment describing the task.

Control structures (for-loops, conditional tests...) are indented with four spaces. Tabulations have to be avoided, as they appear differently depending on the editors.

It is better to avoid writing test or for-loops in a single line.

Example:

    % Data initialisation
    data = zeros(1, N);
    for i = 1:N
        value = getInitValue(i);
        data(i) = computeNewValue(value);
    end

Function headers

General guidelines

The first line contains the function declaration.

The second line is a comment that summarizes the function:

  • first contains function name in upper case (to facilitate inclusion in Contents report)
  • then contains a short summary of the function, starting with an infinite verb, and starting with an upper case digit (again, to homogeneize Contents.m files)
The remaining header contains a description of the function with one or more syntax(es). For each syntax, it is advised to give a typical command line, then the explanations after a line break. Explanations uses conjugate verbs. Input and output arguments may be given in upper case, to facilitate their reading.

It is useful to give one or two (or more!) example use case.

It is also useful to complete the "See Also" section.

Example

Sample header for a function called "demoFunction":

    function res = demoFunction(x, n)
    %DEMOFUNCTION Compute a sample function of x and n
    %
    % RES = demoFunction(X, N)
    % Computes the N-th power of the number X.
    %
    % RES = demoFunction(X, N, METHOD)
    % Specifies the method to use. METHOD can be one of:
    % 'normal': use the normal method
    % 'optimized': use the optimized method

Help files

Some files are used to gather information about functions that work on similar data structures, and can be seen as "sub-sections" of the Contents.m located in the same directory. For example, the `lines2d.m` describes functions operating on straight lines, rays and line segments.

For these files, as well as the `Contents.m` files, it is useful to add a command `help(mfilename)`. Then the help is displayed if ones tries to run the file.

Links

Clone this wiki locally