{"id":434,"date":"2010-03-08T19:41:00","date_gmt":"2010-03-08T19:41:00","guid":{"rendered":"https:\/\/blogs.mathworks.com\/videos\/2010\/03\/08\/top-10-matlab-code-practices-that-make-me-cry\/"},"modified":"2016-10-13T19:41:04","modified_gmt":"2016-10-14T00:41:04","slug":"top-10-matlab-code-practices-that-make-me-cry","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/videos\/2010\/03\/08\/top-10-matlab-code-practices-that-make-me-cry\/","title":{"rendered":"Top 10 MATLAB code practices that make me cry"},"content":{"rendered":"I was chatting with the Application Support Engineers here at MathWorks about what kind of coding practices cause avoidable pain for MATLAB users.\r\n<P>\r\n<P>\r\nWithout further ado:  The top ten and quick ways to not do them:\r\n<P>\r\n<strong>10.) Not using left hand zeros<\/strong>\r\n<P>\r\nCertain things must be learned the hard way.  I learned this one bleary eyed evening as an undergraduate.  Well into the night working on a MATLAB homework assignment that would &#8220;take ten minutes, fifteen if you type slow.&#8221; (yes, Dr. P, 13 years later I still remember that one! -smile-)\r\n<P>\r\nIt is really easy to mistake a <em>.5<\/em> for a <em>5<\/em>.  That is why I always use a left hand zero like <em>0.5<\/em>.\r\n<P>\r\n<P>\r\n<strong>9.) Plotting enormous amounts of data<\/strong>\r\n<P>\r\nMy computer monitor has 2.3 million pixels, total.  If I try to plot datasets with huge amounts of data in them, they will very often just look like a blob and slow the machine down in the process.\r\n<P>\r\nThere is very often a better visualization available.  Here is <a href=\"https:\/\/blogs.mathworks.com\/videos\/2010\/01\/22\/advanced-making-a-2d-or-3d-histogram-to-visualize-data-density\/\">an example<\/a> of changing the visualization to make it clearer and less taxing on memory.\r\n<P>\r\n<strong>8.) GUIs with garish color schemes<\/strong>\r\n<P>\r\nIn an effort to emphasize certain buttons on their GUI, people will change the colors of them.  Very quickly they end up with several different colored buttons, a non-standard background color, extra big buttons, etc&#8230;\r\n<P>\r\nSticking with the default colors is a good move.  Most professionally produced software sticks with the defaults, it ends up looking better.\r\n<P>\r\n<P>\r\n<strong>7.) Using <em>ans<\/em>, or any other MATLAB function as a variable name or function.<\/strong>\r\n<P>\r\nWhen you do this, MATLAB will call whichever one is higher on the path.  Some strange behavior can occur when you redefine a function like that.  Unfortunately, MATLAB does not catch you doing this for the most part.  \r\n<P>\r\nI try to avoid using variables and function names that are common terms like, <em>mean<\/em>, <em>filter<\/em>, etc&#8230;  If there is any doubt, use the <em>which<\/em> command to find out if a function exists of a given name.\r\n<P>\r\n<P>\r\n<strong>6.) Not using white space to good effect in code.<\/strong>\r\n<P>\r\nEven though you can put several commands on one line if separated by a semicolon, these lines can often be hard to notice.  Not putting blank lines between sections of code can also make it harder to read.\r\n<P>\r\nWhite space is free, use it to make your code look good.\r\n<P>\r\n<P>\r\n<strong>5.) Bad variable names<\/strong>\r\n<P>\r\nVariable names are often the only commenting that gets added to people&#8217;s code.  Meaningful variable names are a great opportunity to make the meaning of your code more clear and to some degree, self-documenting.\r\n<P>\r\nAvoid using variable names like <em>temp<\/em>, <em>aaa<\/em>, <em>r247899921<\/em>.  These just do not convey as much information to people that have to read your code as <em>flagPassedInspection<\/em>, <em>centroidX<\/em>, <em>fidCurrentFile<\/em>.\r\n<P>\r\n<P>\r\n<strong>4.) Hard coding data into the MATLAB code file<\/strong>\r\n<P>\r\nSome people like to put some of their variables directly into the MATLAB code.  That makes sense for <em>small<\/em> variables (I will let you define what small means for you).  For instance, I would feel fine putting a 3&#215;3 matrix into my code.  I would think twice about a 10&#215;10, and I would start using one of our file readers for a 100 x 100.\r\n<P>\r\nThe worst instance I ever saw of this was some MATLAB code where the .M file was 4 GIG (not a mistake) long.  All but a small amount of that was data written out in ASCII.  This makes your code hard to read, maintain and understand.\r\n<P>\r\n<P>\r\n<strong>3.) Exceptionally long files<\/strong>\r\n<P>\r\nEven if not hard coding data into a MATLAB code file, it is easy to just add on &#8220;just a few more lines of code&#8221; until you have thousands of lines of code in a single script.  This makes your code hard to understand. \r\n<P>\r\nI <em>try<\/em> to use the rule that I should be able to see an entire script or function in one screen.  This is not entirely practical, so I will at least break the code into logical sections that do fit on screen all at once.\r\n<P>\r\n<P>\r\n<strong>2.) Globals<\/strong>\r\n<P>\r\nI have never seen MATLAB code where globals were the right thing to do.  Exception: functions TIC and TOC use them quite nicely.  Most of the time I have seen globals being used it was a situation where the code author did not understand scoping of variables.  Rather than pass variables from one function to another, they were just being made global.\r\n<P>\r\nWhy are people cautioned against using global variables?  I will leave that to the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Global_variable\">consensus on Wikipedia<\/a>.\r\n<P>\r\n<P>\r\n<strong>1.) Eval<\/strong>\r\n<P>\r\nEVAL is right up there with globals.  MATLAB user will often string together MATLAB commands to get sequential variable names <em>s1<\/em>, <em>s2<\/em>, <em>s3<\/em>&#8230; only to then have to use another EVAL statement to work with the sequential variable names!   Very often, a cell array indexed with s{1}, s{2}, s{3}&#8230; would work much better.\r\n<P>\r\nI will also find that people use EVAL to get at certain fields in a structure (for example data.alpha) when they do not know at the time of writing the code what field they will want.  Now the <a href=\"https:\/\/blogs.mathworks.com\/videos\/2009\/02\/27\/dynamic-field-name-usage\/\">&#8220;.parens&#8221; notation<\/a> makes that easier.\r\n<P> \r\nThe other most common place to see people use EVAL when it is not needed is when they are trying to load a file or some other function like that.  Very often they are trying to EVAL a string like &#8220;load filename.mat&#8221;  not realizing that there is a functional form where you can use fileNameString = &#8216;filename.mat&#8217;; load(fileNameString)\r\n<P>\r\n\r\nFollow <a href=\"https:\/\/twitter.com\/stuartmcgarrity\"> @stuartmcgarrity<\/a> on Twitter to be notified of new posts.\r\n","protected":false},"excerpt":{"rendered":"<p>I was chatting with the Application Support Engineers here at MathWorks about what kind of coding practices cause avoidable pain for MATLAB users.\r\n\r\n\r\nWithout further ado:  The top ten and quick&#8230; <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/videos\/2010\/03\/08\/top-10-matlab-code-practices-that-make-me-cry\/\">read more >><\/a><\/p>","protected":false},"author":68,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/434"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/users\/68"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/comments?post=434"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/434\/revisions"}],"predecessor-version":[{"id":2699,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/posts\/434\/revisions\/2699"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/media?parent=434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/categories?post=434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/videos\/wp-json\/wp\/v2\/tags?post=434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}