Next: , Previous: , Up: Contributing Guidelines   [Contents][Index]


D.6 C++ Sources

Don’t use tabs. Tabs cause trouble. If you are used to them, set up your editor so that it converts tabs to spaces.

Format function headers like this:

static bool
matches_patterns (const string_vector& patterns, int pat_idx,
                  int num_pat, const std::string& name)

The return type of the function and any modifiers are specified on the first line. The function name on the second line should start in column 1, and multi-line argument lists should be aligned on the first char after the open parenthesis. You should put a space before the left open parenthesis and after commas, for both function definitions and function calls.

The recommended indent is 2 spaces. When indenting, indent the statement after control structures (like if, while, etc.). If there is a compound statement, indent both the curly braces and the body of the statement (so that the body gets indented by two indents). This format is known as "GNU style" and is an option for some code formatting tools. Example indenting:

if (have_args)
  {
    idx.push_back (first_args);
    have_args = false;
  }
else
  idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp));

If you have nested if statements, use extra braces for extra clarification.

Split long expressions in such a way that a continuation line starts with an operator rather than identifier. If the split occurs inside braces, continuation should be aligned with the first char after the innermost braces enclosing the split. Example:

SVD::type type = ((nargout == 0 || nargout == 1)
                  ? SVD::sigma_only
                  : (nargin == 2) ? SVD::economy : SVD::std);

Consider putting extra braces around a multi-line expression to make it more readable, even if they are not necessary. Also, do not hesitate to put extra braces anywhere if it improves clarity.

The negation operator is written with a space between the operator and its target, e.g., ! A.

Declare variables just before they are needed. Use variables local to blocks—it helps optimization. Don’t write a multi-line variable declaration with a single type specification and multiple variables. If the variables don’t fit on single line, repeat the type specification. Example:

octave_value retval;

octave_idx_type nr = b.rows ();
octave_idx_type nc = b.cols ();

double d1, d2;

Use lowercase names if possible. Uppercase is acceptable for variable names consisting of 1-2 letters. Do not use mixed case names.

Use Octave’s types and classes if possible. Otherwise, use the C++ standard library. Use of STL containers and algorithms is encouraged. Use templates wisely to reduce code duplication.

Avoid comma expressions, labels and gotos, and explicit typecasts. If you need to typecast, use the modern C++ casting operators. In functions, minimize the number of return statements, but elimination of all but one return is not required.

When an empty string is required, use "", rather than creating an empty string object with std::string ().


Next: , Previous: , Up: Contributing Guidelines   [Contents][Index]