<?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>motlin.com</title>
	<atom:link href="http://motlin.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://motlin.com</link>
	<description>a site about software</description>
	<lastBuildDate>Wed, 21 Nov 2012 00:35:52 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Bottom types, or &#8220;Why would Java be better off without null?&#8221;</title>
		<link>http://motlin.com/2012/bottom-types-or-why-would-java-be-better-off-without-null/</link>
		<comments>http://motlin.com/2012/bottom-types-or-why-would-java-be-better-off-without-null/#comments</comments>
		<pubDate>Wed, 06 Jun 2012 03:14:08 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/?p=130</guid>
		<description><![CDATA[<p>When I first heard the idea that Java would be better off without null, I looked around for an explanation I could understand. Unfortunately, everything I found used code examples from languages without null. If I knew those languages, I wouldn&#8217;t be searching.</p> <p>I wound up learning the &#8220;hard way&#8221; but now I figure I [...]]]></description>
				<content:encoded><![CDATA[<p>When I first heard the idea that Java would be better off without null, I looked around for an explanation I could understand. Unfortunately, everything I found used code examples from languages without null. If I knew those languages, I wouldn&#8217;t be searching.</p>
<p>I wound up learning the &#8220;hard way&#8221; but now I figure I can answer the question using Java examples. Let&#8217;s jump right in.</p>
<hr/>
<h3>Code Example 1</h3>
<p><script type="text/javascript" src="https://gist.github.com/2872321.js?file=BottomType1.java"></script></p>
<p><code>A</code> and <code>B</code> are classes, not interfaces, so no Java class can extend both. There&#8217;s no way the <code>method()</code> can return a <em>meaningful</em> answer. It could return <code>null</code>, but not a real object. So let&#8217;s rename <code>method()</code> to <code>undefined()</code>.</p>
<p><script src="https://gist.github.com/2872379.js"> </script></p>
<p>Think about how you might implement <code>undefined()</code>. Is it even possible to get this code to compile? If you replaced the method calls with <code>null</code>, it would obviously work.</p>
<p><script src="https://gist.github.com/2872419.js"> </script></p>
<p>But methods can&#8217;t <strong>say</strong> they return <code>null</code>.</p>
<p><script src="https://gist.github.com/2872423.js"> </script></p>
<p>We&#8217;d need a type there instead. Ok, so what is the <strong>type</strong> of <code>null</code>? Let&#8217;s look at another example.</p>
<hr/>
<h3>Code Example 2</h3>
<p><script src="https://gist.github.com/2872443.js"> </script></p>
<p>Here we&#8217;re using the same code &#8220;<code>throw new RuntimeException()</code>&#8221; in two contexts that expect two incompatible types. Ok, so what is the type of &#8220;<code>throw new RuntimeException()</code>&#8221; ?</p>
<hr/>
The answer is: they&#8217;re both a form of the bottom type, which is logically a subtype of all other types. It&#8217;s the logical opposite of <code>Object</code>, the top of the Java hierarchy. It&#8217;s a fairly weak form of the bottom type without a name. Let&#8217;s say we enhance Java by giving it the name <code>Nothing</code>. This code would compile:</p>
<p><script src="https://gist.github.com/2872454.js"> </script></p>
<p>How can we fill in the implementation? We&#8217;ve already seen two ways: <code>return null</code>, or <code>throw</code>.</p>
<p>How else? We can recurse in an infinite loop.</p>
<p><script src="https://gist.github.com/2872469.js"> </script></p>
<p>How else? We can cast.</p>
<p><script src="https://gist.github.com/2872473.js"> </script></p>
<p>This code would compile but throw a <code>ClassCastException</code> at runtime, so it&#8217;s logically the same thing as <code>throw new RuntimeException()</code>.</p>
<hr/>
Of these choices, throwing is already really useful for pre-conditions. It&#8217;s idiomatic to call the method &#8220;error&#8221; when it takes a <code>String</code> parameter, and &#8220;undefined&#8221; without any parameters.</p>
<p><script src="https://gist.github.com/2872501.js"> </script></p>
<p>Since Java doesn&#8217;t have the <code>Nothing</code> type, we just use <code>void</code> today. In the following code example, <code>Nothing</code> is slightly more accurate than <code>void</code>, but it doesn&#8217;t really matter since the return type isn&#8217;t used.</p>
<p><script src="https://gist.github.com/2872515.js"> </script></p>
<p>But the point of a bottom type is you <strong>can</strong> return it.</p>
<p><script src="https://gist.github.com/2872536.js"> </script></p>
<p>This is pretty much what Scala has. Its bottom type is called <code>Nothing</code>. Haskell has a bottom type but leaves it unnamed. To see why, let&#8217;s look at my first question again.</p>
<p><script src="https://gist.github.com/2872379.js"> </script></p>
<p>Is it possible to get this code to compile? Yes, we <em>can</em> implement <code>undefined()</code> without the <code>Nothing</code> type, as follows.</p>
<p><script src="https://gist.github.com/2879062.js"> </script></p>
<p>More on this later.</p>
<hr/>
<h3>Optionality</h3>
<p>In Java, we use <code>null</code> for more than just indicating an error or from unreachable code. We also use <code>null</code> to represent optionality.</p>
<p><script src="https://gist.github.com/2879074.js"> </script></p>
<p>If <code>map</code> doesn&#8217;t contain <code>key</code>, <code>value</code> will be <code>null</code>. What about the reverse? Is it true that when <code>value</code> is <code>null</code>, <code>map</code> doesn&#8217;t contain <code>key</code>? <a href="http://docs.oracle.com/javase/7/docs/api/java/util/Map.html#get(java.lang.Object)">Nope</a>, because <code>map</code> may contain the entry (<code>key</code> -> <code>null</code>), in which case we need to remember to check <code>map.containsKey(key)</code>.</p>
<p>In languages without <code>null</code>, we need another way to represent optionality.</p>
<p><script src="https://gist.github.com/2879138.js"> </script></p>
<p>Now methods that sometimes return <code>null</code> could be refactored to return <code>Option</code> instead. For example, <code>Map.get(K)</code> could return <code>Option&lt;V&gt;</code>. Calling <code>None.get()</code> throws, which is logically equivalent to calling a method on <code>null</code> and getting a <code>NullPointerException</code>. If that were the whole story, <code>Option</code> wouldn&#8217;t be very useful, but it&#8217;s normal to add additional functionality. For example, <code>Option.getOrElse(T default)</code> could return <code>Some.value</code> for <code>Some</code> and <code>default</code> for <code>None</code>. Another example, <code>forEach(block)</code> could execute the code block on <code>Some.value</code> and just do nothing on <code>None</code>.</p>
<hr/>
<h3>Putting it all together</h3>
<p>The two concepts (bottom types and Options) come together in <code>None.get()</code>. Since it never returns a real value, it could return the bottom type. The fact that None is generic on type <code>T</code> and <code>get()</code> returns <code>T</code> makes the method look just like our earlier implementation of <code>undefined()</code>. With type variance, we could make <code>None</code> into a Singleton implementing <code>Option&lt;Nothing&gt;</code>.</p>
<p><script src="https://gist.github.com/2914584.js"> </script></p>
<p>Methods like <code>Map.get()</code> could return the Singleton. </p>
<p><script src="https://gist.github.com/2914596.js"> </script></p>
<hr/>
With <code>Option</code>, we can get rid of null from the language completely. But should we? On the JVM, probably not. The <code>Option</code> wrapper objects waste a lot of memory. Let&#8217;s say we have a <code>Person</code> class with field <code>private final Address workAddress</code> which may or may not exist. Replacing the <code>Address</code> with <code>Option&lt;Address&gt;</code> adds an extra 16 bytes per non-null workAddress (on a 64 bit JVM). Maybe this cost could be optimized away in a future version of the JVM but we&#8217;d pay it today.</p>
<p>However, there are big benefits of consistently using <code>Option</code> instead of <code>null</code>. Using the type system to reflect optionality communicates more about your API, plus it prevents common mistakes. It trades one kind of syntax cruft for another. I think it&#8217;s a clear win in languages with lambdas and so <code>Option</code> will be more compelling with Java 8. You can see the difference with Scala today.</p>
<p><script src="https://gist.github.com/2879506.js"> </script></p>
<h3>Scala and Haskell</h3>
<p>Scala actually has two bottom types. <code>Nothing</code> works just like the examples we&#8217;ve seen so far.</p>
<p><script src="https://gist.github.com/2879534.js"> </script></p>
<p>Scala can even infer <code>Nothing</code> as the return type in this example.<br />
<code>def undefined<strong>: Nothing</strong> = throw new RuntimeException<br />
def undefined = throw new RuntimeException</code></p>
<p>Scala has a second bottom type called <code>Null</code>.</p>
<p><code>def undefined: Null = null</code></p>
<p>Why? Primitives.</p>
<p><script src="https://gist.github.com/2879579.js"> </script></p>
<p>So <code>Nothing</code> and <code>Null</code> are both bottom types in Scala, and <code>Nothing</code> is &#8220;below&#8221; <code>Null</code>. In the Java version <code>public static &lt;T&gt; T undefined()</code> we can&#8217;t differentiate between non-normal termination and <code>null</code>. But in Scala we can, because it&#8217;s a compiler error to return <code>null</code> from a method declared to return Nothing.</p>
<p>Haskell doesn&#8217;t have this problem because it doesn&#8217;t have any of the complicating factors we&#8217;ve gone over. Haskell doesn&#8217;t have type casts, primitives, or null! That means the bottom type can <em>only</em> recurse infinitely or throw. Without <code>null</code>, there&#8217;s no need for two bottom types. In fact, Scala only names <code>Nothing</code> to differentiate it from <code>Null</code>. Without two bottom types, Haskell gets by without naming the bottom type (using the generics trick alone).</p>
<p><script src="https://gist.github.com/2879601.js"> </script></p>
<p><code>a</code> is a generic type like Java&#8217;s <code>&lt;T&gt;</code>.<br />
<code>[Char]</code> is a list of <code>Char</code>, in other words, a <code>String</code>.</p>
<p>So in Haskell, the method signature <code>undefined :: a</code> is enough to know with certainty that the body doesn&#8217;t return a value. Haskell&#8217;s type system is very strict and precise, so there are many examples like this where the type signature alone tells you everything you need to know about the function.</p>
<p>Future versions of Java will never eliminate <code>null</code>, but remember that <code>Option</code> is a good option.</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2012/bottom-types-or-why-would-java-be-better-off-without-null/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Maven Best Practices</title>
		<link>http://motlin.com/2011/maven-best-practices/</link>
		<comments>http://motlin.com/2011/maven-best-practices/#comments</comments>
		<pubDate>Sat, 20 Aug 2011 15:25:36 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/?p=127</guid>
		<description><![CDATA[Always use the <a href="http://maven.apache.org/plugins/maven-enforcer-plugin/usage.html">maven-enforcer-plugin</a> Enforce <a href="http://maven.apache.org/enforcer/enforcer-rules/dependencyConvergence.html">dependency convergence</a> Otherwise it&#8217;s possible that you depend on two different jars which both depend on log4j. Which one gets used at compile time depends on a set of rules that you shouldn&#8217;t have to remember. They can both (!) get exported as transitive dependencies. Require <a href="http://maven.apache.org/enforcer/enforcer-rules/requirePluginVersions.html">plugin [...]]]></description>
				<content:encoded><![CDATA[<ul>
<li>Always use the <a href="http://maven.apache.org/plugins/maven-enforcer-plugin/usage.html">maven-enforcer-plugin</a></li>
<ul>
<li>Enforce <a href="http://maven.apache.org/enforcer/enforcer-rules/dependencyConvergence.html">dependency convergence</a></li>
<ul>
<li>Otherwise it&#8217;s possible that you depend on two different jars which both depend on log4j. Which one gets used at compile time depends on a set of rules that you shouldn&#8217;t have to remember. They can both (!) get exported as transitive dependencies.</li>
</ul>
<li>Require <a href="http://maven.apache.org/enforcer/enforcer-rules/requirePluginVersions.html">plugin versions</a> (for all plugins, even the built in ones)</li>
<ul>
<li>Define them in pluginManagement in the parent pom to define versions</li>
<ul>
<li>Otherwise a new version of maven-surefire-plugin could break your build</li>
</ul>
</ul>
</ul>
<li>Use dependencyManagement in the parent pom to use versions consistently across all modules</li>
<li>Periodically run <a href="http://maven.apache.org/plugins/maven-dependency-plugin/analyze-mojo.html">mvn dependency:analyze</a></li>
<ul>
<li>It&#8217;s possible that you&#8217;re getting a dependency transitively that you directly depend on at compile time. If so, it&#8217;s important to add it to your pom with the version you require. This plays nicely with the enforcer plugin.</li>
<li>It&#8217;s possible that you&#8217;re declaring extra dependencies that you don&#8217;t use. This doesn&#8217;t work properly 100% of the time, especially with libraries that are designed to have optional pieces (i.e. slf4j-api gets detected properly, but slf4j-log4j12 fails).</li>
</ul>
<li>Maven CI best practices <a href="http://www.sonatype.com/people/2009/01/maven-continuous-integration-best-practices/">http://www.sonatype.com/people/2009/01/maven-continuous-integration-best-practices/</a></li>
<ul>
<li>Automate Snapshot Deployment through the CI system</li>
<li>Isolate Local Repostitories</li>
<ul>
<li>use -Dmaven.repo.local=%system.teamcity.build.workingDir%/.m2</li>
</ul>
<li>Regularly purge local repositories</li>
<li>Enable Batch Mode with -B</li>
<ul>
<li>This will make the logs shorter since it avoids the dependency download progress logging. It also ensures that the build won’t hang due to waiting for user input.</li>
</ul>
<li>Enable Full Stack Traces with -e</li>
</ul>
<li>Put the parent POM in the root directory, next to the child modules</li>
<li>Other resources</li>
<ul>
<li><a href="http://geertschuring.wordpress.com/2011/02/23/maven-best-practices/">http://geertschuring.wordpress.com/2011/02/23/maven-best-practices/</a></li>
<li><a href="http://www.slideshare.net/brettporter/more-apache-maven-best-practices-presentation">http://www.slideshare.net/brettporter/more-apache-maven-best-practices-presentation</a></li>
</ul>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2011/maven-best-practices/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to set up the rxvt terminal for Cygwin</title>
		<link>http://motlin.com/2011/how-to-set-up-the-rxvt-terminal-for-cygwin/</link>
		<comments>http://motlin.com/2011/how-to-set-up-the-rxvt-terminal-for-cygwin/#comments</comments>
		<pubDate>Thu, 19 May 2011 04:23:08 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/?p=124</guid>
		<description><![CDATA[<p>Download and run the <a href="http://www.cygwin.com/setup.exe">Cygwin setup file</a>.</p> <p>Select Install from Internet.</p> <p>Enter C:\cygwin as the Root Directory.</p> <p>When you get to the package list, search for rxvt and click the word skip so that it changes to a version number.</p> <p>Add any other packages you want. I always install at least these:</p> git git-completion [...]]]></description>
				<content:encoded><![CDATA[<p>Download and run the <a href="http://www.cygwin.com/setup.exe">Cygwin setup file</a>.</p>
<p>Select <strong>Install from Internet</strong>.</p>
<p>Enter <strong>C:\cygwin</strong> as the Root Directory.</p>
<p>When you get to the package list, search for <strong>rxvt</strong> and click the word <strong>skip</strong> so that it changes to a version number.</p>
<p>Add any other packages you want. I always install at least these:</p>
<ul>
<li>git</li>
<li>git-completion</li>
<li>git-gui</li>
<li>git-svn</li>
<li>gitk</li>
<li>openssh</li>
<li>rsync</li>
<li>rxvt</li>
<li>screen</li>
<li>tig</li>
<li>unzip</li>
<li>vim</li>
<li>wget</li>
</ul>
<p>Create a new file called <strong>C:\cygwin\rxvt.bat</strong> and add these contents.</p>
<p><script src="https://gist.github.com/980154.js"> </script></p>
<p>Replace &#8220;<strong>Craig</strong>&#8221; with your user name. If you don&#8217;t like large fonts or Lucida Console, you can change that as well.</p>
<p>Create a desktop shortcut to <strong>C:\cygwin\rxvt.bat</strong> and change its icon to <strong>C:\cygwin\bin\rxvt.exe</strong>.</p>
<p>The default colors aren&#8217;t that great. You can copy my <strong>.bashrc</strong>, <strong>.dircolors</strong>, and <strong>.Xdefaults</strong> from my <a href="https://github.com/motlin/dotfiles/tree/cygwin">dotfiles github repository</a> or you can create your own.</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2011/how-to-set-up-the-rxvt-terminal-for-cygwin/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>How to do Version Numbers</title>
		<link>http://motlin.com/2010/how-to-do-version-numbers/</link>
		<comments>http://motlin.com/2010/how-to-do-version-numbers/#comments</comments>
		<pubDate>Sun, 28 Nov 2010 04:14:39 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/?p=107</guid>
		<description><![CDATA[Motivation <p>Why do we bother with multi-part numbers at all? Why not just use use a single revision number, like version 123? The parts of a version number communicate the magnitude of the change between the two versions. In the case of a library, this translates to how difficult the upgrade will be, which in [...]]]></description>
				<content:encoded><![CDATA[<h3><strong>Motivation</strong></h3>
<p>Why do we bother with multi-part numbers at all? Why not just use use a single revision number, like version 123? The parts of a version number communicate the magnitude of the change between the two versions. In the case of a library, this translates to how difficult the upgrade will be, which in turn tells about the level of source, binary, and serialization compatibility between the two releases.</p>
<p>Setting the right level of compatibility between releases is about striking a balance. Upgrades that are fully source and binary compatible are much easier to take for the client, but much more work for the authors. When releasing frameworks I prefer to use a three part version number of the form major.minor.maintenance. Bug fix (maintenance) releases must be source, binary, and serialization compatible. Minor releases ought to be binary and serialization compatible. Major releases need not be compatible.</p>
<h3><strong>Fix</strong></h3>
<p>When we find a bug, it&#8217;s often serious enough that we want clients to upgrade immediately. We don&#8217;t want to give them any excuse to avoid it. That means we ought to preserve binary, source, and serialization compatibility. That way the new version can be a drop in replacement, meaning it will work without recompilation.</p>
<p>The fact that a library author bothered to produce a minor version number communicates intent to the client. The author went through the effort to create a fully compatible release in order to make your life easier. The author believes the upgrade is important.</p>
<p>Occasionally fixing a bug will cause an incompatibility. It&#8217;s unfortunate but sometimes necessary. If it&#8217;s possible to limit the break to the piece of code that wasn&#8217;t working anyway, then it seems justified. Some work is required to get things working properly, it&#8217;s as simple as that.</p>
<h3><strong>Major</strong></h3>
<p>Incrementing the major version number indicates that backwards compatibility was broken. The new version can be binary incompatible, source incompatible, and the serialized form of its classes may have changed as well.</p>
<p>It&#8217;s like a last resort, but it&#8217;s an important one. Some changes are just too big. The authors will not always be able to or want to preserve compatibility. It&#8217;s important to schedule major releases so that there is balance. If you schedule them too frequently, clients will resist doing upgrades. Every major upgrade requires recompilation, maybe some development work, and testing. There&#8217;s little incentive to upgrade to any particular version at any particular time. It becomes very difficult to synchronize the version used across an entire organization. On the other hand, if you don&#8217;t create them frequently enough then clients may have to wait a very long time for certain features.</p>
<p>In my experience it&#8217;s good to create a major release roughly every six months.</p>
<h3><strong>Minor</strong></h3>
<p>Minor releases are the most complex because there are a few choices for what they can mean. Minor releases can be binary compatible with the previous release, source compatible, or both. There&#8217;s also the choice of whether it should indicate serialization compatibility as well. If you look at what open source projects do, you won&#8217;t find a lot of consistency.</p>
<p>I believe that minor version releases should be binary and serialization compatible with the previous release, but not necessarily source compatible. I&#8217;m assuming familiarity with binary and source compatibility. If not, you can read about it <a href="http://motlin.com/2010/binary-and-source-backwards-compatibility/">here</a>.</p>
<h3><strong>Compatibility Trade-offs</strong></h3>
<p>Let&#8217;s dive into the trade-offs between work on the client side and work on the library developer side. Binary compatibility means that no coordinated releases across teams are necessary. Source compatibility means no development effort is required.</p>
<h3><strong>Why minor versions should be binary compatible</strong></h3>
<p>If your minor versions are binary compatible, it&#8217;s easier for everyone to upgrade. More importantly, teams who are slow to upgrade will not hold up other teams from taking the latest version.</p>
<p>Let&#8217;s say library B depends on version 1.0.0 of library A, and application C depends on both libraries. When binary compatible version 1.0.1 of library A is released, application C can start using it <em>without involving anyone who works on library B.</em> When a new version of a library is not binary compatible, it can take a very long time to propagate through an organization or through a software stack. With binary compatibility, no coordination across teams is necessary.</p>
<h3><strong>Why minor versions should not have to be source compatible</strong></h3>
<p>If a release is source compatible, it still needs to be recompiled but there won&#8217;t be any compiler errors requiring fixes. However, it doesn&#8217;t reduce the overall amount of work, it just pushes it later. If library designers think it&#8217;s a good idea to change API in some source breaking way, then they&#8217;ll do it some day and the clients will see those compiler errors eventually. Postponing just bunches them all up and makes major versions more painful.</p>
<p>In addition, source compatibility doesn&#8217;t affect what can get deployed. You might have to stick with the old jar at compile time to avoid compiler errors, and yet you can still deploy with the new jar if it&#8217;s binary compatible.</p>
<p>Making minor version numbers source compatible adds restrictions and requires more work from the library developers without giving clients much benefit. Therefore I recommend that minor versions are binary compatible but not source compatible. This might not be what clients are expecting though so it&#8217;s important to communicate the compatibility policy clearly.</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2010/how-to-do-version-numbers/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Binary and Source Backwards Compatibility</title>
		<link>http://motlin.com/2010/binary-and-source-backwards-compatibility/</link>
		<comments>http://motlin.com/2010/binary-and-source-backwards-compatibility/#comments</comments>
		<pubDate>Fri, 26 Nov 2010 16:42:37 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/?p=109</guid>
		<description><![CDATA[It's pretty easy to break binary and source compatibility simultaneously. Remove one parameter from a public method or delete a method entirely and no existing code that used the method will compile or run. But how can we break just binary or just source compatibility but not both?]]></description>
				<content:encoded><![CDATA[<p>It&#8217;s pretty easy to break binary and source compatibility simultaneously. Remove one parameter from a public method or delete a method entirely and no existing code that used the method will compile or run. But how can we break just binary or just source compatibility but not both? Let&#8217;s look at examples of each in Java.</p>
<h3>Source Compatibility</h3>
<p>This methods prints out every item in a collection.</p>
<pre class="brush: java">public void printAllElements(Collection&lt;?&gt; collection)
{
  for (Object each : collection)
  {
    System.out.println(each);
  }
}</pre>
<p>The method takes a Collection as its parameter, but it could really operate on any Iterable. So let&#8217;s make that change:</p>
<pre class="brush: java">public void printAllElements(Iterable&lt;?&gt; iterable)
{
  for (Object each : iterable)
  {
    System.out.println(each);
  }
}</pre>
<p>This change is source compatible, meaning all source that compiled against the previous version of this code will still compile. That makes sense, because every Collection is an Iterable, so every existing reference to this method is still valid.</p>
<p>This change is not binary compatible though. If you swap out the old jar for the new one without recompiling, you&#8217;ll get an error at runtime like this:</p>
<p>Exception in thread &#8220;main&#8221; java.lang.NoSuchMethodError: com.motlin.Printer.printAllElements(Ljava/util/Collection;)V</p>
<p>The bytecode of the client application refers to this method very specifically. You can see the method signature in the error message, and the type of the parameter is part of the signature. The method  com.motlin.Printer.printAllElements(Ljava/util/Collection;)V no longer exists. To use the new library, client code must be recompiled. That might not sound so bad, but transitive dependencies need to be recompiled as well. That means if the client application uses a second framework which depends on the first, the second framework will also need to be recompiled. In a large dependency graph, the extra work can add up.</p>
<h3>Binary Compatibility</h3>
<p>How can we break source compatibility while preserving binary compatibility? One way is to add a method to an interface. Let&#8217;s write a tiny library with one interface and one concrete implementation. Here&#8217;s the complete source of version 1.</p>
<pre class="brush: java">public interface MyInterface
{
  void doSomething();
}

public class MyImpl implements MyInterface
{
  public void doSomething()
  {
    System.out.println("");
  }
}</pre>
<p>Version 2 adds a second method to MyInterface called doSomethingElse(). That&#8217;s not a source compatible change.  MyInterface is public, which means any client can write their own implementations of it. When clients upgrade, their implementations won&#8217;t implement doSomethingElse() yet so they&#8217;ll get a compiler error like this:</p>
<p>com.motlin.MyImpl is not abstract and does not override abstract method doSomethingElse() in com.motlin.MyInterface</p>
<p>The rules for binary compatibility are quite complex and you can read the rules <a href="http://java.sun.com/docs/books/jls/second_edition/html/binaryComp.doc.html">elsewhere</a>. The reason why this change is binary compatible is analogous to why the previous change wasn&#8217;t. A client application compiled against version 1 has references in the bytecode to the method doSomething() which is void and takes no parameters. That method is still there in version 2, so it will work, simple as that. Internally, doSomething() can call doSomethingElse() in version 2 and it would still be ok. The internal implementation of a method does not change its signature, so everything is still fine. Binary compatibility is very dynamic. It is determined at runtime and difficult to reason about earlier.</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2010/binary-and-source-backwards-compatibility/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Tabs vs. Spaces</title>
		<link>http://motlin.com/2010/tabs-vs-spaces/</link>
		<comments>http://motlin.com/2010/tabs-vs-spaces/#comments</comments>
		<pubDate>Thu, 04 Nov 2010 12:18:21 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/?p=105</guid>
		<description><![CDATA[... and why I can’t just let it go.

Whitespace is ignored by the compiler so it's purely for readability. It's there for you. Variable names have meaning and describe the intention of code. Similarly, whitespace describes the structure of a program.]]></description>
				<content:encoded><![CDATA[<h3>&#8230; and why I can’t just let it go.</h3>
<p>Before I say which is better, I want to explain why I think the issue is even important. Whitespace is ignored by the compiler so it&#8217;s purely for readability. It&#8217;s there for <em>you</em>. Variable names have meaning and describe the intention of code. Similarly, whitespace describes the structure of a program.</p>
<p>Suppose there is a hypothetical programmer who is intelligent and writes correct programs, but he names his variables things like &#8220;var1&#8243; and &#8220;var2.&#8221; In real life, most code needs to be modified at some point, through no fault of the original programmer. Requirements change. When another developer (or even the original programmer) needs to read this code, he will find it utterly useless. He will waste lots of time trying to understand it. He may even re-implement some existing functionality simply because he couldn&#8217;t find it buried there in the obfuscated code. I would go so far as to say this hypothetical programmer deserves to be fired simply for his unusual naming style.</p>
<p>Code with inconsistent indentation isn&#8217;t so useless &#8211; fortunately for us it is easily repaired by automatic formatting tools. However, leaving a file with inconsistent indention puts that burden on next person who reads it. The next reader will take slightly longer to mentally parse the file since he must figure out scope information which the indentation level could have communicated. And someone will eventually need to fix it, manually or with a code beautifier.</p>
<p>On the one hand, I feel like a programmer who can&#8217;t indent his source consistently also can&#8217;t be trusted to make the proper design and engineering decisions made while implementing even the smallest feature. In reality, I know that most code is already not indented consistently. Even if it was, accidents do happen. A merge tool or a terminal editor may easily cause inconsistencies that would have never happened in the IDE, for example. Thus to truly have a standard, it must be enforced externally &#8211; by the IDE on check-in, or better yet by the version control system.</p>
<p>Now the controversial part: Tabs are better than spaces.</p>
<p>There &#8211; I&#8217;ve said it. Here&#8217;s why. With either choice, a standard is being imposed on all the developers who work on the project. But choosing spaces really forces two separate things &#8211; the ASCII character used for indentation plus the width of the indentation. Choosing tabs only imposes the ASCII character used for indentation, which is the absolute minimum needed to have a standard.</p>
<p>A common argument is that if you use spaces, it looks the same in any editor without doing any extra configuration. But in order to have a standard, you <em>must</em> do extra configuration. Otherwise the first time you hit the tab key, you may introduce an inconsistency. If you use vim with the default configuration, you just inserted a tab. Emacs is worse &#8211; it inserts a mix of tabs and spaces by default. And I&#8217;m sure another editor inserts spaces by default.</p>
<p>An alternate suggestion is to never use the tab key, but that can&#8217;t be enforced. It shouldn&#8217;t be anyway because tapping the space bar is a waste of time. Like I said, a real standard must be enforced by the version control system. As long as a standard is being imposed, it should be tabbed indentation so that each teammate may configure his editor to use the width he prefers. Each teammate <em>must</em> configure his editor, or else waste more time fixing the whitespace when the version control system rejects his check-ins.</p>
<p>There is a second argument in favor of tabs that is extremely simple. Have you ever seen a file that was indented with 4 spaces in some parts and 2 spaces in other parts? Two coders who use tabs could never interfere with each other in this way.</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2010/tabs-vs-spaces/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Risk Calculator</title>
		<link>http://motlin.com/2008/risk-calculator/</link>
		<comments>http://motlin.com/2008/risk-calculator/#comments</comments>
		<pubDate>Mon, 08 Dec 2008 03:00:59 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/software/?p=80</guid>
		<description><![CDATA[Run <p><a href="wp-content/Risk/risk.jnlp"> Launch Risk Calculator</a></p> Additional Information <p><a href="http://svn.motlin.com/dev/Risk/trunk/"> Subversion repository</a><br /> <a href="wp-content/Risk/api"> Javadoc</a><br /> <a href="wp-content/releases/Risk"> Old releases</a></p>]]></description>
				<content:encoded><![CDATA[<h2>Run</h2>
<p><a href="wp-content/Risk/risk.jnlp"><img src="/icons/jar.gif" alt="zip" width="16" height="16" /> Launch Risk Calculator</a></p>
<h2>Additional Information</h2>
<p><a href="http://svn.motlin.com/dev/Risk/trunk/"><img src="/icons/src.gif" alt="src" width="16" height="16" /> Subversion repository</a><br />
<a href="wp-content/Risk/api"><img src="/icons/javadoc.gif" alt="javadoc" width="16" height="16" /> Javadoc</a><br />
<a href="wp-content/releases/Risk"><img src="/icons/folder.gif" alt="releases" width="16" height="16" /> Old releases</a></p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2008/risk-calculator/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Configuration (dot) files</title>
		<link>http://motlin.com/2008/configuration-dot-files/</link>
		<comments>http://motlin.com/2008/configuration-dot-files/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 17:09:39 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/blog/?p=17</guid>
		<description><![CDATA[These are files I used to configure my command line environment, plus the scripts used to generate these pages. I'm always looking to improve my setup so please send me any useful tips.]]></description>
				<content:encoded><![CDATA[<p>These are files I used to configure my command line environment, plus the scripts used to generate these pages. These files are always evolving. You can find the latest versions <a href="https://github.com/motlin/dotfiles/tree/cygwin">on github</a>.</p>
<h2>Configuration files</h2>
<ul>
<li><a href="/wp-content/ConfigurationFiles/alias.html">.alias</a><br />
Aliases used by bash and zsh</li>
<li><a href="/wp-content/ConfigurationFiles/bashrc.html">.bashrc</a><br />
For <a href="http://www.gnu.org/software/bash/">GNU BASH</a></li>
<li><a href="/wp-content/ConfigurationFiles/profile.html">.profile</a><br />
Brings me directly to bash if ksh is my default shell</li>
<li><a href="/wp-content/ConfigurationFiles/screenrc.html">.screenrc</a><br />
For <a href="http://www.gnu.org/software/screen/">GNU Screen</a></li>
<li><a href="/wp-content/ConfigurationFiles/vimrc.html">.vimrc</a><br />
For <a href="http://www.vim.org/">Vim</a></li>
<li><a href="/wp-content/ConfigurationFiles/zshrc.html">.zshrc</a><br />
For <a href="http://www.zsh.org/">Zsh</a></li>
</ul>
<h2>Meta</h2>
<ul>
<li><a href="/wp-content/ConfigurationFiles/makefile.html">makefile</a><br />
To generate the html for the config files</li>
<li><a href="/wp-content/ConfigurationFiles/formatDotFiles.html">formatDotFiles.pl</a><br />
Does some extra formatting of the html output</li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2008/configuration-dot-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Ant Timer</title>
		<link>http://motlin.com/2008/ant-timer/</link>
		<comments>http://motlin.com/2008/ant-timer/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 16:17:41 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/software/?p=47</guid>
		<description><![CDATA[The Ant Timer is a plug-in that prints out diagnostic messages when ant targets and tasks take longer than the configured amount of time.]]></description>
				<content:encoded><![CDATA[<h2>Download</h2>
<p><a href="/wp-content/AntTimer/releases/ant-timer-1.0.1.jar"><img src="/icons/jar.gif" alt="jar" width="16" height="16" /> ant-timer.jar</a> Compiled classes<br />
<a href="/wp-content/AntTimer/releases/ant-timer-1.0.1-src.zip"><img src="/icons/jar_src_obj.gif" alt="src" width="16" height="16" /> ant-timer-src.zip</a> Source code</p>
<h2>Additional Information</h2>
<p><a href="http://svn.motlin.com/dev/AntTimer/trunk/"><img src="/icons/src.gif" alt="src" width="16" height="16" /> Subversion repository</a><br />
<a href="/wp-content/AntTimer/api"><img src="/icons/javadoc.gif" alt="javadoc" width="16" height="16" /> Javadoc</a><br />
<a href="/wp-content/AntTimer/releases"><img src="/icons/folder.gif" alt="releases" width="16" height="16" /> Old releases</a></p>
<h2>Five minute tutorial</h2>
<p>The Ant Timer is a plug-in that prints out diagnostic messages when ant targets and tasks take longer than the configured amount of time. You can use it with any ant build. Here is some sample output when I use it on my renamer program.</p>
<p style="padding-left: 30px;"><code>javadocs:<br />
[javadoc] Generating Javadoc<br />
[javadoc] Javadoc execution<br />
[javadoc] Loading source files for package com.motlin.base...<br />
[javadoc] Loading source files for package com.motlin.base.log4j...<br />
[javadoc] Loading source files for package com.motlin.renamer...<br />
[javadoc] Loading source files for package com.motlin.renamer.exceptions...<br />
[javadoc] Constructing Javadoc information...<br />
[javadoc] Standard Doclet version 1.6.0_06<br />
[javadoc] Building tree for all the packages and classes...<br />
[javadoc] C:\cygwin\home\Administrator\dev\renamer\trunk\src\com\motlin\base\log4j\StdOutErrAppender.java:318: warning - Tag @link: reference not found: org.apache.log4j.net.SocketAppender<br />
[javadoc] Building index for all the packages and classes...<br />
[javadoc] Building index for all classes...<br />
[javadoc] Generating C:\cygwin\home\Administrator\dev\renamer\trunk\dist\api\stylesheet.css...<br />
[javadoc] 1 warning<br />
<span style="color: #ff0000;">XXX task   [renamer.javadocs.javadoc] took 2 seconds to run<br />
XXX task   [renamer.null.sequential] took 2 seconds to run<br />
XXX task   [renamer.javadocs.ac:outofdate] took 2 seconds to run<br />
XXX target [renamer.javadocs] took 2 seconds to run</span></code></p>
<p>BUILD SUCCESSFUL<br />
Total time: 2 seconds</p>
<p>Running ant with the custom listener is pretty simple. Here&#8217;s the command that produced the output above.</p>
<p style="padding-left: 30px;"><code>ant -listener com.motlin.ant.timer.TimerListener -lib ant-timer-1.0.jar -lib lib/joda-time-1.5.2.jar -Dtarget.timer=2 -Dtask.timer=1 -Dtimer.prefix="XXX" clean javadocs</code></p>
<p>Let&#8217;s break it down.</p>
<p style="padding-left: 30px;"><code>-listener com.motlin.ant.timer.TimerListener</code></p>
<p>This is the class name of the custom task.</p>
<p style="padding-left: 30px;"><code>-lib ant-timer-1.0.jar -lib lib/joda-time-1.5.2.jar</code></p>
<p>Unfortunately, the Ant Timer depends on joda time. It didn&#8217;t have to but I like it better than using the built in classes for working with times. You&#8217;ll need to download the joda jar separately.</p>
<p style="padding-left: 30px;"><code>-Dtarget.timer=2 -Dtask.timer=1 -Dtimer.prefix="XXX" </code></p>
<p>You can configure the behavior through these properties. The first two are the timeouts in seconds for targets and tasks. If you leave them off they default to 10 seconds and 1 second respectively. The prefix makes it easy to find the output printed by the custom listener. If you leave it off it defaults to &#8220;!!!&#8221;</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2008/ant-timer/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Universal toString()</title>
		<link>http://motlin.com/2008/universal-tostring/</link>
		<comments>http://motlin.com/2008/universal-tostring/#comments</comments>
		<pubDate>Sat, 06 Sep 2008 15:34:34 +0000</pubDate>
		<dc:creator>Craig P. Motlin</dc:creator>
				<category><![CDATA[software]]></category>

		<guid isPermaLink="false">http://motlin.com/blog/?p=16</guid>
		<description><![CDATA[UniversalToString is a java class that prints out the internal state of an object using reflection. The UniversalToString class builds a String by recursively traversing the contents of an object and printing out the names, types, and values of all of its fields.]]></description>
				<content:encoded><![CDATA[<h2>Download</h2>
<p><a href="/wp-content/UniversalToString/releases/UniversalToString-1.0.3.jar"><img src="/icons/jar.gif" alt="jar" width="16" height="16" /> UniversalToString.jar</a> Compiled classes<br />
<a href="/wp-content/UniversalToString/releases/UniversalToString-1.0.3-src.zip"><img src="/icons/jar_src_obj.gif" alt="src" width="16" height="16" /> UniversalToString-src.zip</a> Source code</p>
<h2>Additional Information</h2>
<p><a href="http://svn.motlin.com/dev/UniversalToString/trunk/"><img src="/icons/src.gif" alt="src" width="16" height="16" /> Subversion repository</a><br />
<a href="/wp-content/UniversalToString/api/index.html"><img src="/icons/javadoc.gif" alt="javadoc" width="16" height="16" /> Javadoc</a><br />
<a href="/wp-content/UniversalToString/releases"><img src="/icons/folder.gif" alt="releases" width="16" height="16" /> Old releases</a></p>
<h2>Five minute tutorial</h2>
<p>UniversalToString is a java class that prints out the internal state of an object using reflection. The UniversalToString class builds a String by recursively traversing the contents of an object and printing out the names, types, and values of all of its fields.  It is loosely based on some code in <a href="http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fo%2FASIN%2F0132354764%3FSubscriptionId%3D11QBANPQ06N60EV6JN02&amp;tag=motlincom-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325">Core Java 2, Volume I &#8211; Fundamentals</a>.  The simplest way to use the UniversalToString class is in the implementation of your own classes&#8217; toString methods. Say we have a simple Person class:</p>
<pre class="brush: java">public class Person {
    private final String first;
    private final String last;
    public Person(final String first, final String last) {
        this.first = first;
        this.last = last;
    }
}</pre>
<p>Person does not implement the toString method, so when it is printed to standard out, Object.toString is used instead. The generated String contains the type name and the address in memory where the object lives. Normally this is not very useful information.</p>
<pre class="brush: java">Person john = new Person("John", "Smith");
System.out.println(john);</pre>
<p>Console output:</p>
<pre class="brush: text">tostring.Person@19821f</pre>
<p>To improve the output, we can override the toString method in the Person class.</p>
<pre class="brush: java">@Override
public String toString() {
    return "Person: first [" + this.first + "] last [" + this.last + "]";
}</pre>
<pre class="brush: text">Person: first [John] last [Smith]</pre>
<p>Implementing the toString method like this is tedious, repetitive, and fragile. That&#8217;s where UniversalToString comes in.</p>
<pre class="brush: java">@Override
public String toString() {
    return UniversalToString.print(this);
}</pre>
<pre class="brush: text">Person(first=John,last=Smith)</pre>
<h2>Collections</h2>
<p>UniversalToString has special logic for arrays, collections, and maps. This example uses a list, but the output is similar for each.</p>
<pre class="brush: java">Person john = new Person("John", "Smith");
Person jane = new Person(“Jane”, “Doe”);
List list = new ArrayList();
list.add(john);
list.add(jane);
System.out.println(UniversalToString.print(list));</pre>
<pre class="brush: text">ArrayList{Person(first=John,last=Smith), Person(first=Jane,last=Doe)}</pre>
<h2>Custom formatting</h2>
<p>Unfortunately our work isn&#8217;t done. The String created by UniversalToString isn&#8217;t particularly informative for certain objects. For example, let&#8217;s change the last example to print one Person and one Date:</p>
<pre class="brush: java">Person john = new Person("John", "Smith");
Date date = new Date(1206552944822l);
List list = new ArrayList();
list.add(john);
list.add(date);
System.out.println(UniversalToString.print(list));</pre>
<pre class="brush: text">ArrayList{Person(first=John,last=Smith), Date(fastTime=1206552944822,cdate=null)}</pre>
<p>We can customize the formatting of UniversalToString by creating an implementation of TypedToString that handles Date objects.</p>
<pre class="brush: java">public class DateToString implements TypedToString {
    private final DateFormat dateFormat;
    public DateToString() {
        this.dateFormat = new SimpleDateFormat();
        this.dateFormat.setTimeZone(TimeZone.getTimeZone("America/New_York"));
    }
    public boolean handlesType(final Object object) {
        return object instanceof Date;
    }
    public String print(final Object object) {
        return this.dateFormat.format((Date) object);
    }
}</pre>
<p>Now we just need to change the line that prints the list.</p>
<pre class="brush: java">System.out.println(UniversalToString.print(list, new DateToString()));</pre>
<pre class="brush: text">ArrayList{Person(first=John,last=Smith), 3/26/08 1:35 PM}</pre>
<p>The second argument to print is varargs TypedToString, so we can pass in as many special formatting rules as we need.</p>
]]></content:encoded>
			<wfw:commentRss>http://motlin.com/2008/universal-tostring/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
