Doug’s MATLAB Video Tutorials

August 21st, 2009

Basics: Code review- the thought process in rewriting code for clarity

This short video covers the thought process of rewriting some code for clarity. The six lines of code are lengthened into twice that. The code is much more readable and maintainable. The problem is not as important as the thought process, however, the problem is about reshaping a matrix.

9 Responses to “Basics: Code review- the thought process in rewriting code for clarity”

  1. Robert replied on :

    rowStart and indeed numSubMat should be variables that change depending on the numSubRows variable, otherwise if you change numSubRows in your code then it will no longer give the expected output

  2. watzabatza replied on :

    @Robert

    aw! yeah you’re right bro.. thank you for this advice…

  3. Daniel Armyr replied on :

    By the way, I notice there is an M-lint warning on line 54 complaining about the [x:y] syntax. Why is that considdered a warning? As your example shows, code usually ends up having them, and I see no reason why Matlab should have a problem with that.

    –DA

  4. dhull replied on :

    @Robert,

    The code would be better with that suggestion, slightly more complicated, but a good check as that is something that would likely be done wrong by coders working with the code later.

    @Daniel,

    The complaint on 54 is about using unneeded brackets on that line. The code runs just as well without them.

    -Thanks
    Doug

  5. Daniel Armyr replied on :

    @dhull
    Yes, but so are an extra pair of parenthesis, but sometimes you leave them in there anyways and you do not see matlab complaining. But that is a discussion to be had in a service request, not here.

  6. matt fig replied on :

    Daniel Armyr,

    I suspect the reason why MLint doesn’t warn about parenthesis is that they invoke no function call. Parenthesis are only used to group in this context. Brackets on the other hand, are used to concatenate. If there is only one array inside the brackets, there is nothing to concatenate, yet the internal concatenate function is called anyway! This is an inefficiency.

  7. Ola replied on :

    Hi, I’m new to this blog, and as a matter of fact I’m lost so I was kinda hoping someone will point to me the right link to post my questions about SimEvent tool box.

    Thanks a lot in advance.

  8. dhull replied on :

    Ola,

    http://www.mathworks.com/support can answer all your questions.

    Thanks,
    Doug

  9. Bruce replied on :

    I’ve been using Matlab for a long time, but never thought of coding a test matrix like you did, e.g.,
    1 2 3 4 …
    11 12 13 14 …
    .
    .
    .

    Great idea for checking how code manipulates data, in stages

Leave a Reply

Wrap code fragments inside <pre> tags, like this:

<pre class="code">
a = magic(3);
sum(a)
</pre>

If you have a "<" character in your code, either follow it with a space or replace it with "&lt;" (including the semicolon).


Doug Hull is a proud MathWorker who is on a mission to help you with MATLAB.

Doug's picture

These postings are the author's and don't necessarily represent the opinions of The MathWorks.