MATLAB Spoken Here
May 11th, 2009
Keep your code readable with smart indenting
The MATLAB Editor automatically indents your code for you as you enter it (if your haven’t turned the preference off). We call this smart indenting, and it makes reading code easier. Logical blocks like if/end and while/end have their content visually demarcated with white space, which helps you see the block of code as a single entity.
You do have some control over exactly how the Editor smart indents, but I think there is one option that results in more readable code. Here are the three options (my favorite is at the end):
In Classic mode, the function keyword does not cause indenting, so a lot of code is stuck against the left margin. I find this to indenting style to be the hardest to read because it’s hard to visually match an end to it’s start.
Indent nested functions
This mode is slightly better than Classic, in that nested functions are fully indented — I could deal with this mode if I had to.
Indent all functions
This mode indents everything, including the content of top level functions, and is therefore my favorite indenting mode. I find the code to be more readable as it better matches it’s logical structure. I’m sure my preference is heavily influenced by my experience with Java.
If you want to play around with the various smart indenting options, go to Preferences -> Editor/Debugger -> Language, select MATLAB as the language, and then change the value in the Function indenting format combo box.
What indenting style do you prefer?
Leave a Reply