Octave style guide: Difference between revisions

1,456 bytes added ,  12 January 2017
(→‎Comments: expand # and ## comment blocks)
(→‎Quoted Strings: new section)
Line 103: Line 103:


=== Variable names ===
=== Variable names ===
== 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 ==
296

edits