{"id":262,"date":"2008-09-08T07:23:12","date_gmt":"2008-09-08T12:23:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/desktop\/2008\/09\/08\/let-m-lint-help-simplify-your-code\/"},"modified":"2008-09-08T07:23:12","modified_gmt":"2008-09-08T12:23:12","slug":"let-m-lint-help-simplify-your-code","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/community\/2008\/09\/08\/let-m-lint-help-simplify-your-code\/","title":{"rendered":"Let M-Lint help simplify your code"},"content":{"rendered":"<p>M-Lint is an indispensable tool when writing code in the MATLAB Editor. It spots errors and potential errors as you edit, which makes writing good code much easier. There&#8217;s one feature that M-Lint offers, though, that isn&#8217;t available through the live mode in the Editor: McCabe complexity calculation.<\/p>\n<p>The McCabe complexity (also referred to as <a href=\"http:\/\/en.wikipedia.org\/wiki\/Cyclomatic_complexity\">Cyclomatic Complexity<\/a>) is a metric used to measure how complex a piece of software is. The more complex it is, the harder it is to maintain and debug. Thus this metric can be used in an effort to help simplify your code.<\/p>\n<p>To calculate the McCabe complexity, you&#8217;ll have to run M-Lint from the command line, using the <b>-cyc<\/b> flag. We&#8217;d like to integrate this warning into the standard set of live warnings, but we&#8217;re going to leave it out until we introduce a mechanism to set the complexity threshold via the M-Lint preferences panel. <\/p>\n<p>At the command prompt, type the following (the first argument is your file name):<\/p>\n<div>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\n>> mlint(<span style=\"color: #A020F0\">'penny'<\/span>,<span style=\"color: #A020F0\">'-cyc'<\/span>)\r\n<\/pre>\n<\/div>\n<p>You&#8217;ll see something like this:<\/p>\n<div>\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">\r\n<span style=\"color: #0000FF\"><u>L 0<\/u><\/span> (C 0): The McCabe complexity is 1.\r\n<\/pre>\n<\/div>\n<p>A value of one is <i>really<\/i> good, though probably not a realistic goal to shoot for. A commonly accepted target value is ten. <\/p>\n<p>I&#8217;ve found that using this metric really helps me write clearer more maintainable code. Even being aware that I&#8217;m going to check my code using this metric helps me design with simplicity in mind.<\/p>\n<p>What&#8217;s the average complexity of your files?<\/p>\n<p>Check out <a href=\"http:\/\/www.literateprogramming.com\/mccabe.pdf\">McCabe&#8217;s original paper<\/a> on measuring complexity for the math behind his measurement technique.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>M-Lint is an indispensable tool when writing code in the MATLAB Editor. It spots errors and potential errors as you edit, which makes writing good code much easier. There&#8217;s one feature that&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/community\/2008\/09\/08\/let-m-lint-help-simplify-your-code\/\">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":[2,9],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/262"}],"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=262"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/posts\/262\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/media?parent=262"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/categories?post=262"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/community\/wp-json\/wp\/v2\/tags?post=262"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}