This page describes guidelines which should be followed when contributing code to Dynare.
This page describes guidelines which should be followed when contributing code to Dynare.
# General
# General
## Spaces instead of tabs
## Spaces instead of tabs
Source code should not contain tabulation characters. Use spaces instead.
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.
This makes the indentation look the same on all machines, whatever the tabulation width is configured to.
## Newline convention
## 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).
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).
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.
*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
## 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).
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
## Comments
Abundantly comment your code (unless it is self-explanatory).
Abundantly comment your code (unless it is self-explanatory).
## Documentation
## Documentation
Whenever relevant, for example an option or command is added or its behaviour is modified, the reference manual should be updated.
Whenever relevant, for example an option or command is added or its behaviour is modified, the reference manual should be updated.
Whenever a new feature is added, the [new features page](NewFeatures) should be updated.
Whenever a new feature is added, the [new features page](NewFeatures) should be updated.
Whenever a bug is fixed, that is present in the stable branch, the [known bugs page](Known-bugs-present-in-the-current-stable-version) should be updated, and the bugfix should be backported to the stable branch.
Whenever a bug is fixed, that is present in the stable branch, the [known bugs page](Known-bugs-present-in-the-current-stable-version) should be updated, and the bugfix should be backported to the stable branch.
## Testing
## Testing
As much as possible, integration and unit tests should be added. See
As much as possible, integration and unit tests should be added. See
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.
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.
% Dynare is free software: you can redistribute it and/or modify
% Dynare is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% (at your option) any later version.
%
%
% Dynare is distributed in the hope that it will be useful,
% Dynare is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU General Public License for more details.
% GNU General Public License for more details.
%
%
% You should have received a copy of the GNU General Public License
% You should have received a copy of the GNU General Public License
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
% along with Dynare. If not, see <https://www.gnu.org/licenses/>.
```
```
## Short-circuit versus Element-wise logical AND/OR
## 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.
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
## 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.
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 code
## Fortran standard
## Fortran standard
We follow the Fortran 2018 standard. However, features not available in gfortran 10 should not be used.
We follow the Fortran 2018 standard. However, features not available in gfortran 10 should not be used.
## Filename extension
## Filename extension
Use the `.f08` filename extension for Fortran source code (or the `.F08` extension if the source file needs to be preprocessed).
Use the `.f08` filename extension for Fortran source code (or the `.F08` extension if the source file needs to be preprocessed).
## Disabling implicit typing and implicit procedure declaration
## Disabling implicit typing and implicit procedure declaration
The following statement should be added to every program unit (module, main program, external subprogram), to disable implicit typing and implicit procedure declaration:
The following statement should be added to every program unit (module, main program, external subprogram), to disable implicit typing and implicit procedure declaration:
```
```
implicit none (type, external)
implicit none (type, external)
```
```
It can also be a good idea to add `implicit none` within interface blocks, when there are a lot of dummy arguments.
It can also be a good idea to add `implicit none` within interface blocks, when there are a lot of dummy arguments.
## Indentation
## Indentation
We follow the indentation rules applied by Emacs.
We follow the indentation rules applied by Emacs.
## Copyright notice
## Copyright notice
You should add the following notice in the header of your all your source files (possibly updating the year):
You should add the following notice in the header of your all your source files (possibly updating the year):
! Dynare is free software: you can redistribute it and/or modify
! Dynare is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
! (at your option) any later version.
!
!
! Dynare is distributed in the hope that it will be useful,
! Dynare is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
! GNU General Public License for more details.
!
!
! You should have received a copy of the GNU General Public License
! You should have received a copy of the GNU General Public License
! along with Dynare. If not, see <https://www.gnu.org/licenses/>.
! along with Dynare. If not, see <https://www.gnu.org/licenses/>.
```
```
# C++ code
# C++ code
## C++ standard
## 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).
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).
NB: The GCC version restriction is enforced in `meson.build`.
NB: The GCC version restriction is enforced in `meson.build`.
## Coding guidelines
## Coding guidelines
As much as possible, one should follow the [C++ Core Guidelines](https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines).
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:
However note that we don’t apply the following rules:
* SF.1: we rather use `.cc` and `.hh` as file extensions
* 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))
* 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
## Coding style
The coding style for Dynare C++ source code is described by the following
The coding style for Dynare C++ source code is described by the following