<?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>500null &#187; Abstraction</title>
	<atom:link href="http://www.500null.com/tag/abstraction/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.500null.com</link>
	<description>thine stack overfloweth</description>
	<lastBuildDate>Mon, 01 Mar 2010 21:00:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Object Property Maps for Higher Levels of&#160;Abstraction</title>
		<link>http://www.500null.com/development/using-object-property-maps-for-higher-levels-of-abstraction/</link>
		<comments>http://www.500null.com/development/using-object-property-maps-for-higher-levels-of-abstraction/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 21:32:26 +0000</pubDate>
		<dc:creator>Adam Detrick</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Abstraction]]></category>
		<category><![CDATA[Javascript]]></category>
		<category><![CDATA[Reuse]]></category>

		<guid isPermaLink="false">http://www.500null.com/?p=44</guid>
		<description><![CDATA[&#8220;Object property maps&#8221; are nothing more than normal Javascript objects, but they really have a lot to offer when it comes to writing nicely abstracted, reusable, and maintainable code. They can be used to separate conditions and configuration from the &#8220;moving parts&#8221; of a program, making it much easier to modify, reuse, or interface with [...]]]></description>
			<content:encoded><![CDATA[<p>&#8220;Object property maps&#8221; are nothing more than normal Javascript objects, but they really have a lot to offer when it comes to writing nicely abstracted, reusable, and maintainable code. They can be used to separate conditions and configuration from the &#8220;moving parts&#8221; of a program, making it much easier to modify, reuse, or interface with parts of your code later on.</p>
<p><span id="more-44"></span></p>
<p>For the purposes of this post, let&#8217;s imagine we have a product review that can receive a letter grade rating (&#8220;A&#8221;, &#8220;B&#8221;, &#8220;C&#8221;, &#8220;D&#8221;, or &#8220;F&#8221;). The designers decided to have a bit of fun with this one and mocked up the rating area so that all grades are visible and grey. The grade letter rating of the product gets highlighted in this list with a color specific to the grade. This is something CSS by itself should handle, but let&#8217;s assume that for this post that we only receive the grade rating via javascript.</p>
<p>Here&#8217;s what the markup looks like:</p>
<pre><code class="html">&lt;ul id=&quot;grade&quot;&gt;
	&lt;li id=&quot;A&quot;&gt;A&lt;/li&gt;
	&lt;li id=&quot;B&quot;&gt;B&lt;/li&gt;
	&lt;li id=&quot;C&quot;&gt;C&lt;/li&gt;
	&lt;li id=&quot;D&quot;&gt;D&lt;/li&gt;
	&lt;li id=&quot;F&quot;&gt;F&lt;/li&gt;
&lt;/ul&gt;</code></pre>
<p>One approach to writing a javascript function to handle this would be to check which grade is being highlighted, and set a &#8220;color&#8221; variable accordingly:</p>
<pre><code class="javascript">function highlightGrade(sGrade) {
    var element = document.getElementById(sGrade);
    var color;

    if (sGrade === "A") {
        color = '#009933';
    } else if (sGrade === "B") {
        color = '#009933';
    } else if (sGrade === "C") {
        color = '#ff9933';
    } else if (sGrade === "D") {
        color = '#cc0000';
    } else if (sGrade === "F") {
        color = '#cc0000';
    };

    element.style.backgroundColor = color;
}
</code></pre>
<p>The problem with the above example is that the color for each grade rating is hard coded within the function. Suppose that later on, someone decides to add an &#8220;C+&#8221; rating (or an &#8220;E&#8221;, for that matter). Better yet, imagine that the colors all change as well. You&#8217;d have to edit the original function and add/remove conditions to it as needed. While it isn&#8217;t hard to do so in this specific example, this is not a good coding habit to have &#8211; as complexity increases, functions get bloated and hard to maintain, and the possibility of reuse is thrown by the wayside. </p>
<p>This is where the idea of property maps come in &#8211; by storing all the possibilities for grade colors in an object rather than conditions in the function, we create a maintainable configuration outside the moving parts of the function:</p>
<pre><code class="javascript">/**
* "colors" is our property map
*/
var colors = {
    &quot;A&quot; : &#39;#009933&#39;,
    &quot;B&quot; : &#39;#009933&#39;,
    &quot;C&quot; : &#39;#ff9933&#39;,
    &quot;D&quot; : &#39;#cc0000&#39;,
    &quot;F&quot; : &#39;#cc0000&#39;
};

function highlightGrade(sGrade) {
    var element = document.getElementById(sGrade);

    element.style.backgroundColor = colors[sGrade];
};
</code></pre>
<h3>Further Abstraction</h3>
<p>As long as we&#8217;re considering abstraction, let&#8217;s think about the reusability of this function. We may have other elements we may want to highlight with conditional colors&#8230;</p>
<pre><code class="javascript">function highlightElementByCondition(sElementId, sCondition, oMap) {
    var element = document.getElementById(sElementId);

    element.style.backgroundColor = oMap[sCondition];
};

//calling it to highlight a letter grade (element, condition, map)
highlightElementByCondition(myId, myGradeLetter, {
    // property map
    &quot;A&quot;     : &#39;#009933&#39;,
    &quot;B&quot;     : &#39;#009933&#39;,
    &quot;C&quot;     : &#39;#ff9933&#39;,
    &quot;D&quot;     : &#39;#cc0000&#39;,
    &quot;F&quot;     : &#39;#cc0000&#39;
});
</code></pre>
<p>Of course, there are two sides to every design pattern &#8211; as always, it comes down to a judgement call. Are the conditions for a function unusually complicated? Will the conditions be likely to change? Does your function have wider applications than the specific problem you&#8217;re trying to solve? </p>
<p>If you make a habit of asking these questions while writing code, your coworkers and/or clients may thank you later.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.500null.com/development/using-object-property-maps-for-higher-levels-of-abstraction/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
