12

I seek to adopt a coding standard for MATLAB, but I am not sure if I picked the right one.

To my best knowledge there is not that much available on topic of programming guidelines for MATLAB, other than this document. The document is well written and has good feedbacks. Standard was published in 2002 (on matlab central) by Richard Johnson but has not being updated since. Is there a more up to date version of it or similar document? (I really failed to google up something else).

Background motivation assumes

  • Coding standards are important
  • Although MATLAB has not change much since 2002, other languages and their approaches have. One could really benefit from those practices.
  • The fact is a lot of people are writing new code using MATLAB or Octave. Although, one would argue the language is virtually dead (blah blah). I would rather not go there (let's mark it as an offtopic).

Why the codestyle is not good enough for me

I'd like to summarize here a few things. If you take time to read the document you might find that it

  • tries to be too hungarian (it's cryptic and I really hate this in most cases)
  • it has too many shortcuts (more less similar to the previous point)
  • it is not supported by Mathworks (but it actually might be a good thing, since all the good stuff in MATLAB came from the user community IMO)
  • there is no automated quality control tools respecting such coding style (here I mean not something like mlint as in *lint family, but more like pep8.py for python)

I guess the reason why such a tool has not been developed is actually the absence of a widely accepted coding standard.

I would really appreciate any of your criticism on the standard or information about a better one.

Do you have any experience on working with this standard? Which parts of it did not work for you? If you never used a formal coding standard but do have a valuable practice that does not fit into it - please provide an example.

Yauhen Yakimovich
  • 13,635
  • 8
  • 60
  • 67
  • 2
    there's this: http://stackoverflow.com/a/5042797/97160. Also some useful stuff here: http://stackoverflow.com/q/2326609/97160 – Amro Jul 03 '13 at 16:27
  • @Amro: this is actually quite great. If "The Elements of MATLAB Style" is that good, I might have my answer! – Yauhen Yakimovich Jul 03 '13 at 16:31
  • 1
    its by the same author (Richard Johnson). I also found this wiki: https://sites.google.com/site/matlabstyleguidelines/ – Amro Jul 03 '13 at 16:33
  • Ordering the book (PDF and a hardcopy) :-) Here is a review http://blogs.mathworks.com/loren/2011/02/10/book-review-the-elements-of-matlab-style/ BTW Amro would you mind to rewrite your comment as an answer? – Yauhen Yakimovich Jul 03 '13 at 16:37
  • It was really a short comment linking to other posts rather than a full answer. Feel free to write something a bit more complete and mark the answer as accepted. I should say that that I have not read the book myself, but the reviews are indeed positive. – Amro Jul 03 '13 at 16:51
  • Related: GNU Octave (like any other open source project) has its own set of coding styles: http://www.gnu.org/software/octave/doc/interpreter/Octave-Sources-_0028m_002dfiles_0029.html#Octave-Sources-_0028m_002dfiles_0029 – Amro Jul 03 '13 at 22:23
  • last comment, I promise. I found this update: http://www.mathworks.com/matlabcentral/fileexchange/36540-updates-to-the-elements-of-matlab-style – Amro Jul 03 '13 at 22:41
  • A post on [Loren Shure's MathWorks blog](http://blogs.mathworks.com/loren/2014/01/29/coding-best-practices-a-good-read/) recommends this article: [Best Practices for Scientific Computing](http://dx.doi.org/10.1371/journal.pbio.1001745). – horchler Feb 01 '14 at 00:56
  • I started a [MEP8](https://github.com/yuval-harpaz/mep8) tool so we could have a PEP8 of our own. See if this helps, and join forces if you see potential there. – Yuval Harpaz Sep 16 '18 at 20:02
  • Since January 2022 we have the MonkeyProof MATLAB Coding Standard, see https://doc.monkeyproofsolutions.nl/code-checker-for-matlab/monkeyproof-matlab-coding-standard/ This standard has a Change Control Board (CCB) and is written together with some industry-leaders and TIOBE https://www.tiobe.com, a company specialized in Coding Standards and Code Quality. From: https://nl.mathworks.com/matlabcentral/answers/95695-is-there-a-document-describing-matlab-coding-standards-or-a-good-programming-guide-available#answer_1067025 – MonkeyProof Solutions Oct 05 '22 at 11:41

1 Answers1

4

One of the best answers so far would be to quote a comment by Amro:

"the same author (Richard Johnson)" has published a book 'The Elements of MATLAB Style' (also see wiki) 2011:

cover

Table of Contents

  1. General principles
  2. Formatting
  3. Naming
  4. Documentation
  5. Programming
  6. Files and organization
  7. Development.

Loren has a blog entry with the review of the book. I will just follow here line of comments:

  • 7 Split Long Code Lines at Graceful Points - I find this one useful as it is a total pain having to trail far off to the right in any editor, even though it is possible.
  • 10 Do Not Use Hard Tabs - This helps keep sanity when working among a group with possibly different editing environments.

  • 43 Use Meaningful Names for Variables with a Large Scope - This makes code much easier to read, understand, and debug, if necessary.

  • 69 Name Functions for What They Do - Since functions perform an action, the name should include information about the action.

  • 86 Use Sortable Numbering in Data Filesnames - If you have many similar files of data, having a rational numbering scheme can only help you out.

  • 97 Be Sure That Comments Agree with the Code - I will never forget the time that my thesis advisor called me because he was really irritated. I had left him a copy of a Fortran program that had copious comments, the final one being "Ignore all the comments above; they were for a previous version."

  • 135 Avoid Cryptic Code - I have found that generally, writing cryptic code buys less than I expect in terms of good things, and more headaches than it warrants. On occasion, I have used cryptic code for performance in something time-critical. When I do, I try to comment it fully, including a straight-forward implementation in the comments which I have tested. That way, when the performance trade-offs change, I understand what the code is supposed to do and have two starting options for doing a code update.

  • 150, 151 Minimize the Use of Global Variables and Minimize the Use of Global Constants -- I would say this even more strongly myself. There are superior techniques for dealing with information you want to share, whether they be function handles, classes and their properties, or some other methods. These techniques are much safer to use for many reasons - e.g., more easily controlled side effects, should any be desired, and code becomes more suitable for parallelism potentially.

  • 172 Use Parenthese - Clarity of meaning is paramount, especially if others need to understand, modify, or translate the code.

  • 176 Avoid Use of eval When Possible - I'm sure it doesn't seem so to some MATLAB users, but eval is avoidable most of the time.

  • 185-188 The first of these is Avoid Complicated Conditional Expressions - These entries contain some useful thoughts on dealing with conditional constructs, the ordering of the cases, etc.

  • 271-275 The first of these is Write Small Tests - I love that Richard has made testing a central tenet of this style guide. I don't see how programmers function well without a robust test suite.

Conclusion

The book seems too general compared to the original document from 2002. I will continue to read it and give more insights, but it does not seem to entirely meet my understanding of strictness required for a coding standard. It mashes a lot of general ideas useful for the beginning programmer, but not strict to program so that they can test code automatically (ones again PEP8).

Yauhen Yakimovich
  • 13,635
  • 8
  • 60
  • 67
  • +1 as promised. I'm afraid there is no PEP8 similar tool for MATLAB that I am aware of to enforce code styles. The IDE *Code Analyzer* (formerly mlint) is the closest thing I can think of, but thats not exactly the same thing... You could always start a new project and implement such a tool :) – Amro Jul 03 '13 at 22:21
  • 1
    Here is something possibly of interest: http://undocumentedmatlab.com/blog/parsing-mlint-code-analyzer-output/ – Amro Jul 03 '13 at 22:33