1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
|
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU Octave: C++ Sources</title>
<meta name="description" content="GNU Octave: C++ Sources">
<meta name="keywords" content="GNU Octave: C++ Sources">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="Contributing-Guidelines.html#Contributing-Guidelines" rel="up" title="Contributing Guidelines">
<link href="Other-Sources.html#Other-Sources" rel="next" title="Other Sources">
<link href="Octave-Sources-_0028m_002dfiles_0029.html#Octave-Sources-_0028m_002dfiles_0029" rel="prev" title="Octave Sources (m-files)">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.indentedblock {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
div.smalllisp {margin-left: 3.2em}
kbd {font-style:oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space:nowrap}
span.nolinebreak {white-space:nowrap}
span.roman {font-family:serif; font-weight:normal}
span.sansserif {font-family:sans-serif; font-weight:normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
<a name="C_002b_002b-Sources"></a>
<div class="header">
<p>
Next: <a href="Other-Sources.html#Other-Sources" accesskey="n" rel="next">Other Sources</a>, Previous: <a href="Octave-Sources-_0028m_002dfiles_0029.html#Octave-Sources-_0028m_002dfiles_0029" accesskey="p" rel="prev">Octave Sources (m-files)</a>, Up: <a href="Contributing-Guidelines.html#Contributing-Guidelines" accesskey="u" rel="up">Contributing Guidelines</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<hr>
<a name="C_002b_002b-Sources-1"></a>
<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:
</p>
<div class="example">
<pre class="example">static bool
matches_patterns (const string_vector& patterns, int pat_idx,
int num_pat, const std::string& name)
</pre></div>
<p>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 before the left open parenthesis and after commas,
for both function definitions and function calls.
</p>
<p>The 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:
</p>
<div class="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></div>
<p>If you have nested <code>if</code> statements, use extra braces for extra
clarification.
</p>
<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:
</p>
<div class="example">
<pre class="example">SVD::type type = ((nargout == 0 || nargout == 1)
? SVD::sigma_only
: (nargin == 2) ? SVD::economy : SVD::std);
</pre></div>
<p>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.
</p>
<p>Declare variables just before they are needed. Use local variables of
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:
</p>
<div class="example">
<pre class="example">octave_value retval;
octave_idx_type nr = b.rows ();
octave_idx_type nc = b.cols ();
double d1, d2;
</pre></div>
<p>Use lowercase names if possible. Uppercase is acceptable for variable
names consisting of 1-2 letters. Do not use mixed case names.
</p>
<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.
</p>
<hr>
<div class="header">
<p>
Next: <a href="Other-Sources.html#Other-Sources" accesskey="n" rel="next">Other Sources</a>, Previous: <a href="Octave-Sources-_0028m_002dfiles_0029.html#Octave-Sources-_0028m_002dfiles_0029" accesskey="p" rel="prev">Octave Sources (m-files)</a>, Up: <a href="Contributing-Guidelines.html#Contributing-Guidelines" accesskey="u" rel="up">Contributing Guidelines</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|