<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" media="screen" href="/~d/styles/rss2full.xsl"?><?xml-stylesheet type="text/css" media="screen" href="http://feeds.feedburner.com/~d/styles/itemcontent.css"?><rss 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/" xmlns:feedburner="http://rssnamespace.org/feedburner/ext/1.0" version="2.0">

<channel>
	<title>Elegant Code</title>
	
	<link>http://elegantcode.com</link>
	<description />
	<lastBuildDate>Sat, 18 May 2013 03:09:31 +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>
		<atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="self" type="application/rss+xml" href="http://feeds.feedburner.com/ElegantCode" /><feedburner:info uri="elegantcode" /><atom10:link xmlns:atom10="http://www.w3.org/2005/Atom" rel="hub" href="http://pubsubhubbub.appspot.com/" /><item>
		<title>Author Report: Daniel Suarez</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/ELp-Ab0Uzf4/</link>
		<comments>http://elegantcode.com/2013/05/17/author-report-daniel-suarez/#comments</comments>
		<pubDate>Sat, 18 May 2013 02:55:44 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6062</guid>
		<description><![CDATA[I stumbled across the novels of Daniel Suarez a few weeks ago via an impulse spend of an Audible credit. I am now well into his latest novel and am looking forward to whatever he&#8217;s working on now. His first two novels are a two-part story: Daemon, and Freedom™. I have totally dug them, especially [...]]]></description>
				<content:encoded><![CDATA[<p>I stumbled across the novels of <a href="http://www.amazon.com/wiki/Daniel_Suarez/ref=ntt_at_bio_wiki">Daniel Suarez</a> a few weeks ago via an impulse spend of an Audible credit. I am now well into his latest novel and am looking forward to whatever he&#8217;s working on now.</p>
<p><a href="http://www.amazon.com/Daemon-Daniel-Suarez/dp/0451228731/ref=wp_h_bl"><img title="image" style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; float: left; padding-top: 0px; padding-left: 0px; margin: 0px 5px 5px; border-left: 0px; display: inline; padding-right: 0px" border="0" alt="image" align="left" src="http://elegantcode.com/wp-content/uploads/2013/05/image.png" width="138" height="240" /></a></p>
<p>His first two novels are a two-part story: <a href="http://www.amazon.com/wiki/Daemon_(technothriller_series)/ref=wp_la_11">Daemon</a>, and <a href="http://www.amazon.com/wiki/Daemon_(technothriller_series)/ref=wp_la_13">Freedom™</a>. I have totally dug them, especially the 7 layers of metaphor and earnest mods to plausibility. </p>
<p>game-theory techno-thriller cyber-punk IT rouge-developer near-sourcing commando near-dystopia AI assassin spy epic cyborg spies viruses the-good&#8211;clichés game-design mad-scientist long-running-workflow <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  anti-consumerism </p>
<p>The most interesting thing is Daniel Suarez himself. According to his website he&#8217;s a specialist consultant-developer slinging for bounty living in southern California. His bio reads as many of ours do at some point at some point. In short this dude seems to just be one of us geeks who also writes a bang-up novel.</p>
<p>I have no doubt I am late to the game on this writer, but I&#8217;m old. I&#8217;ve only just gotten around to a lot of things. If you haven&#8217;t read it yet, I recommend starting with Daemon. It&#8217;s fun and enjoyably thoughtful. </p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/ELp-Ab0Uzf4" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/05/17/author-report-daniel-suarez/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/05/17/author-report-daniel-suarez/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=author-report-daniel-suarez</feedburner:origLink></item>
		<item>
		<title>Developer humor</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/-o35UHr0pCw/</link>
		<comments>http://elegantcode.com/2013/05/09/developer-humor/#comments</comments>
		<pubDate>Thu, 09 May 2013 17:43:13 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6056</guid>
		<description><![CDATA[I literally cried this morning as I scanned these posts. Note: Don&#8217;t have a drink in hand or mouth when reading these. Enjoy: http://martinvalasek.com/blog/pictures-from-a-developers-life http://martinvalasek.com/blog/pictures-from-a-developers-life-part-2 http://martinvalasek.com/blog/pictures-from-a-developers-life-part-3 If you’re a git user. http://wheningit.tumblr.com/]]></description>
				<content:encoded><![CDATA[<p>I literally cried this morning as I scanned these posts.</p>
<blockquote><p>Note: Don&#8217;t have a drink in hand or mouth when reading these.</p></blockquote>
<p>Enjoy:</p>
<ul>
<li><a title="http://martinvalasek.com/blog/pictures-from-a-developers-life" href="http://martinvalasek.com/blog/pictures-from-a-developers-life">http://martinvalasek.com/blog/pictures-from-a-developers-life</a></li>
<li><a title="http://martinvalasek.com/blog/pictures-from-a-developers-life-part-2" href="http://martinvalasek.com/blog/pictures-from-a-developers-life-part-2">http://martinvalasek.com/blog/pictures-from-a-developers-life-part-2</a></li>
<li><a title="http://martinvalasek.com/blog/pictures-from-a-developers-life-part-3" href="http://martinvalasek.com/blog/pictures-from-a-developers-life-part-3">http://martinvalasek.com/blog/pictures-from-a-developers-life-part-3</a></li>
</ul>
<p>If you’re a git user.</p>
<ul>
<li><a title="http://wheningit.tumblr.com/" href="http://wheningit.tumblr.com/">http://wheningit.tumblr.com/</a></li>
</ul>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/-o35UHr0pCw" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/05/09/developer-humor/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/05/09/developer-humor/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=developer-humor</feedburner:origLink></item>
		<item>
		<title>Brian’s Resizer App</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/liEZasVdcr8/</link>
		<comments>http://elegantcode.com/2013/05/06/brians-resizer-app/#comments</comments>
		<pubDate>Tue, 07 May 2013 05:35:32 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6055</guid>
		<description><![CDATA[Today I was pleased to learn Brian’s Sizer App is still out there and still works, even with Windows 8. Just install it and right-click to snap a window to a specific size. This tool was indispensible to me when deving for web and I find myself needing it again these days in building mockups. [...]]]></description>
				<content:encoded><![CDATA[<p>Today I was pleased to learn <a href="http://www.brianapps.net/sizer/">Brian’s Sizer App</a> is still out there and still works, even with Windows 8. Just install it and right-click to snap a window to a specific size. This tool was indispensible to me when deving for web and I find myself needing it again these days in building mockups.</p>
<p><img style="border-top: 0px; border-right: 0px; background-image: none; border-bottom: 0px; padding-top: 0px; padding-left: 0px; border-left: 0px; padding-right: 0px" border="0" alt="Sizer Popup Menu" src="http://www.brianapps.net/sizerpopupmenu.png" /></p>
<p>I am going to send email in hopes of encouraging some new feature development <img class="wlEmoticon wlEmoticon-smile" style="border-top-style: none; border-left-style: none; border-bottom-style: none; border-right-style: none" alt="Smile" src="http://elegantcode.com/files/images/posts/34f23e584b4c_1371F/wlEmoticon-smile.png" />. </p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/liEZasVdcr8" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/05/06/brians-resizer-app/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/05/06/brians-resizer-app/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=brians-resizer-app</feedburner:origLink></item>
		<item>
		<title>Server WebSocket Clients, with Jetty</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/xuFnR3bqDus/</link>
		<comments>http://elegantcode.com/2013/05/03/server-websocket-clients-with-jetty/#comments</comments>
		<pubDate>Fri, 03 May 2013 22:56:04 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[jetty]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6054</guid>
		<description><![CDATA[Previously I discussed how to write a Server which uses a persistent WebSocket Client, using the Netty framework to make things go.&#160; Netty is configured through establishing a Channel Pipeline, which is great if you need a very flexible system for handling network input and output.&#160; For most projects however, this sort of power can [...]]]></description>
				<content:encoded><![CDATA[<p><a href="http://elegantcode.com/2013/04/29/server-websocket-clients/" target="_blank">Previously</a> I discussed how to write a Server which uses a persistent WebSocket Client, using the Netty framework to make things go.&#160; Netty is configured through establishing a Channel Pipeline, which is great if you need a very flexible system for handling network input and output.&#160; For most projects however, this sort of power can be overkill.</p>
<h4>Jetty</h4>
<p>Jetty, among other great things, contains a <a href="http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/websocket/WebSocketClient.html" target="_blank">WebSocketClient</a> implementation.&#160; Getting this up and running is very simple: you’ll need a WebSocketClientFactory, where you can configure various settings, and a WebSocketClient which also has a bunch of settings to flip around to meet your needs.&#160; Once you have those, you’ll need a class that implements <a href="http://download.eclipse.org/jetty/stable-7/apidocs/org/eclipse/jetty/websocket/WebSocket.OnTextMessage.html" target="_blank">WebSocket.OnTextMessage</a> …and you’re pretty much done at this point.&#160; </p>
<p>The code for sending and receiving messages becomes equally simple:</p>
<div style="border-bottom: silver 1px solid; text-align: left; border-left: silver 1px solid; padding-bottom: 4px; line-height: 12pt; background-color: #f4f4f4; margin: 20px 0px 10px; padding-left: 4px; width: 97.5%; padding-right: 4px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; max-height: 200px; font-size: 8pt; overflow: auto; border-top: silver 1px solid; cursor: text; border-right: silver 1px solid; padding-top: 4px" id="codeSnippetWrapper">
<div style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px" id="codeSnippet">
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">@Override</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> send(Message message) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #008000">// a message is going out!</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    String data = null;</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">try</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        data = messageToString(message);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        connection.sendMessage(data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    } <span style="color: #0000ff">catch</span> (Exception e) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        logger.error(<span style="color: #006080">&quot;Failed to send message! &quot;</span> + data, e);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">}</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"> </pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">@Override</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px"><span style="color: #0000ff">public</span> <span style="color: #0000ff">void</span> onMessage(String data) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #008000">// a message is coming in!</span></pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    logger.debug(<span style="color: #006080">&quot;message received: {}&quot;</span>, data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    <span style="color: #0000ff">try</span> {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        Message message = stringToMessage(data);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        handleMessage(message);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    } <span style="color: #0000ff">catch</span> (IOException e) {</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">        logger.error(<span style="color: #006080">&quot;Failed to parse incoming message! &quot;</span> + data, e);</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: #f4f4f4; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">    }</pre>
<p><!--CRLF--></p>
<pre style="border-bottom-style: none; text-align: left; padding-bottom: 0px; line-height: 12pt; background-color: white; margin: 0em; border-left-style: none; padding-left: 0px; width: 100%; padding-right: 0px; font-family: &#39;Courier New&#39;, courier, monospace; direction: ltr; border-top-style: none; color: black; border-right-style: none; font-size: 8pt; overflow: visible; padding-top: 0px">}</pre>
<p><!--CRLF--></div>
</div>
<p>&#160;</p>
<p>The complete code for this example can be found at</p>
<p><a href="https://github.com/trasa/WebSocketClientServer/tree/jettyclient">https://github.com/trasa/WebSocketClientServer/tree/jettyclient</a></p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/xuFnR3bqDus" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/05/03/server-websocket-clients-with-jetty/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/05/03/server-websocket-clients-with-jetty/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=server-websocket-clients-with-jetty</feedburner:origLink></item>
		<item>
		<title>Introducing node-validation</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/31MBSKPnVOk/</link>
		<comments>http://elegantcode.com/2013/05/03/introducing-node-validation/#comments</comments>
		<pubDate>Fri, 03 May 2013 20:26:10 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[Node.js]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6048</guid>
		<description><![CDATA[Some time ago I was looking for a validation library/module for use in a small Express application that I was writing at the time. I couldn’t find anything that suited my taste so I decided to write one myself just for kicks. The goal was learning how to publish a module to npm and making [...]]]></description>
				<content:encoded><![CDATA[<p align="justify">Some time ago I was looking for a validation library/module for use in a small <a href="http://elegantcode.com/2011/12/23/taking-toddler-steps-with-node-js-express/" target="_blank">Express</a> application that I was writing at the time. I couldn’t find anything that suited my taste so I decided to write one myself just for kicks. The goal was learning how to publish a module to <a href="https://npmjs.org/" target="_blank">npm</a> and making a futile attempt to contribute something back to the vibrant Node.js community. <a href="https://github.com/JanVanRyswyck/node-validation" target="_blank">node-validation</a> is a minimal but slightly opinionated validation library for Node.js. </p>
<p align="justify">Installing node-validation can be done using the canonical package manager:</p>
<blockquote><p align="justify"><font style="background-color: #ffffff"><em>$ npm install node-validation</em></font></p>
</blockquote>
<p align="justify">Validation rules must be defined in a custom validator by deriving from the base V<em><code><em>alidator</em></code>.</em></p>
<p align="justify"><em></em></p>
<pre class="csharpcode"><span class="kwrd">var</span> MyObjectValidator = <span class="kwrd">function</span>() {
    Validator.call(<span class="kwrd">this</span>);

    <span class="kwrd">this</span>.ruleFor(<span class="str">'stringProperty'</span>).isNotEmpty();
    <span class="kwrd">this</span>.ruleFor(<span class="str">'otherStringProperty'</span>).hasMaximumLength(10);

    <span class="kwrd">this</span>.ruleFor(<span class="str">'numericStringProperty'</span>).isNumber()
        .withMessage(<span class="str">'Oops, something is wrong ...'</span>);
    <span class="kwrd">this</span>.ruleFor(<span class="str">'dateStringProperty'</span>)
        .matches(/^(19|20)\d\d[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])$/);

    <span class="kwrd">this</span>.ruleFor(<span class="str">'numberProperty'</span>).isInteger();
    <span class="kwrd">this</span>.ruleFor(<span class="str">'otherNumberProperty'</span>).isMaximum(5);

    <span class="kwrd">this</span>.ruleFor(<span class="str">'exoticProperty'</span>).<span class="kwrd">is</span>(<span class="kwrd">function</span>(value) {
        <span class="kwrd">return</span> 3 === value.propertyA + value.propertyB;
    }).withMessage(<span class="str">'Either propertyA or propertyB has an incorrect value.'</span>);
};

util.inherits(MyObjectValidator, Validator);</pre>
<p>&#160;</p>
<p align="justify">After creating a validator object, an object that needs to be validated (the subject) can be passed to the <code><em>validate</em></code> method. The <code><em>validate</em></code> method returns an array of validation errors specifying a message and the name of the violating property.</p>
<p>&#160;</p>
<pre class="csharpcode"><span class="rem">//</span>
<span class="rem">// Validation subject</span>
<span class="rem">//</span>
<span class="kwrd">var</span> subject = {
    stringProperty: <span class="str">''</span>,
    otherStringProperty: <span class="str">'Some string value that is too long ...'</span>,

    numericStringProperty: <span class="str">'65.85 invalid'</span>,
    dateStringProperty: <span class="str">'2013-04-30 invalid'</span>,

    numberProperty: <span class="str">'Some invalid number'</span>,
    otherNumberProperty: 48,

    exoticProperty: {
        propertyA: 1,
        propertyB: 1
    }
};

<span class="rem">//</span>
<span class="rem">// Now it's time to validate</span>
<span class="rem">//</span>
<span class="kwrd">var</span> validator = <span class="kwrd">new</span> MyObjectValidator();
<span class="kwrd">var</span> validationErrors = validator.validate(subject);

<span class="kwrd">for</span>(<span class="kwrd">var</span> i=0; i &lt; validationErrors.length; i++) {
    console.log(<span class="str">'Property name: '</span> + validationErrors[i].propertyName 
                + <span class="str">', Message: '</span> + validationErrors[i].message);
}</pre>
<style type="text/css">
.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p>&#160;</p>
<p align="justify">There you go. Head over to <a href="https://github.com/JanVanRyswyck/node-validation" target="_blank">the GitHub repository</a> and give it a try. I’m definitely looking forward to hear your feedback. </p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/31MBSKPnVOk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/05/03/introducing-node-validation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/05/03/introducing-node-validation/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=introducing-node-validation</feedburner:origLink></item>
		<item>
		<title>Server WebSocket Clients</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/rCit9QfCbZk/</link>
		<comments>http://elegantcode.com/2013/04/29/server-websocket-clients/#comments</comments>
		<pubDate>Mon, 29 Apr 2013 18:00:00 +0000</pubDate>
		<dc:creator>Tony Rasa</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[netty]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6043</guid>
		<description><![CDATA[I&#8217;ve been working on a system involving connecting custom game servers to a larger, message based and multi-tenant infrastructure.  The custom game server I was writing requires a persistent connection to the larger system, since messages originate from all directions (from/to client, from other servers, from the infrastructure, from thin air&#8230;).  And while the code [...]]]></description>
				<content:encoded><![CDATA[<p>I&#8217;ve been working on a system involving connecting custom game servers to a larger, message based and multi-tenant infrastructure.  The custom game server I was writing requires a persistent connection to the larger system, since messages originate from all directions (from/to client, from other servers, from the infrastructure, from thin air&#8230;).  And while the code I&#8217;m working with is all in Java, it&#8217;s expected that other game servers will be written in C#, or python, or ruby, or whatever they feel like, within reason.</p>
<p>Rather than build a custom protocol on top of TCP to go between the custom game server and the rest of the system, we decided to use Web Sockets for the wire protocol and objects serialized via JSON.  There are plenty of client and server implementations for all of the major languages we&#8217;re intending to support – making the job of supporting the other dev teams much easier.  Plus, it’s trivial to create various test harness Web Socket powered clients and servers – useful for developing your custom game without having to be tied to the larger system’s environment.  Particularly since that larger system was still actively under development..</p>
<p>As I was writing the game server, I found many examples of writing a Java server which would listen for incoming clients.  I found plenty of examples for writing Web Socket clients in JavaScript, or clients in Java that did everything in &#8220;public static void main()&#8221;.  But I didn&#8217;t find much in the way of writing a Java Server that kept a persistent Web Socket client around for the life of the server.  So, attached is an example Web Socket &#8220;client&#8221; server (a server application) which can send and receive messages using netty.</p>
<p><a href="https://github.com/trasa/WebSocketClientServer">https://github.com/trasa/WebSocketClientServer</a></p>
<p>This particular example connects to a public &#8220;echo&#8221; server &#8212; which is great for testing out such ideas.</p>
<h3>Netty</h3>
<p>Netty is a client server framework for building network applications.  Netty&#8217;s power comes from being able to construct a high performance Channel Pipeline which describes the steps that network traffic goes through on its way to and from the application.  For this example, we define a pipeline that is built on HTTP and includes a Serializer/Deserializer for translating objects into json (and back), and also the WebSocketClientHandler itself for doing something with those WebSocket messages.</p>
<p>When sending a Message, we send the Message through the pipeline we&#8217;ve established. The Serializer/Deserializer (SerDe) translates the message into a byte array, wraps the bytes into a TextWebSocketFrame, and sends the WebSocketFrame on it&#8217;s way.</p>
<p>Coming back the other way, the SerDe recognizes an incoming stream of bytes as a Message and reconstitutes the java object.  The Message object is sent up the pipeline, handled by WebSocketClientHandler, which then executes that message, finding the correct handler to call, and otherwise &#8220;doing stuff&#8221; with the message received.</p>
<p>The advantage of this pipeline approach is that the actual game logic is easy to separate from the underlying communication, making the game code easier to write and much easier to test in isolation.  For example, a message handler might be defined as:</p>
<div id="codeSnippetWrapper">
<div id="codeSnippet">
<pre>@MessageHandler</pre>
<p><!--CRLF--></p>
<pre><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">void</span> handleConstructArmyRequest(ConstructArmyRequest request) {</pre>
<p><!--CRLF--></p>
<pre>    <span style="color: #008000;">// do whatever you're supposed to do here</span></pre>
<p><!--CRLF--></p>
<pre>    sendResponse(<span style="color: #0000ff;">new</span> ConstructArmyResponse(SUCCESS));</pre>
<p><!--CRLF--></p>
<pre>}</pre>
<p><!--CRLF--></p>
</div>
</div>
<p>Netty met the needs of this particular game server, performance-wise and through it&#8217;s flexibility.  Since we&#8217;re using web sockets, such a system would be easy to put together using a library or different language completely.</p>
<p>&nbsp;</p>
<p>The next step for this system is to look at replacing Netty with Jetty – which provides a servlet container and web server with web sockets support.  Typically a custom game server will need some http-based parts for monitoring (connecting to nagios, viewing and graphing statistics) and for allowing some administrative access to the inner workings of the game server.  Jetty doesn’t provide the same sort of pipeline for serializing and deserializing but the process is pretty much the same.</p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/rCit9QfCbZk" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/04/29/server-websocket-clients/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/04/29/server-websocket-clients/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=server-websocket-clients</feedburner:origLink></item>
		<item>
		<title>Estimating for Software Development, Delivery, and Deadlines</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/vzvt9LDVPdE/</link>
		<comments>http://elegantcode.com/2013/04/15/estimating-for-software-development-delivery-and-deadlines/#comments</comments>
		<pubDate>Mon, 15 Apr 2013 15:10:42 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6040</guid>
		<description><![CDATA[This was a fun talk from the 2013 ALM Summit, back in January. I didn’t even know it was online until someone tweeted it . The ideas expressed here are around the pragmatism of using estimation as a technique, but not getting lost in the weeds. In other words, admit that we need some process [...]]]></description>
				<content:encoded><![CDATA[<p>This was a fun talk from the 2013 ALM Summit, back in January. I didn’t even know it was online until someone tweeted it <img src='http://elegantcode.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . </p>
<p>The ideas expressed here are around the pragmatism of using estimation as a technique, but not getting lost in the weeds. In other words, admit that we need some process and understanding of what we will be doing, but don’t value process over software.</p>
<p>I share some techniques my team and use at Microsoft to keep things understood and on track. Hope you enjoy it.</p>
<p><strong>From the Abstract</strong></p>
<p>Software development is often fundamentally different than the analogies used to describe it. Accordingly, estimating what software will be done and by when can be an exercise in frustration. Yet, some teams seem to just &quot;get it&quot;, making accurate predictions of cost or effort. Other teams struggle with excruciatingly detailed planning meetings or with the results of not estimating at all. Explore estimation for software development from several angles including: empirical planning, time-based estimation, relative comparison, and others. Several effective and proven estimation techniques will be explored, expanding your team&#8217;s capabilities beyond hoping for the best.</p>
<p>&#160;</p>
<p> <iframe style="height: 540px; width: 600px" src="http://channel9.msdn.com/Events/ALM-Summit/ALM-Summit-3/Estimating-for-Software-Development-Delivery-and-Deadlines/player?w=600&amp;h=540" frameborder="0" scrolling="no"></iframe></p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/vzvt9LDVPdE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/04/15/estimating-for-software-development-delivery-and-deadlines/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/04/15/estimating-for-software-development-delivery-and-deadlines/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=estimating-for-software-development-delivery-and-deadlines</feedburner:origLink></item>
		<item>
		<title>My Work Kanban Support in Visual Studio 2012 Update 2</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/Ne88BFQsYwQ/</link>
		<comments>http://elegantcode.com/2013/04/07/my-work-kanban-support-in-visual-studio-2012-update-2/#comments</comments>
		<pubDate>Sun, 07 Apr 2013 17:42:08 +0000</pubDate>
		<dc:creator>David Starr</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6033</guid>
		<description><![CDATA[My team at Microsoft is responsible for the My Work feature in Visual Studio Ultimate. The My Work feature focuses a developer&#8217;s immediate work in a single, actionable view. My Work presents TFS Work Items assigned to me in a view that allows quick and easy integration with checking in code. This aims to reduce [...]]]></description>
				<content:encoded><![CDATA[<p>My team at Microsoft is responsible for the My Work feature in Visual Studio Ultimate.</p>
<p>The My Work feature focuses a developer&#8217;s immediate work in a single, actionable view. My Work presents TFS Work Items assigned to me in a view that allows quick and easy integration with checking in code. This aims to reduce the friction of associating Work Items with check-ins, a process which has an annoyingly high number of clicks without My Work.</p>
<p>In previous versions of My Work, only two types of Work Items appeared in the My Work panel. It has been generally accepted good practice to associate only Tasks or Bugs with check-ins. In a Kanban world, Task level work is often represented as a state or column on the team&#8217;s board. Instead of using Tasks, more teams will not create Task Work Items and instead will orchestrate their work using simple PBI or User Story Work Item types and move those across the board to represent state.</p>
<p>To support Kanban through the My Work feature, developers can now see more Work Item types that might be assigned to them. Generally, any Work Item type that is a:</p>
<ul>
<li>Requirement (User Stories, PBIs, etc)</li>
<li>Task</li>
<li>Bug </li>
</ul>
<p>Here is an example of how this might look using the Scrum 2.2 process template.</p>
<h2><img title="clip_image001" style="border-top: 0px; border-right: 0px; border-bottom: 0px; float: none; margin-left: auto; border-left: 0px; display: block; margin-right: auto" border="0" alt="clip_image001" src="http://elegantcode.com/wp-content/uploads/2013/04/clip_image001.png" width="417" height="464" /></h2>
<p>Any of these Work Items may be associated with pending changes so that future check-ins will associate with them.</p>
<p><img title="clip_image002" style="border-top: 0px; border-right: 0px; border-bottom: 0px; float: none; margin-left: auto; border-left: 0px; display: block; margin-right: auto" border="0" alt="clip_image002" src="http://elegantcode.com/wp-content/uploads/2013/04/clip_image002.png" width="334" height="315" /></p>
<p>There is also a difference in the workflow behavior of the feature. For Tasks associated in My Work:</p>
<h2>Checking In Tasks</h2>
<p>Given a Task is associated with In Progress Work   <br />When a Check-in occurs    <br />Then the Task&#8217;s state transitions to Resolved    <br />And the Task is associated with the Changeset     <br />And the Task will therefore no longer appear in the My Work pane</p>
<p>However, associated Work Items that are generally requirement types behave a bit differently.</p>
<h2>Checking In Bugs and Requirement Work Item Types</h2>
<p>Given a Work Item is associated with In Progress Work   <br />When a Check-in occurs     <br />Then the Work Item&#8217;s state does not transition     <br />And the Work Item is associated with the Changeset     <br />And the Work Item will still appear under &quot;In Progress Work</p>
<p>This new behavior may seem a bit odd at first. When developers resolve Tasks with each check-in, the Task model makes sense. When developers make many check-ins over time associated to a given Product Backlog Item or Requirement, the latter model makes more sense.</p>
<h2>Resolving Requirement Work Item Types in My Work</h2>
<p>When clicking “Finish” under “In Progress Work”, note the language of “if possible” in the popup that appears:</p>
<p><img title="image" style="border-top: 0px; border-right: 0px; border-bottom: 0px; float: none; margin-left: auto; border-left: 0px; display: block; margin-right: auto" border="0" alt="image" src="http://elegantcode.com/wp-content/uploads/2013/04/image.png" width="542" height="331" /> </p>
<p>&#160;</p>
<p>It turns out that for Requirement Work Item types, we cannot determine or infer the next logical workflow state. The reason for this is a long-standing design decision in how TFS WI state transition models work, and it is what it is. Given this, removing the Requirement Work Items from scope for the current developer become a wee bit more manual.</p>
<p>After checking in, the Work Items in &quot;In Progress Work&quot; may be handled in several ways:</p>
<ol>
<li>Open the Work Item and assign it to another person (WI disappears from My Work)</li>
<li>Open the Work Item and manually transition it to its “Done” or “completed” state. (WI disappears from My Work)</li>
<li>Click &quot;Finish&quot;. The WI is still assigned to the current developer, and re-appears in the &quot;Available Work Items&quot; list.</li>
<li>Commit pending changes. Regardless of whether the WI is marked “associated” or “resolved”, it still appears in My Work. </li>
</ol>
<h2>The Bottom Line </h2>
<p>To transition a Requirement Work Item Type from your My Work pane to its next logical owner or state, open it and make the changes to the Work Item as appropriate. All the features allowing associating to current and future code check-ins still work as before, but taking your name off a given Requirement Work Item must be a more deliberate choice as must moving it to a “Done” state. </p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/Ne88BFQsYwQ" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/04/07/my-work-kanban-support-in-visual-studio-2012-update-2/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/04/07/my-work-kanban-support-in-visual-studio-2012-update-2/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=my-work-kanban-support-in-visual-studio-2012-update-2</feedburner:origLink></item>
		<item>
		<title>Basic JavaScript: Prototypical Inheritance vs. Functional Inheritance</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/2H3wqZAylLA/</link>
		<comments>http://elegantcode.com/2013/03/22/basic-javascript-prototypical-inheritance-vs-functional-inheritance/#comments</comments>
		<pubDate>Fri, 22 Mar 2013 22:14:32 +0000</pubDate>
		<dc:creator>Jan Van Ryswyck</dc:creator>
				<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6027</guid>
		<description><![CDATA[Inheritance in JavaScript has been the topic of many discussions in the past and will continue to be the source of future debates and arguments. While we do value composition over inheritance, we don’t want to throw the baby out with the bathwater either. So, from time to time, we run into these cases where [...]]]></description>
				<content:encoded><![CDATA[<p align="justify">Inheritance in JavaScript has been the topic of many discussions in the past and will continue to be the source of future debates and arguments. While we do value <a href="http://en.wikipedia.org/wiki/Composition_over_inheritance" target="_blank">composition over inheritance</a>, we don’t want to throw the baby out with the bathwater either. So, from time to time, we run into these cases where we want some notion of inheritance in JavaScript. Now what? </p>
<p align="justify">As with many things in JavaScript, there is not a single straight answer. We can choose between a couple of options and many different variations of these solutions. But one thing’s for sure: we can’t have it all!</p>
<p align="justify">In this blog post I want to discuss two different styles of inheritance that I have a hard time choosing from when programming JavaScript. And as with everything in life, both styles have their own pros and cons.&#160; </p>
<h4>Prototypical inheritance</h4>
<p align="justify">In ‘classical’ programming languages, one class can directly inherit from another class. JavaScript doesn’t have this notion of classes (yet). Instead, JavaScript has prototypes which you can augment to fit your own needs. This means that having a single augmented object as the prototype for other objects, which <em>‘inherit’</em> all members of the augmented prototype object, kind of simulates a <em>pseudo-classical</em> <em>inheritance</em> pattern. Let’s talk code in order to demystify this concept.</p>
<pre class="csharpcode" style="height: 559px; width: 100%"><span class="rem">// validator.js</span>
<span class="kwrd">var</span> Validator = exports.Validator = <span class="kwrd">function</span>() {
    <span class="kwrd">this</span>._rules = [];
};

Validator.prototype.addRule = <span class="kwrd">function</span>(rule) {
    <span class="kwrd">this</span>._rules.push(rule)
};

Validator.prototype.validate = <span class="kwrd">function</span>(instance) {
    ...
};

<span class="rem">// specificValidator.js</span>
<span class="kwrd">var</span> util = require(<span class="str">'util'</span>);

<span class="kwrd">var</span> SpecificValidator = <span class="kwrd">function</span>() {
    Validator.call(<span class="kwrd">this</span>);
};

util.inherits(SpecificValidator, Validator);

SpecificValidator.prototype.filter = <span class="kwrd">function</span>(instance) {
    ...
};

<span class="rem">// client.js</span>
<span class="kwrd">var</span> validator = <span class="kwrd">new</span> SpecificValidator();

<span class="rem">// Calls function on derived object</span>
validator.filter( { ... } );        

<span class="rem">// Calls function on base object</span>
validator.validate( { ... } );        </pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p align="justify">Here we have a constructor function named <em>Validator</em> which is the base object for other <em>‘derived’</em> objects. We augment the prototype with two functions (<em>addRule</em> and <em>validate</em>). Next we define another constructor function named <em>SpecificValidator</em>. We <em>‘derive’</em> this new&#160; constructor function by calling the base constructor function and wiring the prototype by using the <a href="http://nodejs.org/api/util.html#util_util_inherits_constructor_superconstructor" target="_blank">util.inherits()</a> function from the Node.js core library.</p>
<p align="justify">We have to use the <em>new</em> keyword in order to instantiate a <em>SpecificValidator</em> object. Now we can use the functions that we added to the prototype.</p>
<h4>Functional inheritance</h4>
<p align="justify">This pattern is advocated by <a href="http://www.crockford.com/" target="_blank">Douglas Crockford</a> in his book <a href="http://elegantcode.com/2010/05/24/book-review-javascript-the-good-parts/" target="_blank">JavaScript, The Good Parts</a>. There he offers this particular style as the way to go for inheriting objects. Let’s look at an example.</p>
<pre class="csharpcode" style="height: 585px; width: 100%"><span class="rem">// validator.js</span>
module.exports = <span class="kwrd">function</span>() {
    <span class="kwrd">var</span> rules = [], my = {};

    my.addRule = <span class="kwrd">function</span>(rule) {
        rules.push(rule);
    };

    my.validate = <span class="kwrd">function</span>(instance) {
        ...
    };

    <span class="kwrd">return</span> my;
};

<span class="rem">// specificValidator.js</span>
<span class="kwrd">var</span> validator = require(<span class="str">'...'</span>).validator;

<span class="kwrd">var</span> specificValidator = <span class="kwrd">function</span>() {
    <span class="kwrd">var</span> my = validator();

    my.filter = <span class="kwrd">function</span>(instance) {
        ...
    };
    
    <span class="kwrd">return</span> my;
};

<span class="rem">// client.js</span>
<span class="kwrd">var</span> validator = specificValidator();

<span class="rem">// Calls function on derived object</span>
validator.filter( { ... } );    

<span class="rem">// Calls function on base object</span>
validator.validate( { ... } );        </pre>
<style type="text/css">
<p>.csharpcode, .csharpcode pre
{
	font-size: small;
	color: black;
	font-family: consolas, "Courier New", courier, monospace;
	background-color: #ffffff;
	/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt 
{
	background-color: #f4f4f4;
	width: 100%;
	margin: 0em;
}
.csharpcode .lnum { color: #606060; }</style>
<p align="justify">The base constructor function returns an object that is augmented with functions and is returned at the end. The derived constructor function simple calls the base constructor function and further augments the retrieved object before returning it to the calling code. Here we don’t have to use the new keyword to instantiate anything. Just calling the right constructor function gives us an object which we can use in our client code. </p>
<h4>Conclusion</h4>
<p align="justify">The most important benefit of prototypical inheritance, at least in my humble opinion, is performance. By augmenting the prototype with functions, we only create these functions once. Not matter how many times we instantiate a constructor function, the same functions get (re)used every single time. Functional inheritance on the other hand creates new functions every time a constructor function is called, which is several orders of magnitude slower compared to the prototypical inheritance pattern.</p>
<p align="justify">On the other hand, the prototypical approach doesn’t come with encapsulation. Looking at the example shown earlier, the ‘_rules’ property is publicly available to the client code and can be manipulated at will. By using a simple convention, like prefixing with an underscore, we can indicate that these private members should not be touched in order to guarantee a correct behavior. But again, nothing can be enforced. Using functional constructors, we can have private variables and functions that cannot be manipulated by the calling code.&#160;&#160;&#160;&#160; </p>
<p align="justify">There are more pros and cons, but for me, these are the most important ones to be aware of. You can see that both styles have their strengths and weaknesses. I usually tend to go with prototypical inheritance as this is the ‘JavaScript way’, but I like using the functional approach as well for those cases were I know in advance that not too many objects are created or when I don’t care about performance.</p>
<p align="justify">I would love to hear other takes on this. What particular styles do you use? When do you use them and why?</p>
<p align="justify">Until next time </p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/2H3wqZAylLA" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/03/22/basic-javascript-prototypical-inheritance-vs-functional-inheritance/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/03/22/basic-javascript-prototypical-inheritance-vs-functional-inheritance/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=basic-javascript-prototypical-inheritance-vs-functional-inheritance</feedburner:origLink></item>
		<item>
		<title>RichEditBox gives UnauthorizedAccessException (Access is denied) error when SetText called.</title>
		<link>http://feedproxy.google.com/~r/ElegantCode/~3/mAbd1V0IzvE/</link>
		<comments>http://elegantcode.com/2013/03/19/richeditbox-gives-unauthorizedaccessexception-access-is-denied-error-when-settext-called/#comments</comments>
		<pubDate>Wed, 20 Mar 2013 04:06:37 +0000</pubDate>
		<dc:creator>Jason Jarrett</dc:creator>
				<category><![CDATA[Esoterica]]></category>

		<guid isPermaLink="false">http://elegantcode.com/?p=6026</guid>
		<description><![CDATA[While working on a little WinRT app I recently spent WAY too much time trying to figure out why I was getting the following exception System.UnauthorizedAccessException was unhandled by user code &#160; HResult=-2147024891 &#160; Message=Access is denied. (Exception from HRESULT: 0&#215;80070005 (E_ACCESSDENIED)) &#160; Source=Windows.UI &#160; StackTrace: &#160;&#160;&#160;&#160;&#160;&#160; at Windows.UI.Text.ITextDocument.SetText(TextSetOptions options, String value) when all I [...]]]></description>
				<content:encoded><![CDATA[<p>While working on a little <a href="http://apps.microsoft.com/windows/en-US/app/share-data-inspector/c33bae47-43d7-4d1b-bbee-01f09850d6dc" target="_blank">WinRT app</a> I recently spent WAY too much time trying to figure out why I was getting the following exception</p>
<blockquote><p>System.UnauthorizedAccessException was unhandled by user code      <br />&#160; HResult=-2147024891       <br />&#160; Message=Access is denied. (Exception from HRESULT: 0&#215;80070005 (E_ACCESSDENIED))       <br />&#160; Source=Windows.UI       <br />&#160; StackTrace:       <br />&#160;&#160;&#160;&#160;&#160;&#160; at Windows.UI.Text.ITextDocument.SetText(TextSetOptions options, String value)</p>
</blockquote>
<p>when all I was trying to do was programmatically set the text of a RichEditBox. EX:</p>
<blockquote><p>theRichEditBox.<strong>Document.SetText</strong>(Windows.UI.Text.TextSetOptions.FormatRtf, textValue);</p>
</blockquote>
<p>After searching and searching and eventually taking a walk to cool down, I decided that I would play with the IsReadOnly flag. I have it set to “True” in the XAML because I don’t want users to edit the text. I then wondered if this was causing the problem.</p>
<p>I tweaked the code as shown below and it magically started working. Notice how I’m turning off the IsReadOnly flag off, then setting the text, and returning the read only state after the text has changed.</p>
<blockquote><p>theRichEditBox.<strong>IsReadOnly = false;</strong>       <br />theRichEditBox.<strong>Document.SetText</strong>(Windows.UI.Text.TextSetOptions.FormatRtf, textValue);       <br />theRichEditBox.<strong>IsReadOnly = true;</strong></p>
</blockquote>
<p>Dear Exception: you were not helpful. Sigh.</p>
<img src="http://feeds.feedburner.com/~r/ElegantCode/~4/mAbd1V0IzvE" height="1" width="1"/>]]></content:encoded>
			<wfw:commentRss>http://elegantcode.com/2013/03/19/richeditbox-gives-unauthorizedaccessexception-access-is-denied-error-when-settext-called/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<feedburner:origLink>http://elegantcode.com/2013/03/19/richeditbox-gives-unauthorizedaccessexception-access-is-denied-error-when-settext-called/?utm_source=rss&amp;utm_medium=rss&amp;utm_campaign=richeditbox-gives-unauthorizedaccessexception-access-is-denied-error-when-settext-called</feedburner:origLink></item>
	</channel>
</rss>
