<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Operator Overloading Considered Harmful</title>
	<atom:link href="http://cafe.elharo.com/programming/operator-overloading-considered-harmful/feed/" rel="self" type="application/rss+xml" />
	<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/</link>
	<description>Longer than a blog; shorter than a book</description>
	<lastBuildDate>Tue, 09 Mar 2010 18:31:23 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.1</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: tony</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-413223</link>
		<dc:creator>tony</dc:creator>
		<pubDate>Mon, 22 Jun 2009 07:58:16 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-413223</guid>
		<description>java is differ from c++ becuase it contents multithreading,package &amp; interfaces.</description>
		<content:encoded><![CDATA[<p>java is differ from c++ becuase it contents multithreading,package &amp; interfaces.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Elliotte Rusty Harold</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-346569</link>
		<dc:creator>Elliotte Rusty Harold</dc:creator>
		<pubDate>Mon, 02 Feb 2009 15:59:53 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-346569</guid>
		<description>Not that simple, I&#039;m afraid. Even if you don&#039;t use bad features, or don&#039;t use error prone features in an error prone way, others will; and you&#039;ll have to deal with that. C++ has, I&#039;m afraid, proven this beyond any shadow of a doubt. 

What you need is language-defined support for decimal types and the use of operator symbols such as + on those types, not a general purpose operator overloading mechanism for all user-defined types. The problem with operator overloading is that there really aren&#039;t many good use cases for this. Decimals are one. Complex numbers are another. There are others, but they are increasingly obscure.</description>
		<content:encoded><![CDATA[<p>Not that simple, I&#8217;m afraid. Even if you don&#8217;t use bad features, or don&#8217;t use error prone features in an error prone way, others will; and you&#8217;ll have to deal with that. C++ has, I&#8217;m afraid, proven this beyond any shadow of a doubt. </p>
<p>What you need is language-defined support for decimal types and the use of operator symbols such as + on those types, not a general purpose operator overloading mechanism for all user-defined types. The problem with operator overloading is that there really aren&#8217;t many good use cases for this. Decimals are one. Complex numbers are another. There are others, but they are increasingly obscure.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-345875</link>
		<dc:creator>Steve</dc:creator>
		<pubDate>Sun, 01 Feb 2009 16:16:55 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-345875</guid>
		<description>Guns don&#039;t kill people, people kill people.

Whether a language chooses to support a feature or not does not inherently make it dirty or clean.  It&#039;s a feature that you may either choose to use or not.  Personally I think this whole topic would go away if there was a proper decimal native type in Java.  That&#039;s my need at the moment and BigDecimal (being the abomination that it is) makes using java for a project I&#039;m on untenable.  I would love to use Java but until there is a native decimal type so I can code a = N * 10.5 + 3.25 rather than a = n.multiply(new BigDecimal(10.5) + new BigDecimal(3.25)) I will have to use C#.  Any other argument is pointless because it doesn&#039;t change that basic fact.

Steve</description>
		<content:encoded><![CDATA[<p>Guns don&#8217;t kill people, people kill people.</p>
<p>Whether a language chooses to support a feature or not does not inherently make it dirty or clean.  It&#8217;s a feature that you may either choose to use or not.  Personally I think this whole topic would go away if there was a proper decimal native type in Java.  That&#8217;s my need at the moment and BigDecimal (being the abomination that it is) makes using java for a project I&#8217;m on untenable.  I would love to use Java but until there is a native decimal type so I can code a = N * 10.5 + 3.25 rather than a = n.multiply(new BigDecimal(10.5) + new BigDecimal(3.25)) I will have to use C#.  Any other argument is pointless because it doesn&#8217;t change that basic fact.</p>
<p>Steve</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-327696</link>
		<dc:creator>Mark</dc:creator>
		<pubDate>Mon, 22 Dec 2008 19:01:46 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-327696</guid>
		<description>While I can certainly agree that operator overloading would only be of importance for a few limited types of data which are mathematically related, the problem I see with excluding the facility from the language entire for that reason is that the details of implementation for these types of data can vary widely, depending on need.   Some might require, for example, a 2-dimensional vector, while others might require a 3-dimensional one.  A quaternion is an example of a special type of 4-dimensional vector.  It is simply far too common a case that the implementor might want to create a specialized class for their needs, rather than falling back to a general purpose class that may not exhibit the performance requirements they desire.  Not that I&#039;m saying it&#039;s always wrong to use a general purpose class, but it is certainly not uncommon for an implementor to eventually need to write customized versions of otherwise common types that meet the specific needs of the software being written.  Thus, the actual number of specific cases for which operator overloading could actually be useful is orders of magnitude larger than it might first appear if one only considers the admittedly few categories of classes for which it would be most beneficial.

Now how would I recommend that operator overloading be added to the language in such a way that it wouldn&#039;t be abused?  Unfortunately, as another poster remarked, it&#039;s just as easy for a lazy programmer to name a function  plugh() as it is for somebody to abuse an otherwise useful feature like operator overloading to create code that is not easily understood.   I suspect that adding a notion of const-correctness to java and requiring that overloaded operators observe such correctness on their parameters would help matters somewhat, but of course I&#039;m sure that even that could probably be hacked around.</description>
		<content:encoded><![CDATA[<p>While I can certainly agree that operator overloading would only be of importance for a few limited types of data which are mathematically related, the problem I see with excluding the facility from the language entire for that reason is that the details of implementation for these types of data can vary widely, depending on need.   Some might require, for example, a 2-dimensional vector, while others might require a 3-dimensional one.  A quaternion is an example of a special type of 4-dimensional vector.  It is simply far too common a case that the implementor might want to create a specialized class for their needs, rather than falling back to a general purpose class that may not exhibit the performance requirements they desire.  Not that I&#8217;m saying it&#8217;s always wrong to use a general purpose class, but it is certainly not uncommon for an implementor to eventually need to write customized versions of otherwise common types that meet the specific needs of the software being written.  Thus, the actual number of specific cases for which operator overloading could actually be useful is orders of magnitude larger than it might first appear if one only considers the admittedly few categories of classes for which it would be most beneficial.</p>
<p>Now how would I recommend that operator overloading be added to the language in such a way that it wouldn&#8217;t be abused?  Unfortunately, as another poster remarked, it&#8217;s just as easy for a lazy programmer to name a function  plugh() as it is for somebody to abuse an otherwise useful feature like operator overloading to create code that is not easily understood.   I suspect that adding a notion of const-correctness to java and requiring that overloaded operators observe such correctness on their parameters would help matters somewhat, but of course I&#8217;m sure that even that could probably be hacked around.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Eric Jordan</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-277702</link>
		<dc:creator>Eric Jordan</dc:creator>
		<pubDate>Tue, 02 Sep 2008 03:34:43 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-277702</guid>
		<description>I absolutely understand the motivation to leave powerful features out of Java - most Java code in the Real World is written by pretty mediocre programmers, and people debugging their code need to be protected from them.  This I get.

However, as has been admitted by many people here, there are many use cases where certain overloads absolutely make sense, and where the lack of these overloads makes using these objects in Java quite obnoxious: BigDecimal, BigInteger, vectors, complex numbers, matrices, and quaternions are the ones that people are usually thinking of when they beg for operator overloading.

There&#039;s an obvious solution here: if giving us operator overloading hurts too much (I&#039;m not entirely convinced, but...), we could at least be provided with Magic Objects (like String) for each of these important and commonly used types, and have the compiler specially add that syntax sugar.  That adds some new syntax, so it will probably never happen, but it seems like an extremely reasonable solution to me...</description>
		<content:encoded><![CDATA[<p>I absolutely understand the motivation to leave powerful features out of Java &#8211; most Java code in the Real World is written by pretty mediocre programmers, and people debugging their code need to be protected from them.  This I get.</p>
<p>However, as has been admitted by many people here, there are many use cases where certain overloads absolutely make sense, and where the lack of these overloads makes using these objects in Java quite obnoxious: BigDecimal, BigInteger, vectors, complex numbers, matrices, and quaternions are the ones that people are usually thinking of when they beg for operator overloading.</p>
<p>There&#8217;s an obvious solution here: if giving us operator overloading hurts too much (I&#8217;m not entirely convinced, but&#8230;), we could at least be provided with Magic Objects (like String) for each of these important and commonly used types, and have the compiler specially add that syntax sugar.  That adds some new syntax, so it will probably never happen, but it seems like an extremely reasonable solution to me&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ingo</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-121242</link>
		<dc:creator>Ingo</dc:creator>
		<pubDate>Wed, 22 Aug 2007 09:19:55 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-121242</guid>
		<description>Strange, nobody ever mentioned the built-in ambiguities in methods with names like &quot;add(obj)&quot; or &quot;remove(obj)&quot;. I find it always very confusing that some of those work on their object instance, others don&#039;t change that and return a newly created object with the result. Some - and that&#039;s what really makes it difficult - do both. So often, I have to trace bugs that arise from me expecting a method just to return the result, but on the way changing the instance as well. And vice versa.

My proposal for (dual) operator overloading is such:
1.The result is returned.
2.No object instance is mutated.

1 is obvious. 2 could be enforced by the compiler - at least I&#039;m sure it could. Just make assignments to this.* invalid. This rids you of the problem (which is, in fact, there in C++ as well). [Granted, there may be ways to do it by side-effects, but hey!, in complex languages like Java you can always work-around something. And if it&#039;s by reflection...]
Of course, never anybody will be able to keep dumbheads from inventing strange uses for specific operators. But I&#039;ve seen similar things with named methods.

And there&#039;s oh so many uses for operator overloading. After Java5, it&#039;s the one thing I always said is missing from Java to make it a great programming language. Remember generics? Done mainly with preprocessor in C - baaad. Great implementation in Java. Why not try to do it the same way in Java, getting rid of the problems. Granted, there may be more to it than my two cents above, but that already rids you of one ambiguity...</description>
		<content:encoded><![CDATA[<p>Strange, nobody ever mentioned the built-in ambiguities in methods with names like &#8220;add(obj)&#8221; or &#8220;remove(obj)&#8221;. I find it always very confusing that some of those work on their object instance, others don&#8217;t change that and return a newly created object with the result. Some &#8211; and that&#8217;s what really makes it difficult &#8211; do both. So often, I have to trace bugs that arise from me expecting a method just to return the result, but on the way changing the instance as well. And vice versa.</p>
<p>My proposal for (dual) operator overloading is such:<br />
1.The result is returned.<br />
2.No object instance is mutated.</p>
<p>1 is obvious. 2 could be enforced by the compiler &#8211; at least I&#8217;m sure it could. Just make assignments to this.* invalid. This rids you of the problem (which is, in fact, there in C++ as well). [Granted, there may be ways to do it by side-effects, but hey!, in complex languages like Java you can always work-around something. And if it's by reflection...]<br />
Of course, never anybody will be able to keep dumbheads from inventing strange uses for specific operators. But I&#8217;ve seen similar things with named methods.</p>
<p>And there&#8217;s oh so many uses for operator overloading. After Java5, it&#8217;s the one thing I always said is missing from Java to make it a great programming language. Remember generics? Done mainly with preprocessor in C &#8211; baaad. Great implementation in Java. Why not try to do it the same way in Java, getting rid of the problems. Granted, there may be more to it than my two cents above, but that already rids you of one ambiguity&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: me22</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-57490</link>
		<dc:creator>me22</dc:creator>
		<pubDate>Sun, 18 Feb 2007 06:07:32 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-57490</guid>
		<description>In other news, because programmers can&#039;t be trusted to make a function named add actually add its arguments together, we should get rid of functions.</description>
		<content:encoded><![CDATA[<p>In other news, because programmers can&#8217;t be trusted to make a function named add actually add its arguments together, we should get rid of functions.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: robert</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-53071</link>
		<dc:creator>robert</dc:creator>
		<pubDate>Thu, 01 Feb 2007 20:26:01 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-53071</guid>
		<description>I didn&#039;t mean to say that COBOL-ers use &quot;+&quot; as such; so far as I know, it isn&#039;t defined as an overload.  Rather that, the process of &quot;+&quot; is executed as a matter of course in that world, and the overload of &quot;+&quot; would have that interpretation were it available:  give me one row with the &quot;+&quot; of each numeric column in the rows selected.  This is not, however, how a database developer would likely define the meaning of &quot;+&quot; used in such a syntax.

It does come back to what one means by &quot;+&quot;, thus proving the point that overloading is a loaded subject.  If by &quot;+&quot; database rows (they ain&#039;t records) we mean accumulate the total, then yes, COBOL-ers do it all the time as described, and would appreciate such an overload.  It would make their lives easier; just ask anyone who&#039;s written an AP module.  On the other hand, if &quot;+&quot; database rows means the set of rows meeting the result of multiple statements, that exits with UNION, and MINUS has been implemented as EXCEPT in some database.  My error.  I don&#039;t have cause to use it.

As Alx said, &quot;+&quot; is defined for matrix operations.  This definition is universal within the profession.  If you&#039;re a mathematician, you have a definition.  Overloading of operators within the scope of one program, or even a suite of programs is a different kettle of fish.

Overloading requires knowing (at least) four things:  the complete semantics of the operator in its base (original, as far back as we can trace) context, the complete semantics of that context, the exactly equivalent semantics proposed for the operator in the context of code being created, and that a person of average intelligence would find the result of the overload intuitive.  That really is a tall order.  And likely why only &quot;+&quot; in java; that and the fact that the convention already existed.  hmm.</description>
		<content:encoded><![CDATA[<p>I didn&#8217;t mean to say that COBOL-ers use &#8220;+&#8221; as such; so far as I know, it isn&#8217;t defined as an overload.  Rather that, the process of &#8220;+&#8221; is executed as a matter of course in that world, and the overload of &#8220;+&#8221; would have that interpretation were it available:  give me one row with the &#8220;+&#8221; of each numeric column in the rows selected.  This is not, however, how a database developer would likely define the meaning of &#8220;+&#8221; used in such a syntax.</p>
<p>It does come back to what one means by &#8220;+&#8221;, thus proving the point that overloading is a loaded subject.  If by &#8220;+&#8221; database rows (they ain&#8217;t records) we mean accumulate the total, then yes, COBOL-ers do it all the time as described, and would appreciate such an overload.  It would make their lives easier; just ask anyone who&#8217;s written an AP module.  On the other hand, if &#8220;+&#8221; database rows means the set of rows meeting the result of multiple statements, that exits with UNION, and MINUS has been implemented as EXCEPT in some database.  My error.  I don&#8217;t have cause to use it.</p>
<p>As Alx said, &#8220;+&#8221; is defined for matrix operations.  This definition is universal within the profession.  If you&#8217;re a mathematician, you have a definition.  Overloading of operators within the scope of one program, or even a suite of programs is a different kettle of fish.</p>
<p>Overloading requires knowing (at least) four things:  the complete semantics of the operator in its base (original, as far back as we can trace) context, the complete semantics of that context, the exactly equivalent semantics proposed for the operator in the context of code being created, and that a person of average intelligence would find the result of the overload intuitive.  That really is a tall order.  And likely why only &#8220;+&#8221; in java; that and the fact that the convention already existed.  hmm.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ravi Venkataraman</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-53065</link>
		<dc:creator>Ravi Venkataraman</dc:creator>
		<pubDate>Thu, 01 Feb 2007 19:31:47 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-53065</guid>
		<description>Re robert&#039;s comments about COBOL programmers using the &quot;+&quot; sign to add database records.

It seems to me that they are adding records to a collection of records. The + sign usually suggests that there are two &quot;quantities&quot; of similar type and the result of the operation is another quantity of similar type. Also, it is implicit that there is no general way to uniquely decompose the result into the two original quantittes.

The use of &quot;+&quot; for accumulating database records has a slightly different meaning. Yes, we do add a record to a collection of records, but there the similarity ends. The two &quot;quantities&quot;, record and collection of records are not of the same type; and it is possible to get back the &quot;last&quot; record added if it is an ordered set. 

Hence, the &quot;+&quot; sign should not be used as a shortcut for accumulating records to a collection of records. Although permitted by COBOL conventions, I would definitely not use it.</description>
		<content:encoded><![CDATA[<p>Re robert&#8217;s comments about COBOL programmers using the &#8220;+&#8221; sign to add database records.</p>
<p>It seems to me that they are adding records to a collection of records. The + sign usually suggests that there are two &#8220;quantities&#8221; of similar type and the result of the operation is another quantity of similar type. Also, it is implicit that there is no general way to uniquely decompose the result into the two original quantittes.</p>
<p>The use of &#8220;+&#8221; for accumulating database records has a slightly different meaning. Yes, we do add a record to a collection of records, but there the similarity ends. The two &#8220;quantities&#8221;, record and collection of records are not of the same type; and it is possible to get back the &#8220;last&#8221; record added if it is an ordered set. </p>
<p>Hence, the &#8220;+&#8221; sign should not be used as a shortcut for accumulating records to a collection of records. Although permitted by COBOL conventions, I would definitely not use it.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymoose</title>
		<link>http://cafe.elharo.com/programming/operator-overloading-considered-harmful/comment-page-1/#comment-53045</link>
		<dc:creator>Anonymoose</dc:creator>
		<pubDate>Thu, 01 Feb 2007 17:33:38 +0000</pubDate>
		<guid isPermaLink="false">http://cafe.elharo.com/java/operator-overloading-considered-harmful/#comment-53045</guid>
		<description>&gt;&gt;Who told you you were to use overloaded operators in GUI design? Do you use a knife as a &gt;&gt;hammer? Do you use a chopstick as a toothbrush?

Have you ever had to maintain code that misused language features? You can&#039;t enforce developers to &#039;correctly&#039; use language features. While misuse of language features isn&#039;t a valid reason for not implementing a specific feature I&#039;d argue it&#039;s a smart one.</description>
		<content:encoded><![CDATA[<p>&gt;&gt;Who told you you were to use overloaded operators in GUI design? Do you use a knife as a &gt;&gt;hammer? Do you use a chopstick as a toothbrush?</p>
<p>Have you ever had to maintain code that misused language features? You can&#8217;t enforce developers to &#8216;correctly&#8217; use language features. While misuse of language features isn&#8217;t a valid reason for not implementing a specific feature I&#8217;d argue it&#8217;s a smart one.</p>
]]></content:encoded>
	</item>
</channel>
</rss>
