{"id":2439,"date":"2009-07-17T13:36:44","date_gmt":"2009-07-17T13:36:44","guid":{"rendered":"https:\/\/blogs.mathworks.com\/pick\/2009\/07\/17\/want-to-get-rid-of-your-code-comments\/"},"modified":"2009-07-17T13:36:44","modified_gmt":"2009-07-17T13:36:44","slug":"want-to-get-rid-of-your-code-comments","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/pick\/2009\/07\/17\/want-to-get-rid-of-your-code-comments\/","title":{"rendered":"Want to get rid of your code comments?"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911\">Brett<\/a>'s Pick this week allows you to <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4645---matlab-comment-stripping-toolbox\">strip comments from your code<\/a>, should you need to do so.\r\n      <\/p>\r\n   <\/introduction>\r\n   <p>Several years ago--while building several large, multi-file applications--I found myself getting unwanted output scrolling\r\n      to my command window. Clearly, I had omitted some line-terminating semicolons somewhere in my morass of code. Finding these\r\n      instances was tedious and time-consuming (and a task that <a href=\"https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/mlint.html\"><tt>MLINT<\/tt><\/a> now makes trivially easy).\r\n   <\/p>\r\n   <p>I decided to write a <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/53-shift\">function to detect missing semicolons<\/a>, but quickly found that coding to ignore comments was beyond my capacity--at least at the time.\r\n   <\/p>\r\n   <p>Fortunately, <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/1392\">Peter Acklam<\/a> (whom you may know as a frequent contributor to the fine CSSM newsgroup, and as a MATLAB vectorization guru), came to my\r\n      rescue and, in response to my queries, posted his <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4645---matlab-comment-stripping-toolbox\">Comment-Stripping Toolbox<\/a>.\r\n   <\/p>\r\n   <p>I haven't had to use my <tt>missingsemicolons<\/tt> function in quite a while, but every now and then I still have occasion to strip comments from some MATLAB code. And when\r\n      I do, I still turn to Peter's submission.\r\n   <\/p>\r\n   <p>The Comment-Stripping Toolbox allows you to strip comments from a string, or from a file specified by name or by file I.D.\r\n      One of the things that impressed me the most about Peter's submission was that it reflected his mastery of regular expressions:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/stripcomments.png\"> <\/p>\r\n   <p>If you ever need to strip comments, or just want to see some impressive code, try out Peter's file, and let us know about\r\n      it <a href=\"https:\/\/blogs.mathworks.com\/pick\/?p=2439#respond\">here.<\/a><\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_f8474dcc3dd044fcb60dffe4d7e5a9bd() {\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='f8474dcc3dd044fcb60dffe4d7e5a9bd ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' f8474dcc3dd044fcb60dffe4d7e5a9bd';\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 = 'Brett Shoelson';\r\n        copyright = 'Copyright 2009 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_f8474dcc3dd044fcb60dffe4d7e5a9bd()\"><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.8<br><\/p>\r\n<\/div>\r\n<!--\r\nf8474dcc3dd044fcb60dffe4d7e5a9bd ##### SOURCE BEGIN #####\r\n%% Want to get rid of your code comments?\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/911 Brett>'s Pick this week allows you to <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4645---matlab-comment-stripping-toolbox strip comments from your code>, should you need to do so.\r\n\r\n%%\r\n% Several years agoREPLACE_WITH_DASH_DASHwhile building several large, multi-file\r\n% applicationsREPLACE_WITH_DASH_DASHI found myself getting unwanted output scrolling\r\n% to my command window. Clearly, I had omitted some\r\n% line-terminating semicolons somewhere in my morass of code.\r\n% Finding these instances was tedious and time-consuming (and a\r\n% task that <https:\/\/www.mathworks.com\/help\/releases\/R2009a\/techdoc\/ref\/mlint.html |MLINT|> now makes trivially easy).\r\n%\r\n% I decided to write a <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/53-shift function to detect missing semicolons>, but quickly\r\n% found that coding to ignore comments was\r\n% beyond my capacityREPLACE_WITH_DASH_DASHat least at the time.\r\n\r\n%%\r\n% Fortunately, <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/authors\/1392 Peter Acklam> (whom you may know as a frequent contributor to the fine CSSM newsgroup, and as a MATLAB vectorization guru), \r\n% came to my rescue and, in response to my queries, posted his <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/4645---matlab-comment-stripping-toolbox Comment-Stripping Toolbox>.\r\n\r\n%% \r\n% I haven't had to use my |missingsemicolons| function in quite a while,\r\n% but every now and then I still have occasion to strip comments\r\n% from some MATLAB code. And when I do, I still turn to Peter's\r\n% submission.\r\n\r\n%%\r\n% The Comment-Stripping Toolbox allows you to strip comments from a string, or\r\n% from a file specified by name or by file I.D. One of the things\r\n% that impressed me the most about Peter's submission was that it reflected his mastery of\r\n% regular expressions:\r\n% \r\n% <<https:\/\/blogs.mathworks.com\/pick\/..\/images\/pick\/stripcomments.png>>\r\n% \r\n\r\n%%\r\n% If you ever need to strip comments, or just want to see some\r\n% impressive code, try out Peter's file, and let us know about it\r\n% <https:\/\/blogs.mathworks.com\/pick\/?p=2439#respond here.>\r\n\r\n\r\n##### SOURCE END ##### f8474dcc3dd044fcb60dffe4d7e5a9bd\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Brett's Pick this week allows you to strip comments from your code, should you need to do so.\r\n      \r\n   \r\n   Several years ago--while building several large, multi-file applications--I... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/pick\/2009\/07\/17\/want-to-get-rid-of-your-code-comments\/\">read more >><\/a><\/p>","protected":false},"author":34,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[16],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2439"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/users\/34"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/comments?post=2439"}],"version-history":[{"count":0,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/posts\/2439\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/media?parent=2439"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/categories?post=2439"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/pick\/wp-json\/wp\/v2\/tags?post=2439"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}