Octave style guide: Difference between revisions

→‎Formatting: add section about whitespace ported from the manual
(Start page describing our style guide for Octave code.)
 
(→‎Formatting: add section about whitespace ported from the manual)
Line 21: Line 21:
We use spaces for indentation. Absolutely do not use tabs in your code.
We use spaces for indentation. Absolutely do not use tabs in your code.
You should probably set your editor to emit spaces when you hit the tab key.
You should probably set your editor to emit spaces when you hit the tab key.
=== Whitespace ===
When calling functions, put spaces after commas and before the calling
parentheses, like this:
<pre>x = max (sin (y+3), 2);</pre>
An exception are matrix or cell constructors:
<pre>
[sin(x), cos(x)]
{sin(x), cos(x)}
</pre>
Here, putting spaces after {{codeline|sin}}}, {{codeline|cos}}} would result in a
parse error.
For indexing expressions, do ''not'' put a space after the
identifier (this differentiates indexing and function calls nicely).
The space after a comma is not necessary if index expressions are simple,
i.e., you may write
<pre>A(:,i,j)</pre>
but
<pre>A([1:i-1;i+1:n], XI(:,2:n-1))</pre>
When constructing matrices, prefer using the comma rather than the space to
distinguish between columns.
<pre>
  M = [1, 2, 3
      4, 5, 6];
</pre>
However, if the matrix is large or the indentation makes it clear the comma
may be dropped.
<pre>
  prices = [ 1.01  2.02  3.03
            44.04 55.05  6.06];
</pre>


== Naming ==
== Naming ==