Skip to content

Code conventions

Mani Chandra edited this page Jun 17, 2015 · 2 revisions
In order for the code to be readable and maintainable, we request that the code adhere to the following standards:
  1. Indents should be made with 2 tab spaces. For emacs, set:
;; make indentation commands use space only (never tab character)
(setq indent-tabs-mode nil)

;; set default tab char's display width to 2 spaces
(setq tab-width 2)

To make default, change setq to setq-default.

  1. Variables names : first word starts with a small letter, subsequest words start with capital letters : variableName, do not use variable_name or VariableName

  2. Function names : same as variable names : functionName, do not use function_name or FunctionName

  3. Macro names : capitalize every letter and seperate words with an underscore : MACRO_NAME, do not use MACRONAME

  4. Braces should follow the convention:

for ()
{
  if ()
  {

  }
  else
  {

  }
}
  1. Comments should always be made with /* */

  2. Do not exceed 80 columns in width.

  3. Long formulas are written as follows:

  elem->gamma = 
    sqrt(1 + geom->gCov[1][1]*elem->primVars[U1]*elem->primVars[U1]
           + geom->gCov[2][2]*elem->primVars[U2]*elem->primVars[U2] 
           + geom->gCov[3][3]*elem->primVars[U3]*elem->primVars[U3]

         + 2*(  geom->gCov[1][2]*elem->primVars[U1]*elem->primVars[U2]
              + geom->gCov[1][3]*elem->primVars[U1]*elem->primVars[U3] 
              + geom->gCov[2][3]*elem->primVars[U2]*elem->primVars[U3]
             )
        );

In particular, notice that

  • All the brackets at a particular depth of the equation are aligned vertically.
  • Mathematical signs are put at the beginning of the intermediate code.
  1. All the functions should have a comment above them describing the input, output and what they do, along with the author.

  2. Functions that implement nontrivial formulas should reference which publications they are picked out from. For example, the function gCovFunc() has the following description above the function definition

/* The covariant metric in X^mu = {t, X1, X2, phi} coordinates. Inside the
 * function, the metric is first defined in the x^mu Kerr-Schild coordinates and
 * then transformed into the X^mu coordinates using:
 *
 * ds^2 = g_mu_nu dx^mu dx^nu
 *      = (g_mu_nu (dx^mu/dX^alpha) (dx^nu/dX^beta)) dX^alpha dX^beta
 *      = g_alpha_beta dX^alpha dX^beta
 *
 *  Ref: "A Measurement of the Electromagnetic Luminosity of a Kerr Black Hole"
 *       Jonathan C. McKinney and Charles F. Gammie
 *   
 * This function is not directly called by the user. All geometrical quantities
 * are set by calling setGeometry(). 
 *
 * @param Input: X, X^mu coordinates REAL X[NDIM] array
 * @param Output: gCov, The covariant metric, REAL gCov[NDIM][NDIM] array
*/