Technical Articles

Clean Up Your Code

By Loren Shure, MathWorks and Steve Johnson, MathWorks


Why is writing “good” code so important? Efficient code executes faster and uses fewer resources. Maintainable code enables you to review, learn from, reuse, and adapt the code. Individual developers working in a team often need to understand the entire code of the project on which they are working, even if they are responsible for only a portion of the code.

Often, untidy code is poorly planned—it may contain bugs, run slowly, and be hard to maintain. Bugs can be very difficult to find, simply because logic or flow is hard to follow. Whether you write in C, M, or some other language, it is important to ensure that your code is both efficient and maintainable.

After 20 years of programming in MATLAB, MathWorks engineers have developed many best practices for writing efficient, maintainable M-code. This article describes M-Lint, a new tool in MATLAB 7 that checks your code against these guidelines and returns a list of recommendations to improve its efficiency and maintainability.

What is Lint?

In the programming world, lint is a C program checker/verifier that examines a program closely for style, language usage, and portability problems.


Some common tasks, though they seem tedious, can become good habits. For example, saving a file as you work is often wise. Those of us who have lost work in the past have learned to save files early and often. Similarly, it is worth ensuring that you leave the code you write in a good state. Sometimes when you try to reuse code you wrote but have not used for a while, you find it does not work quite so well for the new purpose you envision. If you had written clean code in the first place, you might have avoided this unpleasant surprise. We recommend you use the M-Lint tool throughout your code development process, or at least before a code review and final code deployment.

M-Lint Example

Suppose our task is to develop some code to find lines in a plot and determine if they are two- or three-dimensional. We store the code sample in a file named lol.m, displayed in Figure 1.

mlint_codefig2_w.gif
Figure 1: Source code for lol.m. Click on image to see enlarged view.

The code returns the handles for each line object and a number, either 2 or 3, denoting the dimensionality of the associated line. We’ll use mlint to see if our code can be improved for efficiency and maintainability. Figure 2 shows the M-Lint report output for lol.m.

All the messages for this particular file merit attention, as they indicate ways to make the code more efficient and more maintainable. Let’s examine a few.

mlint_codefig1_w.gif
Figure 2. M-Lint report output for lol.m. Click on image to see enlarged view.

The first message, for line 1, tells us that the M-file name, lol, is the name that MATLAB will recognize for this function, rather than the name lengthofline, which is the function name in the M-file. To avoid later confusion, we should change either the name of the file or the name of the function so that they match. In this case, the filename is less descriptive than the function name, so renaming the file lengthofline.m is probably best.

The message for line 17 recommends that we use a call to a single function, numel, instead of a call to two functions, prod(size)). This is generally good advice to follow so as to avoid incurring the overhead required to call an extra function. The corrected line 17 would read

 for nh = 1:numel(hline)

There are two messages for line 28. The first says that we are creating an array and growing it during the for-loop execution. The recommendation, to preallocate the array before the for-loop, should significantly speed up the code. We could place this line of code just after line 26

data = cell(size(fdata));

We see similar preallocation messages for lines 18 and 37 and should consider explicitly preallocating memory for each of these cases as well.

The second message for line 28 tells us that it is more efficient to use dynamic fieldnames than the getfield function when accessing structures (the message points you to the documentation for details). We should therefore replace the code on line 28 with

data{nd} = flds.(fdata(n));

M-Lint messages like these help you learn new MATLAB features, as well as make it easier to write more efficient code. M-Lint lets you carefully evaluate your code to assess opportunities to make it cleaner, more efficient, and more maintainable.

Ways to call M-Lint
  • From the MATLAB Editor/Debugger, select Tools and then Check Code with M-Lint.
  • From Emacs, use matlab-mode to view M-Lint messages.
  • In the Current Directory browser, select the M-Lint Code Check Report from the list of Directory Reports presented on the toolbar.
  • Type the mlint command in the MATLAB command window.

Published 2004