{"id":494,"date":"2012-07-16T13:58:59","date_gmt":"2012-07-16T18:58:59","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=494"},"modified":"2012-07-16T13:58:59","modified_gmt":"2012-07-16T18:58:59","slug":"who-what-why-but-not-this","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2012\/07\/16\/who-what-why-but-not-this\/","title":{"rendered":"Who, what, why, but not this"},"content":{"rendered":"<!DOCTYPE html\r\n  PUBLIC \"-\/\/W3C\/\/DTD HTML 4.01 Transitional\/\/EN\">\r\n<style type=\"text\/css\">\r\n\r\nh1 { font-size:18pt; }\r\nh2.titlebg { font-size:13pt; }\r\nh3 { color:#4A4F55; padding:0px; margin:5px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:11pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\nh4 { color:#4A4F55; padding:0px; margin:0px 0px 5px; font-family:Arial, Helvetica, sans-serif; font-size:10pt; font-weight:bold; line-height:140%; border-bottom:1px solid #d6d4d4; display:block; }\r\n   \r\np { padding:0px; margin:0px 0px 20px; }\r\nimg { padding:0px; margin:0px 0px 20px; border:none; }\r\np img, pre img, tt img, li img { margin-bottom:0px; } \r\n\r\nul { padding:0px; margin:0px 0px 20px 23px; list-style:square; }\r\nul li { padding:0px; margin:0px 0px 7px 0px; background:none; }\r\nul li ul { padding:5px 0px 0px; margin:0px 0px 7px 23px; }\r\nul li ol li { list-style:decimal; }\r\nol { padding:0px; margin:0px 0px 20px 0px; list-style:decimal; }\r\nol li { padding:0px; margin:0px 0px 7px 23px; list-style-type:decimal; }\r\nol li ol { padding:5px 0px 0px; margin:0px 0px 7px 0px; }\r\nol li ol li { list-style-type:lower-alpha; }\r\nol li ul { padding-top:7px; }\r\nol li ul li { list-style:square; }\r\n\r\npre, tt, code { font-size:12px; }\r\npre { margin:0px 0px 20px; }\r\npre.error { color:red; }\r\npre.codeinput { padding:10px; border:1px solid #d3d3d3; background:#f7f7f7; }\r\npre.codeoutput { padding:10px 11px; margin:0px 0px 20px; color:#4c4c4c; }\r\n\r\n@media print { pre.codeinput, pre.codeoutput { word-wrap:break-word; width:100%; } }\r\n\r\nspan.keyword { color:#0000FF }\r\nspan.comment { color:#228B22 }\r\nspan.string { color:#A020F0 }\r\nspan.untermstring { color:#B20000 }\r\nspan.syscmd { color:#B28C00 }\r\n\r\n.footer { width:auto; padding:10px 0px; margin:25px 0px 0px; border-top:1px dotted #878787; font-size:0.8em; line-height:140%; font-style:italic; color:#878787; text-align:left; float:none; }\r\n.footer p { margin:0px; }\r\n\r\n  <\/style><div class=\"content\"><!--introduction--><p>Dave Foti, who manages development of object oriented programming features in the MATLAB language, is back with a discussion of object parameters in methods.<\/p><!--\/introduction--><p>A fairly frequent question from my colleagues and some of you is why MATLAB classes don&#8217;t have a keyword like &#8220;this&#8221; inside methods and support for the other behaviors found in languages with a &#8220;this&#8221; keyword. Having to declare the name of every object parameter to a method may be a minor inconvenience, but having to prefix properties with that object parameter is much more cumbersome.  For example, consider the following Java method:<\/p><pre>public int getArea() {\r\n    return Width * Height;\r\n}<\/pre><p>In the MATLAB language, such a function would be written as something like:<\/p><pre class=\"codeinput\">type <span class=\"string\">Rect\/getArea<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction A = getArea(R)\r\n    A = R.Width .* R.Height;\r\nend\r\n<\/pre><p>While the above example looks simpler in Java, there are situations where implicit object arguments get in the way.  Many functions such as binary operators work on multiple objects.  When a function needs to work on data from multiple objects, languages with implicit object parameters can lead to an asymmetry in handling the different objects.  For example, consider a Java intersect method to return the intersection of two input rectangles:<\/p><pre>public Rect intersect(Rect B) {\r\n    Rect C = new Rect();\r\n    C.X = max(X, B.X);\r\n    C.Y = max(Y, B.Y);\r\n    C.Width = min(X+Width, B.X + B.Width) &#8211; C.X;\r\n    C.Height = min(Y+Height, B.Y + B.Height) &#8211; C.Y;\r\n    return C;\r\n}<\/pre><p>To restore some symmetry, sometimes I see the following pattern:<\/p><pre>public Rect intersect(Rect that) {\r\n    Rect other = new Rect();\r\n    other.X = max(this.X, that.X);\r\n    other.Y = max(this.Y, that.Y);\r\n    other.Width = min(this.X+Width, that.X + that.Width) &#8211; other.X;\r\n    other.Height = min(this.Y+Height, that.Y + that.Height) &#8211; other.Y;\r\n    return other;\r\n}<\/pre><p>However, a MATLAB method naturally accesses all rectangles in the same manner:<\/p><pre class=\"codeinput\">type <span class=\"string\">Rect\/intersect<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction C = intersect(A, B) \r\n    C.X = max(A.X, B.X);\r\n    C.Y = max(A.Y, B.Y);\r\n    C.Width = min(A.X+A.Width, B.X+B.Width) - C.X;\r\n    C.Height = min(A.Y+A.Height, B.Y+B.Height) - C.Y;\r\nend\r\n<\/pre><p>Another important consideration for the MATLAB language is operators that work on objects and numbers or different kinds of objects.  For example, a Rational number class may provide arithmetic operators that work on any combination of rational values and built-in numeric values.<\/p><pre class=\"codeinput\">x = Rational(5,7)\r\n<\/pre><pre class=\"codeoutput\">x = \r\n5\/7\r\n\r\n<\/pre><pre class=\"codeinput\">y = 3 .* x\r\n<\/pre><pre class=\"codeoutput\">y = \r\n15\/7\r\n\r\n<\/pre><pre class=\"codeinput\">z = x .* 3\r\n<\/pre><pre class=\"codeoutput\">z = \r\n15\/7\r\n\r\n<\/pre><pre class=\"codeinput\">t = y .* x\r\n<\/pre><pre class=\"codeoutput\">t = \r\n75\/49\r\n\r\n<\/pre><p>The Rational function times is called in all three multiplications above:<\/p><pre class=\"codeinput\">type <span class=\"string\">Rational\/times<\/span>\r\n<\/pre><pre class=\"codeoutput\">\r\nfunction R = times(X,Y)\r\n    if isa(X, 'Rational')\r\n        if isa(Y, 'Rational')\r\n            R= Rational(X.Numerator .* Y.Numerator, ...\r\n                        X.Denominator .* Y.Denominator);\r\n        else\r\n            if ~isnumeric(Y)\r\n                error('Y must be a number.');\r\n            end\r\n            R = Rational(X.Numerator .* Y, X.Denominator);\r\n        end\r\n    else\r\n        if ~isnumeric(X)\r\n            error('X must be a number.');\r\n        end\r\n        R = Rational(X .* Y.Numerator, Y.Denominator);\r\n    end\r\nend\r\n<\/pre><p>In this example we can see the value of making all arguments explicit. It allows operators to work on a first double argument and a second Rational argument.  It also reinforces the fact that functions inside the class can access private data and methods on any instance of the class. The times method is a method of the Rational class that works on one or more instances of Rational numbers.  When it works on two Rational numbers to produce a new Rational result, there is nothing special about any one of these three instances.<\/p><p>MATLAB functions, methods, and variables share the same namespace. Object properties are scoped to the class of the object and their names can be the same as MATLAB keywords.  While it would sometimes be more convenient, implicit references to an object parameter would also mean additional restrictions on property names and potential conflicts with function names.<\/p><p>I would be interested in hearing <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=494#respond\">your thoughts<\/a> on this subject.  In your code, what is the relative frequency of methods that work on a single object versus multiple objects?  How important is the convenience of implicit object access to you?  Would you like a way to declare that a block of methods define access operations on an object and make the object implicit within these methods?<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_ed6e25745b51431b9597f646a9fdb547() {\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='ed6e25745b51431b9597f646a9fdb547 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' ed6e25745b51431b9597f646a9fdb547';\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 2012 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_ed6e25745b51431b9597f646a9fdb547()\"><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; 7.14<br><\/p><p class=\"footer\"><br>\r\n      Published with MATLAB&reg; 7.14<br><\/p><\/div><!--\r\ned6e25745b51431b9597f646a9fdb547 ##### SOURCE BEGIN #####\r\n%% Who, what, why, but not this\r\n% \r\n% Dave Foti, who manages development of object oriented programming\r\n% features in the MATLAB language, is back with a discussion of object\r\n% parameters in methods.\r\n%% \r\n% A fairly frequent question from my colleagues and some of you is why\r\n% MATLAB classes don\u00e2\u20ac\u2122t have a keyword like \u00e2\u20ac\u0153this\u00e2\u20ac\ufffd inside methods and\r\n% support for the other behaviors found in languages with a \u00e2\u20ac\u0153this\u00e2\u20ac\ufffd keyword.\r\n% Having to declare the name of every object parameter to a method may be a\r\n% minor inconvenience, but having to prefix properties with that object\r\n% parameter is much more cumbersome.  For example, consider the following\r\n% Java method:\r\n%\r\n%  public int getArea() {\r\n%      return Width * Height;\r\n%  } \r\n%% \r\n% In the MATLAB language, such a function would be written as something\r\n% like:\r\ntype Rect\/getArea \r\n%% \r\n% While the above example looks simpler in Java, there are situations where\r\n% implicit object arguments get in the way.  Many functions such as binary\r\n% operators work on multiple objects.  When a function needs to work on\r\n% data from multiple objects, languages with implicit object parameters can\r\n% lead to an asymmetry in handling the different objects.  For example,\r\n% consider a Java intersect method to return the intersection of two input\r\n% rectangles:\r\n% \r\n%  public Rect intersect(Rect B) {\r\n%      Rect C = new Rect(); \r\n%      C.X = max(X, B.X); \r\n%      C.Y = max(Y, B.Y);\r\n%      C.Width = min(X+Width, B.X + B.Width) \u00e2\u20ac\u201c C.X; \r\n%      C.Height = min(Y+Height, B.Y + B.Height) \u00e2\u20ac\u201c C.Y; \r\n%      return C;\r\n%  }\r\n% \r\n% To restore some symmetry, sometimes I see the following pattern:\r\n%  \r\n%  public Rect intersect(Rect that) {\r\n%      Rect other = new Rect(); \r\n%      other.X = max(this.X, that.X); \r\n%      other.Y = max(this.Y, that.Y); \r\n%      other.Width = min(this.X+Width, that.X + that.Width) \u00e2\u20ac\u201c other.X;\r\n%      other.Height = min(this.Y+Height, that.Y + that.Height) \u00e2\u20ac\u201c other.Y;\r\n%      return other;\r\n%  }\r\n% \r\n% However, a MATLAB method naturally accesses all rectangles in the same\r\n% manner:\r\n% \r\ntype Rect\/intersect\r\n%% \r\n% Another important consideration for the MATLAB language is operators that\r\n% work on objects and numbers or different kinds of objects.  For example,\r\n% a Rational number class may provide arithmetic operators that work on any\r\n% combination of rational values and built-in numeric values.  \r\nx = Rational(5,7)\r\n%%\r\ny = 3 .* x\r\n%%\r\nz = x .* 3\r\n%%\r\nt = y .* x\r\n%%\r\n% The Rational function times is called in all three multiplications above:\r\ntype Rational\/times\r\n%%\r\n% In this example we can see the value of making all arguments explicit. It\r\n% allows operators to work on a first double argument and a second Rational\r\n% argument.  It also reinforces the fact that functions inside the class\r\n% can access private data and methods on any instance of the class. The\r\n% times method is a method of the Rational class that works on one or more\r\n% instances of Rational numbers.  When it works on two Rational numbers to\r\n% produce a new Rational result, there is nothing special about any one of\r\n% these three instances.\r\n%% \r\n% MATLAB functions, methods, and variables share the same namespace.\r\n% Object properties are scoped to the class of the object and their names\r\n% can be the same as MATLAB keywords.  While it would sometimes be more\r\n% convenient, implicit references to an object parameter would also mean\r\n% additional restrictions on property names and potential conflicts with\r\n% function names.\r\n% \r\n%%\r\n% I would be interested in hearing\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=494#respond your thoughts> on this\r\n% subject.  In your code, what is the relative frequency of methods that\r\n% work on a single object versus multiple objects?  How important is the\r\n% convenience of implicit object access to you?  Would you like a way to\r\n% declare that a block of methods define access operations on an object and\r\n% make the object implicit within these methods?\r\n\r\n\r\n##### SOURCE END ##### ed6e25745b51431b9597f646a9fdb547\r\n-->","protected":false},"excerpt":{"rendered":"<!--introduction--><p>Dave Foti, who manages development of object oriented programming features in the MATLAB language, is back with a discussion of object parameters in methods.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2012\/07\/16\/who-what-why-but-not-this\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[44],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/494"}],"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=494"}],"version-history":[{"count":6,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/494\/revisions"}],"predecessor-version":[{"id":502,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/494\/revisions\/502"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=494"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=494"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=494"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}