-
Notifications
You must be signed in to change notification settings - Fork 95
Coding Style
This page defines the global coding style and Python and C coding styles. C++ coding style is defined in a separate page.
When creating new code, prefer C to C++ programming language, except if a C++ dependency is required or if a complex object oriented design, including inheritance and other C++ features are strictly needed. C++ has a great complexity overhead over C that should be avoided as much as possible. Even with a C++ project, prefer a C public interface.
Don't complexify algorithms or data structure for optimization reasons. Optimization should take place at the very stage of development, only after the bottlenecks in a standard use case has been identified.
Don't declare or implement unused variables or functions, under the pretext they could be used later. Implement them only when you need them.
Use global static variables and static functions (C/C++) wherever needed to avoid polluting the global namespace.
The name of a function or variable should be sufficiently explicit to avoid a descriptive comment.
Don't use abbreviations. Exceptions are: n
for "number of", init
for "initialize", max
for "maximum" and min
for "minimum". C++ example:
computeAverage(); // NOT: compAvg();
C++ example:
exportHtmlSource(); // NOT: exportHTMLSource();
C example:
open_dvd_player(); // NOT: open_DVD_player();
Don't comment unless strictly needed.
Source code must not be commented out without an explanation. If commented out, the explanation must include one of the following keywords (allcaps) TODO:
or FIXME:
, so that we can easily find out and fix this later.
The only exception to this rule is commenting out debug statements, for example in C:
// printf(stderr, "n_camera = %d\n", n_camera);
An object definition (header file in C/C++ or source file in other languages for an object, interface, module, etc.) should include a short comment describing what is the purpose of this object.
In headers of files, comments do not include:
- copyright (it is redundant with the general copyright of Webots)
- author (it is maybe true for the first commit, but it becomes very quickly obsolete once someone changes the file)
- modifications (it is difficult to maintain and it is redundant with the change log)
- file name (it is redundant with the file name)
- date (we don' care about it)
When not specified otherwise by our coding style rules, use the PEP 8 standard.
Note: using Atom with the linter-flake8 linter package ensures that we respect our Python coding styles: apm install linter-flake8
.
Line size is limited to 128 characters (instead of 80 in PEP8).
We don't force PEP257 comments on all functions (especially, simple constructors, getters, setters, etc.) and therefore don't use the flake8-docstrings
Atom package. However, we strongly encourage to respect this rule when it makes sense.
If not specified otherwise by our coding style, use the C++ coding style
Variables and functions names use the underscore_case
notation (for type names use #CS301.
Preprocessor constants and macros names use the UPPERCASE_UNDERSCORE_CASE
notation.