<?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>appendTo</title>
	<atom:link href="http://appendto.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://appendto.com</link>
	<description></description>
	<lastBuildDate>Sun, 19 May 2013 01:28:34 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Watch a Recorded Virtual jQuery Training Class</title>
		<link>http://appendto.com/blog/2013/05/watch-a-recorded-jquery-virtual-training-class/</link>
		<comments>http://appendto.com/blog/2013/05/watch-a-recorded-jquery-virtual-training-class/#comments</comments>
		<pubDate>Sun, 19 May 2013 01:28:34 +0000</pubDate>
		<dc:creator>rwhitbeck</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Mobile]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[Webinar]]></category>

		<guid isPermaLink="false">http://appendto.com/?p=882</guid>
		<description><![CDATA[<p>Due to popular demand from those outside the United Kingdom region who couldn't make the times for the live training classes we will now record the classes and offer them for a limited time on our password protected server after the live training class.</p><p>The post <a href="http://appendto.com/blog/2013/05/watch-a-recorded-jquery-virtual-training-class/">Watch a Recorded Virtual jQuery Training Class</a> appeared first on <a href="http://appendto.com">appendTo</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>Due to popular demand from those outside the United Kingdom region who couldn&#8217;t make the times for the live training classes we will now record the classes and offer them for a limited time on our password protected server after the live training class.</p>

<p>To get the recorded version just pay for the class you&#8217;d like to participate in and then we&#8217;ll email everyone that is registered a link, username and password once we host the file. The class will be available on our servers to watch for one week after we send the email out.</p> 

<h2>About our Virtual jQuery Training</h2>
<p>Our next set of <a href="http://appendto.com/training/jquery-virtual-training-uk/">public virtual jQuery training classes</a> will be 21 May 2013 &#8211; 23 May 2013 from 1PM &#8211; 5PM (GMT+1 or BST).  These classes are timed so they will work well for those in the UK and Europe but anyone is welcome to attend.  We are offering three separate classes, <a href="http://appendto.com/training/jquery-virtual-training-uk/#intro">JavaScript and jQuery Foundations</a>, <a href="http://appendto.com/training/jquery-virtual-training-uk/#advanced">Advanced jQuery Techniques</a>, and <a href="http://appendto.com/training/jquery-virtual-training-uk/#mobile">jQuery Mobile</a>.  You can choose just the class that is right for you for £50 a class or you can get an All-access pass that&#8217;ll get you into all three classes for £140.</p>

<table width="575" border="0">
<tr>
<td colspan="3"><h2 style="margin:0">appendTo&#8217;s jQuery Virtual Training: UK</h2></td>
</tr>
<tr>
<td colspan="3"><strong>All-Access Pass, All 3 classes £140, <a href="http://appendto.com/training/jquery-virtual-training-uk/">Register</a></strong></td>
</tr>
<tr>
<td style=" width: 191px; text-align: left; background-color: #ffffff; vertical-align: top; padding-top: 20px;">
<img src="http://appendto.com/wp-content/uploads/2013/04/jQuery-Logo.png" alt="jQuery-Logo" width="191">
<h3>Day 1</h3>
<h2 style="color: #9c0000;margin: 10px 0;">JavaScript and jQuery Foundations</h2>
<p><strong>Tuesday, 21 May 2013<br />
1:00-5:00PM (GMT+1)<br />
COST: £50</strong></p>

</td>

<td style=" width: 191px; text-align: left; background-color: #ffffff; vertical-align: top; padding-top: 20px;">
<img src="http://appendto.com/wp-content/uploads/2013/04/jQuery-Logo.png" alt="jQuery-Logo" width="191">
<h3>Day 2</h3>
<h2 style="color: #9c0000;margin: 10px 0;">Advanced jQuery Techniques</h2>
<p><strong>Wednesday, 22 May 2013<br />
1:00-5:00PM (GMT+1)<br />
COST: £50</strong></p>
</td>

<td style=" width: 191px; text-align: left; background-color: #ffffff; vertical-align: top; padding-top: 20px;">
<img src="http://appendto.com/wp-content/uploads/2013/04/jQuery_Mobile-Logo.png" alt="jQuery_Mobile-Logo" width="191" style="margin-bottom: 5px;">
<h3>Day 3</h3>
<h2 style="color: #9c0000; margin: 10px 0;">jQuery Mobile<BR><BR></h2>
<p><strong>Thursday, 23 May 2013<br />
1:00-5:00PM (GMT+1)<br />
COST: £50</strong></p>
</td>
</tr>
<tr>
<td><a href="http://appendto.com/training/jquery-virtual-training-uk/#intro" class="button small">Learn More<em></em></a></td>
<td><a href="http://appendto.com/training/jquery-virtual-training-uk/#advanced" class="button small">Learn More<em></em></a></td>
<td><a href="http://appendto.com/training/jquery-virtual-training-uk/#mobile" class="button small">Learn More<em></em></a></td>
</tr>
</table>

<h2>An Official jQuery Training Event</h2>
<img src="http://appendto.com/wp-content/uploads/2013/05/jquery-bronze-member-badge.png" alt="jquery-bronze-member-badge" width="100" style="float: right; margin-left: 20px;"><p>appendTo is a <a href="https://jquery.org/members/">Bronze Member of the jQuery Foundation</a> and we are putting this event on as an <a href="http://events.jquery.org/">official jQuery training event</a>.  10% of all our proceeds will go to the jQuery Foundation to further development of jQuery. Use of jQuery Logo is licensed as part of our jQuery Membership.</p>

<h2 style="clear: both;">What classes would you like to see?</h2>
<p>We&#8217;d love to hear from you.  What classes in front-end web development would you like to see offered through appendTo?  Let us know in the comments below and we&#8217;ll build our next public class around what you want. </p><p>The post <a href="http://appendto.com/blog/2013/05/watch-a-recorded-jquery-virtual-training-class/">Watch a Recorded Virtual jQuery Training Class</a> appeared first on <a href="http://appendto.com">appendTo</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://appendto.com/blog/2013/05/watch-a-recorded-jquery-virtual-training-class/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Upcoming Public Virtual jQuery Trainings</title>
		<link>http://appendto.com/blog/2013/05/upcoming-public-virtual-jquery-trainings/</link>
		<comments>http://appendto.com/blog/2013/05/upcoming-public-virtual-jquery-trainings/#comments</comments>
		<pubDate>Thu, 02 May 2013 18:36:12 +0000</pubDate>
		<dc:creator>rwhitbeck</dc:creator>
				<category><![CDATA[appendTo]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[jQuery Mobile]]></category>
		<category><![CDATA[Training]]></category>
		<category><![CDATA[Training_class]]></category>

		<guid isPermaLink="false">http://appendto.com/?p=874</guid>
		<description><![CDATA[<p>appendTo is proud to announce our next set of public virtual jQuery training classes.  We are offering three separate classes, JavaScript and jQuery Foundations, Advanced jQuery Techniques, and jQuery Mobile.  You can choose just the class that is right for you for £50 a class or you can get an All-access pass that'll get you into all three classes.</p><p>The post <a href="http://appendto.com/blog/2013/05/upcoming-public-virtual-jquery-trainings/">Upcoming Public Virtual jQuery Trainings</a> appeared first on <a href="http://appendto.com">appendTo</a>.</p>]]></description>
				<content:encoded><![CDATA[<p>appendTo is proud to announce our next set of <a href="http://appendto.com/training/jquery-virtual-training-uk/">public virtual jQuery training classes</a>, 21 May 2013 &#8211; 23 May 2013 from 1PM &#8211; 5Pm (GMT+1 or BST).  These classes are timed so they will work well for those in the UK and Europe but anyone is welcome to attend.  We are offering three separate classes, <a href="http://appendto.com/training/jquery-virtual-training-uk/#intro">JavaScript and jQuery Foundations</a>, <a href="http://appendto.com/training/jquery-virtual-training-uk/#advanced">Advanced jQuery Techniques</a>, and <a href="http://appendto.com/training/jquery-virtual-training-uk/#mobile">jQuery Mobile</a>.  You can choose just the class that is right for you for £50 a class or you can get an All-access pass that&#8217;ll get you into all three classes.</p>

<table width="575" border="0">
<tr>
<td colspan="3"><h2 style="margin:0">appendTo&#8217;s jQuery Virtual Training: UK</h2></td>
</tr>
<tr>
<td colspan="3"><strong>All-Access Pass, All 3 classes £140, <a href="http://appendto.com/training/jquery-virtual-training-uk/">Register</a></strong></td>
</tr>
<tr>
<td style=" width: 191px; text-align: left; background-color: #ffffff; vertical-align: top; padding-top: 20px;">
<img src="http://appendto.com/wp-content/uploads/2013/04/jQuery-Logo.png" alt="jQuery-Logo" width="191">
<h3>Day 1</h3>
<h2 style="color: #9c0000;margin: 10px 0;">JavaScript and jQuery Foundations</h2>
<p><strong>Tuesday, 21 May 2013<br />
1:00-5:00PM (GMT+1)<br />
COST: £50</strong></p>

</td>

<td style=" width: 191px; text-align: left; background-color: #ffffff; vertical-align: top; padding-top: 20px;">
<img src="http://appendto.com/wp-content/uploads/2013/04/jQuery-Logo.png" alt="jQuery-Logo" width="191">
<h3>Day 2</h3>
<h2 style="color: #9c0000;margin: 10px 0;">Advanced jQuery Techniques</h2>
<p><strong>Wednesday, 22 May 2013<br />
1:00-5:00PM (GMT+1)<br />
COST: £50</strong></p>
</td>

<td style=" width: 191px; text-align: left; background-color: #ffffff; vertical-align: top; padding-top: 20px;">
<img src="http://appendto.com/wp-content/uploads/2013/04/jQuery_Mobile-Logo.png" alt="jQuery_Mobile-Logo" width="191" style="margin-bottom: 5px;">
<h3>Day 3</h3>
<h2 style="color: #9c0000; margin: 10px 0;">jQuery Mobile<BR><BR></h2>
<p><strong>Thursday, 23 May 2013<br />
1:00-5:00PM (GMT+1)<br />
COST: £50</strong></p>
</td>
</tr>
<tr>
<td><a href="http://appendto.com/training/jquery-virtual-training-uk/#intro" class="button small">Learn More<em></em></a></td>
<td><a href="http://appendto.com/training/jquery-virtual-training-uk/#advanced" class="button small">Learn More<em></em></a></td>
<td><a href="http://appendto.com/training/jquery-virtual-training-uk/#mobile" class="button small">Learn More<em></em></a></td>
</tr>
</table>

<h2>How does a Virtual Training class work?</h2>
<p>After you pay for a workshop we will email you a couple of hours before the event with a link to the event and a password. You follow the instructions in the email and the workshop is streamed right to you. This isn’t a prerecorded video, this will be an interactive live session with one of appendTo’s Senior Trainers. Slides will be made available to all attendees after the event for private use only.</p>

<h2>An Official jQuery Training Event</h2>
<img src="http://appendto.com/wp-content/uploads/2013/05/jquery-bronze-member-badge.png" alt="jquery-bronze-member-badge" width="100" style="float: right; margin-left: 20px;"><p>appendTo is a <a href="https://jquery.org/members/">Bronze Member of the jQuery Foundation</a> and we are putting this event on as an <a href="http://events.jquery.org/">official jQuery training event</a>.  10% of all our proceeds will go to the jQuery Foundation to further development of jQuery. Use of jQuery Logo is licensed as part of our jQuery Membership.</p>

<h2 style="clear: both;">What classes would you like to see?</h2>
<p>We&#8217;d love to hear from you.  What classes in front-end web development would you like to see offered through appendTo?  Let us know in the comments below and we&#8217;ll build our next public class around what you want. </p><p>The post <a href="http://appendto.com/blog/2013/05/upcoming-public-virtual-jquery-trainings/">Upcoming Public Virtual jQuery Trainings</a> appeared first on <a href="http://appendto.com">appendTo</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://appendto.com/blog/2013/05/upcoming-public-virtual-jquery-trainings/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Encapsulate What Varies</title>
		<link>http://appendto.com/blog/2013/04/encapsulate-what-varies/</link>
		<comments>http://appendto.com/blog/2013/04/encapsulate-what-varies/#comments</comments>
		<pubDate>Wed, 10 Apr 2013 13:41:55 +0000</pubDate>
		<dc:creator>rwhitbeck</dc:creator>
				<category><![CDATA[appendTo]]></category>
		<category><![CDATA[Articles]]></category>
		<category><![CDATA[Consulting]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://appendto.com/?p=852</guid>
		<description><![CDATA[<p>Encapsulate what varies. This principle has become my motto for development. Whenever I evaluate my own code (or the code of others), I always look for ways to use this principle to make code more expressive and maintainable.</p><p>The post <a href="http://appendto.com/blog/2013/04/encapsulate-what-varies/">Encapsulate What Varies</a> appeared first on <a href="http://appendto.com">appendTo</a>.</p>]]></description>
				<content:encoded><![CDATA[<p><img src="http://appendto.com/images/team/nickcloud-thumb.jpg" style="float: right; margin-left: 20px;width: 100px;" alt="Nicholas Cloud"><em><a href="http://appendto.com/team/nicholas-cloud/">Nicholas Cloud</a> is a Software Architect at appendTo and has a real passion for deconstructing business problems, creating abstract, reusable and flexible solutions, and creating awesome software that rocks.  Nicholas writes about what drives him when he is architecting software for clients, encapsulating code for ease of maintainability and readability.</em></p>

<p>I was once asked in an interview: <em>Which object-oriented design principle do you think is most important?</em>. The answer was an easy one. <em>Encapsulate what varies</em>. This principle has become my motto for development, and is the tag-line for <a href="http://www.nicholascloud.com/">my own blog</a>. Whenever I evaluate my own code (or the code of others), I always look for ways to use this principle to make code more expressive and maintainable. I want to write simple, terse, bite-sized code that is easily digested. When I pay attention to what varies in my code&#8211;when I start to see where things are common yet divergent&#8211;I can refactor to enable future changes while attaining further clarity in the present. The tangible artifacts of this practice tend to be:</p>

<ul>
<li><em>expressive</em> methods/functions</li>
<li><em>concrete</em> member names</li>
<li><em>more</em> modules/classes</li>
<li><em>shorter</em> modules/classes</li>
<li><em>fewer</em> mortal enemies</li>
</ul>

<p>When I return to my own code after a period away it is easier to grok and extend. And clients like that.</p>

<p>In this post, I will examine two common scenarios in which encapsulation can be used to deconstruct poor code and create better implementations. These examples, though contrived, are not exaggerated.</p>

<h3 id="branching-statements">Branching statements</h3>

<p>Branching statements often indicate variation. You have probably seen (or written) code that looks like this:</p>

<pre class="syntax javascript"><code>//assumes no particular view framework
function UserDetailsView (user) {
  this.user = user;
}

UserDetailsView.prototype.render = function () {
  if (this.user.role === 'teacher') {
    this.showClassRoster();
  } else if (this.user.role === 'student' &amp;&amp; !this.user.isGraduated) {
    if (this.user.classmates.length) {
      this.showClassmates();
    }
  } else {
    this.showWelcome();
  }
};
</code></pre>

<p>Though this example uses an <code>if/else</code> statement, we can easily imagine a <code>switch/case</code> in its place. There are several things that vary in this method:</p>

<ul>
<li>how the <strong>view</strong> determines which DOM elements it must render</li>
<li>what the <strong>view</strong> actually does when it renders</li>
</ul>

<p>Notice that it is the view that varies, not the user, though the user is tested to determine which path execution must travel. It is entirely possible (and likely) that we will see these kinds of branching statements elsewhere in our view:</p>

<pre class="syntax javascript"><code>UserDetailsView.prototype.onContactInfoFormSubmitted = function (e) {
  var validationMethod = this.defaultValidationMethod;
  if (this.user.role === 'teacher') {
    validationMethod = this.teacherValidationMethod;
  }
  if (!validationMethod()) {
    this.showValidationError();
    e.stopPropagation();
    e.preventDefault();
  }
};
</code></pre>

<p>As more roles are added, these methods will be expanded to account for diverging scenarios, and the code will become complex and unmaintainable. At any given time, the state of this view <em>should</em> adapt to one&#8211;and only one&#8211;user role, but bugs will be introduced and the view state will become corrupted. Suddenly behavior intended for students will mysteriously execute for teachers, and we will find ourselves spending hours debugging to figure out why.</p>

<p>The solution is simple: encapsulate what varies. And what varies is the view itself, with respect to user roles. There are three states we need to account for: a default state, a teacher state, and a student state. We can see that behavior diverges when rendering, and during validation.</p>

<pre class="syntax javascript"><code>// DEFAULT VIEW
function UserDetailsView (user) {
  this.user = user;
}
UserDetailsView.prototype.showWelcome = function () {/*...*/};
UserDetailsView.prototype.showValidationError = function () {/*...*/};
UserDetailsView.prototype.render = function () {
  this.showWelcome();
};
UserDetailsView.prototype.onContactInfoFormSubmitted = function (e) {
  if (!this.validate()) {
    this.showValidationError();
    e.stopPropagation();
    e.preventDefault();
  }
};

// STUDENT VIEW
function StudentDetailsView(user) {
  UserDetailsView.call(this, user);
}
StudentDetailsView.prototype = new UserDetailsView();
StudentDetailsView.prototype.render = function () {
  if (this.user.classmates.length) {
    this.showClassmates();
  }
};

// TEACHER VIEW
function TeacherDetailsView(user) {
  UserDetailsView.call(this, user);
}
TeacherDetailsView.prototype = new UserDetailsView();
TeacherDetailsView.prototype.validate = function () {
  //teacher-specific validation
};
TeacherDetailsView.prototype.render = function () {
  this.showClassRoster();
};
</code></pre>

<p>Each view now manages its own role-based state, and all methods are more expressive and more terse than our original design. The problem of instantiation still remains. It may be solved any number of ways, but with encapsulation in mind, we will allow each view to determine if it should be rendered or not. We will do this by:</p>

<ol>
<li>Adding a <code>handles</code> method to the constructor of each view. It returns a boolean value after evaluating its arguments to determine if the view should be instantiated.</li>
<li>Registering all views with a &#8220;static&#8221; collection on the <code>UserDetailsView</code> constructor via a <code>register</code> method.</li>
<li>Instantiating a view by calling <code>UserDetailsView.create()</code> instead of newing up one directly. This method acts as a simple factory method. Its arguments will be passed to each registered constructor&#8217;s <code>handle</code> method, and the first view that can handle them will be instantiated.</li>
</ol>

<pre class="syntax javascript"><code>function UserDetailsView (user) {/*...*/}
/*
 * Constructors can be registered with the base
 * constructor
 */
UserDetailsView.ctors = [];
UserDetailsView.register = function (ctor) {
  UserDetailsView.ctors.push(ctor);
};
/*
 * The `create` method will be called in lieu
 * of direct view instantiation.
 * Assume underscore.js
 */
UserDetailsView.create = function (/*arguments*/) {
  var args = arguments;

  // find the ctor that can handle arguments
  var ctor = _.first(UserDetailsView.ctors, function (ctor) {
    return ctor.handles.apply(null, args);
  });

  // if no ctor handles arguments, assume UserDetailsView
  ctor = ctor || UserDetailsView;

  // factory function can pass all args to any view ctor by
  // using `Function.apply`; this allows each view to have
  // any constructor parameters that it wishes
  function Factory() {
    return ctor.apply(this, args);
  }
  Factory.prototype = ctor.prototype;
  var instance = new Factory();
  ctor.prototype.constructor.apply(instance, args);
  return instance;
};

/*
 * Set up `handles` method for teacher view
 * and register it with the base constructor
 */
function TeacherDetailsView(user) {/*...*/}
TeacherDetailsView.handles = function (user) {
  return !!user &amp;&amp;
    user.role === 'teacher';
};
UserDetailsView.register(TeacherDetailsView);

/*
 * Set up `handles` method for student view
 * and register it with the base constructor
 */
function StudentDetailsView(user) {/*...*/}
StudentDetailsView.handles = function (user) {
  return !!user &amp;&amp;
    user.role === 'student' &amp;&amp;
    !user.isGraduated;
};
UserDetailsView.register(StudentDetailsView);

/*
 * Get a view instance from the factory method
 */
var user = {
  role: 'teacher',
  name: 'Obi Wan Kenobi'
};

var view = UserDetailsView.create(user);
// (view instanceof UserDetailsView) === true
// (view instanceof TeacherDetailsView) === true
</code></pre>

<p>This becomes even more practical when using a loader like require.js. Sub-views can be loaded and registered with the generic, default view. Other modules only need to require the default view to gain access to its factory method and all views it subsumes.</p>

<pre class="syntax javascript"><code>define([
  'underscore',
  'views/StudentDetailView',
  'views/TeacherDetailView'
], function (_) {

  function UserDetailView(user) {/*...*/}
  UserDetailView.register = function (ctor) {/*...*/};

  var ctors = Array.prototype.slice.call(arguments, 1);

  _.each(ctors, function (ctor) {
    UserDetailView.register(ctor);
  });

  return UserDetailView;
});

// elsewhere...

define([
  'views/UserDetailView'
], function (UserDetailView) {
  var user = {
    role: 'student',
    name: 'Anikan "Cranky Pants" Skywalker'
  };

  var view = UserDetailsView.create(user);
  view.render();
});
</code></pre>

<p>Each view is now responsible for its own state, and for rendering only the DOM elements with which it should be concerned. Errors are less likely to be introduced because each view does a limited number of well understood operations, and state manipulation is uncomplicated. Code that interacts with these views, via the factory method, will not care that the views are actually different implementations, only that they share a uniform interface and may be used in identical ways.</p>

<h3 id="external-state-checks">External state checks</h3>

<p>Encapsulation can also be a powerful tool for eliminating external state checks (checking an object&#8217;s state from &#8220;the outside&#8221;). I see this kind of code often:</p>

<pre class="syntax javascript"><code>var library = {
  checkoutBook: function (customer, book) {
    if (customer &amp;&amp;
      customer.fine &lt;= 0.0 &amp;&amp;
      customer.card &amp;&amp;
      customer.card.expiration === null &amp;&amp;
      book &amp;&amp;
      !book.isCheckedOut &amp;&amp;
      (!book.reserveDate || book.reserveDate.getTime() &gt; (new Date()).getTime())) {
      customer.books.push(book);
      book.isCheckedOut = true;
    }
    return customer;
  }
};
</code></pre>

<p>This code has several problems:</p>

<ol>
<li>You have to read, and understand, every condition in this function to know what it actually does.</li>
<li>The criteria for determining if a customer can check out a book, and if a book can itself be checked out, is external to those objects. This means that this code must be duplicated if this check needs to be performed in another location.</li>
<li>New team members, ignorant that it already exists in the code base, might try to re-implement this algorithm elsewhere. They will likely get it wrong because they are unaware of every condition involved (unless this has been studiously documented somewhere, which is a laughable).</li>
</ol>

<p>The variations in this method are, of course, the conditions that need to be checked. We can eliminate the ambiguity by reducing this method to a few lines and encapsulating the conditions into their respective objects:</p>

<pre class="syntax javascript"><code>var library = {
  checkoutBook: function (customer, book) {
    if (customer.canCheckoutBook() &amp;&amp; book.isAvailable()) {
      customer.checkout(book);
    }
    return customer;
  }
};

var customer = {
  canCheckoutBook: function () {
    return !this.hasFine() &amp;&amp;
      this.hasActiveLibraryCard();
  },
  hasFine: function () {
    return this.fine &gt; 0.0;
  },
  hasActiveLibraryCard: function () {
    return this.card !== null &amp;&amp;
      this.card.expiration === null;
  },
  checkout: function (book) {
    //implementation
  }
};

var book = {
  isAvailable: function () {
    return !this.isCheckedOut &amp;&amp;
      !this.isReserved();
  },
  isReserved: function () {
    return this.reserveDate !== null &amp;&amp;
      !this.isFutureReserve();
  },
  isFutureReserve: function () {
    return this.reserveDate.getTime() &gt; 
      (new Date()).getTime();
  }
};
</code></pre>

<p>And, of course, when checkout conditions change&#8211;as we know they will&#8211;we can update our code in <code>customer</code> and <code>book</code> without touching <code>library</code>, or any other module or object that depends on these entities. Since no other code makes external state checks, our objects are well encapsulated.</p>

<h3 id="conclusion">Conclusion</h3>

<p>In the real world, every person has different tastes, preferences, goals and values. People who live in close communities tend to share a homogeneous culture, and so their values align more often than not. As society grows and new members are introduced, the likelihood of cultural divergence grows. Values begin to differ. People make different choices about how to use their time, energy, resources, labor. </p>

<p>Direct exchange of goods and services becomes difficult because of specialization, and so a common exchange medium is needed for society to scale. This medium&#8211;this tool of encapsulation&#8211;is currency. When I hand a few dollars to a barista for my coffee, I avoid the necessity of exchanging my particular skills or or time for hers. Indeed, I would most likely be long in the throes of caffeine withdrawal before we could agree on an exchange rate for &#8220;hours of programming&#8221; to &#8220;cups of coffee&#8221;. Instead we use currency to encapsulate our labor (I want to spend my time programming; the barista, crafting fine coffee). That the person in line behind me writes books, and the person behind her fixes automobiles, and the person behind him runs a non-profit is irrelevant; they will all use the same currency to buy coffee. The currency <em>encapsulates what varies</em>.</p>

<p>Our code is not unlike the real world. As complexity increases, as our code base grows, things will vary greatly as new business cases are introduced and old ones are refined. The reason we encapsulate code is so that we can isolate those differences behind common, unambiguous protocols, though the implementation of each variation may in fact be complicated.</p>

<p>Now go forth and encapsulate what varies.</p><p>The post <a href="http://appendto.com/blog/2013/04/encapsulate-what-varies/">Encapsulate What Varies</a> appeared first on <a href="http://appendto.com">appendTo</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://appendto.com/blog/2013/04/encapsulate-what-varies/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
