Differences between revisions 5 and 26 (spanning 21 versions)
Revision 5 as of 2008-07-31 09:15:24
Size: 7814
Comment:
Revision 26 as of 2022-11-21 17:03:40
Size: 189
Comment: Page moved
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
#pragma section-numbers 2
Line 4: Line 3:
= Coding Standards for Dynare project =

This page describes conventions which should be respected in Dynare source code.

[[TableOfContents]]

== 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.

See [#Emacs Emacs section] on how to replace tabs with spaces under Emacs.

=== Newline convention ===

For historical reasons, there exists two ways of marking the end of a line in a text file:
 * with a linefeed (LF) character, which is the UNIX convention,
 * with a carriage return plus a linefeed (CR+LF), which is the DOS convention.
This is the reason why, when opening a DOS text file under UNIX, you may see "^M" characters (CR) at end of lines.
However, this won't happen if you're using Emacs, since it is able to autodetect the convention used in the file.

The preferred convention for Dynare source code is UNIX convention.

Here is the default convention used by several editors when creating a new file:
 * Emacs always uses UNIX convention
 * Matlab editor uses UNIX convention under Linux, but DOS convention under Windows

For more on the newline subject, and in particular on how to convert a file from one convention to another see [http://en.wikipedia.org/wiki/Newline Wikipedia article on Newline]. It is also possible to make the conversion with Emacs, see [#Emacs Emacs section].

''Note'': when converting a file present in the SVN repository from one convention to another, all lines will be marked as changed when comitting the new revision.

== C++ code ==

=== Coding style ===

The preferred coding style for Dynare C++ source code is the [http://www.gnu.org/prep/standards/html_node/Formatting.html#Formatting GNU Coding style].

'''''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(void *a);
private:
  void bar();
};
}}}
The keywords {{{public}}} and {{{private}}} should be on the same column than 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 column zero.

See also the [http://www.gnu.org/prep/standards/html_node/Formatting.html#Formatting GNU Coding style] on how long lines should be broken, and other matters.

=== File naming conventions ===

C++ source files should be named with the extension {{{.cc}}}, and C++ headers with {{{.hh}}}, so that Emacs automatically enters the C++ mode when opening them.

Extensions {{{.c}}} and {{{.h}}} should only be used for pure C files.

=== Copyright notice ===

You should add the following notice at the very beginning of your all your source files (even headers), after having replaced "2008" by the correct year(s):

{{{
/*
 * Copyright (C) 2008 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 <http://www.gnu.org/licenses/>.
 */
}}}

== Matlab/Octave code ==

The basic indentation level is set to 4 spaces.

[[Anchor(Emacs)]]
== Emacs howtos ==

=== Spaces instead of tabs ===

To ensure that Emacs uses spaces instead of tabs for indentation, you should set the variable {{{indent-tabs-mode}}} to {{{nil}}}.

This can be done in your {{{.emacs}}}, or via the configuration interface, via the ''Indent Tabs Mode'' option in the ''Editing->Indent group''.

To replace tabs by spaces in the current buffer, use the command {{{untabify}}}: select the whole buffer with {{{Ctrl-x h}}}, then run the command with {{{
Alt-x <Return> untabify <Return>
}}}
The number of spaces used to replace the tabs is set by the {{{tab-width}}} option (defaults to 8, found in ''Editing->Editing Basics'' configuration group).

=== Newline convention ===

While editing a file, Emacs indicates in the ''mode line'' which newline convention is used in the current buffer. The ''mode line'' is located on the lower left corner of the buffer window, and contains a colon "{{{:}}}" when the UNIX convention is used, or contains "{{{(DOS)}}}" when the DOS convention is used (for more on this subject, see the [http://www.gnu.org/software/emacs/manual/html_node/Mode-Line.html#Mode-Line Emacs manual section on the mode line]).

Here is the procedure to convert a file: suppose you are editing a file in DOS convention (indicated by {{{(DOS)}}} in the mode line). Then type: {{{
Ctrl-x <Return> f unix <Return>}}} to convert the buffer in UNIX convention (the mode line should now display a colon "{{{:}}}"). Then save it via {{{Ctrl-x Ctrl-s}}}.

=== C++ code ===

No special configuration is required (unless you have customized your Emacs), since GNU coding style is the default for Emacs.

Emacs can be used to reindent code which doesn't follow the right coding style. Select the whole buffer with {{{Ctrl-x h}}}, and then run:
{{{
Alt-x indent-region <Return>
}}}
This will replace tabs with spaces, and use right indentation levels, but many conventions of the GNU Coding Style will not be enforced though. In particular, braces not placed on a line of their own will not be moved to a newline.

=== Matlab code ===

There exists an Emacs package for colorization and indentation of Matlab code. Under Debian, it is contained in the {{{emacs-goodies-el}}} package. For other systems, you can download it from [http://matlab-emacs.sourceforge.net/ Matlab-emacs home page] and you then have to install the {{{.el}}} file in the right location.

You should then add the following lines to your {{{.emacs}}} initialization file:

{{{
;; Matlab mode
(autoload 'matlab-mode "matlab" "Matlab Editing Mode" t)
(setq auto-mode-alist (cons '("\\.m\\'" . matlab-mode) auto-mode-alist))
(autoload 'matlab-shell "matlab" "Interactive Matlab mode." t)
(setq matlab-indent-level 4)
}}}

The default indentation level in Matlab mode is set to 2 spaces and we want 4. -- MichelJuillard [[DateTime(2008-02-03T17:29:42Z)]]

The mode allows to run a Matlab session inside an Emacs buffer, and has many other interesting features.
This page moved to https://git.dynare.org/Dynare/dynare/-/wikis/CodingGuidelines

DynareWiki: CodingStandards (last edited 2022-11-21 17:03:40 by SébastienVillemot)