{"id":9,"date":"2005-12-28T14:12:15","date_gmt":"2005-12-28T19:12:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=9"},"modified":"2018-01-16T10:33:24","modified_gmt":"2018-01-16T15:33:24","slug":"evading-eval","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2005\/12\/28\/evading-eval\/","title":{"rendered":"Evading eval"},"content":{"rendered":"<p>If you are a regular reader of the MATLAB Usenet newsgroup, <kbd>comp.soft-sys.matlab<\/kbd>, found on the MathWorks User Community page, you are probably familiar with repeated advice to not use the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/eval.html\"><kbd>eval<\/kbd><\/a> .  Today I plan to tell you more about why this is generally good advice.<\/p>\n<h3>Code Clarity \/ Readability<\/h3>\n<p>Statements containing a call to <kbd>eval<\/kbd> have a way of obscuring the underlying intent of the code though this is rarely a goal of the author.  What do I mean by this?   Well, in under 3 seconds, can you discern what the following code means?<\/p>\n<pre class=\"code\">\r\na = who ;\r\na = a(~cellfun('isempty',regexp(a,'2$'))) ;\r\nstr = sprintf('%s,',a{:}) ;\r\nstr = sprintf('B = cat(1,%s) ;',str(1:end-1)) ;\r\neval(str) ; \r\n<\/pre>\n<p>Unless you are one of the proponents of this code pattern (see <a>this newsgroup post<\/a>, for example), you likely couldn't tell that the code is saying<\/p>\n<blockquote><p>\nConcatenate all variables that have '_2' in their name, i.e. a_2, b_2, c_2\n<\/p><\/blockquote>\n<h3>Efficiency<\/h3>\n<p>When a function M-file is run in MATLAB for the first time in a session, MATLAB reads in the file, translates it into an internal representation, analyzes the contents, and executes the code.  The next time you use this function in the same MATLAB session, assuming the file hasn't changed, MATLAB doesn't need to reread and translate the file.  <\/p>\n<p>However, if the M-file contains any calls to the function <kbd>eval<\/kbd>, at least those lines of code will need to be reinterpreted each time the function is run.  Why?  Because there is no guarantee, and it is generally not true, that the expression that was executed in the first instance is the same as subsequent ones.  The expression may require different MATLAB functions or variables.  In any case, to be sure MATLAB gets the right answer, at least the lines of code containing <kbd>eval<\/kbd> statements need to be re-analyzed with each invocation.<\/p>\n<h3>Analyzability<\/h3>\n<p>In an M-file, if the functions being called are coded into strings so they can be placed into <kbd>eval<\/kbd> statements, it is very difficult to build up a complete understanding of the functions and files that your M-file depends upon, especially in a static way.  It might be possible, though probably difficult and perhaps error-prone, to scan through an M-file to see where strings are used in <kbd>eval<\/kbd> statements and to try to pull out the possible function and file names from these.<\/p>\n<h3>Techniques to evade <kbd>eval<\/kbd><\/h3>\n<ul>\n<li>Use structs and have fields with the chosen names (<a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=6\">using dynamic field references<\/a>) instead of naming variables with those names<\/li>\n<li><a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/str2func.html\"><kbd>str2func<\/kbd><kbd><\/kbd><\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>If you are a regular reader of the MATLAB Usenet newsgroup, comp.soft-sys.matlab, found on the MathWorks User Community page, you are probably familiar with repeated advice to not use the MATLAB... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2005\/12\/28\/evading-eval\/\">read more >><\/a><\/p>\n","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/9"}],"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=9"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"predecessor-version":[{"id":2644,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/9\/revisions\/2644"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}