wiki:coding_rules

Version 7 (modified by Mathieu Morlighem, 9 years ago) ( diff )

--

General guidelines

  • comment your code (everybody must understand what is being done)
  • NEVER more than one blank line please!
  • Align operators vertically to emphasize local program structure and semantics when possible
  • Do not use excessive blank spaces (especially in equations)

C/C++

  • if/for should follow this:
    • no space fetween if/for and its statement
    • If an if/for holds on one line, then do not use brackets
    • Otherwise, use brackets
      for(int i=0<i<n;i++)  A[i]=i;
      
      for(int i=0<i<n;i++){
         A[i]=B[i];
         B[i]=0;
      }
      
      if(a==0) bool=true;
      
      if(a==0)
         bool=true;
      else if(a==1)
         bool=flase;
      else
         _error_("a=%g not supported",a);
      
      if(a==0){
          output=true;
          c=b
      }
      else{
          output=false
          c=a;
      }
      
  • Comments should follow the code indentation and there should not be any blank line between a comment and the code it is referring to
       /*Assigning values of A*/
       for(int i=0<i<n;i++){
    
          /*The comment here is indented*/
          A[i]=i;
       }
    
  • Function declaration should hold on one line only
    bool Test(int a,double b,char* c){
    

Matlab

  • All matlab routines should start with a help (Example and See Also are not mandatory):
    function outputs=FunctionName(inputs)
    %FUNCTIONNAME - one line description
    %
    %   Extensive description of what is being done, inputs
    %   outputs, etc...
    %
    %   Usage:
    %      outputs=FunctionName(inputs)
    %
    %   Example:
    %      md.test=FunctionName(1);
    %
    %   See Also:
    %      FunctionName2, FunctionName3, ...
    

At the very least, the first line and the Usage should be provided. Use indentations of 3 and 6 spaces. Example:

function outputs=hello()
%HELLO - prints hello to the screen
%
%   Usage:
%      outputs=hello()

Variable/Enum/Functions Names

  • variables should not use capital letters. Use underscores to make variables more understandable.
  • Function names and enums should not use any underscore. Use capital letters to make names more understandable.

Example:

  Input* vx_input=GetInput(inputs,VxInput);
Note: See TracWiki for help on using the wiki.