Octave style guide: Difference between revisions

1,271 bytes added ,  21 August 2016
add more details about syntax imported from the contribution guidelines in the manual
(→‎Formatting: add section about whitespace ported from the manual)
(add more details about syntax imported from the contribution guidelines in the manual)
Line 67: Line 67:


== Naming ==
== Naming ==
Use lowercase names if possible.  Uppercase is acceptable for variable
names consisting of 1-2 letters.  Do not use mixed case names.  Function
names must be lowercase.  Function names are global, so choose them
wisely.


=== General naming functions ===
=== General naming functions ===
Line 85: Line 90:


=== Variable names ===
=== Variable names ===
== ending blocks ==
Always use a specific end-of-block statement (like {{codeline|endif}},
{{codeline|endswitch}}) rather than the generic {{codeline|end}}.
Enclose the condition of an {{codeline|if}}, {{codeline|while}}, {{codeline|until}}, or
{{codeline|switch}} statement in parentheses, as in C:
<pre>
if (isvector (a))
  s = sum (a);
endif
</pre>
Do not do this, however, with the iteration counter portion of a {{codeline|for}}
statement.  Write:
<pre>
for i = 1:n
  b(i) = sum (a(:,i));
endfor
</pre>
== ! operator ==
The Octave operator {{codeline|!}} should be used for logical negation, rather than
{{codeline|~}}.  The negation operator is written with a space between the operator
and its target, e.g., {{codeline|! A}}.




Line 105: Line 139:
Do not comment code out.  If the code is no longer used, remove it.  We use
Do not comment code out.  If the code is no longer used, remove it.  We use
version control, we can always bring it back.
version control, we can always bring it back.
=== %! for test and demo blocks ===
Any demos or Built-In Self Tests (BIST) using the {{codeline|%!demo}} or
{{codeline|%!test}} syntax should begin two lines after the {{codeline|endfunction}}
keyword.  Demo blocks should be listed before test blocks.
See the section Writing tests on the [[Tests]] page.