{"id":62,"date":"2015-02-10T17:51:18","date_gmt":"2015-02-10T17:51:18","guid":{"rendered":"https:\/\/blogs.mathworks.com\/developer\/?p=62"},"modified":"2015-02-12T17:03:30","modified_gmt":"2015-02-12T17:03:30","slug":"global","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/developer\/2015\/02\/10\/global\/","title":{"rendered":"Global In Global Out"},"content":{"rendered":"<div class=\"content\"><!--introduction--><!--\/introduction--><p>Over the past couple years at MathWorks we have been transitioning from a legacy procedural testing framework to what is now MATLAB's native unit test framework. It has been great to see the internal adoption rate of this framework increase steadily as more teams begin to see and utilize its various benefits.<\/p><p>However one of the biggest challenges we have seen in migrating to the new MATLAB unit testing paradigm is the existence of team specific helpers, tools, and ecosystems that are incompatible with the new framework.<\/p><p>Why are they incompatible?<\/p><p>Well, because a central feature of the legacy framework is globally available and <a href=\"http:\/\/michaelfeathers.typepad.com\/michael_feathers_blog\/2012\/12\/global-variables-destroy-design-information.html\">Global Variables Destroy Design Information<\/a>. Read that post first it isn't too long. Near the end of these comments I started jumping out of my chair with my fist in the air yelling Yes! YES! YESSS!!!<\/p><p>Go ahead read it. I'll wait...<\/p><p>Ready? Great stuff eh? Any of that sound familiar? How about:<\/p><p>\r\n  <p style=\"margin-right:40px; margin-left:40px;font-style: italic;\">\r\n      \"Many people say that the wouldn't want to [design without globals]\r\n      because they imagine that they'd end up passing extra parameters\r\n      all over the place in their code.\"\r\n  <\/p>\r\n<\/p><p>...or maybe<\/p><p>\r\n  <p style=\"margin-right:40px; margin-left:40px;font-style: italic;\">\r\n      \"When a thing is global it is accessible everywhere, and people\r\n      tend to use it without discipline.\"\r\n  <\/p>\r\n<\/p><p>And therein lies the true cost of using global state. There are many, many reasons why global state is a bad thing, but this is perhaps the most pervasive and sinister: global state turns the entire design of the entire ecosystem into one big coupled garbled hard-to-change mess. Global state acts as an implicit parameter to <b>every single function and method call in your ecosystem<\/b> . When we allow global state we are saying, \"Feel free to use this as you like, oh and by the way, since you need it so often we will just go ahead and waive your requirement to tell me you need to use it.\" That's a little bit like a bank saying, \"You seem to withdraw money from your account so often that we will just go ahead and waive the requirement for you to use a bank card and pin. No need to identify yourself with that context, we'll assume that whenever money is pulled from your account it is approved by you. Look how easy we've made it for you. You don't even have to carry around a wallet!\"<\/p><p>What could go wrong?<\/p><p>The fallacy is that the global state makes things seem easier at first because it does not require design discipline. It's only down the road that we realize the problems it creates, but by that time the design damage has been done. It is nigh impossible to back out designs that rely on it. The poor design choice of introducing global state negatively effects the design of the entire set of tools that operate in its ecosystem.<\/p><p>Well, in the MATLAB unit test framework we have not relied on global state and have instead required those who operate in the framework to have the appropriate context to do things like verifications and assertions. In tests that do not require using the same ecosystem as legacy tests this has not lead to any significant problems. When the ecosystem is clean there are many in model approaches and patterns to writing tests and related test tools.<\/p><p>However, on the flipside when a test utilizes the full ecosystem and tools built around global accessibility of its parameters it is a much harder problem.<\/p><p>This is not just a testing problem, this is a very general software engineering principle. Introducing global state to the software system causes problems with concurrency (e.g. parallelism using Parallel Computing Toolbox), significantly complicates reentrancy, and is almost always marked by <a href=\"https:\/\/en.wikipedia.org\/wiki\/Action_at_a_distance_(computer_programming)\">spooky action at a distance<\/a>.<\/p><p>What is global state in MATLAB? The obvious answer is usage of <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/global.html\">global variables<\/a>. However here is a short list of other constructs which can easily be used to introduce global state in MATLAB:<\/p><div><ul><li><tt>persistent<\/tt> variables<\/li><li>objects with <tt>Constant<\/tt> properties containing mutable handle objects<\/li><li>default property values in MATLAB classes containing handle objects<\/li><li>appdata on a global object, such as the root object<\/li><li>the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Singleton_pattern\">Singleton<\/a> design pattern if it gives access to any mutable state<\/li><li>Variables defined in the globally accessible (and mutable) base workspace<\/li><\/ul><\/div><p>I have found that very rarely (almost never) does the solution to a given problem truly require using such global state. Think twice (twenty times!) about your design if you are tempted. If it is used in the end, always make sure that the exposure of this state is limited to as few clients as possible.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_cb3d207b649d4e3e97578586ee2b9823() {\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='cb3d207b649d4e3e97578586ee2b9823 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' cb3d207b649d4e3e97578586ee2b9823';\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        copyright = 'Copyright 2015 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_cb3d207b649d4e3e97578586ee2b9823()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015b<br><\/p><\/div><!--\r\ncb3d207b649d4e3e97578586ee2b9823 ##### SOURCE BEGIN #####\r\n%% Global In Global Out\r\n\r\n%%\r\n% Over the past couple years at MathWorks we have been transitioning from a\r\n% legacy procedural testing framework to what is now MATLAB's native unit\r\n% test framework. It has been great to see the internal adoption rate of\r\n% this framework increase steadily as more teams begin to see and utilize\r\n% its various benefits.\r\n% \r\n% However one of the biggest challenges we have seen in migrating to the\r\n% new MATLAB unit testing paradigm is the existence of team specific helpers,\r\n% tools, and ecosystems that are incompatible with the new framework.\r\n% \r\n% Why are they incompatible?\r\n% \r\n% Well, because a central feature of the legacy framework is globally\r\n% available and\r\n% <http:\/\/michaelfeathers.typepad.com\/michael_feathers_blog\/2012\/12\/global-variables-destroy-design-information.html\r\n% Global Variables Destroy Design Information>. Read that post first it\r\n% isn't too long. Near the end of these comments I started jumping out of\r\n% my chair with my fist in the air yelling Yes! YES! YESSS!!!\r\n% \r\n% Go ahead read it. I'll wait...\r\n% \r\n% Ready? Great stuff eh? Any of that sound familiar? How about:\r\n%\r\n% <html>\r\n%   <p style=\"margin-right:40px; margin-left:40px;font-style: italic;\">\r\n%       \"Many people say that the wouldn't want to [design without globals]\r\n%       because they imagine that they'd end up passing extra parameters\r\n%       all over the place in their code.\"\r\n%   <\/p>\r\n% <\/html>\r\n% \r\n%  \r\n% \r\n% ...or maybe\r\n% \r\n% <html>\r\n%   <p style=\"margin-right:40px; margin-left:40px;font-style: italic;\">\r\n%       \"When a thing is global it is accessible everywhere, and people\r\n%       tend to use it without discipline.\"\r\n%   <\/p>\r\n% <\/html>\r\n% \r\n% And therein lies the true cost of using global state. There are many,\r\n% many reasons why global state is a bad thing, but this is perhaps the\r\n% most pervasive and sinister: global state turns the entire design of the\r\n% entire ecosystem into one big coupled garbled hard-to-change mess. Global\r\n% state acts as an implicit parameter to *every single function and method\r\n% call in your ecosystem* . When we allow global state we are saying, \"Feel\r\n% free to use this as you like, oh and by the way, since you need it so\r\n% often we will just go ahead and waive your requirement to tell me you\r\n% need to use it.\" That's a little bit like a bank saying, \"You seem to\r\n% withdraw money from your account so often that we will just go ahead and\r\n% waive the requirement for you to use a bank card and pin. No need to\r\n% identify yourself with that context, we'll assume that whenever money is\r\n% pulled from your account it is approved by you. Look how easy we've made\r\n% it for you. You don't even have to carry around a wallet!\"\r\n% \r\n% What could go wrong?\r\n% \r\n% The fallacy is that the global state makes things seem easier at first\r\n% because it does not require design discipline. It's only down the road\r\n% that we realize the problems it creates, but by that time the design\r\n% damage has been done. It is nigh impossible to back out designs that rely\r\n% on it. The poor design choice of introducing global state negatively\r\n% effects the design of the entire set of tools that operate in its\r\n% ecosystem.\r\n% \r\n% Well, in the MATLAB unit test framework we have not relied on global\r\n% state and have instead required those who operate in the framework to\r\n% have the appropriate context to do things like verifications and\r\n% assertions. In tests that do not require using the same ecosystem as\r\n% legacy tests this has not lead to any significant problems. When the\r\n% ecosystem is clean there are many in model approaches and patterns to\r\n% writing tests and related test tools.\r\n% \r\n% However, on the flipside when a test utilizes the full ecosystem and\r\n% tools built around global accessibility of its parameters it is a much\r\n% harder problem.\r\n%\r\n% This is not just a testing problem, this is a very general software\r\n% engineering principle. Introducing global state to the software system\r\n% causes problems with concurrency (e.g. parallelism using Parallel\r\n% Computing Toolbox), significantly complicates reentrancy, and is almost\r\n% always marked by\r\n% <https:\/\/en.wikipedia.org\/wiki\/Action_at_a_distance_(computer_programming)\r\n% spooky action at a distance>.\r\n%\r\n% What is global state in MATLAB? The obvious answer is usage of\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/global.html global variables>.\r\n% However here is a short list of other constructs which can easily be used\r\n% to introduce global state in MATLAB:\r\n%\r\n% * |persistent| variables\r\n% * objects with |Constant| properties containing mutable handle objects\r\n% * default property values in MATLAB classes containing handle objects\r\n% * appdata on a global object, such as the root object\r\n% * the <https:\/\/en.wikipedia.org\/wiki\/Singleton_pattern Singleton> design\r\n% pattern if it gives access to any mutable state\r\n% * Variables defined in the globally accessible (and mutable) base workspace\r\n%\r\n% I have found that very rarely (almost never) does the solution to a given\r\n% problem truly require using such global state. Think twice (twenty\r\n% times!) about your design if you are tempted. If it is used in the end,\r\n% always make sure that the exposure of this state is limited to as few\r\n% clients as possible.\r\n##### SOURCE END ##### cb3d207b649d4e3e97578586ee2b9823\r\n-->","protected":false},"excerpt":{"rendered":"<p>Over the past couple years at MathWorks we have been transitioning from a legacy procedural testing framework to what is now MATLAB's native unit test framework. It has been great to see the internal... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/developer\/2015\/02\/10\/global\/\">read more >><\/a><\/p>","protected":false},"author":90,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[6,5],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/62"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/users\/90"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/comments?post=62"}],"version-history":[{"count":5,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/62\/revisions"}],"predecessor-version":[{"id":67,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/posts\/62\/revisions\/67"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/media?parent=62"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/categories?post=62"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/developer\/wp-json\/wp\/v2\/tags?post=62"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}