<html lang="en"> <head> <title>C++ Sources - GNU Octave</title> <meta http-equiv="Content-Type" content="text/html"> <meta name="description" content="GNU Octave"> <meta name="generator" content="makeinfo 4.13"> <link title="Top" rel="start" href="index.html#Top"> <link rel="up" href="Contributing-Guidelines.html#Contributing-Guidelines" title="Contributing Guidelines"> <link rel="prev" href="Octave-Sources-_0028m_002dfiles_0029.html#Octave-Sources-_0028m_002dfiles_0029" title="Octave Sources (m-files)"> <link rel="next" href="Other-Sources.html#Other-Sources" title="Other Sources"> <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> <meta http-equiv="Content-Style-Type" content="text/css"> <style type="text/css"><!-- pre.display { font-family:inherit } pre.format { font-family:inherit } pre.smalldisplay { font-family:inherit; font-size:smaller } pre.smallformat { font-family:inherit; font-size:smaller } pre.smallexample { font-size:smaller } pre.smalllisp { font-size:smaller } span.sc { font-variant:small-caps } span.roman { font-family:serif; font-weight:normal; } span.sansserif { font-family:sans-serif; font-weight:normal; } --></style> </head> <body> <div class="node"> <a name="C++-Sources"></a> <a name="C_002b_002b-Sources"></a> <p> Next: <a rel="next" accesskey="n" href="Other-Sources.html#Other-Sources">Other Sources</a>, Previous: <a rel="previous" accesskey="p" href="Octave-Sources-_0028m_002dfiles_0029.html#Octave-Sources-_0028m_002dfiles_0029">Octave Sources (m-files)</a>, Up: <a rel="up" accesskey="u" href="Contributing-Guidelines.html#Contributing-Guidelines">Contributing Guidelines</a> <hr> </div> <h3 class="section">D.6 C++ Sources</h3> <p>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: <pre class="example"> static bool matches_patterns (const string_vector& patterns, int pat_idx, int num_pat, const std::string& name) </pre> <p class="noindent">The function name 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 after the left open parenthesis and after commas, for both function definitions and function calls. <p>Recommended indent is 2 spaces. When indenting, indent the statement after control structures (like <code>if</code>, <code>while</code>, etc.). If there is a compound statement, indent <em>both</em> the curly braces and the body of the statement (so that the body gets indented by <em>two</em> indents). Example: <pre class="example"> if (have_args) { idx.push_back (first_args); have_args = false; } else idx.push_back (make_value_list (*p_args, *p_arg_nm, &tmp)); </pre> <p class="noindent">If you have nested <code>if</code> statements, use extra braces for extra clarification. <p>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: <pre class="example"> SVD::type type = ((nargout == 0 || nargout == 1) ? SVD::sigma_only : (nargin == 2) ? SVD::economy : SVD::std); </pre> <p class="noindent">Consider putting extra braces around a multiline 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. <p>Declare variables just before they're needed. Use local variables of blocks—it helps optimization. Don't write 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: <pre class="example"> octave_value retval; octave_idx_type nr = b.rows (); octave_idx_type nc = b.cols (); double d1, d2; </pre> <p>Use lowercase names if possible. Uppercase is acceptable for variable names consisting of 1-2 letters. Do not use mixed case names. <p>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 <code>return</code> statements—use nested <code>if</code> statements if possible. </body></html>