Path: news.mathworks.com!newsfeed-00.mathworks.com!lon-transit.news.telstra.net!pit-in1.telstra.net!news.telstra.net!news-south.connect.com.au!bunyip2.cc.uq.edu.au!dh177.physics.uq.edu.au!timo
From: Timo Nieminen <timo@physics.uq.edu.au>
Newsgroups: comp.soft-sys.matlab
Subject: Re: How to be a good Matlab programmer
Date: Sun, 20 Apr 2008 14:34:06 +1000
Organization: University of Queensland
Lines: 90
Message-ID: <Pine.LNX.4.50.0804201357230.32104-100000@localhost>
References: <d6f9728b-07d7-4ebc-8264-e2485cde6259@c65g2000hsa.googlegroups.com>
NNTP-Posting-Host: dh177.physics.uq.edu.au
Mime-Version: 1.0
Content-Type: TEXT/PLAIN; charset=US-ASCII
X-Trace: air.soe.uq.edu.au 1208665868 9106 130.102.173.177 (20 Apr 2008 04:31:08 GMT)
X-Complaints-To: news@uq.edu.au
NNTP-Posting-Date: Sun, 20 Apr 2008 04:31:08 +0000 (UTC)
X-X-Sender: timo@localhost
In-Reply-To: <b375ae00-dba3-42a5-a138-31be2142591c@s50g2000hsb.googlegroups.com>
Xref: news.mathworks.com comp.soft-sys.matlab:464098


On Sat, 19 Apr 2008, Rex wrote:

> I also want to learn to write more elegant
> and concise code. For example, here's an example of how I used to
> write for loops:
> 
> dims = [10 18 21 25 40 65];
> [a,b] = size(dims);
> for i = 1:b,
>     dlmwrite(sprintf('matrix%d', dims(b)), ones(dims(b)));
> end
> 
> A friend recently pointed out that I can refactor it as follows:
> 
> dims = [10 18 21 25 40 65];
> for i = dims,
>     dlmwrite(sprintf('matrix%d', i), ones(i));
> end

"Concise" is not always good. "Readable" is always good. If conciseness 
reduces readability, then it's bad - longer code that's easier to read, 
understand, debug, and maintain is better. Often enough, even slower code 
can be better if it's easier to read, debug, and maintain, for example, 
if said code occupies less than 1% of the runtime.

Consider the joys of ultra-concise C code ...

While your example above is short enough to not suffer, if "dims = ..." 
was a long way from the loop, and the loop was longer, the reader might 
well assume that "i" is an integer loop counter when reading the code. 
Apart from using i or j as counters being bad practice in matlab 
(sqrt(-1) and all that), "for dim = dims" is better for readability.

Possible tips for matlab programmers coming from other languages:

(a) Matrix operations are simple and readable. Much more so than loops.

(b) You don't need to declare variables. Thus, beware of mistyped (or 
easily mistypeable) variable names - you won't have a compiler telling you 
it doesn't know what the new variable is.

(c) When performing calculations using a formula from some source, stick 
to the original notation as far as reasonably possible. For example, have 
a variable "tau" rather than "OpticalDepth". Some might disagree with 
this, given that "OpticalDepth" is more descriptive than "tau", but 
assuming you and other readers of the code are familiar with the field, 
having matlab code that looks like the formulae in the textbooks is easier 
to read IMHO.

(d) As above, don't use i or j as counters.

(e) Functions can return more than one value, e.g., [a,b,c] = foo(d,e).

(f) You can check how many output values are asked for when a function is 
called (and, of course, how many input values are passed to the function).

(g) When you write a function, include the text that will be displayed 
when the user types "help foo". Make this text useful.

(h) Matlab is nice for debugging. If running scripts, you have access to 
all of the variables in memory if it crashes. What about functions? One 
approach is to start with it as a script, and then turn it into a 
function. Or, use "keyboard" to interrupt execution and give you access to 
the function's memory workspace. Also use "keyboard" to interrupt a script 
at a particular point.

(i) "whos" is a useful debugging tool.

(j) The profiler ("help profile") can be very useful to help you decide 
what parts of your code are worth trying to optmise.

(k) It's easy to use external programs or OS commands ("help system").

(l) While Matlab has functions like "strcat", it can be easier to read, 
and more concise, to write things like "a = [ foo bar 'and more text' ];". 
This doesn't behave the same as strcat in general, but in the most common 
cases, it will.

(m) While you can edit graphs etc using the GUI figure editor (e.g., 
changing line styles and colours, line thicknesses, font sizes, etc.), you 
can do this kind of thing via the command line. If you need to do it more 
than once, and especially when you need to do it the same way more than 
once, a few lines of code (in a function, or easily copy-and-pasted) can 
make this easy compared to remembering what you did last time (possibly 
months ago).

(o) Graphs can also be good debugging tools.

-- 
Timo, who has written much bad Matlab code