<?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>The Grimoire</title>
	<atom:link href="http://codex.grimoire.ca/feed/" rel="self" type="application/rss+xml" />
	<link>http://codex.grimoire.ca</link>
	<description>Any insufficiently advanced magic is indistinguishable from technology.</description>
	<lastBuildDate>Wed, 18 Jan 2012 02:30:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Importing OpenSSL certificate/key pairs into Java keystores</title>
		<link>http://codex.grimoire.ca/2012/01/17/importing-openssl-certificatekey-pairs-into-java-keystores/</link>
		<comments>http://codex.grimoire.ca/2012/01/17/importing-openssl-certificatekey-pairs-into-java-keystores/#comments</comments>
		<pubDate>Wed, 18 Jan 2012 02:28:06 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=249</guid>
		<description><![CDATA[My day job ran into a situation where we were acquiring an SSL certificate (using the openssl(1) tools), which would be used by a third party in a Glassfish installation. Until recently, that required various hacks to convert the PKCS#10 key/cert files into something keytool(1) could manage, but no more: as of Java 6, keytool(1) [...]]]></description>
			<content:encoded><![CDATA[<p>My day job ran into a situation where we were acquiring an SSL certificate (using the <code>openssl(1)</code> tools), which would be used by a third party in a Glassfish installation. Until recently, that required various hacks to convert the PKCS#10 key/cert files into something <code>keytool(1)</code> could manage, but no more: as of Java 6, <code>keytool(1)</code> can handle PKCS#12 files, which makes it much easier to deal with this.</p>
<p><span id="more-249"></span></p>
<p>I&#8217;ve left a few variables in the commands below, which you&#8217;ll need to replace with the actual paths and values:</p>
<ul>
<li><code>GLASSFISH_HOME</code> should be replaced with the root directory of your Glassfish installation. In my case, that&#8217;s <code>/opt/glassfish3</code>, but your environment is probably different.</li>
<li><code>GLASSFISH_DOMAIN</code> should be replaced with the name of the Glassfish domain you&#8217;re configuring. That might be <code>domain1</code>, or if you&#8217;ve created your own domain with <code>asadmin create-domain</code>, use the name you used then.</li>
<li><code>KEY_ALIAS</code> should be replaced with some meaningful name for the SSL certificate pair. I used <code>https-www.example.com</code>, but you can use anything you like other than the two aliases used by Glassfish internally.</li>
</ul>
<p>You will need the Java 6 tools installed, including <code>keytool</code>, as well as the <code>asadmin</code> command set up to manage your domain. You will also need write access to the <code>keystore.jks</code> file stored in the domain&#8217;s <code>config</code> directory.</p>
<p>The domain should be stopped while you modify its keystore. If the domain is running, restart the domain in step three rather than starting the domain.</p>
<h3>Step one: convert the key/cert pair into a single PKCS#12 keystore</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">openssl pkcs12 <span style="color: #660033;">-export</span> \
	<span style="color: #660033;">-in</span> www.example.com.cert \
	<span style="color: #660033;">-inkey</span> www.example.com.key \
	<span style="color: #660033;">-out</span> https-certificates.pkcs12</pre></div></div>

<p>The openssl command will prompt for a password to use when creating the new PKCS#12 keystore. This is not optional, but since we only need this keystore temporarily, setting it to an unsafe password like &#8220;password&#8221; is fine. Just be careful to delete the keystore away safely afterwards.</p>
<h3>Step two: import the key/cert pair from the PKCS#12 keystore into Glassfish&#8217;s JKS keystore</h3>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">keytool <span style="color: #660033;">-importkeystore</span> \
	<span style="color: #660033;">-srckeystore</span> https-certificates.pkcs12 \
	<span style="color: #660033;">-srcstoretype</span> PKCS12 \
	<span style="color: #660033;">-srcstorepass</span> password \
	<span style="color: #660033;">-deststorepass</span> changeit \
	<span style="color: #660033;">-destkeypass</span> changeit \
	<span style="color: #660033;">-destkeystore</span> <span style="color: #007800;">$GLASSFISH_HOME</span><span style="color: #000000; font-weight: bold;">/</span>glassfish<span style="color: #000000; font-weight: bold;">/</span>domains<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">$GLASSFISH_DOMAIN</span><span style="color: #000000; font-weight: bold;">/</span>config<span style="color: #000000; font-weight: bold;">/</span>keystore.jks \
	<span style="color: #660033;">-srcalias</span> <span style="color: #000000;">1</span> \
	<span style="color: #660033;">-destalias</span> <span style="color: #007800;">$KEY_ALIAS</span></pre></div></div>

<p>(If you changed your Glassfish master password, you may need to change <code>-deststorepass</code> and <code>-destkeypass</code> to match. If you used a different password when creating the PKCS#12 keystore above, change <code>-srcstorepass</code> appropriately.)</p>
<h3>Step 3: configure Glassfish</h3>
<p>By default, Glassfish sets up a network listener called <code>http-listener-2</code> to handle HTTPS connections on port 8181. We&#8217;ll need to tell it to use the new key/cert pair:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">asadmin start-domain <span style="color: #007800;">$GLASSFISH_DOMAIN</span>
asadmin <span style="color: #000000; font-weight: bold;">set</span> server.network-config.protocols.protocol.http-listener-<span style="color: #000000;">2</span>.ssl.cert-nickname=<span style="color: #007800;">$KEY_ALIAS</span></pre></div></div>

<p>At this point, you should be able to point a browser at https://your-glassfish-server:8181/ and pick up the new key.</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2012/01/17/importing-openssl-certificatekey-pairs-into-java-keystores/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>LDAP 101: It&#8217;s A Database, Stupid</title>
		<link>http://codex.grimoire.ca/2011/11/25/ldap-101-its-a-database-stupid/</link>
		<comments>http://codex.grimoire.ca/2011/11/25/ldap-101-its-a-database-stupid/#comments</comments>
		<pubDate>Fri, 25 Nov 2011 20:30:27 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=228</guid>
		<description><![CDATA[We&#8217;re doing an LDAP-for-authentication rollout at my day job &#8211; the sort of thing there are lots of docs about already. One of the things we&#8217;ve run into is the lack of a single, complete document describing the whole tool ecosystem, from what LDAP is and how it works all the way through to how [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;re doing an <abbr title="Lightweight Directory Access Protocol">LDAP</abbr>-for-authentication rollout at my day job &#8211; the sort of thing there are <a href="https://help.ubuntu.com/community/OpenLDAPServer">lots</a> <a href="https://help.ubuntu.com/community/LDAPClientAuthentication">of</a> <a href="http://tldp.org/HOWTO/LDAP-HOWTO/authentication.html">docs</a> about already. One of the things we&#8217;ve run into is the lack of a single, complete document describing the whole tool ecosystem, from what LDAP is and how it works all the way through to how to use it to authenticate users.</p>
<p>So I thought I&#8217;d write one.</p>
<p>This post will cover some introductory knowledge about LDAP. Subsequent posts will introduce some LDAP tools, go into more detail on the data stored in a directory and on the implementation of an authentication system.</p>
<p>&#8220;LDAP&#8221; is an extensive subject, and I&#8217;m not going to try to cover every aspect of it. (For that, see the links at the bottom of this post.) I&#8217;ll be demonstrating simple bind authentication, without SASL or Kerberos/GSSAPI, and I won&#8217;t be going into too much detail outside of users and groups. In particular, I won&#8217;t be covering too much history, and I won&#8217;t be covering ActiveDirectory (the <em>other</em> widely-deployed authentication and directory service built on LDAP).</p>
<p><span id="more-228"></span></p>
<h3>LDIF</h3>
<p>I&#8217;ll be using <abbr title="LDAP Data Interchange Format">LDIF</abbr> to describe LDAP data throughout this series. Don&#8217;t worry about the details too hard for now; I&#8217;ll cover it in more detail later on. For now, all you&#8217;ll need to understand is how attributes and objects will appear in simple common cases.</p>
<h3>It&#8217;s A Database, Stupid</h3>
<p>LDAP is a protocol and a data model for providing access to a hierarchal (tree-shaped) database. The database itself is normally referred to as a &#8220;directory&#8221; (hence the name), a bit of nomenclature inherited from its predecessor, X.500, populated with objects that are bundles of attributes. LDAP supports direct lookup of an object by name, searches through subtrees or through the whole directory for objects matching a sophisticated pattern language, atomic updates to part of or all of individual objects, and network federation.</p>
<h4>Attributes</h4>
<p>LDAP&#8217;s data model is built on top of attributes. Attributes form the basis of object names, store data for user applications, and store data maintained by and for the directory service itself.</p>
<p>Each attribute has a name and a value. Some attributes can appear multiple times in the same object; others can appear only once.</p>
<p>Attributes are written in LDIF at the start of line. The attribute name appears first, followed by a colon, followed by the attribute value. Attributes can span multiple lines; a line that begins with whitespace is assumed to be a continuation of the preceeding line&#8217;s attribute value.</p>
<p>This LDIF fragment describes a single attribute:</p>
<pre>
givenName: John
</pre>
<p>This is a pair of attributes:</p>
<pre>
givenName: John
surname: Franklin
</pre>
<p>This is an attribute with multiple values:</p>
<pre>
mail: john@example.com
mail: jfranklin@example.net
</pre>
<p>Attributes are divided into two groups: user attributes, which hold data for applications using the directory, and operational attributes, which hold data for use by the directory system.</p>
<h4>Names</h4>
<p>Every object in an LDAP directory has a distinguished name (<abbr title="Distinguished Name">DN</abbr>) which identifies its location in the directory&#8217;s tree. An object&#8217;s name is a sequence of attributes, written with the root of the tree on the right. In a DN, attributes are written in <code>name=value</code> form and are separated by commas, rather than LDIF&#8217;s <code>name: value</code> form.</p>
<p>This is a DN:</p>
<pre>
givenName=John,o=Example
</pre>
<p>DNs have a parent-and-child relationship. In the example above, <code>givenName=John,o=Example</code> is the DN of a child of the object named <code>o=Example</code>. Directory trees can have very deep hierarchies of names, or very shallow hierarchies of names, depending on the needs of the application.</p>
<p>Every directory tree has a special DN called its base DN which acts as the root of the directory tree. The base DN of a directory tree may contain multiple segments, which would imply that it has parents, but the parent objects do not need to exist in any directory tree. Every directory service has a second special DN, identified by the empty string, naming the root of the directory service.</p>
<h4>Objects</h4>
<p>Objects hold the data stored in the directory. Each object has a name, distinct from the names of all other objects, and a suite of attributes describing the object. </p>
<p>In LDIF, objects are written as a list of attributes, starting with a <code>dn</code> attribute holding the object&#8217;s DN. Multiple objects can appear in a single LDIF file, separated by a blank line.</p>
<p>This is a pair of objects:</p>
<pre>
dn: o=Example
objectClass: organization
o: Example

dn: commonName=John Franklin,o=Example
objectClass: person
givenName: John
surname: Franklin
commonName: John Franklin
</pre>
<p>(The <code>dn</code> attribute in the LDIF description is not an attribute of the object, but rather part of the LDIF language. It must appear first in any LDIF representation.)</p>
<p>Every object has a user attribute named <code>objectClass</code>, which identifies what <em>kind</em> of data the object holds.</p>
<h4>Schemata</h4>
<p>Unlike more recent key/value databases, LDAP validates stored data against a schema. Each object&#8217;s <code>objectClass</code> attributes identify the schemata that apply to that object.</p>
<p>Object classes are broken down into two groups: structural classes and auxiliary classes. (There&#8217;s a third group, abstract classes, which we won&#8217;t cover here. Abstract classes do not usually appear in directory objects&#8217; <code>objectClass</code> values.) Every object in the directory <em>must</em> have exactly one <code>objectClass</code> attribute identifying a structural class, and can have many (or no) <code>objectClass</code> attributes identifying auxiliary classes.</p>
<p>The schema for an object class includes the class&#8217;s names (most object classes have a single name, but aliases are permitted, with the first name in the schema being the canonical one), a description, the name of the class it expands on, the structural or auxiliary nature of the class, a list of attributes objects with the class must have, and a list of attributes objects with the class may have.</p>
<p>Each attribute in an object class definition has a corresponding attribute type definition. The attribute type definition includes the attribute&#8217;s names (as with object classes, attributes can have multiple names; this is relatively common), description, the rules for comparing two values for equality, ordering, or substring presence, the syntax rules for the attribute&#8217;s values, flags indicating whether the attribute can have multiple values, and flags indicating whether the attribute is for user or directory tree usage.</p>
<p>The LDAP specifications (RFC 4510 and friends) provide a set of common object classes and attribute types that are available on most LDAP servers. There are also common schemata for a number of use cases, including acting as an address book, authentication database, or network information service. Most directory servers also allow users to provide their own schemata to customize the directory for their particular needs.</p>
<p>Parent/child relationships between objects are <em>not</em> specified by schema information. Instead, each application and deployment works out the hierarchy that best fits the problem at hand. Several common hierarchy conventions are documented in RFCs and reference manuals, but most LDAP tools are flexible enough to work with a wide variety of tree shapes.</p>
<h3>Finding Things</h3>
<p>LDAP provides the ability to search for objects within a directory tree. A search request comes with four things:</p>
<ul>
<li>The search base DN.</li>
<li>The search scope.</li>
<li>A filter.</li>
<li>A list of attributes to return.</li>
</ul>
<p>The search base and the search scope combine to limit the parts of the tree that the search will examine. There are four scopes:</p>
<dl>
<dt><code>base</code></dt>
<dd>Return only the object identified by the search base DN, or no object.</dd>
<dt><code>one</code></dt>
<dd>Return only objects that are immediate children of the object identified by the search base DN.</dd>
<dt><code>subtree</code></dt>
<dd>Return objects that are descendents of the object identified by the search base DN, and the object identified by the search base DN.</dd>
<dt><code>children</code></dt>
<dd>Return objects that are descendents of the object identified by the search base DN. (This is a common extension, rather than part of the LDAP standard.)</dd>
</dl>
<p>The directory applies the search filter to the objects that are in the search scope, including only objects that match the filter in the final result. The filter is specified using a small expression language that permits several kinds of matching on attributes. Primitive filters, which match attributes against values or patterns, have the form <code>(NAME OPERATOR VALUE)</code>, while composite filters, which combine filters together, have the form <code>(OPERATOR FILTER [FILTER...])</code>.</p>
<p>Some examples:</p>
<table>
<thead>
<tr>
<th>Filter</th>
<th>Description</th>
</tr>
</thead>
<tr>
<td><code>(givenName=John)</code></td>
<td>Match any object with a <code>givenName</code> attribute whose value is equal to <code>John</code> (under the equality rules given in the <code>givenName</code> attribute type definition in the schema).</td>
</tr>
<tr>
<td><code>(objectClass=person)</code></td>
<td>Match any <code>person</code> object.</td>
</tr>
<tr>
<td><code>(objectClass=*)</code></td>
<td>Match any object. (Idiomatic; every object has an objectClass attribute, and * matches all values. This is the default for many tools if no other filter is provided.)</td>
</tr>
<tr>
<td><code>(&#038;(objectClass=person)<br/>(mail=*@example.com))</code></td>
<td>Match any <code>person</code> object with a <code>mail</code> attribute that ends with <code>@example.com</code>. The filters are logically ANDed together.</td>
</tr>
</table>
<p>The full filter language includes support for partial matches, numerical comparison, presence or absence of an attribute, and several other features that I&#8217;m not going to go into here.</p>
<p>Finally, the directory server extracts each of the attributes named in the search request. Searches can ask for attributes by exact name, or by requesting the attribute <code>*</code> (which returns all user attibutes) or <code>+</code> (which returns all operational attributes). Attributes with multiple names are normally returned under their canonical name &#8211; for example, asking for the <code>surname</code> attribute will normally return attributes named <code>sn</code> under commonly used schemata. The final search result includes the matched objects&#8217; DNs, as well as the requested attributes.</p>
<h4>Indexing</h4>
<p>Like any database system, LDAP directory services generally provide indexing to ensure common searches return quickly, without having to walk the entire directory tree to find the matches. I&#8217;ll cover this in more detail for OpenLDAP in a later post.</p>
<h3>Next</h3>
<p>Next time, we&#8217;ll look at OpenLDAP and the UNIX ldap utility programs, and construct a simple address book.</p>
<h3>References</h3>
<ul>
<li><a href="http://tools.ietf.org/html/rfc4510">RFC 4510 &#8211; Lightweight Directory Access Protocol: Technical Specification Road Map</a></li>
<li><a href="http://www.zytrax.com/books/ldap/">LDAP for Rocket Scientists</a></li>
</ul>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2011/11/25/ldap-101-its-a-database-stupid/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Changing The Engine While You&#8217;re Driving Down The Highway</title>
		<link>http://codex.grimoire.ca/2011/10/13/alter-table-without-downtime/</link>
		<comments>http://codex.grimoire.ca/2011/10/13/alter-table-without-downtime/#comments</comments>
		<pubDate>Thu, 13 Oct 2011 15:13:48 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Databases]]></category>
		<category><![CDATA[Examples]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[uptime]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=220</guid>
		<description><![CDATA[In my day job, I&#8217;m the gatekeeper for changes to the company&#8217;s production databases. We recently ran into an interesting and difficult problem: how do we change a large, heavily-used table without violating our &#8220;avoid downtime if possible&#8221; mantra? The following is a slightly expurgated version of the postmortem I sent the team. Table names [...]]]></description>
			<content:encoded><![CDATA[<p>In my day job, I&#8217;m the gatekeeper for changes to the company&#8217;s production databases. We recently ran into an interesting and difficult problem: how do we change a large, heavily-used table without violating our &#8220;avoid downtime if possible&#8221; mantra?</p>
<p>The following is a slightly expurgated version of the postmortem I sent the team. Table names have been changed to protect the guilty.</p>
<p><span id="more-220"></span></p>
<h3>What Hotfixes?</h3>
<p>In the deployment plan for Sinistar&#8217;s post-release migrations, there were two <code>ALTER TABLE</code> migrations slated to make structural changes to the <code>customer</code> and <code>customer_login</code> tables (changing the types of some columns, specifically). When we tested these migrations on a copy of the production system&#8217;s data, we discovered that they would have caused those tables to be inaccessible for two and five minutes respectively. Since customers are core to our applications, we determined that this qualified as &#8220;downtime&#8221; in the release plan.</p>
<p>I prototyped an alternate approach to the <code>customer</code> changes (outlined below) intended to make changes to the table without taking the app offline while they were happening. The alternate approach was considerably more complex than the original <code>ALTER TABLE</code> statement, as well as being considerably slower (~15 minutes for our largest shard, as compared to two minutes); we discussed whether it was worth taking the app offline at zero-dark-hundred on a weekend instead and concluded that we&#8217;d need to be able to make online changes to expensive tables eventually and opted to go ahead with the alternate approach as a learning experience.</p>
<h3>How Did It Work?</h3>
<p>The alternate approach we used relied on more MySQL-side tools to make the changes. Specifically, we:</p>
<ol>
<li>Created a new, empty version of the <code>customer</code> table (the &#8220;target&#8221;) table based on the current <code>customer</code> table plus intended structural changes. Structural changes to an empty table that the app does not use are quite cheap.</li>
<li>Installed triggers on <code>INSERT</code>, <code>UPDATE</code>, and <code>DELETE</code> operations on the <code>customer</code> table that copied rows (using <code>REPLACE</code>) into the target table, applying the intended transforms to the affected columns.</li>
<li>Installed a server-side procedure (&#8220;the migration procedure&#8221;) that:
<ol>
<li>Selected all of the keys from the original <code>customer</code> table.</li>
<li>For each key, copied the corresponding row from the <code>customer</code> table to the target table using <code>REPLACE</code>.</li>
</ol>
</li>
<li>Ran the migration procedure. (This part is where most of the time went.)</li>
<li>Verified that the contents of the target table were in 1:1 correspondence with the contents of the original <code>customer</code> table, where the only permitted differences between corresponding rows were the changes intended by the original migration.</li>
<li>Renamed the original <code>customer</code> table out of the way and renamed the target table to <code>customer</code>.</li>
<li>Verified that nothing had gone horribly wrong in our apps.</li>
<li>Dropped the renamed version of the original <code>customer</code> table, along with the triggers and the migration procedure.</li>
</ol>
<p>The steps are grouped into four phases: <code>prepare.xml</code> (covering steps 1-3), <code>execute.xml</code> (covering step 4), <code>cutover.xml</code> (covering step 6) and <code>cleanup.xml</code> (covering step 8). The steps not covered by phases were performed semi-manually.</p>
<p>The changes to <code>customer_login</code> proceeded along the same paths.</p>
<h3>What Went Right</h3>
<p>Well, first of all, we noticed a potential app outage hazard before it went live.</p>
<p>We were able to back out and rewrite the migrations without affecting existing development environments, where the original <code>ALTER TABLE</code> might have already run, thanks largely to <a href="http://liquibase.org">Liquibase</a>&#8216;s precondition support.</p>
<p>We ran through several versions of this plan, both on my development environment image and with QA (on our release candidate environment), which ferretted out a handful of subtle bugs in the migration procedure that would&#8217;ve damaged customer data. Our QA team&#8217;s extensive experience and automation tooling around our applications prevented any of those mistakes from going live.</p>
<p>Designing the hotfix in several phases permitted us to run the relatively safe phases over the weekend, with relatively little involvement from our operations team beyond monitoring.</p>
<p>The verification steps did not take out our applications in the process, and revealed a way to move at least some verification steps out of our applications&#8217; scripts directory and into the <code>core-dbs</code> source tree.</p>
<p>Finally, it worked. Despite the relatively high complexity and the large collection of moving parts, this alternate plan worked very well. Rather than taking our applications offline, everything stayed up (and didn&#8217;t slow down too badly).</p>
<h3>What Went Wrong, And What We Learned</h3>
<h4>Finding Problems Late is A Problem</h4>
<p>We didn&#8217;t catch the initial problem before it went out to everyone&#8217;s development environments. I&#8217;ve habitually not worried about migration timing until code freeze, which means that potentially-troublesome migrations will have already run on our bleeding-edge environment (and possibly our release candidate environment) before we notice the problem. This lead to increased complexity in the alternate migration path&#8217;s Liquibase configuration &#8211; we made extensive use of preconditions to ensure that the alternate migration would be harmless on systems where the original <code>ALTER TABLE</code> had already run, which seems to have worked, but I&#8217;d rather not have had that problem in the first place as it&#8217;s another moving part that can have problems.</p>
<h4>Concurrency is a Hard Problem</h4>
<p>The first version of the migration procedure looked like this:</p>
<ol>
<li>Select all of the <strong>rows</strong> from the original <code>customer</code> table.</li>
<li>For each row, insert that row into the target table using <code>REPLACE</code>.</li>
</ol>
<p>The select and loop steps use a MySQL cursor to iterate over their results. While the <a href="http://dev.mysql.com/doc/refman/5.0/en/cursors.html">MySQL documentation</a> states that it&#8217;s undefined whether a cursor is over a snapshot of the query results or over the underlying tables, in practice we found that it was using a snapshot. Since the loop took considerable time to run, and since it (intentionally) did not prevent further changes to the original <code>customer</code> table from occurring after the snapshot, the loop would insert, into the target table, a &#8220;stale&#8221; version of any <code>customer</code> row that had been modified since the snapshot was taken (overwriting the correct version copied into the target table by the triggers on the original table).</p>
<p>We only noticed this on our release candidate environent. When we ran through the migration plan initially to ensure that the procedure did not block our applications&#8217; normal usage, QA ran part of their Selenium suite, which produces a relatively high volume of test traffic. Out of ten thousand customers in the <code>release-candidate</code> databases, four customers had stale rows copied. This is not a bug we would ever have identified using only manual testing (even with a verification tool), so I&#8217;m intensely pleased that we had automation in place and very glad I got QA involved in testing the hotfix as extensively as we did.</p>
<p>Switching from a snapshot of the rows in <code>customer</code> to a snapshot of the keys in <code>customer</code> and selecting out the row itself only immediately before copying it to the target table addressed the problem; even in production, our verification script identified exactly zero mismatches between the original <code>customer</code> table and the target table (across ten million customers). It also made the procedure body somewhat shorter and simpler by reducing the number of fields copied from the cursor on each step of the iteration.</p>
<h4>I&#8217;ve Got Ten Million Queries, Is This Bad?</h4>
<p>Boy, is it slow. The migration procedure, which issues one query per affected <code>customer</code> row, takes approximately ten times as long as a straight <code>ALTER TABLE</code>, and produces considerably more IO load. This lead to the <code>customer</code> hotfix running into Friday morning (we kicked it off Thursday afternoon). I made some changes to the <code>customer_login</code> migration procedure to attempt to compensate &#8211; instead of issuing a <code>REPLACE</code> query for each row, we used a prefix of <code>customer_login</code>&#8216;s primary key (<code>customerid</code>) instead to issue only one <code>REPLACE</code> query for each customer&#8217;s worth of rows. This seems to have helped, but not by much. Future iterations of this process might even go by larger groups of rows, depending on the average and worst-case number of rows per group.</p>
<h4>The Best Laid Plans&#8230;</h4>
<p>We ran through three iterations of the migration procedure for the <code>customer</code> table before we settled on a working version. While I did design recovery migrations to back out the broken versions of the procedure that were intended to leave trunk and other environments unaffected by the iterative testing process, they didn&#8217;t quite work as planned. Somehow (and we still don&#8217;t know how for sure), all of the customers on our bleeding-edge environment were deleted outright instead of being migrated over. We weren&#8217;t able to reproduce the problem with the final plan, and we do keep backups of our testing databases, so we were able to recover the missing customers, but it&#8217;s another thing that we only noticed because of QA&#8217;s intervention.</p>
<p>We avoided this with <code>customer_login</code> by not building the migrations for that table until we&#8217;d ironed out issues in the <code>customer</code> migrations, and in the future we&#8217;ll avoid rolling this sort of hotfix into master (for our bleeding edge environment and development environments) until we&#8217;re confident that it&#8217;s correct.</p>
<h4>No Space Left On Device</h4>
<p>This is one that caught me totally by surprise. When we ran the migration procedure for <code>customer</code> in production, we learned that passing 10m queries through the MySQL binary logs (for replication) uses up a lot of space. Fortunately, we keep the binlogs on a separate partition from the main data store, so this did not take MySQL out, but it could&#8217;ve if our Ops folks weren&#8217;t so sharp. This did, however, take replication out temporarily. Operations resized the partition the binlogs live on, which kept things running (slowly, but smoothly) while the <code>customer_login</code> migration procedure ran.</p>
<h4>Read Locking Is Not Your Friend</h4>
<p>Our initial version of a verification tool, a PHP script in one of our apps&#8217; <code>scripts/</code> directory that ran against the live databases on our master database, would&#8217;ve taken our applications offline in roughly the same way as the original <code>ALTER TABLE</code> statements would&#8217;ve. I only realized this when I ran the verification queries (outside of their script) against one of our replica databases&#8217; shards and realized how long they took, shortly before we had planned to run the verification step in the hotfix plan. Fortunately, the verification used very little from our app&#8217;s environment; I ported it to a simple shell script and ran it against a replica (after replication caught up) instead. In the future, I&#8217;m going to try to get more of our verification scripts written that way where possible, since it does a good job of insulating our applications from side effects.</p>
<h3>In Summary</h3>
<p>Working with large data sets is hard, especially in MySQL (where even reads can cause locks). Making structural changes to those data sets is even harder, especially while the application&#8217;s in flight. However, I feel like this went relatively well even with the &#8220;creative&#8221; issues discovered during testing, and we&#8217;ll be using this approach for large tables more frequently in the future. If you&#8217;re making a change to a table and you&#8217;re not sure if it&#8217;s &#8220;large&#8221; or not, we&#8217;re here to help.</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2011/10/13/alter-table-without-downtime/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Juggling Plates in Variable Gravity</title>
		<link>http://codex.grimoire.ca/2011/09/19/juggling-plates-in-variable-gravity/</link>
		<comments>http://codex.grimoire.ca/2011/09/19/juggling-plates-in-variable-gravity/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 23:08:32 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Manifestos and Rants]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=201</guid>
		<description><![CDATA[We all know from painful experience that modifying and extending existing software is far more expensive than creating software from scratch. Whether you call it &#8220;refactoring&#8221; or &#8220;maintenance&#8221; or something else, once a software system goes live, suddenly changes become significantly more challenging. However, we don&#8217;t often talk about why, so permit me to indulge [...]]]></description>
			<content:encoded><![CDATA[<p>We all know from painful experience that modifying and extending existing software is far more expensive than creating software from scratch. Whether you call it &#8220;refactoring&#8221; or &#8220;maintenance&#8221; or something else, once a software system goes live, suddenly changes become significantly more challenging. However, we don&#8217;t often talk about why, so permit me to indulge in an analogy.</p>
<p><span id="more-201"></span></p>
<p>Imagine that you, and a few of your close friends, are practicing a juggling act that will involve throwing fine china plates, donated by volunteers in the audience, to one another. Knowing that this is an expensive and risky act, your troupe first procures a large stack of &#8220;practice&#8221; plates &#8211; they&#8217;re about the right shape, and about the right weight, and they&#8217;re <em>cheap</em> so that when you inevitably drop a few and smash them, they&#8217;re easy to replace.</p>
<p>Your troupe&#8217;s practice goes about as well as can be expected &#8211; incremental improvements, many smashed plates, a few complete reworks of your blocking and choreography, many smashed plates, a lot of feedback from a handful of folks you&#8217;ve dragged into rehearsals with you, and many smashed plates. However, every time the act falls apart and the plates hit the ground, the group of you shrug, pick up some more plates, and try again. These &#8220;practice&#8221; plates have <em>no intrinsic value</em>, so losing them all means nothing and costs very little.</p>
<p>Little by little, you refine your act, until you can do it without any dropped plates at all! You and your troupe can smoothly keep enough plates in the air to serve a five-course meal without having any mishaps.</p>
<p>Then comes opening night.</p>
<p>Suddenly, you&#8217;re up there with your expensive, real plates, in front of an audience of hundreds or thousands. These plates <em>matter</em>, and even if you drop one, you&#8217;ve still got many others in the air that also matter.</p>
<p>In fact, these plates matter so much that if you do drop one, you need to somehow put it back together and get it back into the show <em>without dropping the others.</em></p>
<p>Two nights later, you discover that you need to change your juggling act, right in the middle of the show, without dropping any plates.</p>
<p>So it goes with software, too. While you&#8217;re building a new system, it&#8217;s relatively easy to gut your test data and start over, and nobody cares. Once your system holds real data, though, you need some way to keep the plates in the air while you make changes. Suddenly, the data matters, and the longer your system&#8217;s been around, the more it matters to everyone involved.</p>
<p>While there are other factors that drive up the cost of maintaining existing software, screwing up your users&#8217; data is one of the fastest ways to destroy your users&#8217; trust. How well does your system juggle plates?</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2011/09/19/juggling-plates-in-variable-gravity/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Some Notes on Replicating OpenDirectory to OpenLDAP</title>
		<link>http://codex.grimoire.ca/2010/09/18/some-notes-on-replicating-opendirectory-to-openldap/</link>
		<comments>http://codex.grimoire.ca/2010/09/18/some-notes-on-replicating-opendirectory-to-openldap/#comments</comments>
		<pubDate>Sat, 18 Sep 2010 19:15:03 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[ldap]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=181</guid>
		<description><![CDATA[I did some work on a contract recently that involved creating an OpenLDAP replica of an OpenDirectory database. Here&#8217;s what I learned: All of these notes assume you&#8217;re setting up an LDAP replica on OpenLDAP running with cn=config configuration. I did this on Ubuntu 10.04, and many commands include Ubuntu- or Debian-specific assumptions. Translating to [...]]]></description>
			<content:encoded><![CDATA[<p>I did some work on a contract recently that involved creating an OpenLDAP replica of an OpenDirectory database. Here&#8217;s what I learned:</p>
<p><span id="more-181"></span></p>
<p>All of these notes assume you&#8217;re setting up an LDAP replica on OpenLDAP running with <code>cn=config</code> configuration. I did this on Ubuntu 10.04, and many commands include Ubuntu- or Debian-specific assumptions. Translating to <code>slapd.conf</code> or to, say, Redhat Enterprise Linux is left as an exercise. Obviously, most of this is <strong>not safe to apply to an existing OpenLDAP server</strong> and there are a few places where we step outside of Apple&#8217;s comfortable padded box on the OpenDirectory side, too. I&#8217;ve tried to point those out, but try this at your own risk.</p>
<h3>Directory Access</h3>
<p>You can use <code>ldapsearch</code> and <code>ldapmodify</code> and so on to manipulate and search the OpenDirectory master. Apple configures <code>slapd</code> to allow access to the <code>cn=config</code> tree to anyone who connects via <code>ldapi://%2fvar%2frun%2fldapi</code>, even &#8220;anonymously&#8221;. For example, to read the <code>cn=config</code> entry you&#8217;d use</p>
<pre>
master$ sudo ldapsearch -x -H ldapi://%2fvar%2frun%2fldapi -b cn=config \
    -s base '*' '+'
</pre>
<h3>Passwords</h3>
<p>&#8220;Open Directory&#8221; passwords live in Apple Password Server, which I didn&#8217;t try to replicate over, rather than in the directory. For users who should be able to authenticate against the replica, I switched their passwords to Crypt passwords in Workgroup Manager. This has all of the downsides of Crypt password storage: 8-character limit, weak digest, and so on.</p>
<h4>Open Questions</h4>
<p>Does Workgroup Manager use the moral equivalent of <code>ldappasswd(1)</code> when setting users&#8217; passwords? If so, configuring &#8220;Crypt&#8221; passwords to use salted SHA instead may be a matter of changing <code>olcPasswordHash</code> to <code>{SSHA}</code>. We weren&#8217;t brave enough to try.</p>
<h3>Certificates</h3>
<p>On the OpenDirectory master, I generated a self-signed &#8220;SSL Server&#8221; cert with a long expiry (ten years) and configured OpenDirectory to use it for SSL. The resulting <code>.pem</code> file is named after the server (<code>ldap.example.com</code>) and lives in <code>/etc/certificates/ldap.example.com.A REALLY LONG HEX STRING.cert.pem</code>. You can find the exact name by looking at the <code>olcTLSCertificateFile</code> entry in <code>cn=config</code>. If you&#8217;re using SSL for replication (and you should be), grab a copy of this file and put it on the replica:</p>
<pre>
master$ scp /etc/certificates/ldap.example.com.*.cert.pem \
    replica:/tmp/ldap.example.com_cert.pem
</pre>
<p>and</p>
<pre>
replica$ sudo cp /tmp/ldap.example.com_cert.pem /etc/ssl/certs/
</pre>
<h3>Replication Provider</h3>
<p>OpenDirectory compiles <code>syncrepl</code> directly into <code>slapd</code>, for use with OpenDirectory replica servers. However, you can abuse that support to provide replication to OpenLDAP as well. You&#8217;ll need to add the syncrepl configuration to your directory manually. Here&#8217;s the LDIF I used:</p>
<pre>
master$ sudo ldapadd -x -H ldapi://%2fvar%2frun%2fldapi &lt;&lt;'SYNCREPL'
dn: olcOverlay=syncprov,olcDatabase={1}bdb,cn=config
objectClass: olcSyncProvConfig
olcOverlay: syncprov
olcSpCheckpoint: 100 10
olcSpSessionlog: 100
SYNCREPL
</pre>
<p>You can use any of the syncrepl provider configuration options here.</p>
<h4>Open Questions</h4>
<p>This configuration uses basic syncrepl. In theory delta syncrepl should work as well, but I was trying to make as few changes to the OpenDirectory server as possible.</p>
<p>It&#8217;s unclear what changes in Server Admin will blow away the syncrepl overlay, but I suspect setting up a real OpenDirectory replica may affect this config.</p>
<h3>Schemata</h3>
<p>Hoo boy.</p>
<p>OpenDirectory&#8217;s directory server is a patched version of OpenLDAP. The patches include baked-in support for a handful of attribute types and object classes, which are omitted in Apple&#8217;s <code>apple.schema</code> and <code>apple_auxiliary.schema</code> files. I opted to use Apple&#8217;s schema files as-is to create the <code>cn=schema,cn=config</code> subtree:</p>
<pre>
master$ mkdir ~/schema
master$ cp /etc/openldap/schema/*.schema ~/schema
master$ cat > ~/schema/extensions.schema &lt;&lt;'EXTENSIONS_SCHEMA'
attributetype (
	1.3.6.1.4.1.63.1000.1.1.2.16.1
	NAME 'authAuthority'
	DESC 'password server authentication authority'
	EQUALITY caseExactIA5Match
	SUBSTR caseExactIA5SubstringsMatch
	SYNTAX 1.3.6.1.4.1.1466.115.121.1.26 )

attributetype (
	1.3.6.1.4.1.63.1000.1.1.1.21.1
	NAME 'apple-acl-entry'
	DESC 'acl entry'
	EQUALITY caseExactMatch
	SUBSTR caseExactSubstringsMatch
	SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )

objectclass (
	1.2.840.113556.1.3.23
	NAME 'container'
	STRUCTURAL
	MUST cn )
EXTENSIONS_SCHEMA
</pre>
<p>Looking at OpenDirectory&#8217;s <code>cn=schema,cn=config</code> shows that Apple uses only nine schema files (and one of them is empty, so I omitted it). Use <code>slaptest</code> to convert the <code>.schema</code> files to a <code>cn=schema</code> directory:</p>
<pre>
master$ cat > ~/schema/convert.conf &lt;&lt;CONVERT_CONF
include $HOME/schema/core.schema
include $HOME/schema/cosine.schema
include $HOME/schema/nis.schema
include $HOME/schema/inetorgperson.schema
include $HOME/schema/misc.schema
include $HOME/schema/samba.schema
include $HOME/schema/fmserver.schema
include $HOME/schema/extensions.schema
include $HOME/schema/apple_auxillary.schema
include $HOME/schema/apple.schema
CONVERT_CONF
master$ mkdir ~/schema/output
master$ slaptest -f ~/schema/convert.conf -F ~/schema/output
</pre>
<p>This converts the whole configuration file into a <code>cn=config</code> tree, but we&#8217;re only interested in the <code>cn=schema,cn=config</code> subtree:</p>
<pre>
master$ (cd ~/schema/output/cn\=config/ &#038;&#038; \
    tar czf ~/cn_schema.tar.gz cn\=schema)
master$ scp ~/cn_schema.tar.gz replica:/tmp/
</pre>
<p>On the replica, we&#8217;ll need to blow away OpenLDAP&#8217;s default schemata and replace them:</p>
<pre>
replica$ sudo -i # Yes.
replica# service slapd stop
replica# cd /etc/ldap/slapd.d/cn\=config/
replica# rm -rf cn\=schema
replica# tar xzf /tmp/cn_schema.tar.gz
replica# chown -R openldap:openldap cn\=schema
replica# chmod -R g+rX cn\=schema # Files 0640, dirs 0750
replica# service slapd start
replica# exit
</pre>
<p>If slapd fails to start, run it in debug mode as <code>root</code> to see why:</p>
<pre>
slapd -h 'ldap:/// ldapi:///' -g openldap -u openldap \
    -F /etc/ldap/slapd.d/ -d 5
</pre>
<p>If I&#8217;ve omitted a schema element, or if you need to recover a schema element from the master, use the following query:</p>
<pre>
master$ sudo ldapsearch -x -H ldapi://%2fvar%2frun%2fldapi \
    -b cn=Subschema -s base '*' '+' | less
</pre>
<p>The output will include many entries of the form</p>
<pre>
attributeTypes: ( 1.3.6.1.4.1.63.1000.1.1.2.16.2 NAME 'authAuthority' DESC 'pa
 ssword server authentication authority' EQUALITY caseExactMatch SUBSTR caseEx
 actSubstringsMatch SYNTAX 1.3.6.1.4.1.1466.115.121.1.15 )
</pre>
<p>and</p>
<pre>
objectClasses: ( 1.2.840.113556.1.3.23 NAME 'container' STRUCTURAL MUST cn )
</pre>
<p>These are easy to convert to OpenLDAP schema entries. (In fact, go back and look at the contents of extensions.schema – these two entries should look familiar.) Find the ones you&#8217;re looking for, add them to extensions.schema, regenerate the schema directory as above, and blow away and replace the replica&#8217;s <code>cn=schema,cn=config</code> tree with the updated version.</p>
<h3>Replication Consumer</h3>
<p>Once the schemata are reasonably sane and <code>slapd</code> starts properly with them installed, we can create the replica. We&#8217;ll also need to create a database to hold it; we can do both in one step.</p>
<pre>
replica$ sudo ldapadd -Y EXTERNAL -H ldapi:/// &lt;&lt;'REPLICA'
dn: olcDatabase=hdb,cn=config
objectClass: olcDatabaseConfig
objectClass: olcHdbConfig
olcDatabase: {1}hdb
olcSuffix: dc=example,dc=com
olcDbDirectory: /var/lib/ldap
olcRootDN: uid=diradmin,cn=users,dc=example,dc=com
olcRootPW: DIRADMIN_PASSWORD
olcDbConfig: set_cachesize 0 2097152 0
olcDbConfig: set_lk_max_objects 1500
olcDbConfig: set_lk_max_locks 1500
olcDbConfig: set_lk_max_lockers 1500
olcDbIndex: objectClass eq
olcDbIndex: uid eq
olcDbIndex: cn eq
olcDbIndex: member eq
olcDbIndex: entryUUID eq
olcLastMod: TRUE
olcDbCheckpoint: 512 30
olcAccess: to attrs=userPassword
    by dn="uid=diradmin,cn=users,dc=example,dc=com" manage
    by anonymous auth
    by self read
    by * none
olcAccess: to dn.base=""
    by dn="uid=diradmin,cn=users,dc=example,dc=com" manage
    by users read
    by * none
olcAccess: to *
    by dn="uid=diradmin,cn=users,dc=example,dc=com" manage
    by users read
    by * none
olcSyncRepl: rid=100
    provider=ldaps://ldap.example.com/
    bindmethod=simple
    binddn="uid=diradmin,cn=users,dc=example,dc=com"
    searchbase="dc=example,dc=com"
    credentials=DIRADMIN_PASSWORD
    schemachecking=off
    type=refreshAndPersist
    retry="60 +"
    interval="00:00:10:00"
    tls_cacert="/etc/ssl/certs/ldap.example.com_cert.pem"
REPLICA
</pre>
<p>Replication will use the <code>diradmin</code> login you set up when you created the OpenDirectory master. Obviously, you can use a different user, but if you want passwords to be replicated that user needs to be a directory administrator.</p>
<p>The <code>interval</code> and <code>retry</code> settings can be tweaked; we&#8217;ve found that ten minute refresh intervals plus one minute retries on failure are a reasonable balance of currency and bandwidth use. If you need to force an immediate refresh for any reason, restart <code>slapd</code> on the replica.</p>
<p>The ACL given by the <code>olcAccess</code> attributes allows users to authenticate against the replica and read information from it, but disallows anonymous access and changes (except to the root DN for the database, which provides an escape hatch if you need to fix something by hand).</p>
<p>The database should replicate over immediately. If it doesn&#8217;t, start <code>slapd</code> up in debug mode (see above) and look at the errors. In particular, if you see a message similar to</p>
<pre>
syncrepl_message_to_entry: rid=100 mods check (objectClass: value
#0 invalid per syntax)
</pre>
<p>it means you&#8217;re missing some schema entries. The DN of the object it was trying to replicate is on the preceeding line that looks like</p>
<pre>
&lt;&lt;&lt; dnPrettyNormal: &lt;cn=users,dc=example,dc=com&gt;, &lt;cn=users,dc=example,dc=com&gt;
</pre>
<p>and you can check the affected attributes against the OpenDirectory master.</p>
<h4>Open Questions</h4>
<p>The same remark about delta syncrepl applies here, too.</p>
<h3>Testing</h3>
<p>If you have a user in your OpenDirectory master, you can test the replica by running searches against it:</p>
<pre>
replica$ ldapsearch -x -W -D uid=user,cn=users,dc=example,dc=com \
    -b dc=example,dc=com '(uid=user)'
</pre>
<p>This should prompt you for the user&#8217;s password, then print back the user&#8217;s entry.</p>
<h3>PAM and NSS Notes</h3>
<p>OpenDirectory&#8217;s extensions sit on top of a mostly-normal RFC 2307 directory structure. Users are <code>inetOrgPerson, posixAccount, shadowAccount</code> objects, with some ancillary schemata to support the Apple features. The subtrees for users, groups, and so on are named <code>cn=users</code> rather than <code>ou=users</code> as in the RFC, so you may have to twiddle <code>ldap.conf</code> to recognize the correct subtrees.</p>
<p>OS X home directories are usually in <code>/Users</code>, not <code>/home</code>. It&#8217;s not really worth arguing with it; just create <code>/Users</code> home directories on your linux client boxes as well.</p>
<p>Again, only users with Crypt passwords in the OpenDirectory master will be able to use the replica for authentication.</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2010/09/18/some-notes-on-replicating-opendirectory-to-openldap/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Abandoning switch In Three (And A Bit) Steps</title>
		<link>http://codex.grimoire.ca/2010/07/23/breaking-away-from-switch/</link>
		<comments>http://codex.grimoire.ca/2010/07/23/breaking-away-from-switch/#comments</comments>
		<pubDate>Fri, 23 Jul 2010 19:32:46 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Examples]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=163</guid>
		<description><![CDATA[Everyone who&#8217;s worked in a C-derived language has seen a switch statement or two in their lives. There are a few well-known reasons not to use them, but somehow they crop up now and then anyways because they&#8217;re just so convenient — initially. Switch is a holdover from lower-level languages (mostly, various assembly languages) where [...]]]></description>
			<content:encoded><![CDATA[<p>Everyone who&#8217;s worked in a C-derived language has seen a <code>switch</code> statement or two in their lives. There are a few well-known reasons not to use them, but somehow they crop up now and then anyways because they&#8217;re just so convenient — initially.</p>
<p>Switch is a holdover from lower-level languages (mostly, various assembly languages) where the destination of a <code>jmp</code> instruction could be computed or looked up in a table. In modern languages, there are usually more appropriate structures for determining which piece of code to jump to: that&#8217;s what <a href="http://en.wikipedia.org/wiki/Virtual_method_table">virtual dispatch</a> does, for example.</p>
<p>Unfortunately, it&#8217;s not always obvious how to &#8220;fix&#8221; code that makes heavy use of <code>switch</code>. </p>
<p><span id="more-163"></span></p>
<p>I normally try to avoid this, but for the sake of clarity I&#8217;ve omitted a couple of good style points. Online presentation makes certain Java conventions difficult to present; just because the following examples use a single compilation unit with default-visibility symbols doesn&#8217;t mean you should, too.</p>
<h3>Starting Points</h3>
<p>Let&#8217;s take a simple example that uses a <code>switch</code> statement to control the state of some service. The surrounding context involves a simple control protocol for a light dimmer: it can either be turned fully off, turned fully on, or set to a &#8220;dim&#8221; state halfway between the two.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> States <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> OFF <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> DIM <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> ON <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DimmerController <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntensity<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        doStuffBeforeChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">case</span> OFF<span style="color: #339933;">:</span>
                disable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">case</span> DIM<span style="color: #339933;">:</span>
                beginDimCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">case</span> ON<span style="color: #339933;">:</span>
                beginFullCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        doStuffAfterChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> disable<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginDimCycle<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginFullCycle<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Eminent Domain</h3>
<p>There are a few obvious problems with the version above. For starters, there&#8217;s nothing preventing someone from passing the wrong value, either from the wrong set of constants or by hard-coding the literal values instead of using the constants. We could add a <code>default</code> clause that raises an <code>IllegalArgumentException</code>, but we&#8217;d still only find out about the problem at runtime. We&#8217;d really like to find out about errors in our programs as early as possible: compile time, or even as we type them. Most languages provide a tool for doing that; since we&#8217;re using Java, we&#8217;ll use an <code>enum</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">enum</span> State <span style="color: #009900;">&#123;</span>
    OFF,
    DIM,
    ON
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DimmerController <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntensity<span style="color: #009900;">&#40;</span>State intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        doStuffBeforeChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">switch</span> <span style="color: #009900;">&#40;</span>intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">case</span> OFF<span style="color: #339933;">:</span>
                disable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">case</span> DIM<span style="color: #339933;">:</span>
                beginDimCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">case</span> ON<span style="color: #339933;">:</span>
                beginFullCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        doStuffAfterChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> disable<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginDimCycle<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginFullCycle<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>On Your Best Behaviour</h3>
<p>That&#8217;s better, but it still has most of the fundamental problems <code>switch</code> statements bring with them. While I&#8217;m only showing one <code>switch</code>, apps that have them tend to have multiple <code>switch</code>es over the same set of constants, which leads to code duplication throughout the app and makes it hard to determine what a given constant will cause the system to do. In an object-oriented system, we normally try to keep behaviour with the data it works with, and as a side effect of refactoring to using an enumerated type, we now have real objects to use. Let&#8217;s add some methods to our enumerated type to handle the per-case behaviour.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">enum</span> State <span style="color: #009900;">&#123;</span>
    OFF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            disable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> disable<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;">/* ... */</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    DIM<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            beginDimCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginDimCycle<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;">/* ... */</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    ON<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            beginFullCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginFullCycle<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;">/* ... */</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DimmerController <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntensity<span style="color: #009900;">&#40;</span>State intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        doStuffBeforeChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intensity.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        doStuffAfterChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Opening the Set</h3>
<p>You can stop here and have a reasonably well-structured program. The specifics of what each constant means are stored with the constant, and it&#8217;s now impossible to pass an illegal value into <code>setIntensity</code>. However, we&#8217;ve still embedded into <code>DimmerController</code> the assumption that we will only ever be setting the intensity of a three-state light &#8211; but we don&#8217;t actually use that assumption anywhere. We can refactor further to eliminate this dependency by realizing that <code>State</code>&#8216;s public interface really represents any arbitrary dimmer state callback:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">&nbsp;
<span style="color: #000000; font-weight: bold;">interface</span> State <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">enum</span> DimmerState <span style="color: #000000; font-weight: bold;">implements</span> State <span style="color: #009900;">&#123;</span>
    OFF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            disable<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> disable<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;">/* ... */</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    DIM<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            beginDimCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginDimCycle<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;">/* ... */</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    ON<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            beginFullCycle<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginFullCycle<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;">/* ... */</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> apply<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DimmerController <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntensity<span style="color: #009900;">&#40;</span>State intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        doStuffBeforeChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intensity.<span style="color: #006633;">apply</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        doStuffAfterChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Everything In Its Place</h3>
<p>On the other hand, sometimes the heavy lifting (in our case, the work of actually changing the intensity of a light) actually belongs outside the enumeration. This is true most of the time: enumerations can&#8217;t hold per-use state, and creating a new <code>State</code> instance (from the interface-based example above) for each light isn&#8217;t always appropriate. We can pull those methods back out of the enumeration by introducing a third class to act as a callback for the <code>apply</code> method:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">enum</span> State <span style="color: #009900;">&#123;</span>
    OFF<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> applyTo<span style="color: #009900;">&#40;</span>DimmerCallback dimmerCallback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dimmerCallback.<span style="color: #006633;">disable</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    DIM<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> applyTo<span style="color: #009900;">&#40;</span>DimmerCallback dimmerCallback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dimmerCallback.<span style="color: #006633;">beginDimCycle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>,
    ON<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> applyTo<span style="color: #009900;">&#40;</span>DimmerCallback dimmerCallback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            dimmerCallback.<span style="color: #006633;">beginFullCycle</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> applyTo<span style="color: #009900;">&#40;</span>DimmerCallback dimmerCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #666666; font-style: italic;">/* or interface */</span> DimmerCallback <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> disable<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginDimCycle<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">void</span> beginFullCycle<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;">/* ... */</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> DimmerController <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Or from dependency injection, or...</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> DimmerCallback dimmerCallback <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DimmerCallback<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setIntensity<span style="color: #009900;">&#40;</span>State intensity<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        doStuffBeforeChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        intensty.<span style="color: #006633;">applyTo</span><span style="color: #009900;">&#40;</span>dimmerCallback<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        doStuffAfterChangingIntensity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Ok, But I Have This Integer</h3>
<p>In most of the cases where people feel compelled to use magic ints or an enumerated type, there&#8217;s an external force at work. The most common scenario involves file formats or network protocols where an integer or short known string needs to map directly to program behaviour. With the behaviour separated out into a real type with testable features, you lose the ability to near-blindly pass the protocol value directly into your logic.</p>
<p>Remember when I said that <code>switch</code> statements are really a giant lookup table? That should give you a hint. You can still use a lookup table to convert from an external representation to an internal one; depending on how far down the chain of refactorings you&#8217;ve gone, you may even get one for free. Consider the following, assuming we still have a <code>State</code> enumeration:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> State parseRequestedState<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> state<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">ProtocolException</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> State.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#91;</span>state<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IndexOutOfBoundsException</span> ioobe<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> UnexpectedValueException<span style="color: #009900;">&#40;</span>ioobe<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
   <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Java&#8217;s enumeration support gives us the lookup table we need for free. We can do the same thing with strings, if they happen to match the enum constants&#8217; names; alternately, we can use an explicit lookup table stored in an array or a <code>HashMap</code>. There are also plenty of strategies for automatically populating a lookup table using mechanisms like classpath scanning, <a href="http://download.oracle.com/docs/cd/E17409_01/javase/6/docs/api/java/util/ServiceLoader.html"><code>ServiceLoader</code></a>, annotation processing tools, and so on.</p>
<p>There are lots of places to go from here. For example, enum-based implementations of the <a href="http://c2.com/cgi/wiki?StatePattern">state object pattern</a> begin with a very similar refactoring. In languages without strong enums, the structure outlined here can be implemented using normal classes with a fixed pool of instances, instead (Java&#8217;s enum support does exactly that, under the hood), skipping directly to the interface-based version near the end.</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2010/07/23/breaking-away-from-switch/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>God Money, I&#8217;ll do anything for you</title>
		<link>http://codex.grimoire.ca/2010/01/08/php-fread-sucks/</link>
		<comments>http://codex.grimoire.ca/2010/01/08/php-fread-sucks/#comments</comments>
		<pubDate>Fri, 08 Jan 2010 18:54:24 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Finite Hate Machine]]></category>
		<category><![CDATA[fail]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=143</guid>
		<description><![CDATA[The loop 1 2 3 4 5 &#60;?php while &#40;$read &#60; $n &#38;&#38; &#40;false !== &#40;$buf = fread&#40;$this-&#62;sock, $n - $read&#41;&#41;&#41;&#41; &#123; /* ... */ &#125; ?&#62; contains a subtle bug. Go ahead, read the relevant function documentation and see if you can spot it. PHP&#8217;s fread function directly maps the C library&#8217;s fread function. [...]]]></description>
			<content:encoded><![CDATA[<p>The loop</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$read</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$buf</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sock</span><span style="color: #339933;">,</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$read</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><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;">?&gt;</span></pre></td></tr></table></div>

<p>contains a subtle bug. Go ahead, read the <a href="http://php.net/manual/en/function.fread.php">relevant function documentation</a> and see if you can spot it.</p>
<p><span id="more-143"></span></p>
<p>PHP&#8217;s <code>fread</code> function directly maps the C library&#8217;s <code>fread</code> function. As <a href="http://www.php.net/manual/en/function.fread.php#95413">TBlue</a> explains, the C <code>fread</code> function returns <code>0</code> when reading at the end of a stream. PHP does the simplest possible thing with this and returns an empty string; to determine that the stream is at end of file, programs must check with <code>feof</code> as well.</p>
<p>Most modern languages realize that explicitly checking for errors is a fairly error-prone convention. Java&#8217;s <a href="http://java.sun.com/javase/6/docs/api/java/io/InputStream.html#read(byte[])">InputStream.read(byte[])</a> methods return -1 on EOF (and only on EOF) and raise exceptions on errors. Python&#8217;s <a href="http://docs.python.org/library/stdtypes.html#file-objects">File-like objects</a> convention supports mechanisms other than raw byte reads (primarily, iteration) that make stopping at end of file implicit, on top of only returning an empty string at end of input (as with Java, Python raises exceptions when a stream read fails). Not PHP, though! In PHP, the loop above has to look like</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$read</span> <span style="color: #339933;">&lt;</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #339933;">!</span><span style="color: #990000;">feof</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sock</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #009900;">&#40;</span><span style="color: #009900; font-weight: bold;">false</span> <span style="color: #339933;">!==</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$buf</span> <span style="color: #339933;">=</span> <span style="color: #990000;">fread</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">sock</span><span style="color: #339933;">,</span> <span style="color: #000088;">$n</span> <span style="color: #339933;">-</span> 
<span style="color: #000088;">$read</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><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;">?&gt;</span></pre></td></tr></table></div>

<p>Encountered in the wild: <a href="http://code.google.com/p/php-amqplib/issues/detail?id=12">php-amqplib bug #12</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2010/01/08/php-fread-sucks/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Singling Out to Spring</title>
		<link>http://codex.grimoire.ca/2009/03/11/singling-out-to-spring/</link>
		<comments>http://codex.grimoire.ca/2009/03/11/singling-out-to-spring/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 15:43:39 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Examples]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[spring]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=98</guid>
		<description><![CDATA[Last time, I wrote about why Singletons get a bad rap, and I wrote some high-level advice for moving away from them sanely. I want to cover a practical (if somewhat idealistic) example using Java and Spring to move a Singleton creation dependency out of a class. First, a quick terminology aside. The term &#8220;Singleton&#8221; [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://codex.grimoire.ca/2009/03/10/singled-out/">Last time</a>, I wrote about why Singletons get a bad rap, and I wrote some high-level advice for moving away from them sanely. I want to cover a practical (if somewhat idealistic) example using Java and Spring to move a Singleton creation dependency out of a class.<br />
<span id="more-98"></span><br />
First, a quick terminology aside. <a href="http://mathworld.wolfram.com/SingletonSet.html">The term &#8220;Singleton&#8221; is actually drawn from set theory</a>, where it means &#8220;a set with only one element&#8221;. The set of integers that equal 5 is a singleton. The analogy is that types (which are sets of values) that are Singletons also only have one value (the Singleton instance). This has caused a bit of confusion.</p>
<p>The Singleton pattern specifically refers to the object creational pattern where a class manages a single instance of itself and, usually, forbids other instances from existing. There&#8217;s also the &#8220;singleton&#8221; <a href="http://static.springframework.org/spring/docs/2.5.x/reference/beans.html#beans-factory-scopes">scope</a> in Spring, which means something more limited: Spring will only create one instance of the bean. Don&#8217;t confuse the arguments that Singleton classes are a bad idea for arguments that objects you only have one of are a bad idea.</p>
<p>I mention this because Spring scopes come up in the following examples, and the default Spring scope is also called <code>singleton</code>. Now, with that out of the way, on with the show.</p>
<h3>Moving a Singleton creation dependency to Spring</h3>
<p>Injecting a singleton into its clients involves replacing code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mailer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Mailer instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Mailer getInstance <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendMail <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient, <span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">printf</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;I am sending %s to %s!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>,
      message, recipient<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* Using a Struts2 Action as an example &quot;client&quot; */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SendMailAction <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setRecipient <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">recipient</span> <span style="color: #339933;">=</span> recipient<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMessage <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> execute <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mailer.<span style="color: #006633;">getInstance</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">sendMail</span> <span style="color: #009900;">&#40;</span>recipient, message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> recipient<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>with code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SendMailAction <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">public</span> SendMailAction <span style="color: #009900;">&#40;</span>Mailer mailer<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">mailer</span> <span style="color: #339933;">=</span> mailer<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setRecipient <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">recipient</span> <span style="color: #339933;">=</span> recipient<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setMessage <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">message</span> <span style="color: #339933;">=</span> message<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> execute <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    mailer.<span style="color: #006633;">sendMail</span> <span style="color: #009900;">&#40;</span>recipient, message<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Mailer mailer<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> message<span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #003399;">String</span> recipient<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The corresponding Spring configuration file looks like</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mailer&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.grimoire.examples.Mailer&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;getInstance&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;action&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">scope</span>=<span style="color: #ff0000;">&quot;request&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.grimoire.examples.SendMailAction&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;constructor-arg</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;mailer&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- more definitions --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>If getInstance, for some reason, needs parameters, they can be passed as <code><constructor-arg></code> elements nested inside its bean definition.</p>
<p>You can now write isolated tests for <code>SendMailAction</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Using JMock rather than a hand-crafted alternate implementation.</span>
@RunWith<span style="color: #009900;">&#40;</span>JMock.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SendMailTest <span style="color: #009900;">&#123;</span>
  <span style="color: #666666; font-style: italic;">// This requires some extension to work with class mocks</span>
  <span style="color: #666666; font-style: italic;">// See http://www.jmock.org/mocking-classes.html</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> Mockery mockery <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> JUnit4Mockery<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span>
    setImposteriser<span style="color: #009900;">&#40;</span>ClassImposteriser.<span style="color: #006633;">INSTANCE</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
  @Test
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendsExpectedMessage <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> Mailer mailer <span style="color: #339933;">=</span> mockery.<span style="color: #006633;">mock</span> <span style="color: #009900;">&#40;</span>Mailer.<span style="color: #000000; font-weight: bold;">class</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    mockery.<span style="color: #006633;">checking</span> <span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> Expectations <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#123;</span>
      oneOf <span style="color: #009900;">&#40;</span>mailer<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">sendMail</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Recipient&quot;</span>, <span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    SendMailAction action <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SendMailAction <span style="color: #009900;">&#40;</span>mailer<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    action.<span style="color: #006633;">setRecipient</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Recipient&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    action.<span style="color: #006633;">setMessage</span> <span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Hello World&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    action.<span style="color: #006633;">execute</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Prior to this change, writing a test for SendMailAction that did not also involve Mailer would&#8217;ve been difficult and error-prone.</p>
<h3>Injecting dependencies into Singletons</h3>
<p>You can also inject dependencies, including other Singleton instances, into a Singleton instance, which is helpful for breaking Singleton graphs. Let&#8217;s give <code>Mailer</code> another Singleton it depends on:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SmtpConnection <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> SmtpConnection instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SmtpConnection <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> SmtpConnection getInstance <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #003399;">PrintStream</span> getSmtpStream <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mailer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Mailer instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Mailer getInstance <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendMail <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient, <span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    SmtpConnection connection <span style="color: #339933;">=</span> SmtpConnection.<span style="color: #006633;">getInstance</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    connection.<span style="color: #006633;">getSmtpStream</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">printf</span> <span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">&quot;I am sending %s to %s!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, message, recipient<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>You can move this dependency into Spring if you change Mailer:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mailer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Mailer instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Mailer getInstance <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setConnection <span style="color: #009900;">&#40;</span>SmtpConnection connection<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">connection</span> <span style="color: #339933;">=</span> connection<span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendMail <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient, <span style="color: #003399;">String</span> message<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    connection.<span style="color: #006633;">getSmtpStream</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">printf</span> <span style="color: #009900;">&#40;</span>
      <span style="color: #0000ff;">&quot;I am sending %s to %s!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span>, message, recipient<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">private</span> SmtpConnection connection<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The corresponding Spring configuration looks like:</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;beans</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://www.springframework.org/schema/beans&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;connection&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.grimoire.examples.SmtpConnection&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;getInstance&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
&nbsp;
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;bean</span> <span style="color: #000066;">id</span>=<span style="color: #ff0000;">&quot;mailer&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;ca.grimoire.examples.Mailer&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">factory-method</span>=<span style="color: #ff0000;">&quot;getInstance&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;property</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;connection&quot;</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;connection&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/bean<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
&nbsp;
  <span style="color: #808080; font-style: italic;">&lt;!-- more definitions --&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/beans<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Even clients who aren&#8217;t using Spring to inject the <code>Mailer</code> Singleton can be used with this, so long as the Spring injection happens before anything tries to use the <code>Mailer</code>.</p>
<p>Without an interface limiting the operations, though, clients of <code>Mailer</code> may decide to call <code>setConnection</code> &#8211; <em>don&#8217;t do this</em> because it becomes a form of mutable global state. If you&#8217;re going to follow this route, either force clients to use a narrower interface without the setters or be very careful about code review.</p>
<address>Edited at Mar 11, 2009 @ 10:43: <a href="http://dashi.ca/blog/">Taavi</a> pointed out that my SmtpConnection Spring example was still calling <code>getInstance</code>. Whoops!</address>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2009/03/11/singling-out-to-spring/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Singled Out</title>
		<link>http://codex.grimoire.ca/2009/03/10/singled-out/</link>
		<comments>http://codex.grimoire.ca/2009/03/10/singled-out/#comments</comments>
		<pubDate>Wed, 11 Mar 2009 02:24:05 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Manifestos and Rants]]></category>
		<category><![CDATA[architecture]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[patterns]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=78</guid>
		<description><![CDATA[One of my colleagues blindsided me the other day with a question about software architecture that I should&#8217;ve been able to answer on the spot: &#8220;Are singletons really all that bad?&#8221; The Singleton pattern is one of the common solutions identified in Design Patterns. At its core, a Singleton is a class that only allows [...]]]></description>
			<content:encoded><![CDATA[<p>One of my colleagues blindsided me the other day with a question about software architecture that I should&#8217;ve been able to answer on the spot: &#8220;Are singletons really all that bad?&#8221;<br />
<span id="more-78"></span><br />
<!--<br />
* What's a singleton?<br />
* What are the consequences?<br />
 * Only one instance of the class exists.<br />
 * That instance can be referenced from anywhere.<br />
--></p>
<p>The Singleton pattern is one of the common solutions identified in <a href="http://www.amazon.com/Design-Patterns-Object-Oriented-Addison-Wesley-Professional/dp/0201633612">Design Patterns</a>. At its core, a <a href="http://c2.com/cgi/wiki?SingletonPattern">Singleton</a> is a class that only allows one instance. All clients of the class will operate against the same instance. The implementation outlined in Design Patterns provides a class-level function replacing the class&#8217; constructor in its public API which manages the existence of the singleton instance.</p>
<p>A fairly stock Java Singleton implementation for, for example, sending mail runs like this:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Mailer <span style="color: #009900;">&#123;</span>
  <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> Mailer instance <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> Mailer getInstance <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #000000; font-weight: bold;">return</span> instance<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #666666; font-style: italic;">/* Note the access specifier: no other class can access this
     constructor. */</span>
  <span style="color: #000000; font-weight: bold;">private</span> Mailer <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;">/* ... */</span>
  <span style="color: #009900;">&#125;</span>
&nbsp;
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> sendMail <span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> recipient, <span style="color: #003399;">String</span> message<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><!--<br />
* What are the costs of only having a single instance? (Manageable.)<br />
 * Any clients of the singleton must not modify its state (singletons that call other services that are being stubbed in).<br />
 * Impact on garbage collection?<br />
 * Impact on synchronization?<br />
 * "Workaround" methods for creating multiple instances may invalidate singleton assumptions.<br />
  * Examples (Logger managers like log4j, global transaction management)<br />
  * Multiple singletons come up naturally in containerized applications, where multiple classloaders or appdomains are part of deployment.<br />
 * Ultimately not a big deal.<br />
 * Can be the Right Thing<br />
  * Accurately models the problem (Runtime in Java)<br />
  * Appropriate client interface (Logger in Log4j)<br />
--></p>
<p>From an implementor&#8217;s point of view, Singleton classes cost almost nothing, either in terms of extra code to maintain or in terms of assumptions to test. In the average application, each class providing some service to the application only has one instance, so constraining the class to <em>only</em> one instance isn&#8217;t a huge leap. These kinds of classes tend to be stateless, or almost stateless, so clients don&#8217;t necessarily need to be aware that they&#8217;re sharing instances with other clients as there are no extra synchronization or garbage collection hazards to handle. There are even times when Singletons are the right thing: either they accurately model some aspect of the problem where only a single representative can exist (for example, Java&#8217;s <code>Runtime</code> class, which exposes services provided by the JVM running the program), or the resulting API is compellingly simple and easy to use (for example, Log4J&#8217;s <code>Logger</code>, which is effectively backed by a Singleton registry of Logger instances).</p>
<p>Implementing a Singleton correctly can easily depend on the dark corners of the language&#8217;s object model. The most common source of Singleton implementation bugs are caused by language or deployment models that scope &#8220;global&#8221; state to only part of the system. Networked systems can easily have an instance on every node, and applications that have multiple <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.aspx">AppDomains</a> or <a href="http://java.sun.com/javase/6/docs/api/java/lang/ClassLoader.html">ClassLoaders</a> can easily create multiple Singleton instances in a single address space. In multithreaded apps (that is, almost anything written this decade), Singletons can also be threading hazards: if they manage access to an external resource, then it&#8217;s very easy to build a synchronization bottleneck into your entire application in the name of keeping access to that resource thread-safe.</p>
<p><!--<br />
* What are the costs of calling getInstance everywhere? (Huge.)<br />
 * Difficult or impossible to replace the singleton with a stub (eg., for testing).<br />
 * No "traction" for applying behaviour-modifying patterns (decorator, proxy, AOP).<br />
 * Embeds an assumption that every other client of the (implicit?) interface is using the same implementation.<br />
 * Easy to accidentally create huge meshes of singletons in a single call if singletons depend on each other.<br />
 * Comes up with any inline creation technique.<br />
  * 'new'ing dependencies is bad!<br />
  * Use dependency injection techniques to factor out creation.<br />
--></p>
<p>Singletons cause most of the problems associated with them on the consumer side. The most common implementation pattern for Singleton clients is very simple: every occurrence of</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Mailer m <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Mailer <span style="color: #009900;">&#40;</span><span style="color: #666666; font-style: italic;">/* ... */</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>gets replaced with</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Mailer m <span style="color: #339933;">=</span> Mailer.<span style="color: #006633;">getInstance</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This wins <em>nothing</em>: the client is tightly bound to a number of assumptions about the identity and lifecycle of the provider. In fact, it loses a little: instead of giving each client its own isolated state, now there&#8217;s a guarantee that some state is shared between client instances, via the Singleton instance. There are also knock-on effects: because the client controls exactly which provider it uses, it&#8217;s difficult to isolate only the client class for testing. It also becomes difficult, if not impossible, to insert behaviour between the client and the singleton provider &#8211; no <a href="http://www.onjava.com/pub/a/onjava/2004/01/14/aop.html">AOP</a>, no <a href="http://developer.apple.com/DOCUMENTATION/Cocoa/Reference/Foundation/Classes/NSProxy_Class/index.html">proxies</a>, and no <a ref="http://pyro.sourceforge.net/">remoting</a>, without making invasive changes to the client or the singleton.</p>
<p>Over the long term, as software evolves, patterns tend to propagate. Inline <code>getInstance()</code> calls for Singleton clients tend to get repeated in new code. In a sufficiently evolved codebase that uses Singletons extensively, a badly-placed <code>getInstance()</code> call can easily cause half the objects in the system to spring into being and initialize themselves. Even when this doesn&#8217;t cause performance issues on some code paths, it can easily cause startup and initialization order to become unpredictable during apparently-safe changes. Remembering which code paths are &#8220;supposed&#8221; to initialize singletons becomes an extra form of friction for developers working on the codebase.</p>
<p><!--<br />
* Why do developers create singletons?<br />
 * Developers create singletons because they're convenient.<br />
  * Compare implementing an injected dependency (Java + Spring) to a singleton, with code samples.<br />
 * Incorrect extrapolation from only needing a single instance to only allowing a single instance.<br />
--></p>
<p>Singletons aren&#8217;t popular by accident, though. In most modern object-oriented systems, there is a constellation of core classes which, in the final system, only need to exist once. The <code>Mailer</code> example I&#8217;ve been using is actually a good example: if it manages connections to the underlying mail system itself in a sane way, then there probably only needs to be a single instance of <code>Mailer</code> in the deployed app. If <code>Mailer</code> handles any connection pooling, then ensuring the creation of new instances is carefully controlled becomes almost mandatory. Singletons provide a really simple way to provide a single instance to an application.</p>
<p>Most development time, thought, and effort goes into the code paths that get used in the final product, so there&#8217;s an easy conceptual leap to make from &#8220;the system I&#8217;m building only needs one instance&#8221; to &#8220;this object should only allow one instance.&#8221; Developers without fairly extensive experience with <a href="http://www.mockobjects.com/">mocking</a> and other unit-testing techniques, or who don&#8217;t value the ability to insert new behaviour without changing existing code, can easily see no problem with using Singletons to manage lifetime <em>and to provide access to the instance</em>, since it&#8217;s <a href="http://www.jwz.org/doc/worse-is-better.html">convenient</a>.</p>
<p>There are alternatives to Singletons for managing service objects: the standard Java examples are <a href="http://java.sun.com/products/ejb/">EJBs</a>, which delegates control over object lifecycles to the app container, and <a href="http://www.springsource.org/about">Spring</a>, which <a href="http://martinfowler.com/articles/injection.html">takes responsibility for lifecycle management and wiring between providers and consumers</a> within the app. Other languages have similar tools available, either in the language or as a library. However, these tools have a higher barrier to entry than &#8220;just another Singleton&#8221;: even the simplest inversion of control implementation is more lines of code than a Singleton.</p>
<p>The kinds of problems Singletons cause only really become problems in medium-sized and larger codebases. Not coincidentally, medium-sized and larger codebases tend to be the product of long evolution from a small, relatively clean initial product, which means there&#8217;s been lots of time for people to forget the program&#8217;s overall structure. Refactoring to clean up the problems isn&#8217;t difficult, but surprisingly little has been written about it.</p>
<p><!--<br />
* What do we fix first?<br />
 * Context of a large (500,000 LOC) system with lots of singletons.<br />
 * Moving creation of single objects out of singletons is low-reward, high-risk.<br />
  * Touches the singleton class and every client at once.<br />
  * Doesn't affect the flexibility or testability of the singleton much.<br />
 * Moving obtaining an instance out of clients is high-reward, low-risk.<br />
  * Can be done one client at a time.<br />
  * Dramatically improves testability and compsability/flexibility.<br />
--></p>
<p>For most Singletons, where obtaining an instance is a parameter-less call to a <code>getInstance()</code>-like method, refactoring that call out to an injected dependency via a constructor or method parameter is a low-risk change that can be made and tested for one class at a time. Most, if not all, <code>getInstance()</code> calls follow a very simple, predictable code path, which means there are no side effects to take care of, and no changes at all have to happen in the provider. The only potential &#8220;gotcha&#8221; is the first call, which may do initialization work. This kind of change also has high rewards: injected dependencies can be replaced with alternate implementations, which makes testing much easier and lets you augment or replace the implementation without altering the clients.</p>
<p>There&#8217;s more risk in changing the Singleton itself first. Refactoring the lifecycle management code out of the Singleton and into a higher layer does help, but while clients are calling <code>getInstance()</code> directly, any change to the lifecycle management means changing <em>every</em> client at the same time, which in turn means you need to re-test all of those clients. Once the clients have been refactored, though, it&#8217;s much safer and easier to change the lifecycle managment.</p>
<p>Singletons are not the <a href="http://blogs.msdn.com/scottdensmore/archive/2004/05/25/140827.aspx">Great Evil</a> they&#8217;re sometimes depicted as. However, they&#8217;re used in a lot of situations as a crutch to avoid writing real inter-object dependency management, <a href="http://steve.yegge.googlepages.com/singleton-considered-stupid">to the detriment of software using them</a>. Their convenience can outweigh the problems, especially in small systems or prototypes, but you need to have a way out when they start to cause problems.</p>
<p>Next time: implementation examples, for Spring.</p>
<address>Edited at March 11, 2009 at 12:45 am: I weeded the links a bit &#8211; it sounded too much like Jeff Atwood. If you want to know what I removed, drop a comment.<br />
Edited again at March 12, 2009 at 2:23 pm: HTML fail. Thanks, Justin!</address>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2009/03/10/singled-out/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Automatic Code Formatting and Merges</title>
		<link>http://codex.grimoire.ca/2009/01/10/automatic-code-formatting-and-merges/</link>
		<comments>http://codex.grimoire.ca/2009/01/10/automatic-code-formatting-and-merges/#comments</comments>
		<pubDate>Sat, 10 Jan 2009 20:21:14 +0000</pubDate>
		<dc:creator>Owen</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://codex.grimoire.ca/?p=75</guid>
		<description><![CDATA[&#8230;or, how to give other developers screaming nightmares in 10 seconds flat. The next time you think to reach for the &#8220;reformat source code&#8221; button in your IDE, think twice before you press it. Automatic reformatting is a great way to turn a small merge hazard contained to a single method into a huge merge [...]]]></description>
			<content:encoded><![CDATA[<p>&#8230;or, how to give other developers screaming nightmares in 10 seconds flat.</p>
<p>The next time you think to reach for the &#8220;reformat source code&#8221; button in your IDE, think twice before you press it. Automatic reformatting is a great way to turn a small merge hazard contained to a single method into a huge merge hazard affecting an entire file. Merge tools are relatively stupid: in general, they&#8217;re not aware of the syntactic structure of a source file, just the text by lines; as a result, it&#8217;s very easy to fool them into thinking a one-line change and a reformat are a conflict covering the entire file.</p>
<p>If you&#8217;re using automatic formatting tools, please check with the other developers on your project and make sure that your code formatter settings agree with theirs.  If you can&#8217;t make that happen, then don&#8217;t reformat code unless everyone&#8217;s nobody but you is touching those files.</p>
<p>Please, think of the merges.</p>
]]></content:encoded>
			<wfw:commentRss>http://codex.grimoire.ca/2009/01/10/automatic-code-formatting-and-merges/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

