Styles and Conventions

Two things you can do to make your code more readable is to use a reasonable naming convention, and to include reasonable spacing.

Naming

In writing Matlab code, the following is a suggested naming convention.

  1. Reserve m and n for matrix dimensions,
  2. Start all names of vectors with lower case letters,
  3. Start all names of matrices with upper case letters, and
  4. Start all strings with the prefix str, e.g., strName.

Insistence on using Hungarian notation would be excessive, but some ideas taken from it are quite reasonable.

Keep the history of the variable around. For example, don't just us m and n for the row and column dimensions of a matrix A, but rather indicate this by prefixing A with m and n, respectively.

v = rand( 5, 1 )
A = rand( 5, 5 );
mv = size( A, 2 );
mA, nA = size( A );

If a matrix, vector, or scalar is the result of a simple computation on another matrix or vector, combine the operation and the name of the matrix or vector:

detA = det( A );
AB = A * B;
normA = norm( A );
eigvalsA = eigs( A );
[EigvalsA EigvecsA] = eigs( A );
AT = A';

Note that when calling eigs with two output arguments that the results are a pair of matrices, whereas if there is only one output argument, the result is a vector.

If you're taking the difference of two vectors or matrices, prefix the new name by d or D, respectively. If the names of the matrices are short, and it makes sense to recall where the difference came from, concatenate the two names:

w = rand(1,5);
dvw = v - w;
B = rand(4);
dAB = A - B;

If you feel eager, you can define any basic vectors to be column vectors, and postfix the names of all row vectors with T.

vT = v';
A * vT;
A * v';

The benefit of this is that whenever you have a matrix multiplied by a vector, the vector should either be postfixed with a T or transposed.

Spacing

Use spaces generously. In general, always place a space after any commas, and places spaces on both sides of any binary arithmetic or logical operators other than multiplication or division, though if spaces make this more legible, it never hurts to use them.

When making function calls, it helps to write f( a, b, c ) instead of f(a,b,c).

Always indent the body of any control statement and place the end in the same column as the opening statement. Also, place blank lines around the control statement:

s = 0;

for i = 1:10
  if i < 5
    s = s + i;
  else
    s = s + 2*i;
  end

  s = s + 1;
end

The spacing allows the eye to quickly note the entire control structure as a whole. Compare this example with:

s = 0;
for i=1:10 if i<5 s=s+i;else s=s+2*i;end,s=s+1;end

Be sure to use a consistent number of spaces for indenting. In general, use either 2, 3, 4, or 8 spaces, or use a single tab. Any useful editor will allow the user to set the value of the tab stops. For example, in vi, you can issue :set ts=3 to set the tab stop to 3.

You can also use the continuation operator ... to expand code over multiple lines:

>> (                                    ...
     (3:(6 + 2):17) < 5                 ...
   ) | (                                ...
     (                                  ...
       (5:(2*3):(22 - 5*2)) > (19*4)    ...
     ) & (                              ...
       ~sin( 4:5 ) > 0.2                ...
     )                                  ...
   );

Note that matching parentheses appear in the same column.

Any text editor worth using will have parenthesis matching. For example, in vi, pressing % on any parenthesis will move the cursor to its matching parenthesis.