<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>So Jake Says &#187; Javadoc</title>
	<atom:link href="http://www.jakevoytko.com/blog/tag/javadoc/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.jakevoytko.com/blog</link>
	<description>Ye Olde Computer Science Blogge</description>
	<lastBuildDate>Sun, 17 Jan 2010 15:16:00 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>People are the Problem, not Operator Overloading</title>
		<link>http://www.jakevoytko.com/blog/2008/03/01/people-are-the-problem-not-operator-overloading/</link>
		<comments>http://www.jakevoytko.com/blog/2008/03/01/people-are-the-problem-not-operator-overloading/#comments</comments>
		<pubDate>Sun, 02 Mar 2008 02:34:29 +0000</pubDate>
		<dc:creator>Jake</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Javadoc]]></category>
		<category><![CDATA[Operator overloading]]></category>
		<category><![CDATA[People are the problem]]></category>

		<guid isPermaLink="false">http://www.jakevoytko.com/blog/2008/03/01/people-are-the-problem-not-operator-overloading/</guid>
		<description><![CDATA[Alternate Title I&#8217;m 12 years late to the party. Again. Hear me out. Java Does it Wrong Java&#8217;s design leans heavily on the (correct) belief that the average programmer is imperfect, lazy, and ill-informed. In response, Java acts like a typical overbearing parent. Java knows that you&#8217;re too lazy to check whether or not you&#8217;re [...]]]></description>
			<content:encoded><![CDATA[<h2>Alternate Title</h2>
<p>I&#8217;m 12 years late to the party. Again. Hear me out.</p>
<h2>Java Does it Wrong</h2>
<p>Java&#8217;s design leans heavily on the (correct) belief that the average programmer is imperfect, lazy, and ill-informed. In response, Java acts like a typical overbearing parent. Java knows that you&#8217;re too lazy to check whether or not you&#8217;re REALLY writing inside of array bounds, so Java does it for you. Programmers are too forgetful to free all memory that they have initialized. Java does it for you. In fact, pointers are hard, so you don&#8217;t get to use them in the traditional sense. At all. Maybe when you&#8217;re older.</p>
<p>Some very good things came from this policy, like memory management, batteries-included mentality, and <code>Javadoc.</code> <code>Javadoc</code> is a tool that automatically generates pleasant documentation based on your comments, and it&#8217;s a freebie. You&#8217;re left with no excuse but to actually document your program, unless you refuse out of malice.</p>
<p>However, operator overloading must have <em>really</em> burned the designers at Sun, because overriding operators isn&#8217;t in the language. Maybe the designers were offended that C++ overrode the bit-shift operators (<code>&lt;&lt;</code> and <code>&gt;&gt;</code>) for input and output. Maybe they came across a home-grown database system that used every operator under the Sun to perform tasks. Whatever the reason, it&#8217;s just not in the language.</p>
<p>Well, almost not in the language. As a convenience, they decided to throw C++&#8217;s unwashed masses a bone when it came to <code>Strings</code>. You get the &#8216;<code>+</code>&#8216; operator.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span> a <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Hello &quot;</span> <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot;World!&quot;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Do you feel the wind rushing through your hair yet? Me neither.</p>
<p>According to <a href="http://java.sun.com/javase/6/docs/api/java/lang/String.html" target="_blank">the documentation</a> this is just syntactic sugar, as Java treats this as using <code>StringBuilder</code> or a <code>StringBuffer</code> along with <code>append</code> methods.</p>
<h2>Consequences</h2>
<p>In the event that you don&#8217;t think that the lack of operator overloading is harmful, let&#8217;s consider the discriminant in the quadratic formula.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a, b, c are floats</span>
discriminant <span style="color: #339933;">=</span> b<span style="color: #339933;">*</span>b <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span><span style="color: #339933;">*</span>a<span style="color: #339933;">*</span>c<span style="color: #339933;">;</span></pre></div></div>

<p>This looks pretty easy, right? Now let&#8217;s look at it as a BigDecimal:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// a, b, c are BigDecimals</span>
discriminant <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>b.<span style="color: #006633;">multiply</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">subtract</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">mult</span><span style="color: #009900;">&#40;</span>a<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">mult</span><span style="color: #009900;">&#40;</span>c<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This doesn&#8217;t look <em>awful</em>, but saying that it&#8217;s as readable as the normal arithmetic example is wrong. You could try to make it as neat and pretty as you can by commenting and indenting and spreading it out. I would, within reason. Do you think that this still doesn&#8217;t look bad? Then try doing the cubic formula. Or the quartic. The syntax is going to get in your way.</p>
<p>For big or complex implementations, this can be a pain in the ass. It becomes its own issue for the programmer, and gets directly in the way of actually solving problems.</p>
<h2>People are the Problem</h2>
<p>What is the problem? The cop-out answer is &#8220;<a href="http://www.artima.com/weblogs/viewpost.jsp?thread=221622">People</a>!&#8221;. To elaborate, we first need to look at the reasons that programming languages exist.</p>
<ul>
<li>So that people can efficiently talk to the computer.</li>
<li>So that people can read how other people talk to the computer.</li>
</ul>
<p>That&#8217;s it. There are no more reasons. If you think there might be, consider whether your reason is a <em>reason</em> or a <em>consequence</em>: I.E. programming language <em>a</em> has a problem, and programming language<em> b</em> was created to solve that problem. Programming languages help us quickly define and solve problems, and they exist so that other people can easily work on the problems based on what we wrote. Operator overloading deals directly with the readability of a language.</p>
<p>On one hand, overloading is very useful. For example, it fits perfectly into the C++ paradigm; the programmer does a little bit more work so that the user doesn&#8217;t have to. It fits well into Python&#8217;s paradigm, as it lets you get things done. If I knew anything about Ruby (and I&#8217;m reading enough Ruby blogs that it looks like I might start this summer when I have free time), it might fit into that.</p>
<p>It&#8217;s entirely syntactic sugar, and exists so that humans don&#8217;t yell &#8220;UGGGGH!&#8221; when they have to type in a huge formula for arbitrary precision arithmetic. If you have a <code>BigInteger</code>, why shouldn&#8217;t you be able to overload the + operator?</p>
<p>On the other hand, people will take any advantage that you give them, and they will abuse the hell out of it. They will either forget to update comments when they change the class, or they might just directly lie. They will swear on a stack of Bibles not to modify the data of a member you just passed, and then will use a <code>const_cast&lt;&gt;</code> and do it anyways. They will make gigantic utility classes named <code>Utility</code> that performs every function they know of. They will create 75 overrides of the same function that do entirely different things. All of these are supported by Java and/or C++, and the language does nothing to stop it.</p>
<p>All of these abuses are very easy for a person to perpetrate. We&#8217;ve all done at least one at some point or another. Our programming languages allow abuses with no hesitation, and some of these are committed extremely frequently.  You can&#8217;t even remove the possibility of these abuses because some things, like commenting, are free-form, and could contain anything.</p>
<h2>The Solution to Java&#8217;s Woes</h2>
<p>I always feel bad complaining about something without trying to come up with a solution (one of the skills from my <a href="http://www.jakevoytko.com/blog/2007/12/22/reading-comprehension-will-make-you-a-better-programmer/">reading comprehension</a> toolkit) so I tried to develop a Java-y solution that lets you overload operators while still &#8220;protecting the programmer from himself&#8221;. My solution is based on a single premise and two added constructs.</p>
<p>If we let the programmer override the base functions <code>multiply()</code>, <code>divide(), subtract()</code>, and <code>add()</code>, they will be allowed to use the overloaded operators (it&#8217;s not my fault/problem the designers of <code>ArrayList</code> used add instead of <code>append()</code> or <code>insert()</code>). For our discriminant example, this means that we are able to write <img src='/blog/wp-content/plugins/latexrender/pictures/78142dcc962f15db95107f42abcbfc86_1.0pt.gif' title='b^2' alt='b^2'  style="vertical-align:-1.0pt;" > as we would expect to if <code>BigDecimal</code> had <em>multiply</em> overridden. It will just automatically be called.</p>
<p>Now, here is the premise that will hold it all together.</p>
<ul>
<li>You can only use type <em>a</em>&#8216;s operators with other objects of type <em>a</em>.</li>
</ul>
<p>In order to make this useful, a second construct will be added to classes, the <code>convert()</code> construct. Let&#8217;s say the user wants to add two different types, such as the following example:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">BigDecimal</span> a<span style="color: #339933;">;</span>
<span style="color: #003399;">BigInteger</span> b<span style="color: #339933;">;</span>
a <span style="color: #339933;">=</span> a <span style="color: #339933;">+</span> b<span style="color: #339933;">;</span></pre></div></div>

<p>The user will have to override the <code>convert() </code>method in BigDecimal. The class would end up looking like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">BigDecimal</span><span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">BigDecimal</span> add<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigDecimal</span> a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #003399;">BigDecimal</span> convert<span style="color: #009900;">&#40;</span><span style="color: #003399;">BigInteger</span> a<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// ...</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The example would then be expanded to the following:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">BigDecimal</span> a<span style="color: #339933;">;</span>
<span style="color: #003399;">BigInteger</span> b<span style="color: #339933;">;</span>
a <span style="color: #339933;">=</span> a.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">BigDecimal</span>.<span style="color: #006633;">convert</span><span style="color: #009900;">&#40;</span>b<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>There we go, it&#8217;s easy! The Java compiler/interpreter can automatically perform this work just as it does for the <code>String</code>. It&#8217;s really no different, the mechanism is already built into the language. All that&#8217;s missing is order of operations, and that could easily be taken care of by processing the lines with a few passes.</p>
<p>If you don&#8217;t like this, what&#8217;s your way?</p>
<img src="http://www.jakevoytko.com/blog/?ak_action=api_record_view&id=57&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://www.jakevoytko.com/blog/2008/03/01/people-are-the-problem-not-operator-overloading/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
	</channel>
</rss>
