{"id":308,"date":"2009-05-11T18:07:05","date_gmt":"2009-05-11T18:07:05","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2009\/05\/11\/keep-your-code-readable-with-smart-indenting\/"},"modified":"2009-05-11T18:07:05","modified_gmt":"2009-05-11T18:07:05","slug":"keep-your-code-readable-with-smart-indenting","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2009\/05\/11\/keep-your-code-readable-with-smart-indenting\/","title":{"rendered":"Keep your code readable with smart indenting"},"content":{"rendered":"<p>The MATLAB Editor automatically indents your code for you as you enter it (if your haven&#8217;t turned <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/matlab_env\/brqxeeu-49.html#brqxeeu-52\">the preference<\/a> off). We call this smart indenting, and it makes reading code easier. Logical blocks like <tt>if\/end<\/tt> and <tt>while\/end<\/tt> have their content visually demarcated with white space, which helps you see the block of code as a single entity.<\/p>\n<p>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):<\/p>\n<p><b>Classic<\/b><br \/>\nIn Classic mode, the <tt>function<\/tt> 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&#8217;s hard to visually match an <tt>end<\/tt> to it&#8217;s start.<\/p>\n<div align=\"left\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/ken_orr_smart_indenting\/classic.png\">\n<\/div>\n<p><\/p>\n<p><b>Indent nested functions<\/b><br \/>\nThis mode is slightly better than Classic, in that nested functions are fully indented &#8212; I could deal with this mode if I had to.<\/p>\n<div align=\"left\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/ken_orr_smart_indenting\/indent_nested_functions.png\">\n<\/div>\n<p><\/p>\n<p><b>Indent all functions<\/b><br \/>\nThis 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&#8217;s logical structure. I&#8217;m sure my preference is heavily influenced by my experience with Java.<\/p>\n<div align=\"left\">\n<img decoding=\"async\" border=\"0\" src=\"https:\/\/blogs.mathworks.com\/images\/desktop\/ken_orr_smart_indenting\/indent_all_functions.png\">\n<\/div>\n<p><\/p>\n<p>If you want to play around with the various smart indenting options, go to <b>Preferences -> Editor\/Debugger -> Language<\/b>, select <b>MATLAB<\/b> as the language, and then change the value in the <b>Function indenting format<\/b> combo box.<\/p>\n<p>What indenting style do you prefer?<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The MATLAB Editor automatically indents your code for you as you enter it (if your haven&#8217;t turned the preference off). We call this smart indenting, and it makes reading code easier. Logical&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2009\/05\/11\/keep-your-code-readable-with-smart-indenting\/\">read more >><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/308"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/comments?post=308"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/308\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=308"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=308"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=308"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}