<?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>Guilherme Chapiewski &#187; Programming</title>
	<atom:link href="http://guilherme.pro/category/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://guilherme.pro</link>
	<description>Software Development stuff</description>
	<lastBuildDate>Tue, 05 Jan 2010 00:55:21 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Why I don&#8217;t write code comments</title>
		<link>http://guilherme.pro/2009/04/05/why-i-dont-write-code-comments/</link>
		<comments>http://guilherme.pro/2009/04/05/why-i-dont-write-code-comments/#comments</comments>
		<pubDate>Sun, 05 Apr 2009 07:43:25 +0000</pubDate>
		<dc:creator>Guilherme Chapiewski</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Best practices]]></category>
		<category><![CDATA[Fluent Interfaces]]></category>
		<category><![CDATA[Fluent Mail API]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[JavaMail]]></category>

		<guid isPermaLink="false">http://guilherme.pro/?p=47</guid>
		<description><![CDATA[These days I was configuring some personal projects on ohloh and one thing called my attention. Ohloh showed the message &#8220;very few source code comments&#8221; in all my projects.
This is no surprise to me. I really don&#8217;t like to write source code comments. Why? Because if my code isn&#8217;t clear and easy enough for one [...]]]></description>
			<content:encoded><![CDATA[<p>These days I was configuring some personal projects on <a href="http://www.ohloh.net/" onclick="pageTracker._trackPageview('/outgoing/www.ohloh.net/?referer=');">ohloh</a> and one thing called my attention. Ohloh showed the message <em>&#8220;very few source code comments&#8221;</em> in all my projects.</p>
<p>This is no surprise to me. I really don&#8217;t like to write source code comments. Why? Because if my code isn&#8217;t clear and easy enough for one to read and easily understand, I need to refactor. Many times I ask friends to take a look at code snippets and check if they understand. When they don&#8217;t, the method is usually too big or the variable/method names are not clear enough, and then I refactor to make it better to understand.</p>
<p>One example that I love (and use in many presentations) is the code to send e-mails using <a href="http://java.sun.com/products/javamail/" onclick="pageTracker._trackPageview('/outgoing/java.sun.com/products/javamail/?referer=');">Java Mail API</a>. The code would be something like this (from <a href="http://www.javaworld.com/javaworld/jw-06-1999/jw-06-javamail.html?page=4" onclick="pageTracker._trackPageview('/outgoing/www.javaworld.com/javaworld/jw-06-1999/jw-06-javamail.html?page=4&amp;referer=');">Java World tutorial</a>):</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// create some properties and get the default Session</span>
<span style="color: #003399;">Properties</span> props <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Properties</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
props.<span style="color: #006633;">put</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;mail.smtp.host&quot;</span>, _smtpHost<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
Session session <span style="color: #339933;">=</span> Session.<span style="color: #006633;">getDefaultInstance</span><span style="color: #009900;">&#40;</span>props, <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// create a message</span>
Address replyToList<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">new</span> InternetAddress<span style="color: #009900;">&#40;</span>replyTo<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
Message newMessage <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> MimeMessage<span style="color: #009900;">&#40;</span>session<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>_fromName <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
    newMessage.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> InternetAddress<span style="color: #009900;">&#40;</span>from,
        _fromName <span style="color: #339933;">+</span> <span style="color: #0000ff;">&quot; on behalf of &quot;</span> <span style="color: #339933;">+</span> replyTo<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">else</span>
    newMessage.<span style="color: #006633;">setFrom</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> InternetAddress<span style="color: #009900;">&#40;</span>from<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    newMessage.<span style="color: #006633;">setReplyTo</span><span style="color: #009900;">&#40;</span>replyToList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    newMessage.<span style="color: #006633;">setRecipients</span><span style="color: #009900;">&#40;</span>Message.<span style="color: #006633;">RecipientType</span>.<span style="color: #006633;">BCC</span>, 
            _toList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    newMessage.<span style="color: #006633;">setSubject</span><span style="color: #009900;">&#40;</span>subject<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    newMessage.<span style="color: #006633;">setSentDate</span><span style="color: #009900;">&#40;</span>sentDate<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// send newMessage</span>
Transport transport <span style="color: #339933;">=</span> session.<span style="color: #006633;">getTransport</span><span style="color: #009900;">&#40;</span>SMTP_MAIL<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
transport.<span style="color: #006633;">connect</span><span style="color: #009900;">&#40;</span>_smtpHost, _user, _password<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
transport.<span style="color: #006633;">sendMessage</span><span style="color: #009900;">&#40;</span>newMessage, _toList<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>I know that this is just an example but <strong>you know</strong> that it&#8217;s not difficult to find monsters like this one in production code.</p>
<p>This code is <strong>so difficult</strong> to read that the programmer had to say what he was doing, otherwise nobody would understand its purpose. There is a lot of infrastructure and business code together resulting on a huge <a href="http://en.wikipedia.org/wiki/Leaky_abstraction" onclick="pageTracker._trackPageview('/outgoing/en.wikipedia.org/wiki/Leaky_abstraction?referer=');">abstraction leak</a>.</p>
<p>Now take a look at the following code (from <a href="http://code.google.com/p/fluentmailapi/" onclick="pageTracker._trackPageview('/outgoing/code.google.com/p/fluentmailapi/?referer=');">Fluent Mail API</a>) and tell me how many seconds do you need to understand what the code does:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">new</span> EmailMessage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">from</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;demo@guilhermechapiewski.com&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">to</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;destination@address.com&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">withSubject</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Fluent Mail API&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">withBody</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Demo message&quot;</span><span style="color: #009900;">&#41;</span>
    .<span style="color: #006633;">send</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This is an extreme example, of course. Maybe you will not write <a href="http://martinfowler.com/bliki/FluentInterface.html" onclick="pageTracker._trackPageview('/outgoing/martinfowler.com/bliki/FluentInterface.html?referer=');">Fluent Interfaces</a> for every little thing you need in your code, but if you add just a little bit of organization (using a better abstraction) it will already improve code readability:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Email <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Email<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> from, <span style="color: #003399;">String</span> to, ...<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setFrom<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> from<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setTo<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> to<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> ... <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> send<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// put your complicated Java Mail code here</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Just by encapsulating the code in an abstraction that makes sense the <a href="http://en.wiktionary.org/wiki/wtf" onclick="pageTracker._trackPageview('/outgoing/en.wiktionary.org/wiki/wtf?referer=');">WTF</a> effect will be sensibly reduced. The programmers can now at least assume that the code sends e-mail messages since it&#8217;s included in a class called &#8220;Email&#8221;.</p>
<p>And that&#8217;s the point. When I create a culture of prohibiting myself from writing code comments, I&#8217;m obligating me to write easy, maintainable and understandable code.</p>
<p>That&#8217;s very extreme, I know, but the results of this approach are awesome.</p>
]]></content:encoded>
			<wfw:commentRss>http://guilherme.pro/2009/04/05/why-i-dont-write-code-comments/feed/</wfw:commentRss>
		<slash:comments>22</slash:comments>
		</item>
	</channel>
</rss>
