{"id":121,"date":"2008-01-04T09:03:08","date_gmt":"2008-01-04T14:03:08","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/2008\/01\/04\/should-mlint-recommend\/"},"modified":"2016-11-10T20:29:43","modified_gmt":"2016-11-11T01:29:43","slug":"should-mlint-recommend","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2008\/01\/04\/should-mlint-recommend\/","title":{"rendered":"Should M-Lint Try to Make a Recommendation?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>At The MathWorks, we continue to explore ways to ease your programming learning curve and help you write better code.  As\r\n         such, we have been discussing how stringent to make the messages in <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlint.html\"><tt>mlint<\/tt><\/a> with respect to catching errors.  Currently, MATLAB doesn't make any particular recommendation about checking to be sure\r\n         you know which error you got before moving forward.  Is that a really good idea?  We aren't sure and thought find out your\r\n         thoughts on this.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">An Example<\/a><\/li>\r\n         <li><a href=\"#2\">Snippet #1<\/a><\/li>\r\n         <li><a href=\"#3\">Snippet #2<\/a><\/li>\r\n         <li><a href=\"#4\">Pros and Cons<\/a><\/li>\r\n         <li><a href=\"#5\">There's No catch<\/a><\/li>\r\n         <li><a href=\"#6\">What Do You Think M-Lint Should Do?<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>An Example<a name=\"1\"><\/a><\/h3>\r\n   <p>Let me show you two snippets of code trying to accomplish basically the same task. I will discuss pros and cons of each. \r\n      Note, I will be using the new <tt>MException<\/tt> syntax for <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/try.html\"><tt>try<\/tt><\/a> and <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/try.html\"><tt>catch<\/tt><\/a> though you can do similar things using the older <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/lasterror.html\"><tt>lasterror<\/tt><\/a> mechanism as well.\r\n   <\/p>\r\n   <h3>Snippet #1<a name=\"2\"><\/a><\/h3><pre>   try\r\n       doSomething;\r\n       doMore;\r\n   catch myException\r\n       cleanUpHere;\r\n       maybeThrowOwnNewException;\r\n   end<\/pre><h3>Snippet #2<a name=\"3\"><\/a><\/h3><pre>   try\r\n       doSomething;\r\n       doMore;\r\n   catch myException\r\n       expectedExceptionID = 'MATLAB:dimagree';\r\n       if strcmp(myException.identifier,expectedExceptionID)\r\n          cleanUpHereAndMaybeThrowError;\r\n       else\r\n          doDifferentCleanUp;\r\n          throwUnexpectedException;\r\n       end\r\n   end<\/pre><h3>Pros and Cons<a name=\"4\"><\/a><\/h3>\r\n   <div>\r\n      <ul>\r\n         <li>snippet #1 is shorter than snippet #2<\/li>\r\n         <li>snippet #1 never checks that the caught error is the expected one<\/li>\r\n         <li>snippet #2 does two different operations, depending on whether the error was expected or not<\/li>\r\n         <li>when snippet #1 does throw its own new exception, since it didn't look at the actual one, the cause may not be correct or\r\n            meaningful to the user\r\n         <\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>There's No catch<a name=\"5\"><\/a><\/h3>\r\n   <p>Another related question arises if the <tt>try<\/tt> is not paired with a <tt>catch<\/tt>.\r\n   <\/p>\r\n   <h3>What Do You Think M-Lint Should Do?<a name=\"6\"><\/a><\/h3>\r\n   <p>For the first example, neither code snippet is outright \"wrong.\" Moreover, there are cases, no doubt, where no matter what\r\n      the actual error is, you have no choice but to do a particular set of operations. However, that clearly isn't always so. \r\n      So, how forceful would you like to see M-Lint be for the case of snippet #1?  Would getting a message there (though not a\r\n      red one) be helpful?\r\n   <\/p>\r\n   <p>What about the situation in which there is no <tt>catch<\/tt> accompanying the <tt>try<\/tt>?  Again, there may be a few cases where this makes sense, but typically that won't be so.\r\n   <\/p>\r\n   <p>Let us know your thoughts about these questions <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=121#respond\">here<\/a>.\r\n   <\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_cd71bd107cb84c8f9985e384e86aeb68() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='cd71bd107cb84c8f9985e384e86aeb68 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cd71bd107cb84c8f9985e384e86aeb68';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Loren Shure';\r\n        copyright = 'Copyright 2008 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n      \r\n-->\r\n<\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_cd71bd107cb84c8f9985e384e86aeb68()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.5<br><\/p>\r\n<\/div>\r\n<!--\r\ncd71bd107cb84c8f9985e384e86aeb68 ##### SOURCE BEGIN #####\r\n%% Should M-Lint Try to Make a Recommendation?\r\n% At The MathWorks, we continue to explore ways to ease your programming\r\n% learning curve and help you write better code.  As such, \r\n% we have been discussing how stringent to make the messages in\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/mlint.html |mlint|>\r\n% with respect to catching errors.  Currently, MATLAB doesn't make any\r\n% particular recommendation about checking to be sure you know which error\r\n% you got before moving forward.  Is that a really good idea?  We aren't\r\n% sure and thought find out your thoughts on this.\r\n%% An Example\r\n% Let me show you two snippets of code trying to accomplish basically the \r\n% same task. I will discuss pros and cons of each.  Note, I will be using\r\n% the new <https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/mexception.html |MException|>\r\n% syntax for <https:\/\/www.mathworks.com\/help\/matlab\/ref\/try.html |try|> and\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/try.html |catch|>\r\n% though you can do similar things using the older\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/lasterror.html |lasterror|>\r\n% mechanism as well.\r\n%% Snippet #1\r\n%\r\n%     try\r\n%         doSomething;\r\n%         doMore;\r\n%     catch myException\r\n%         cleanUpHere;\r\n%         maybeThrowOwnNewException;\r\n%     end\r\n%\r\n%% Snippet #2\r\n%\r\n%     try\r\n%         doSomething;\r\n%         doMore;\r\n%     catch myException\r\n%         expectedExceptionID = 'MATLAB:dimagree';\r\n%         if strcmp(myException.identifier,expectedExceptionID)\r\n%            cleanUpHereAndMaybeThrowError;  \r\n%         else\r\n%            doDifferentCleanUp;\r\n%            throwUnexpectedException;\r\n%         end\r\n%     end\r\n%% Pros and Cons\r\n%\r\n% * snippet #1 is shorter than snippet #2\r\n% * snippet #1 never checks that the caught error is the expected one\r\n% * snippet #2 does two different operations, depending on whether the\r\n% error was expected or not\r\n% * when snippet #1 does throw its own new exception, since it didn't look\r\n% at the actual one, the cause may not be correct or meaningful to the user\r\n%\r\n%% There's No catch\r\n% Another related question arises if the |try| is not paired with a\r\n% |catch|.  \r\n%% What Do You Think M-Lint Should Do?\r\n% For the first example, neither code snippet is outright \"wrong.\"\r\n% Moreover, there are cases, no doubt, where no matter what the actual\r\n% error is, you have no choice but to do a particular set of operations.\r\n% However, that clearly isn't always so.  So, how forceful would you like\r\n% to see M-Lint be for the case of snippet #1?  Would getting a message\r\n% there (though not a red one) be helpful?  \r\n%%\r\n% What about the situation in which there is no |catch| accompanying the\r\n% |try|?  Again, there may be a few cases where this makes sense, but\r\n% typically that won't be so.\r\n%%\r\n% Let us know your thoughts about these questions\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=121#respond here>.\r\n##### SOURCE END ##### cd71bd107cb84c8f9985e384e86aeb68\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      At The MathWorks, we continue to explore ways to ease your programming learning curve and help you write better code.  As\r\n         such, we have been discussing how stringent to make... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2008\/01\/04\/should-mlint-recommend\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16,14,11],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/121"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=121"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/121\/revisions"}],"predecessor-version":[{"id":2108,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/121\/revisions\/2108"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}