{"id":171,"date":"2006-07-15T23:43:08","date_gmt":"2006-07-15T23:43:08","guid":{"rendered":"http:\/\/localhost:8000\/?p=171"},"modified":"2006-07-15T23:43:08","modified_gmt":"2006-07-15T23:43:08","slug":"operator-overloading-and-naming","status":"publish","type":"post","link":"https:\/\/blog.gwadej.org\/programmer-musings\/2006\/07\/operator-overloading-and-naming\/","title":{"rendered":"Operator Overloading and Naming"},"content":{"rendered":"<p>One of the things Java programmers seem to really like to hate in C++ is the concept of <em>operator overloading<\/em>. I have heard this from many Java programmers and seen it written by others. According to many, operator overloading makes C++ impossible to understand because <em>you can&#8217;t tell what a piece of code is doing just by looking at it<\/em>.<\/p>\n<p>When looking for this statement on-line, the first example I found was Joel Spolsky&#8217;s article, <a href=\"http:\/\/www.joelonsoftware.com\/articles\/Wrong.html\">Making Wrong Code Look Wrong &#8211; Joel on Software<\/a> under the section <em>A General Rule<\/em>. I&#8217;ll pick on Joel for two reasons here. The first is that the comment has very little to do with the topic of an extremely good article. Second, that I usually agree with Joel on the big things, but often disagree on smaller issues.<\/p>\n<p>Joel uses the following example to prove his point:<\/p>\n<p><code>i = j * 5;<\/code><\/p>\n<p>He states, with complete conviction, that <em>if you see that same snippet of code in C++, you don\u2019t know anything. Nothing.<\/em><\/p>\n<p>I believe that the operator overloading issue is a red herring. To prove this, I will transform the code a little.<\/p>\n<p><code>assign( i, multiply( j, 5 ) );<\/code><\/p>\n<p>This would be perfectly legal code in C++, Java, or a number of other common general purpose languages. I could easily define this code to do exactly the same as the original code when <code>i<\/code> and <code>j<\/code> are integers.<\/p>\n<p>Now, when you see this snippet of code, what do you know? Even without operator overloading, you don&#8217;t really know anything. If the programmer used good naming conventions, however, you could <em>infer<\/em> that <code>j<\/code> is multiplied by <code>5<\/code> where the multiplication is something appropriate for an object of <code>j<\/code>&#8216;s type. You could further infer that the result of this operation is assigned to <code>i<\/code> using whatever conversions are appropriate for this assignment.<\/p>\n<p>Furthermore, if we used a more object oriented syntax:<\/p>\n<p><code>i.assign( j.multiply( 5 ) );<\/code><\/p>\n<p>Although we now know that both <code>i<\/code> and <code>j<\/code> are object types, we still don&#8217;t know any more than we did from the previous two examples. But, we still don&#8217;t <em>know<\/em> what those methods do. For example, <code>multiply<\/code> could make a call to a relational database with the supplied parameter to give a string that <code>i.assign()<\/code> uses to make an HTTP request to a web server to update its current state.<\/p>\n<p>If you found that this was the case, you would probably abuse the original programmer about using extremely bad naming. You probably wouldn&#8217;t ban the use of the method names <code>multiply<\/code> and <code>assign<\/code>.<\/p>\n<p>The issue, as I see it, is not that operator overloading is a problem. It&#8217;s that some programmers name methods, variables, and\/or classes badly. The fix is not to ban certain names, but to educate those programmers.<\/p>\n<p>In my experience, most of the bad uses of operator overloading happen when programmers are new to the idea and they think it will be <em>clever<\/em> or <em>cool<\/em> to use an operator when a method name works better. Education and experience usually fixes that. (Others may have had different experiences, but that is what I saw.)<\/p>\n<p>Maybe we should stop beating the operator overloading <em>dead horse<\/em>, and spend more time educating junior programmers about the importance of good names and the <em>Principle of Least Surprise<\/em>. In fact, the advice cited in Joel&#8217;s article above is a good start for this kind of education.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One of the things Java programmers seem to really like to hate in C++ is the concept of operator overloading. I have heard this from many Java programmers and seen it written by others. According to many, operator overloading makes C++ impossible to understand because you can&#8217;t tell what a piece of code is doing\u2026 <span class=\"read-more\"><a href=\"https:\/\/blog.gwadej.org\/programmer-musings\/2006\/07\/operator-overloading-and-naming\/\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[12],"tags":[53,62,66,106,182,222,229],"_links":{"self":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts\/171"}],"collection":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/comments?post=171"}],"version-history":[{"count":0,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/posts\/171\/revisions"}],"wp:attachment":[{"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/media?parent=171"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/categories?post=171"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.gwadej.org\/programmer-musings\/wp-json\/wp\/v2\/tags?post=171"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}