|
|
|
This page describes guidelines which should be followed when contributing code to Dynare.
|
|
|
|
|
|
|
|
# General conventions
|
|
|
|
|
|
|
|
## Spaces instead of tabs
|
|
|
|
|
|
|
|
Source code should not contain tabulation characters. Use spaces instead.
|
|
|
|
|
|
|
|
This makes the indentation look the same on all machines, whatever the tabulation width is configured to.
|
|
|
|
|
|
|
|
## Newline convention
|
|
|
|
|
|
|
|
All source files should use the linefeed (LF) end of line convention (which is the default under macOS and GNU/Linux, but not under Windows).
|
|
|
|
|
|
|
|
For more on the newline subject, and in particular on how to convert a file from one convention to another see the [Wikipedia article on Newline](https://en.wikipedia.org/wiki/Newline).
|
|
|
|
|
|
|
|
*Note:* when converting a file present in the Git repository from one convention to another, all lines will be marked as changed when comitting the new revision.
|
|
|
|
|
|
|
|
## License and copyright
|
|
|
|
|
|
|
|
The copyright should be attributed to “Dynare Team”, and the license should be the GNU General Public License, version 3 or later (unless the code is borrowed from a third-party, in which case we must keep the pre-existing copyright notice and license).
|
|
|
|
|
|
|
|
## Comments
|
|
|
|
|
|
|
|
Abundantly comment your code (unless it is self-explanatory).
|
|
|
|
|
|
|
|
# MATLAB/Octave code
|
|
|
|
|
|
|
|
## Indentation
|
|
|
|
|
|
|
|
The basic indentation level is set to 4 spaces.
|
|
|
|
|
|
|
|
Function bodies should not be indented.
|
|
|
|
|
|
|
|
## Copyright notice
|
|
|
|
|
|
|
|
You should add the following notice in your files, just after the help text, but leaving a blank line between the help text and the copyright notice (so that the copyright notice doesn’t appear when one types `help function`). Don’t forget to update the year(s) if needed.
|
|
|
|
|
|
|
|
```
|
|
|
|
% Copyright © 2022 Dynare Team
|
|
|
|
%
|
|
|
|
% This file is part of Dynare.
|
|
|
|
%
|
|
|
|
% Dynare is free software: you can redistribute it and/or modify
|
|
|
|
% it under the terms of the GNU General Public License as published by
|
|
|
|
% the Free Software Foundation, either version 3 of the License, or
|
|
|
|
% (at your option) any later version.
|
|
|
|
%
|
|
|
|
% Dynare is distributed in the hope that it will be useful,
|
|
|
|
% but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
% GNU General Public License for more details.
|
|
|
|
%
|
|
|
|
% You should have received a copy of the GNU General Public License
|
|
|
|
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
```
|
|
|
|
|
|
|
|
# Short-circuit versus Element-wise logical AND/OR
|
|
|
|
|
|
|
|
The Short-circuit logical AND/OR (*i.e.* `&&`, `||`), returns `true` or `false`, depending on whether an entire expression evaluates to true or false. It returns this value as soon as it is known. On the other hand, the Element-wise AND/OR (*i.e.* `&`, `|`) returns a logical array of the compared values. Even though MATLAB and Octave substitutes in short-circuit functionality when an element-wise AND/OR is encountered in an `if` or `while` statement, Octave issues a warning every time this situation is encountered. Hence, in Dynare, all conditional statements should make use of the short-circuit logical AND and OR.
|
|
|
|
|
|
|
|
## Recursive removal of sub-directories should be avoided
|
|
|
|
|
|
|
|
MATLAB/Octave offer the option `s` in function `rmdir`. This option requires to delete all inferior sub-directories. We consider this option to be extremely dangerous as we don’t want a possible Dynare bug to wipe out user’s entire directory trees. It shouldn’t be used. We always know the name of the directories that we are creating and we should remove them one by one when needed, not recursively.
|
|
|
|
|
|
|
|
# Fortran code
|
|
|
|
|
|
|
|
## Fortran standard
|
|
|
|
|
|
|
|
We follow the Fortran 2018 standard. However, features not available in gfortran 10 should not be used.
|
|
|
|
|
|
|
|
## Filename extension
|
|
|
|
|
|
|
|
Use the `.f08` filename extension for Fortran source code (or the `.F08` extension if the source file needs to be preprocessed).
|
|
|
|
|
|
|
|
## Indentation
|
|
|
|
|
|
|
|
We follow the indentation rules applied by Emacs.
|
|
|
|
|
|
|
|
## Copyright notice
|
|
|
|
|
|
|
|
You should add the following notice in the header of your all your source files (possibly updating the year):
|
|
|
|
|
|
|
|
```
|
|
|
|
! Copyright © 2022 Dynare Team
|
|
|
|
!
|
|
|
|
! This file is part of Dynare.
|
|
|
|
!
|
|
|
|
! Dynare is free software: you can redistribute it and/or modify
|
|
|
|
! it under the terms of the GNU General Public License as published by
|
|
|
|
! the Free Software Foundation, either version 3 of the License, or
|
|
|
|
! (at your option) any later version.
|
|
|
|
!
|
|
|
|
! Dynare is distributed in the hope that it will be useful,
|
|
|
|
! but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
! GNU General Public License for more details.
|
|
|
|
!
|
|
|
|
! You should have received a copy of the GNU General Public License
|
|
|
|
! along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
```
|
|
|
|
|
|
|
|
# C++ code
|
|
|
|
|
|
|
|
## C++ standard
|
|
|
|
|
|
|
|
We follow the C++20 standard. However, C++20 features not available in GCC 10 should not be used (see [this table](https://en.cppreference.com/w/cpp/compiler_support/20) to know in which GCC version a given feature was implemented).
|
|
|
|
|
|
|
|
## Coding guidelines
|
|
|
|
|
|
|
|
As much as possible, one should follow the [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines).
|
|
|
|
|
|
|
|
However note that we don’t apply the following rules:
|
|
|
|
* SF.1: we rather use `.cc` and `.hh` as file extensions
|
|
|
|
* C.12: we deliberately make some data members `const` (*e.g.* in [ComputingTasks.hh](https://git.dynare.org/Dynare/preprocessor/-/blob/master/src/ComputingTasks.hh))
|
|
|
|
|
|
|
|
## Coding style
|
|
|
|
|
|
|
|
The coding style for Dynare C++ source code is the [GNU Coding style](https://www.gnu.org/prep/standards/html_node/Formatting.html#Formatting).
|
|
|
|
|
|
|
|
*One exception is made to the GNU coding style:* in a function call or declaration, it is not necessary to insert a space between a function name and the parenthesis preceding the arguments. In GNU coding style, one should normally write `printf ("string")`, but in Dynare one should stick to `printf("string")`.
|
|
|
|
|
|
|
|
Here is a source code example for a class declaration:
|
|
|
|
```
|
|
|
|
class ABC
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int foo(string a);
|
|
|
|
private:
|
|
|
|
void bar();
|
|
|
|
};
|
|
|
|
```
|
|
|
|
The keywords `public` and `private` should be on the same column as the `class` keyword, and function declarations indented with two spaces.
|
|
|
|
|
|
|
|
Here is a source code example for a function body:
|
|
|
|
```
|
|
|
|
int
|
|
|
|
class_name::function_name(int arg1, int arg2)
|
|
|
|
{
|
|
|
|
if (x < foo(y, z))
|
|
|
|
haha = bar[4] + 5;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
while (z)
|
|
|
|
{
|
|
|
|
haha += foo(z, z);
|
|
|
|
z--;
|
|
|
|
}
|
|
|
|
return (++x + bar());
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
a = foo(a);
|
|
|
|
}
|
|
|
|
while (a > 0);
|
|
|
|
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'a':
|
|
|
|
return (0);
|
|
|
|
default:
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
The basic indentation level is of 2 spaces. Braces are always put on the following line, and are indented with 2 spaces (except for namespace, class and function declarations). Note that the function name should be placed on the first column.
|
|
|
|
|
|
|
|
## Copyright notice
|
|
|
|
|
|
|
|
You should add the following notice at the very beginning of your all your source files (possibly updating the year):
|
|
|
|
|
|
|
|
```
|
|
|
|
/*
|
|
|
|
* Copyright © 2022 Dynare Team
|
|
|
|
*
|
|
|
|
* This file is part of Dynare.
|
|
|
|
*
|
|
|
|
* Dynare is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Dynare is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with Dynare. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
```
|
|
|
|
|