Octave style guide: Difference between revisions

Jump to navigation Jump to search
1,125 bytes added ,  21 January 2020
(→‎Quoted Strings: new section)
(4 intermediate revisions by 2 users not shown)
Line 27: Line 27:
parentheses, like this:
parentheses, like this:


<pre>x = max (sin (y+3), 2);</pre>
<pre>x = max (sin (y + 3), 2);</pre>


An exception are matrix or cell constructors:
An exception are matrix or cell constructors:
Line 64: Line 64:
   prices = [ 1.01  2.02  3.03
   prices = [ 1.01  2.02  3.03
             44.04 55.05  6.06];
             44.04 55.05  6.06];
</pre>
Do include spaces around all binary arithmetic operators, for example
<pre>
  x = 1 / (1 + y) ^ 2;
</pre>
An exception is for extremely simple expressions like <pre>n+1</pre>, in
particular when used as an argument to a function or as part of an indexing
expression. For example, you may write
<pre>
  x(1:end-1)
</pre>
Another exception is for complex arithmetic expressions. It may improve
readability to omit spaces around higher precedence operators, for example
<pre>
  z = cat (dim, (x2.*y3 - x3.*y2), (x3.*y1 - x1.*y3), (x1.*y2 - x2.*y1));
</pre>
</pre>


Line 103: Line 124:


=== Variable names ===
=== Variable names ===
Avoid reusing the names of other functions as local variable names.  For
example, try to avoid naming local variables {{codeline|abs}},
{{codeline|log}}, or {{codeline|pow}}.  These functions may be used in a
later change and may lead to confusing errors.
An exception is the use of {{codeline|i}} and {{codeline|j}} as loop indices.
If a function has nothing to do with complex arithmetic, it is common and
acceptable to use {{codeline|i}} and {{codeline|j}} as local variables in
for loops.


== Quoted Strings ==
== Quoted Strings ==
Line 159: Line 190:
== ! operator ==
== ! operator ==


The Octave operator {{codeline|!}} should be used for logical negation, rather than
* The Octave operator <code>!</code> should be used for logical negation, rather than <code>~</code>.
{{codeline|~}}. The negation operator is written with a space between the operator
* The negation operator is written with a space between the operator and its target, e.g., <code>! A</code>.
and its target, e.g., {{codeline|! A}}.
* For comparisons use <code>!=</code> instead of <code>~=</code>.
 


== Comments ==
== Comments ==

Navigation menu