Editing Octave style guide

Jump to navigation Jump to search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.

Latest revision Your text
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 124: Line 103:
  
 
=== 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 ==
 
 
Always use double quotes for strings and characters rather than the Matlab single quote convention. Both quote types are accepted by Octave, but double quoted strings are interpreted slightly differently (see [https://www.gnu.org/software/octave/doc/interpreter/Strings.html Strings] in the manual for details).
 
 
'''Do:'''
 
 
<pre>
 
a = "Hello, world";
 
b = "x";
 
disp ("This \"string\" contains a\nnewline");
 
</pre>
 
 
'''Don't:'''
 
 
<pre>
 
s = 'Hello, world';
 
if (x(1) == 'c')
 
  disp ('Don''t quote one character this way, even if you''re a C programmer');
 
endif
 
</pre>
 
 
There are a few edge cases where single quoted strings may be preferable, and are permitted as exceptions under this style guide.
 
 
; String containing double quotes
 
: A string that contains many double quote characters itself, where escaping all of them with backslashes becomes inconvenient, may be easier with single quotes.
 
; String containing backslashes
 
: A string that contains literal backslashes, in particular a regular expression pattern, where doubly escaping certain character sequences is both inconvenient and harder to read, is usually better done with single quotes.
 
; Argument interpreted differently
 
: A string argument to the regexp family of functions may be interpreted differently depending on whether it is a double quoted or single quoted string. Certain escape sequences are interpreted only in a single quoted string for Matlab compatibility.
 
  
 
== ending blocks ==
 
== ending blocks ==
Line 190: Line 129:
 
== ! operator ==
 
== ! operator ==
  
* The Octave operator <code>!</code> should be used for logical negation, rather than <code>~</code>.
+
The Octave operator {{codeline|!}} should be used for logical negation, rather than
* The negation operator is written with a space between the operator and its target, e.g., <code>! A</code>.
+
{{codeline|~}}. The negation operator is written with a space between the operator
* For comparisons use <code>!=</code> instead of <code>~=</code>.
+
and its target, e.g., {{codeline|! A}}.
 +
 
  
 
== Comments ==
 
== Comments ==
Line 248: Line 188:
 
The preferred comment mark for places that may need further attention is
 
The preferred comment mark for places that may need further attention is
 
with {{codeline|FIXME:}} comments.
 
with {{codeline|FIXME:}} comments.
 
[[Category:Development]]
 

Please note that all contributions to Octave may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see Octave:Copyrights for details). Do not submit copyrighted work without permission!

To edit this page, please answer the question that appears below (more info):

Cancel Editing help (opens in new window)

Template used on this page: