{"id":311,"date":"2010-01-11T07:00:01","date_gmt":"2010-01-11T12:00:01","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2010\/01\/11\/about-the-unused-argument-syntax-in-r2009b\/"},"modified":"2019-10-29T13:20:01","modified_gmt":"2019-10-29T17:20:01","slug":"about-the-unused-argument-syntax-in-r2009b","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2010\/01\/11\/about-the-unused-argument-syntax-in-r2009b\/","title":{"rendered":"About the unused-argument syntax in R2009b"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Last fall <a href=\"https:\/\/blogs.mathworks.com\/loren\">Loren<\/a> wrote a <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/\">blog post<\/a> about a new syntax in R2009b for ignoring function inputs or function outputs.  For example, suppose you call <tt>sort<\/tt> and only want the second output.  In R2009b you can do that using this syntax:\r\n   <\/p><pre> [~, idx] = sort(A);<\/pre><p>The tilde character here is used as a placeholder to indicate that you don't need the first output argument of <tt>sort<\/tt>.\r\n   <\/p>\r\n   <p>For another example, suppose you write a function that has to take three input arguments (because another function is always\r\n      going to pass it three arguments), but your function doesn't need the second argument.  Then you can write the first line\r\n      of your function this way:\r\n   <\/p><pre> function out = myfun(A, ~, C)<\/pre><p>This new syntax has drawn a startling amount of discussion, both in the <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/#comments\">comments<\/a> on Loren's post as well as in the comp.soft-sys.matlab newsgroup. (See the thread <a>\"Getting indexes of rows of matrix with more than n repetitions,\"<\/a> for example.)\r\n   <\/p>\r\n   <p>Responses to this new syntax have fallen into roughly four categories:<\/p>\r\n   <div>\r\n      <ul>\r\n         <li>\"Finally, I've been waiting for this!\"<\/li>\r\n         <li>Clarification questions about how it works and how to use it.<\/li>\r\n         <li>Complaints about the specific syntax chosen and suggestions for alternatives.<\/li>\r\n         <li>\"The intro of the tilde op was nothing but a big, useless blunder.\"<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>The passionate arguments in comp.soft-sys.matlab caught my eye and have prompted me to add my two cents here.  Although I\r\n      have no particular expectation of changing anyone's mind, I thought it might be interesting to address one particular question\r\n      that was expressed well by <a href=\"https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/#comment-30611\">Matt Fig<\/a>: \"If something already works, why complicate things?\"\r\n   <\/p>\r\n   <p>Matt means that we already have at least a couple of ways to ignore an output variable.  The first is to use a variable name\r\n      that (hopefully) makes the programmer's intent clear:\r\n   <\/p><pre>  [unused, idx] = sort(A);<\/pre><p>The second technique takes advantage of the way MATLAB assigns function outputs from left to right:<\/p><pre>  [idx, idx] = sort(A);<\/pre><p>So why go to the trouble of introducing a new syntax?<\/p>\r\n   <p>Well, you'll probably get a somewhat different answer from every MATLAB developer you ask.  Some might even agree.  (I'll\r\n      note, however, that this was one of the least-controversial MATLAB syntax proposals ever considered by the MATLAB language\r\n      team.)\r\n   <\/p>\r\n   <p>The proposed syntax was originally considered years ago, sometime around 2001. The proposal was approved internally at that\r\n      time but then didn't get implemented right away because of competing priorities.\r\n   <\/p>\r\n   <p>To understand why we eventually did decide to go ahead and implement it, you have to understand how this fairly minor syntactic\r\n      issue is connected to our long-term efforts to do something much more important:  provide automatic (and helpful!) advice\r\n      to MATLAB users about their code.  If you've used the MATLAB Editor at all over the last few years, you've probably observed\r\n      how it marks code sections and makes suggestions to you.  These suggestions fall into several categories, such as errors,\r\n      possible errors, performance, new features, etc.\r\n   <\/p>\r\n   <p>One of the things we try to flag is potential programming errors. A coding pattern that very often indicates a programming\r\n      error is when you save a computed value by assigning it to a variable, but then you never use that saved value.  At the very\r\n      least, that pattern may indicate \"dead code,\" or code that was doing something useful at one time but is now just <a href=\"http:\/\/dictionary.reference.com\/browse\/cruft\">cruft<\/a>.\r\n   <\/p>\r\n   <p>Both of the conventions mentioned above (<tt>[unused,idx] = sort(A)<\/tt> and <tt>[idx,idx] = sort(A)<\/tt>) exhibit this pattern of computing and saving a value and then never using it.\r\n   <\/p>\r\n   <p>That might not be obvious for the <tt>[idx,idx] = sort(A)<\/tt> case, but it's true. The first output of <tt>sort<\/tt> is assigned to the variable <tt>idx<\/tt>.  Then the second output is assigned to <tt>idx<\/tt>, causing the first output value to be discarded without ever being used.\r\n   <\/p>\r\n   <p>These cases aren't programming errors, though, because we've given you no other way to ignore outputs.  We can't automatically\r\n      and reliably distinguish between the intentional <tt>[idx,idx] = sort(A)<\/tt> and the similar-looking <tt>[y,y] = foobar(x)<\/tt> that is the result of a typo.\r\n   <\/p>\r\n   <p>I think it's important for the programmer to communicate his or her intent very clearly (which is why I tend to prefer the\r\n      <tt>[unused,idx] = sort(A)<\/tt> convention).  It is useful to have a way to communicate intent syntactically instead of by convention.  And the new syntax\r\n      helps us, in a small way, progress toward our long-term goal of helping MATLAB users write better MATLAB code.\r\n   <\/p>\r\n   <p>OK, fire away!<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_91a571737bf74c4eabd78b8a1fdbb8a6() {\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='91a571737bf74c4eabd78b8a1fdbb8a6 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 91a571737bf74c4eabd78b8a1fdbb8a6';\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 = '';\r\n        copyright = 'Copyright 2010 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_91a571737bf74c4eabd78b8a1fdbb8a6()\"><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.9<br><\/p>\r\n<\/div>\r\n<!--\r\n91a571737bf74c4eabd78b8a1fdbb8a6 ##### SOURCE BEGIN #####\r\n%%\r\n% Last fall <https:\/\/blogs.mathworks.com\/loren Loren> wrote a \r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/ \r\n% blog post> about a new syntax in R2009b for ignoring\r\n% function inputs or function outputs.  For example, suppose you call |sort| and\r\n% only want the second output.  In R2009b you can do that using this syntax:\r\n%\r\n%   [~, idx] = sort(A);\r\n%\r\n% The tilde character here is used as a placeholder to indicate that you don't\r\n% need the first output argument of |sort|.\r\n%\r\n% For another example, suppose you write a function that has to take three input\r\n% arguments (because another function is always going to pass it three\r\n% arguments), but your function doesn't need the second argument.  Then you can\r\n% write the first line of your function this way:\r\n%\r\n%   function out = myfun(A, ~, C)\r\n%\r\n% This new syntax has drawn a startling amount of discussion, both in the\r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/#comments \r\n% comments> on Loren's post as well as in the comp.soft-sys.matlab newsgroup.\r\n% (See the thread \r\n% <http:\/\/view_thread\/269584#706689 \r\n% \"Getting indexes of rows of matrix with more than n repetitions,\"> for example.)\r\n%\r\n% Responses to this new syntax have fallen into roughly four categories:\r\n%\r\n% * \"Finally, I've been waiting for this!\"\r\n% * Clarification questions about how it works and how to use it.\r\n% * Complaints about the specific syntax chosen and suggestions for\r\n% alternatives.\r\n% * \"The intro of the tilde op was nothing but a big, useless blunder.\"\r\n%\r\n% The passionate arguments in comp.soft-sys.matlab caught my eye and have\r\n% prompted me to add my two cents here.  Although I have no particular\r\n% expectation of changing anyone's mind, I thought it might be interesting to\r\n% address one particular question that was expressed well by\r\n% <https:\/\/blogs.mathworks.com\/loren\/2009\/09\/11\/matlab-release-2009b-best-new-feature-or\/#comment-30611 Matt Fig>: \"If something already works, why complicate\r\n% things?\"\r\n%\r\n% Matt means that we already have at least a couple of ways to ignore an\r\n% output variable.  The first is to use a variable name that (hopefully) makes the\r\n% programmer's intent clear:\r\n%\r\n%    [unused, idx] = sort(A);\r\n%\r\n% The second technique takes advantage of the way MATLAB assigns function\r\n% outputs from left to right:\r\n%\r\n%    [idx, idx] = sort(A);\r\n%\r\n% So why go to the trouble of introducing a new syntax?\r\n%\r\n% Well, you'll probably get a somewhat different answer from every MATLAB\r\n% developer you ask.  Some might even agree.  (I'll note, however, that this was\r\n% one of the least-controversial MATLAB syntax proposals ever considered by the\r\n% MATLAB language team.)\r\n%\r\n% The proposed syntax was originally considered years ago, sometime around 2001.\r\n% The proposal was approved internally at that time but then didn't get\r\n% implemented right away because of competing priorities.\r\n%\r\n% To understand why we eventually did decide to go ahead and implement it, you\r\n% have to understand how this fairly minor syntactic issue is connected to our\r\n% long-term efforts to do something much more important:  provide automatic (and\r\n% helpful!) advice to MATLAB users about their code.  If you've used the MATLAB\r\n% Editor at all over the last few years, you've probably observed how it marks\r\n% code sections and makes suggestions to you.  These suggestions fall into\r\n% several categories, such as errors, possible errors, performance, new\r\n% features, etc.\r\n%\r\n% One of the things we try to flag is potential programming errors. A coding\r\n% pattern that very often indicates a programming error is when you save a\r\n% computed value by assigning it to a variable, but then you never use that saved\r\n% value.  At the very least, that pattern may indicate \"dead code,\" or\r\n% code that was doing something useful at one time but is now just \r\n% <http:\/\/dictionary.reference.com\/browse\/cruft cruft>.\r\n%\r\n% Both of the conventions mentioned above (|[unused,idx] = sort(A)| and\r\n% |[idx,idx] = sort(A)|) exhibit this pattern of computing and saving a value\r\n% and then never using it.\r\n%\r\n% That might not be obvious for the |[idx,idx] = sort(A)| case, but it's true.\r\n% The first output of |sort| is assigned to the variable |idx|.  Then the second\r\n% output is assigned to |idx|, causing the first output value to be discarded\r\n% without ever being used.\r\n%\r\n% These cases aren't programming errors, though, because we've given you no other\r\n% way to ignore outputs.  We can't automatically and reliably distinguish\r\n% between the intentional |[idx,idx] = sort(A)| and the similar-looking |[y,y] =\r\n% foobar(x)| that is the result of a typo.\r\n%\r\n% I think it's important for the programmer to communicate his or her intent\r\n% very clearly (which is why I tend to prefer the |[unused,idx] = sort(A)|\r\n% convention).  It is useful to have a way to communicate intent syntactically\r\n% instead of by convention.  And the new syntax helps us, in a small way,\r\n% progress toward our long-term goal of helping MATLAB users write better MATLAB\r\n% code.\r\n%\r\n% OK, fire away!\r\n##### SOURCE END ##### 91a571737bf74c4eabd78b8a1fdbb8a6\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Last fall Loren wrote a blog post about a new syntax in R2009b for ignoring function inputs or function outputs.  For example, suppose you call sort and only want the second output.  In R2009b... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2010\/01\/11\/about-the-unused-argument-syntax-in-r2009b\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[484],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/311"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=311"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/311\/revisions"}],"predecessor-version":[{"id":2750,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/311\/revisions\/2750"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=311"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=311"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=311"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}