<?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>Craig Tataryn&#039;s .plan</title>
	<atom:link href="http://tataryn.net/feed/" rel="self" type="application/rss+xml" />
	<link>http://tataryn.net</link>
	<description></description>
	<lastBuildDate>Sat, 19 Nov 2011 17:45:40 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Java8 Lambda Expressions &#8211; Perhaps not as sexy as intended?</title>
		<link>http://tataryn.net/2011/11/java8-lambda-expressions-perhaps-not-as-sexy-as-intended/</link>
		<comments>http://tataryn.net/2011/11/java8-lambda-expressions-perhaps-not-as-sexy-as-intended/#comments</comments>
		<pubDate>Tue, 15 Nov 2011 15:24:22 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[functionalprogramming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java8]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://tataryn.net/?p=242</guid>
		<description><![CDATA[Edit: Updated the lambda syntax to match that which is found in Brian Goetz&#8217;s November 17th presentation at Devoxx In my defence of Java, one of the features I looked forward to in Java8 was the introduction of Lambda expressions. However, a lot of people are dissing the new feature as being &#8220;watered down&#8221;, or [...]]]></description>
			<content:encoded><![CDATA[<div style="border-style: double;border-width:thick;padding: 5px;"><strong>Edit:</strong> Updated the lambda syntax to match that which is found in <a href="http://blogs.oracle.com/briangoetz/resource/devoxx-lang-lib-vm-co-evol.pdf" target="_blank">Brian Goetz&#8217;s November 17th presentation</a> at <a href="http://www.devoxx.com/" target="_blank">Devoxx</a></div>
<p><br/><br />
In my <a title="Java is not the new COBOL" href="http://tataryn.net/2011/11/java-is-not-the-new-cobol/" target="_blank">defence of Java</a>, one of the features I looked forward to in Java8 was the introduction of <a href="http://en.wikipedia.org/wiki/Lambda_(programming)" target="_blank">Lambda expressions</a>. However, a lot of people are dissing the new feature as being &#8220;watered down&#8221;, or a &#8220;poor man&#8217;s lambda expressions&#8221;.  To understand why, it may help to see how Lambdas are used and implemented in a language like Scala and then contrast this to Java8&#8242;s <a href="http://www.jcp.org/en/jsr/proposalDetails?id=335" target="_blank">Project Lambda</a> adoption.</p>
<h2>How Lambdas work in Scala</h2>
<p>Scala gives you a nice syntax for specifying type-safe lambdas.  You can specify a Lambda type like so:</p>
<pre style="padding-left: 30px;">(paramType1, paramType2,...,paramTypeN) =&gt; returnType</pre>
<p>As an example let&#8217;s look at your classic &#8220;take a number and square it&#8221; lambda:</p>
<pre style="padding-left: 30px;"><strong>val</strong> squareIt: (Int) =&gt; Double = (x) =&gt; x * x</pre>
<p>The Lambda type (or Function type) in the example appears immediately after the colon but before the second equal sign.  The variable <code>squareIt</code> is type-compatible with a function that takes an Int and returns a Double.  If this were a <code>powerOf</code> function, we could make it&#8217;s type:<br />
<code>(Int, Int) =&gt; Double</code>.</p>
<p>The syntax Scala uses for specifying the &#8220;type&#8221; of a Lambda expression; the parameter types and return type; is very readable once you get to know it. However, I could have written the above example without the nice syntactic sugar like so:</p>
<pre style="padding-left: 30px;"> <strong>val</strong> squareIt: Function1[Int, Double] = (x) =&gt; x * x</pre>
<p>The <code>Function1</code> trait in Scala&#8217;s core library denotes a Function type that accepts exactly one parameter.  In this case, the incoming parameter type is an <code>Int</code> and the return value type is a <code>Double</code>.  For the <code>powerOf</code> example it would be of type <code>Function2[Int, Int, Double]</code>.</p>
<p>Open up a REPL and type the following to prove this concept to yourself:</p>
<pre style="padding-left: 30px;">scala&gt; val squareIt: Function1[Int, Double] = (x) =&gt; x * x
 squareIt: (Int) =&gt; Double =</pre>
<pre style="padding-left: 30px;">scala&gt; squareIt(2)
 res0: Double = 4.0</pre>
<h2>Java8 vs Scala Lambdas &#8211; Function Types vs. SAM</h2>
<p>Given Scala&#8217;s support for Function types, there is a convention Scala uses such that if an instance of a Class or Trait is being used as the subject of a function call then Scala makes sure the Class or Trait has implemented an appropriate <code>apply</code> method.</p>
<p>For instance, in Scala:</p>
<pre style="padding-left: 30px;"><strong>class</strong> SquareInt {
   <strong>def</strong> apply(x: Int) = x * x
}
.
.
<strong>val</strong> squareInt = new SquareInt
squareInt(2)</pre>
<p>On the last line above, Scala is really just calling the <code>apply</code> method on <code>SquareInt</code>.  <code>SquareInt</code> isn&#8217;t actually a proper function, but rather a Class instead.</p>
<p>Expanding on this convention, when Scala sees&#8230;</p>
<pre style="padding-left: 30px;"><strong>val</strong> squareIt: (Int) =&gt; Double = (x) =&gt; x * x</pre>
<p>&#8230; you can imagine Scala taking the following steps before compiling to byte-code:</p>
<ol>
<li>Make the type of the <code>squareIt</code> variable a <code>Function1</code></li>
<li>Create a new anonymous Class definition that extends <code>Function1</code></li>
<li>Make the <code>apply</code> method of this new class accept an <code>Int</code> as a parameter and return a <code>Double</code></li>
<li>Make the body of the <code>apply</code> method contain everything on the RHS of the equal sign</li>
<li>Instantiate a new instance of this class and assign it to the <code>squareIt</code> variable</li>
</ol>
<p>This convention really boils down to: If you use the squareIt variable like a function [i.e. sqaureIt(2)], Scala simply invokes the <code>apply</code> method of the instance.  If no appropriate apply method exists, you get red-squigglies in your IDE or compile errors.<br />
Try it for yourself:</p>
<pre style="padding-left: 30px;">scala&gt; <strong>val</strong> squareIt: (Int) =&gt; Double = (x) =&gt; x * x
squareIt: (Int) =&gt; Double = &lt;function1&gt;

scala&gt; squareIt(2)
res2: Double = 4.0

scala&gt; squareIt.<strong>apply</strong>(2)
res3: Double = 4.0</pre>
<h2>How Java8 handles Lambdas: SAMs</h2>
<p><code>EventHandler</code> has a single abstract function named <code>handle</code>. Types that follow this pattern are known as <strong>SAMs &#8211; Single Abstract Method</strong> types. In our case, our SAM&#8217;s  <code>handle</code> function accepts a parameter of type <code>Event</code>:</p>
<pre style="padding-left: 30px;"><strong>public</strong> <strong>interface</strong> EventHandler {
   <strong>public</strong> void handle(Event evt);
}</pre>
<p>Normally, in Java, I&#8217;d use Anonymous Inner Classes to pass functionality to a function which conveys events through an <code>EventHandler</code> interface, like a <code>Button</code> alerting anyone who cares to listen that it was clicked:</p>
<pre style="padding-left: 30px;">myButton.whenClicked(new EventHandler() {
   public void handle(Event evt) {
      out.println("I was clicked!");
   }
});</pre>
<p>This chunk of code, in Java8, becomes the following Lambda expression:</p>
<pre style="padding-left: 30px;">myButton.whenClicked(Event evt -&gt; out.println("I was clicked!"))</pre>
<p>You can even let Java infer the type of the <code>evt</code> parameter by leaving out the reference to <code>Event</code>, it&#8217;s smart enough to figure that out by itself.  So you can imagine Java taking the following steps:</p>
<ol>
<li>Look up the expected parameter type of the <code>whenClicked</code> method and finds it to be <code>EventHandler</code></li>
<li>Scan <code>EventHandler</code> to make sure it has one, and only one, &#8220;abstract method&#8221;.  In this case, <code>EventHandler</code> is an interface and all methods on an interface are considered abstract.  In this case it finds the <code>void handle(Event evt)</code> method, and it indeed is the only method.</li>
<li>Create an anonymous class which extends/implements <code>EventHandler</code> and:</li>
<ol>
<li>Converts everything on the LHS of the -&gt; within the lambda expression into parameters for the <code>handle</code> method</li>
<li>Make everything on the RHS of the -&gt; into the body of the <code>handle</code> method</li>
</ol>
<li>Pass the instance of the <code>EventHandler</code> to the <code>whenClicked</code> function</li>
</ol>
<p>The above list &#8220;sounds&#8221; almost exactly like what Scala does when converting its Lambda expressions into functions except for two key differences:</p>
<ol>
<li>There is no nice syntax for expressing the type of the Lambda expression</li>
<li>Java has no general invocation convention concerning Lambda expressions</li>
</ol>
<p><span style="text-decoration: underline;"><strong>Point 1</strong></span> has to do with the fact that in Scala we could declare:</p>
<pre style="padding-left: 30px;">val handler: <strong>(Event) =&gt; Unit</strong> = (evt) =&gt; println("I was clicked!")</pre>
<p>Whereas in Java we have no &#8220;function type&#8221; per say, instead we end up having to specify the <code>EventHandler</code> type explicitly.  Even though, in the context of a Lambda expression, the <code>EventHandler</code> interface really just is a specific synonym for &#8220;a function which takes an Event and has a void return type&#8221;.</p>
<p><span style="text-decoration: underline;"><strong>Point 2</strong></span> refers to the fact that there is no <span style="text-decoration: underline;">general</span> convention for invoking lambda expression in Java8 like there is in Scala. Remember, in Scala we could:</p>
<pre style="padding-left: 30px;">scala&gt; <strong>val</strong> squareIt: (Int) =&gt; Double = (x) =&gt; x * x
squareIt: (Int) =&gt; Double = &lt;function1&gt;

scala&gt; <strong><span style="color: #ff0000;">squareIt(2)</span></strong>
res2: Double = 4.0</pre>
<p>Instead the only way we could invoke the lambda expression directly in Java8 is to invoke the proper method on the type this specific lambda expression resolves to, in our case:</p>
<pre style="padding-left: 30px;">EventHandler handler = (Event evt) -&gt; out.println("I was clicked!");
handler.<strong>handle</strong>(someEvent);</pre>
<h2>Thoughts&#8230;</h2>
<p>I&#8217;m not a compiler writer, however I suspect adding a convention to an existing language syntax is probably not a trivial task.  In the case of Java, allowing classes to be called like functions would probably be tough to shoehorn in.  As well, adding a &#8220;function type declaration syntax&#8221; (re: <code>(Int) =&gt; Double</code>) might also be non-trivial.</p>
<p>The Project Lambda authors chose to allow Lambda expressions to be compatible with *any* type of SAM, they did this so Lambdas could be compatible with older APIs without rewriting them, or adding an extra compatibility layer. They didn&#8217;t want you to code your APIs any differently in Java8 than you would have in previous versions. Instead they want us to keep on passing those Anonymous Inner Classes around, and they&#8217;ll just give us a nicer syntax in which to create them.</p>
<h2>Making Lambdas more general&#8230;</h2>
<p>What will happen, no doubt, is we&#8217;ll see people follow the Scala convention by creating a library of generic Function interfaces each containing a single <code>apply</code> method.  We can do this and still be completely compatible with how Java8 makes Lambda expressions type-compatible with SAMs.</p>
<p>For instance given a library of &#8220;generic function types&#8221;:</p>
<pre style="padding-left: 30px;"><strong>public</strong> <strong>class</strong> Functions {

   <strong>public</strong> <strong>static</strong> <strong>class</strong> Function1 {
      public RT apply(P1 p1);
   }

   <strong>public</strong> <strong>static</strong> <strong>class</strong> Function2 {
      <strong>public</strong> RT apply(P1 p1, P2 p2);
   }
.
.
. 
}</pre>
<p>One might build an API like so:</p>
<pre style="padding-left: 30px;"></pre>
<pre style="padding-left: 30px;"><strong>import</strong> java.lang.function.Functions.Function1; 

 <strong>public</strong> <strong>class</strong> Button {

     <strong>private</strong> List&lt;Function1&lt;Event, Void&gt;&gt; clickHandlers = <strong>new</strong> ArrayList&lt;&gt;();

     /**
      * Add a handler to the list of handlers that are invoked 
      * when the button is clicked
      */</pre>
<pre style="padding-left: 30px;">     <strong>public</strong> <strong>void</strong> whenClicked(<strong>Function1&lt;Event, Void&gt;</strong> handler) {
         clickHandlers.add(handler);
     }

     /**
      * Internal function which is invoked by the Event system when a
      * the button was clicked
      */
     <strong>private</strong> <strong>void</strong> wasClicked() {
         Event event = /* create event */
         <strong>for</strong> (Function1&lt;Event, Void&gt; handler : clickHandlers) {
             handler.<strong>apply(event)</strong>;
         }
     }
}</pre>
<pre style="padding-left: 30px;"></pre>
<p>And then use the API with the Lambda syntax:</p>
<pre style="padding-left: 30px;">myButton.whenClicked(evt -&gt; out.println("I was clicked!"));</pre>
<h2>Not First Class but&#8230;</h2>
<p>So no, functions aren&#8217;t exactly &#8220;first class&#8221; in Java8, however the new Lambda syntax will absolutely help us make our code more readable. Silver lining found.</p>
]]></content:encoded>
			<wfw:commentRss>http://tataryn.net/2011/11/java8-lambda-expressions-perhaps-not-as-sexy-as-intended/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Java is not the new COBOL</title>
		<link>http://tataryn.net/2011/11/java-is-not-the-new-cobol/</link>
		<comments>http://tataryn.net/2011/11/java-is-not-the-new-cobol/#comments</comments>
		<pubDate>Fri, 04 Nov 2011 21:10:20 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java7]]></category>
		<category><![CDATA[java8]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://tataryn.net/?p=221</guid>
		<description><![CDATA[If you Google &#8220;Java is the new COBOL&#8221; you&#8217;ll find a glut of articles proliferating this mantra. I don&#8217;t know its origins, however I&#8217;m inclined to think it&#8217;s mostly repeated (and believed) by the Ruby community. Ruby, from a developer&#8217;s perspective is a low-friction language. A developer can just sit down at a text editor and [...]]]></description>
			<content:encoded><![CDATA[<p>If you Google &#8220;<a href="http://www.google.ca/search?q=java+is+the+new+cobol" target="_blank">Java is the new COBOL</a>&#8221; you&#8217;ll find a glut of articles proliferating this mantra. I don&#8217;t know its origins, however I&#8217;m inclined to think it&#8217;s mostly repeated (and believed) by the Ruby community. Ruby, from a developer&#8217;s perspective is a low-friction language. A developer can just sit down at a text editor and start banging out code without really thinking about such superflous things as types. Java on the other hand, well, you have to think a lot about types. Java is a statically typed language after all, and it makes the developer do all the heavy lifting.  That alone may answer your question as to why us Java devs think IDEs are so important.</p>
<p>Back when a lot of <a href="http://www.jroller.com/obie/entry/top_10_reasons_why_java" target="_blank">Java developers switched over to Ruby</a>, Java was a bit stagnant. We hadn&#8217;t really seen much happen with the language, and anyone who has written a Swing application can tell you: Anonymous inner classes make your code 5x bigger than it should be. The point being: there&#8217;s a lot of boilerplate code present in a typical Java application and this type of code has nothing to do with the application/business logic you&#8217;re trying to implement.  It&#8217;s just language cruft.</p>
<p>So, I can see why one would have been inclined to say that Java is like a modern COBOL. It had the following characteristics in common with COBOL:</p>
<ol>
<li>The language is verbose</li>
<li>The language is stagnant</li>
<li>Only big stodgy Enterprises use it</li>
</ol>
<p>Well, I&#8217;m happy to report that these are no longer the case.  Let&#8217;s address the issues at hand one-by-one shall we?</p>
<p><img class="alignright size-medium wp-image-222" title="java-trollface" src="http://tataryn.net/wp-content/uploads/2011/11/java-trollface-202x300.png" alt="" width="202" height="300" /></p>
<h2>Verbose &amp; Stagnant</h2>
<p>The cure for these problems lie within a combination of the features found in Java7 &amp; Java8.  Java7 addresses a lot of issues with verbosity, while Java8 pushes the language forward into adopting new paradigms (namely Functional Programming)</p>
<p>Java8 is really a turning point for Java, and shows that Oracle is actually serious about evolving the language while championing it&#8217;s proliferation while maintaining backward compatibility. Oracle can&#8217;t just gut the language and then post a meme to github to justify itself.  It&#8217;s a bit more mature than that.</p>
<h3>Java7 addresses verbosity</h3>
<p>As Mark Reinhold, chief architect of the Java platform said: Java7 is <em>evolutionary</em>, Java8 is <em>revolutionary</em>. With Java7 we gain some nifty features from <a href="http://wikis.sun.com/display/ProjectCoin/Home" target="_blank">Project Coin</a>, a project which  set out to determine what language features could be added to improve developer friction and language readability without being all too complicated to add. Here are some of the features I&#8217;m looking forward to:</p>
<h4>A switch statement that I&#8217;ll actually use</h4>
<p>Yes, finally my Java brethren we have a switch statement that actually works on Strings!</p>
<pre style="padding-left: 30px;"><strong>switch</strong> (lang) {
   <strong>case</strong> "Java" :
      out.println("I like frameworks!");
      <strong>break</strong>;
   <strong>case</strong> "Ruby" :
      out.println("I like Pabst Blue Ribbon!");
      <strong>break</strong>;
   <strong>case</strong> "PHP" :
      out.println("I like WordPress!");
      <strong>break</strong>;
}</pre>
<p>My God this has been a long time coming.</p>
<h4>Type Inference: Yes, I REALLY DO MEAN Map&lt;String, List&lt;String&gt;&gt;</h4>
<p>One of the reasons I like Scala is it&#8217;s ability to infer types.  This is such a pain-point in Java, when you use a highly complex parameterized type, both your carpal tunnel flares up and you&#8217;re eyeballs want to bleed.</p>
<p>So this:</p>
<pre style="padding-left: 30px;"><strong>Map</strong>&lt;String, List&lt;String&gt;&gt; peopleByDept = <strong>new</strong> <strong>HashMap</strong>&lt;String, List&lt;String&gt;&gt;();</pre>
<p>Becomes:</p>
<pre style="padding-left: 30px;"><strong>Map</strong>&lt;String, List&lt;String&gt;&gt; peopleByDept = <strong>new</strong> <strong>HashMap</strong>&lt;&gt;();</pre>
<p>They call this enhancement the &#8220;diamond syntax&#8221; as you are leaving out the types on the RHS of the expression and replacing them with a diamond &lt;&gt;.</p>
<p>I won&#8217;t go over all the nice little character-reducing features of Java7, Dustin&#8217;s <a href="http://marxsoftware.blogspot.com/2011/10/javaone-2011-opening-keynote.html" target="_blank">Inspired by Actual Events JavaOne 2011 keynote post</a> does an excellent job, there&#8217;s nothing more I could add really.</p>
<h2>Java8 Moves the Language Forward</h2>
<p>What do we get with Java8?  The two major features are:</p>
<ol>
<li>Lambdas / Closures</li>
<li>A proper module system (aka <a href="http://openjdk.java.net/projects/jigsaw/" target="_blank">Jigsaw</a>)</li>
</ol>
<p>I&#8217;ll only address Lambdas here because the module system is more an &#8220;in behind the scenes&#8221; feature. In my opening paragraph I spoke about how Anonymous Inner Classes cause so much bloat in a Java application.  Lambdas pave the way out of that mess.<br />
For example&#8230;</p>
<pre style="padding-left: 30px;">Component button = new Button("Click me!");
button.onClick(new EventHandler() {
   public onEvent(Event e) {
      out.println("I was clicked!");
   }
}</pre>
<p>We&#8217;d write, using Lambdas (look ma! no types!):</p>
<pre style="padding-left: 30px;">button.onClick(e =&gt; out.println("I was clicked!"));</pre>
<p>I like!</p>
<h2>Cool Companies use Java too!</h2>
<p>Is Java used in the Enterprise? Absolutely.  However <a href="http://www.readwriteweb.com/archives/apple_joins_openjdk_open_sources_java_for_mac_os_x.php" target="_blank">Apple</a>, <a href="https://dev.twitter.com/blog/twitter-open-source-and-jvm" target="_blank">Twitter</a>, <a href="http://blog.linkedin.com/2008/06/24/linkedin-is-99-java-but-100-mac/" target="_blank">LinkedIn</a>, <a href="http://highscalability.com/squarespace-architecture-grid-handles-hundreds-millions-requests-month" target="_blank">SquareSpace</a> are all committed to it too. These companies are the antithesis of the stodgy Enterprise.  Through JRuby, Jython, Scala and Clojure companies like these plus countless startups can both directly and indirectly use Java by means of leveraging the vast open source ecosystem written in Java.</p>
<h2>Stereotypes&#8230;</h2>
<p>Given the above facts I think you&#8217;ll agree, Java is not stagnant, it&#8217;s getting much better on the eyes and fingers and it has been adopted by cool, non-stodgy companies. Then perhaps you&#8217;ll also agree it&#8217;s wrong to say Java is the new COBOL, just as wrong as saying Ruby is the new Java.</p>
]]></content:encoded>
			<wfw:commentRss>http://tataryn.net/2011/11/java-is-not-the-new-cobol/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
		<item>
		<title>What&#8217;s in a Scala For Comprehension?</title>
		<link>http://tataryn.net/2011/10/whats-in-a-scala-for-comprehension/</link>
		<comments>http://tataryn.net/2011/10/whats-in-a-scala-for-comprehension/#comments</comments>
		<pubDate>Fri, 28 Oct 2011 20:07:52 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[functionalprogramming]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://tataryn.net/?p=171</guid>
		<description><![CDATA[When I first started learning Scala the &#8220;For Comprehension&#8221; threw me for a bit of a loop (pun intended). On the surface it just seems like your every day for-each statement, however it does much more than that.  A For Comprehension not only offers the ability to iterate over something like a List, it also [...]]]></description>
			<content:encoded><![CDATA[<p>When I first started learning Scala the &#8220;For Comprehension&#8221; threw me for a bit of a loop (pun intended). On the surface it just seems like your every day for-each statement, however it does much more than that.  A For Comprehension not only offers the ability to iterate over something like a List, it also provides filtering options and the ability to generate new Lists<a href="#footnotes"><span style="font-size: xx-small; vertical-align: top;">1</span></a>.</p>
<h2>Not just looping&#8230;</h2>
<p>For Comprehensions are able to actually &#8220;yield&#8221; results if so desired.  For example:</p>
<pre style="padding-left: 30px;"><strong>val</strong> sqlStatements = List[String]("select * from...", "selec...")
<strong>val </strong>resultSets = <strong>for</strong> (sql &lt;- sqlStatements) <strong>yield</strong> new ResultSet(sql)</pre>
<p>If you<a title="Learning Scala?  Learn the Fundamentals First" href="http://tataryn.net/2011/10/learning-scala-learn-the-fundamentals-first/" target="_blank"> read my last article</a>, you already know about the &#8220;map&#8221; function. As it turns out, Scala will &#8220;boil down&#8221; a For Comprehension containing a <code>yield</code> statement into a <code>map</code> function.  I know this because <code>scalac</code>, the Scala compiler, has a <code>-Xprint</code> option which shows you how the syntactic sugar used in every-day-Scala code &#8220;boils away&#8221;.</p>
<p>Using the command:</p>
<pre style="padding-left: 30px;">scalac -Xprint:typer &lt;filename&gt;.scala<a href="#footnotes"><span style="font-size: xx-small; vertical-align: top;">2</span></a></pre>
<p>scalac will show you that the For Comprehension in our example boils down to roughly<a href="#footnotes"><span style="font-size: xx-small; vertical-align: top;">3</span></a>:</p>
<pre style="padding-left: 30px;"><strong>val</strong> resultSets: List[ResultSet] = 
   sqlStatements.<strong>map</strong>[ResultSet, List[ResultSet]](
      (sql: String) =&gt; new ResultSet(sql)
   )</pre>
<p>As you can see, scalac did two things here:</p>
<ol>
<li>Added in all the type information you so lazily omitted</li>
<li>Converted the body of the yield statement into a function it can pass to the higher-order map function</li>
</ol>
<h2>Ah hah!</h2>
<p>It was at this point in my learning of Scala that I started to grok the power of Functional Programming.  Simple concepts like the map function can form the fundamental building blocks underlying the broader syntax which is Scala.</p>
<p>Back to our example&#8230;<br />
What about For Comprehensions that don&#8217;t yield results?</p>
<pre style="padding-left: 30px;"><strong>for</strong> (sql &lt;- sqlStatements) {
<strong> </strong>   println(sql)
}</pre>
<p>One might wonder how the above statement &#8220;boils down&#8221; to code which uses map as map returns a value and our example renders no such value in absence of a yield statement. In this case, scalac will produce code which uses the foreach function instead:</p>
<pre style="padding-left: 30px;">sqlStatements.<strong>foreach</strong>[Unit](((sql: String) =&gt; scala.this.Predef.println(sql)))</pre>
<h2>Filtering</h2>
<p>Another trick For Comprehensions can do is filtering of data.  For instance:</p>
<pre>   <strong>val</strong> empList = List(
        Employee("Craig", 1000000),
        Employee("Amir", 50001),
        Employee("John", 40000)
    )

    /* Find all employees with a salary over 50,000        */
    <strong>val</strong> layoffs = <strong>for</strong> (emp &lt;- empList
        <strong>if</strong> (emp.salary &gt; 50000)) yield emp</pre>
<p>In this case Craig and Amir wind up in the layoffs List and scalac boils down the statement to<a href="#footnotes"><span style="font-size: xx-small; vertical-align: top;">4</span>:</a></p>
<pre style="padding-left: 30px;"><strong>val</strong> layoffs = empList.<strong>withFilter</strong>(
   (emp) =&gt; emp.salary.&gt;(50000)
).<strong>map</strong>(...)</pre>
<p>What you are witnessing here is Scala unwinding the if statement in the For Comprehension and feeding the condition to a method called &#8220;withFilter&#8221;.  Essentially withFilter will produce a List which contains only the elements that have passed the condition.  It is this &#8220;filtered&#8221; List that map is then executed against.</p>
<p>But why didn&#8217;t Scala use the &#8220;filter()&#8221; method on List instead? The short answer is: several if statements within a For Comprehension (yes you can have more than one) will generate several chained calls to the filter function.  That sounds inefficient because you&#8217;ll wind up with statements being generated like so:</p>
<pre style="padding-left: 30px;">empList.<strong>filter</strong>(cond1).<strong>filter</strong>(cond2)...<strong>filter</strong>(condn).<strong>map</strong>(...)</pre>
<p>The first call to filter will iterate the entire empList, the second call will iterate the list generated by the first call to filter, the third will iterate the list generated by the second call to filter, etc&#8230;</p>
<p>Scala uses withFilter instead because it delays evaluation of the filter conditional until absolutely necessary.  The List that withFilter generates and passes on down the chain is often referred to as a &#8220;View&#8221; a &#8220;Projection&#8221; or a &#8220;Non-Strict&#8221; List.  Instead of withFilter iterating all the elements in the List it is to act on and applying the conditional to each element, it simply returns a type-compatible wrapper around the List it was passed. Anytime something tries to iterate the wrapped list, the filter is applied to each element to see if it &#8220;exists&#8221; based on the conditional that withFilter was originally passed. Very clever indeed.</p>
<p>So while a chain of <em>m</em> filter() methods executing over a list of <em>n</em> elements would execute up to <em>n<span style="font-size: xx-small; vertical-align: top;">m </span></em>times, in contrast a chain of withFilter methods executes <em>n</em> times and only at the time when you need to iterate the List; in our case when <code>map</code> is called.</p>
<h2>Conclusion</h2>
<p>To really understand why Scala is considered a Functional Programming language it&#8217;s well worth it to glimpse how Scala reasons about high level concepts in a functional manner.  Give the -Xprint:typer option a spin, just keep in mind you might have to factor out a lot of the &#8220;cruft&#8221; which is injected into your Scala code to help the compiler better reason about your code and it&#8217;s high-level syntax.</p>
<p>&nbsp;</p>
<p><span class="Apple-style-span" style="font-weight: bold;">Footnotes</span></p>
<ol>
<li>For Comprehensions not only work for Lists, but in the context of an &#8220;Intro to For Comprehensions&#8221; we won&#8217;t use its generalized form</li>
<li>For a list of phases you can use with -Xprint, type: scalac -Xshow-phases. typer is a common phase to use when you want to boil away most of the syntactic sugar</li>
<li>I say &#8220;roughly&#8221; because there is a lot of cruft which gets added to the code to help the compiler out, I&#8217;ve omitted some of it and focus more at the core concept of map.</li>
<li>Omitting the types here as they distract from the point at hand</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://tataryn.net/2011/10/whats-in-a-scala-for-comprehension/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Learning Scala?  Learn the Fundamentals First</title>
		<link>http://tataryn.net/2011/10/learning-scala-learn-the-fundamentals-first/</link>
		<comments>http://tataryn.net/2011/10/learning-scala-learn-the-fundamentals-first/#comments</comments>
		<pubDate>Thu, 20 Oct 2011 18:53:21 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[functionalprogramming]]></category>
		<category><![CDATA[javaone]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://tataryn.net/?p=111</guid>
		<description><![CDATA[A few weeks back I gave my talk at JavaOne 2011 titled &#8220;The Scala Language Tour&#8221;, if you&#8217;re at all interested you can grab the slides and examples from github. The session was very well received, my only enemy was time! Given 1 hour, how does one give 170+ people a taste of all that&#8217;s [...]]]></description>
			<content:encoded><![CDATA[<p>A few weeks back I gave my talk at JavaOne 2011 titled &#8220;The Scala Language Tour&#8221;, if you&#8217;re at all interested you can grab the <a href="https://github.com/ctataryn/ScalaLangTour" target="_blank">slides and examples</a> from github.</p>
<p>The session was very well received, my only enemy was time! Given 1 hour, how does one give 170+ people a taste of all that&#8217;s Scala without completely starving them of details? Lots and lots and lots of dry-runs of your presentation, that&#8217;s how. I must have iterated my talk a dozen or more times. I just couldn&#8217;t bring myself to trimming any more fat. The short story is, I could have used 5-10 more minutes. A crucial set of slides had to be omitted concerning the &#8220;Tuple&#8221; in Scala.</p>
<h2>Tuple?</h2>
<p>The Tuple is such a simple concept and it winds up being fundamental. The trick is recognizing their various uses throughout code.  One of the first slides I show is of this rather &#8220;toy problem&#8221;:</p>
<div id="attachment_112" class="wp-caption aligncenter" style="width: 310px"><a href="http://tataryn.net/wp-content/uploads/2011/10/tuple.006.png" target="_blank"><img class="size-medium wp-image-112 " title="Tuple in Action" src="http://tataryn.net/wp-content/uploads/2011/10/tuple.006-300x225.png" alt="" width="300" height="225" /></a><p class="wp-caption-text">Believe it or not this is a really simple example</p></div>
<p>I chose this example because it shows off almost everything you need to know in order to get started with Scala.  How so you may ask?  Well, it demonstrates the use of:</p>
<ul>
<li>Two Fundamental Syntax Rules</li>
<li>Higher-Order Functions</li>
<li>Anonymous Function Literals</li>
<li>Type Inference</li>
<li>Tuples</li>
</ul>
<h3>Fundamental Syntax Rules</h3>
<p>First of all there are some syntax rules in Scala which, if not studied early enough in your learning of the language, could really throw you for a loop.  In the example above, two such rules are at play (three if you count the creation of a Tuple using only parenthesis, more on that later):</p>
<div>
<ol>
<li>A member function of a Class or Trait which accepts exactly one parameter can use a space instead of a dot when calling the function and use braces instead of parenthesis to surround the one parameter the function accepts</li>
<li>The last expression of a function is it&#8217;s return value</li>
</ol>
<div>So in our example:</div>
<pre>   m.<strong>map</strong> { t =&gt; val (s,i) = t; (s, i+1) }</pre>
</div>
<div>The instance &#8220;m&#8221; has a function called &#8220;map&#8221; which takes one parameter.  Instead of using the typical parenthesis characters (…) to contain the parameter, we use braces {…}.  Why?  Mostly because syntactically this looks nicer.  In fact, let&#8217;s rewrite our compressed example as follows:</div>
<pre>   m <strong>map</strong> { 
     t =&gt; 
        val (s,i) = t
        (s, i+1)
   }</pre>
<p>I got rid of the dot between the instance variable &#8220;m&#8221; and the function &#8220;map&#8221;.  I also uncompressed the code a bit by removing  the semicolon and replacing it by a newline. Semicolons are only needed in Scala when you want to write two separate expressions on the same line.</p>
<h3>Higher-order Functions</h3>
<div style="text-align: left;"><a href="http://tataryn.net/wp-content/uploads/2011/10/Inception-HO-Functions.jpg"><img class="alignright size-medium wp-image-115" title="Higher-Order Functions" src="http://tataryn.net/wp-content/uploads/2011/10/Inception-HO-Functions-180x300.jpg" alt="" width="180" height="300" /></a>One of the tennets of Functional Programming (FP) is Higher-Order functions.  Scala being a language which supports FP has them.  So what are they?  Well, they are functions that can accept another function as a parameter, or return another function as a value.</div>
<div style="text-align: left;">Remember how I applied the first of the two syntax rules above?  I could only do that  because &#8220;map&#8221; is a function that takes one parameter.  The type of that parameter is itself a Function.  So &#8220;map&#8221; is a higher-order function; in this case, a function that takes a function as a parameter.</div>
<p><br/></p>
<h3>Anonymous Function Literals</h3>
<div>You might be scratching your head and saying to yourself: &#8220;map doesn&#8217;t appear to take a function at all but rather it seems to be passed a block of code!&#8221;.  While Scala does facilitate such things via <em>named-parameters</em>, that&#8217;s not what is happening in the example.  Instead, a <em>Function Literal</em> is being created.  You can think of Function Literals as Anonymous Functions.  Where the normal syntax for defining a named function in Scala looks like this:</div>
<pre>   <strong>def</strong> foo(x: Int): String = "You passed me a: " + x</pre>
<div>A function literal does away with the name of the function and instead you can just inline the parameter and the function body like so:</div>
<div>
<pre>   (x: Int) =&gt; "You passed me a: " + x</pre>
</div>
<div>If we weren&#8217;t passing that function as a parameter to another function, we might just as well assign it to a variable for later usage:</div>
<div>
<pre>   <strong>val</strong> bar = (x: Int) =&gt; "You passed me a: " + x</pre>
</div>
<div>So getting back to our original example, everything between the braces is a function literal:</div>
<div>
<pre>   m <strong>map</strong> { 
<span style="color: #0000ff;">      t =&gt;  
         val (s,i) = t
         (s, i+1)</span>
   }</pre>
<p>You&#8217;ll notice two things here, the first being we never did specify a type for the parameter &#8220;t&#8221;, and secondly where the heck does the parameter &#8220;t&#8221; come from?  <em>The map function holds the key</em>.  The map function expects, as it&#8217;s parameter, a function and that function should accept a parameter who&#8217;s type is compatible with the parametric type specified in the Class that &#8220;m&#8221; is an instance of.  <em>Say waaaaht?</em>  Before I explain what that all meant, we have to cover two topics: Type Inference and The Tuple</p>
</div>
<h3>Type Inference</h3>
<p>Scala is particularly clever about inferring types, the benefit being the developer can become very lazy and omit types when Scala can infer them.  Let&#8217;s see a simple example:</p>
<pre>   <strong>val</strong> s = "I'm a String"</pre>
<p>Here, Scala can infer that you want &#8220;s&#8221; to be of type String, because you&#8217;re assigning it a literal of type String.  In our example:</p>
<pre>   m <strong>map</strong> { 
<span style="color: #0000ff;">      t =&gt;  
         val (s,i) = t
         (s, i+1)</span>
   }</pre>
<p>Scala needs to infer the type of &#8220;t&#8221; because we haven&#8217;t explicitly specified it, but how will it do that?  Well, remember I said: <em>The map function holds the key</em>?  Let&#8217;s take a look at the map function&#8217;s signature:</p>
<pre>   def <strong>map</strong> [B] (f: (A) =&gt; B): List[B]<a href="#footnotes"><span style="font-size: xx-small; vertical-align: top;">1</span></a></pre>
<p>What the map function&#8217;s definition is indicating is that it expects a function, which it will rename as &#8220;f&#8221; and that function takes a type &#8220;A&#8221; as an argument, that is,  A could be an Int, String, Foo, we don&#8217;t know until map is used.  The A type placeholder is presumably defined at the Class or Trait level for the Class or Trait &#8220;m&#8221; is an instance of.</p>
<pre><strong> class</strong> List[<span style="color: #ff0000;">A</span>] ... {</pre>
<pre>      <strong>def</strong> map [<span style="color: #0000ff;">B</span>] (f: (<span style="color: #ff0000;">A</span>) =&gt; <span style="color: #0000ff;">B</span>): List[<span style="color: #0000ff;">B</span>] {...}</pre>
<pre>      .
      .
      . 
   }</pre>
<p>Now, there&#8217;s nothing to say that the types A and B can&#8217;t actually be the <em>same</em> types when map is used.  For example, if &#8220;m&#8221; were declared as a List[Int] and you pass it&#8217;s map function a function which returns an Int, then map will pass you back another List[Int].  Both A &amp; B == Int in this scenario.</p>
<p>Getting back to our original problem, that is, how Scala infers the type of &#8220;t&#8221;; it does so by looking at the function type of the &#8220;f&#8221; parameter.  t&#8217;s type will be inferred as A (whatever that might be).  So long as the mapping function you pass it treats it &#8220;like an A&#8221;, you&#8217;re good to go.</p>
<h3>Tuples</h3>
<p>In the example, which I will repeat below (I don&#8217;t want you to have to scroll) there are two instances where Tuples are being used.  A Tuple is &#8220;unpacked&#8221; and a new Tuple is created. In fact, that&#8217;s all the function does really.</p>
<pre>   m <strong>map</strong> { 
      t =&gt; 
         <span style="color: #0000ff;"><strong>val</strong> (s,i) = t // 1</span>
         <span style="color: #ff0000;">(s, i+1) // 2</span>
   }</pre>
<p>As a developer of the code above we had knowledge of the fact that m contains a bunch of Tuples.  In this case m was defined as List[Tuple2[String, Int]].  A List that contains a Tuple, and that Tuple is made up of two components.  The first component a String, the second component is an Int.</p>
<p>In <span style="color: #0000ff;">step 1</span>, we unpack the components of the Tuple into the variables &#8220;s&#8221; for the first component and &#8220;i&#8221; for the second.  Notice once again we&#8217;re letting Scala infer the types of &#8220;s&#8221; and &#8220;i&#8221; from what it can gleam of the type &#8220;A&#8221;.</p>
<p>In<span style="color: #ff0000;"> step 2</span>, we add 1 to the &#8220;i&#8221; value and then repack the Tuple.  We do this in Scala by using a comma delimited list of values surrounded with parenthesis. (Bonus Round: (a,b,c) would resolve to the type Tuple3[...], (a,b,c,d) Tuple4[...] and so on)</p>
<p>Remembering back to the second &#8220;Fundamental Syntax Rule&#8221;, we see that the last executable statement of a Function is its return value.  Since the last executable statement in our Anonymous Function is a Tuple, that&#8217;s the return value (and type) of the function.  The B type, is Tuple2[String,Int], in this case.</p>
<h3>Conclusion</h3>
<p>Can you guess then what the code sample does?  Assuming map returns a List of the things which are the output values from the function you pass it, it looks to me like you get a new List where the second component of each Tuple in the original list (re: &#8220;m&#8221;) has its value incremented by 1.  </p>
<p>What was the biggest mistake I made when I first started learning Scala?  I jumped in without looking.  I felt I could just &#8220;pickup&#8221; the language, like a C# developer might pickup Java. So the point I&#8217;m trying to make is to start off in Scala by understanding some core concepts.  If you do, the veil of the Matrix is lifted and you start to free your mind of the syntax which you aren&#8217;t yet used to.</p>
<p><a href="http://www.artima.com/shop/programming_in_scala"><img src="http://tataryn.net/wp-content/uploads/2011/10/pins2Cover500x500-300x300.gif" alt="Programming in Scala 2ed" title="Programming in Scala 2ed" width="300" height="300" class="alignright size-medium wp-image-169" /></a></p>
<h3>Programming in Scala 2ed</h3>
<p>I should mention, as I&#8217;ve been working on The Scala Language Tour my &#8220;book of reference&#8221; has been Programming in Scala 2nd Edition by Odersky (the language creator), Spoon &amp; Venners.  It&#8217;s an invaluable resource for really understanding Scala and getting to the heart of the syntax and concepts which make you scratch your head when you&#8217;re new to the language.  I can&#8217;t say enough good things about it, it&#8217;s completely worth <a href="http://www.artima.com/shop/programming_in_scala" target="_blank">grabbing a copy for yourself</a>.</p>
<h4>Footnotes</h4>
<ol>
<li>The map signature in Scala is actually a bit more complicated than what is depicted here. For sake of not mudding the waters with a lot of detail, the spirit of the map function is demonstrated instead</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://tataryn.net/2011/10/learning-scala-learn-the-fundamentals-first/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Functionally Inept Developer Survival Guide</title>
		<link>http://tataryn.net/2011/09/functionally-inept-developer-survival-guide/</link>
		<comments>http://tataryn.net/2011/09/functionally-inept-developer-survival-guide/#comments</comments>
		<pubDate>Tue, 27 Sep 2011 18:43:53 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[practices]]></category>

		<guid isPermaLink="false">http://tataryn.net/?p=51</guid>
		<description><![CDATA[The functionally inept developer will typically work hard and produce artifacts that satisfy the outward facing requirements set forth on the project plan.  What the Project Manager doesn't understand is that the quality of code being produced is not robust, doesn't stand the test of time...]]></description>
			<content:encoded><![CDATA[<div id="attachment_52" class="wp-caption alignright" style="width: 281px"><img class="size-medium wp-image-52 " title="Dr. Gregory House" src="http://tataryn.net/wp-content/uploads/2011/09/quotes-dr-house-271x300.jpg" alt="" width="271" height="300" /><p class="wp-caption-text">House wouldn&#39;t put up with your ineptitude</p></div>
<p>Professionalism in my line of business (Software Development) is quite literally all over the map.  Putting aside issues regarding slackers (people who spend more time on social media than their code) we find the <strong>functionally inept</strong>.</p>
<h1>Functional Ineptitude</h1>
<p>Dealing with a <em>normally inept</em> person might be easy, they can&#8217;t do their job so to justify their removal from your team is a simple process of pointing out their failure to deliver.  However, the <em>functionally inept</em> will typically work hard and produce artifacts that satisfy the outward facing requirements set forth on the project plan.  What the Project Manager doesn&#8217;t understand is that the <em>quality</em> of code being produced is not robust, doesn&#8217;t stand the test of time and is so tightly coupled to the rest of the system it&#8217;s like a symbiotic parasite feeding happily unnoticed.  The problem is, once this code is noticed, it would kill the host system to remove the parasite.</p>
<blockquote><p><strong>Functionally Inept Developer</strong> &#8211; a Developer who by all outward facing means gets their work done; however, does so at the expense of code quality and robustness with little to no regard for the rest of the development team.</p></blockquote>
<h1></h1>
<h1>Coping Strategies</h1>
<p>Unless you are blessed with a quality-driven Project Manager or Technical Lead worth a damn, you will be stuck with the functionally inept developer for as long as he/she can crank out code.  Given that I am a contractor, I typically step foot into companies who don&#8217;t know my track record.  In fact, a PM at one of my previous gigs told me as much after I raised concerns about a functionally inept developer.  It&#8217;s a very frustrating situation to be put in, one where you have no power to influence or change your team, yet are expected to deliver and work as a team.</p>
<p>So, after realizing I wielded little to no power with the &#8220;higher-ups&#8221; I decided to take matters into my own hands.</p>
<h2>1. Break the build, get an email</h2>
<p>What plagues software project team development more than &#8220;the broken build&#8221;?  Pretty much nothing IMHO!  It&#8217;s the fastest way to completely stop development dead in its tracks.  If you can&#8217;t build the system,  it means you can&#8217;t continue without first tracking down the source of the failure and correcting it.</p>
<p>The first thing you should do is setup a version control policy that reads something like this:</p>
<ol>
<li>Do an update from your <a href="http://en.wikipedia.org/wiki/Revision_control" target="_blank">VCS</a>, fix any local conflicts which might have resulted</li>
<li>Do a full compile of the code base</li>
<li>Run unit tests, ensure no failures</li>
<li>If it took you a while to resolve conflict, check once again for changes in VCS and repeat from step 1</li>
<li>Now check-in your changes with a USEFUL commit message (<a href="http://twitter.com/#!/unhappycoder/status/8909490370">unlike these ones</a>)</li>
</ol>
<p>9 times out of 10 nobody on the team will listen to you.  So the second thing to do is setup a <a href="http://en.wikipedia.org/wiki/Continuous_integration" target="_blank">Continuous Integration</a> (CI) server like <a href="http://www.jetbrains.com/teamcity/" target="_blank">TeamCity</a> or <a href="http://hudson-ci.org/" target="_blank">Hudson</a>.  If you are at <a href="http://simpsons.wikia.com/wiki/Compu-Global-Hyper-Mega-Net" target="_blank">Compu-Global-Hyper-Mega-Net</a> and they don&#8217;t have CI servers, set one up yourself, even if it&#8217;s just running on your own desktop.<br />
What a CI server will do is:</p>
<ol>
<li>Checkout code from VCS</li>
<li>Compile &amp; Run Unit Tests</li>
<li>Email the offending developer (or the team if you configure it thusly) stating the actual piece of committed code which caused a build to break</li>
</ol>
<p>Before I had set up a CI server on a previous contract, I cringed anytime I had to update from VCS.  It was such a massive waste of my time to have to sift through the various commits and pinpoint when and how a build was broken.  Even with all my awesome CSI-esque investigation, I&#8217;d often be ignored by the guilty developer, or be told &#8220;works on my machine&#8221; as a defence.  Fast-forward to after CI was setup:  the guilty party now gets an email from an impartial build machine. That seems to prompt people to realize it was really them who broke the build, and they should do something about it.  Especially considering they&#8217;ll most likely get the email 5 minutes after their last commit.  When they get 10-20 emails a day, it becomes embarrassing for them, and they have no one else to blame but themselves.</p>
<p><br/><br />
<div id="attachment_61" class="wp-caption alignleft" style="width: 310px"><a href="http://tataryn.net/wp-content/uploads/2011/09/gordon_ramsay-300x3001.jpg"><img class="size-full wp-image-61" title="Chef Gordon Ramsay" src="http://tataryn.net/wp-content/uploads/2011/09/gordon_ramsay-300x3001.jpg" alt="" width="300" height="300" /></a><p class="wp-caption-text">You broke the build? Bollocks!</p></div></p>
<h2>2. CYA with Unit Tests</h2>
<p>Convincing a team of developers to write unit tests from the point of view of someone who&#8217;s not in charge is next to impossible.  Don&#8217;t waste your breath.  The smart ones will see your bug count and realize why it&#8217;s so low.</p>
<p>
When you have a functionally inept developer, their tests (if they even know what one is) will suck just as badly as their code.  Instead, you need to look at Unit Tests as a <em><strong>defensive measure</strong></em>.  You are protecting your code, from theirs.  If the inept one changes the codebase in such a way as to break your functionality they&#8217;ll find out right away because a test which ran successfully before their changes ceases to afterward.<br />
Sure the functionally inept will tell you &#8220;it&#8217;s your test that is broken, not mine&#8221;, however you have to make them understand that it was their changes which broke existing functionality and they better have a pretty good reason for doing so.<br />
<br/></p>
<h2>3. Followup Code Reviews</h2>
<p>I&#8217;m not even going to go on about code reviews and how important they are.  What people seldom understand is the importance of a followup review.  Case in point, I was at a gig where a functionally inept developer was up for code review.  Of the various problems noted, two were outrageously inept.  The first was a <em><strong>650 line long</strong></em> method (<a href="http://stackoverflow.com/questions/20702/whats-your-a-good-limit-for-cyclomatic-complexity" target="_blank">Cyclomatic Complexity</a> of 14+ if memory serves).  The second was that the aforementioned  method was <em><strong>not thread safe</strong></em>.  This means, if two threads called the method at the same time, you could never guarantee a proper response.  It would be like a database which didn&#8217;t support transactions, you never know if your application was prone to dirty reads. Comp/Sci 101.<br />
<br/><br />
The developer in question was more mad at me than embarrassed.  He saw no problem with his 650 line long method and didn&#8217;t know what thread-safe meant or why it was important.  He was a &#8220;<em>senior developer</em>&#8220;.  I proceeded to kindly explain to him that we were writing web services and thus multiple requests could be calling the same function at the same time.  I spelled out a pretty simple solution to fix the code, but alas it fell on deaf ears.<br />
For a few weeks after that review I kept my eye on commits by this developer, none of which pertained to fixing either of the aforementioned problems.  I proposed a followup review and the tech-lead stated there was no time in the plan for one.  That code went to production as far as I know.<br />
<br/></p>
<h2>4. Demand from Others what you Expect from Yourself</h2>
<p>You are a professional, you shouldn&#8217;t be ashamed of demanding as much from others.  You&#8217;ll notice the pictures I chose for this article are of the fictional character <a href="http://en.wikipedia.org/wiki/Gregory_House" target="_blank">Dr. Gregory House</a> and the very real character <a href="http://en.wikipedia.org/wiki/Gordon_Ramsay" target="_blank">Chef Gordon Ramsay</a>.  I did so because Dr. House will call you on your bull if you are inept or cut corners and Chef Ramsay expects nothing but the best possible job you can do.  A lot of people think Ramsay is &#8220;an a-hole&#8221;.  I just think he has a high standard that he strives for himself, and is very disappointed when others fall short of that mark.<br />
<br/><br />
The truth is, Software Developers are not held to the same standards as Professional Engineers.  So stop calling yourself a <em>Software Engineer</em>.  Anytime I hear that designation it reminds me of <a href="http://www.youtube.com/watch?v=Ob7Fin6aVtA#at=41" target="_blank">this Denis Leary skit</a>.  The fact of the matter is every company allows their developers to operate in different ways, most of which are dysfunctional.  I don&#8217;t know why we&#8217;ve gotten away with this for so long, but it&#8217;s the reality of our profession.<br />
<br/><br />
So in closing, strive to do the best you can, educate those willing to learn and keep inept developers (functional or otherwise) at as far a distance from yourself as you can.  The code-grenades they produce are theirs to jump on, don&#8217;t become collateral damage when the pin is pulled.</p>
]]></content:encoded>
			<wfw:commentRss>http://tataryn.net/2011/09/functionally-inept-developer-survival-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>First!</title>
		<link>http://tataryn.net/2011/09/first/</link>
		<comments>http://tataryn.net/2011/09/first/#comments</comments>
		<pubDate>Thu, 08 Sep 2011 20:34:53 +0000</pubDate>
		<dc:creator>craig</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://tataryn.net/?p=18</guid>
		<description><![CDATA[You know, I&#8217;ve had this domain for about 11 years now and have never put up a blog, but dammit, I&#8217;m getting so good at setting up wordpress that it&#8217;s really a no brainer for me to do so.  I used to blog a long time ago over at JRoller and I do have The [...]]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><a href="http://tataryn.net/wp-content/uploads/2011/09/finger.jpg"><img class="size-full wp-image-30 aligncenter" title="finger" src="http://tataryn.net/wp-content/uploads/2011/09/finger.jpg" alt="" width="558" height="132" /></a></p>
<p style="text-align: left;">You know, I&#8217;ve had this domain for about 11 years now and have never put up a blog, but dammit, I&#8217;m getting <a href="http://basementcoders.com">so</a> good <a href="http://grindsoftware.com">at</a> setting <a href="http://wfpg.ca">up</a> wordpress that it&#8217;s really a no brainer for me to do so.  I used to blog a long time ago over at <a href="http://jroller.com/craiger/">JRoller</a> and I do have <a href="http://basementcoders.com">The Basement Coders Podcast</a> blog, however this domain seems to be the logical place for me to Blog as it&#8217;s just for me.</p>
<p>Back when I was attending university in the late &#8217;90s we used to call Blogs &#8220;.plans&#8221;.  Why?  Because within your Unix home directory you could place a file named &#8220;.plan&#8221; which would show up if someone <a href="http://en.wikipedia.org/wiki/Finger_protocol">finger</a>ed you.  Sounds a lot dirtier than it was.  This was the internet before we had the social graph, before websites were even prevalent; it was a time when you had to know the username and host where a person lived.</p>
<p>One of the most famous .plans to reach the web that I can remember is none other than <a href="http://en.wikipedia.org/wiki/John_D._Carmack" target="_blank">John Carmack</a>. John, of course, is the demi-god responsible for bringing us such games as Doom and Quake, not to mention he pioneered (in my mind) the idea that a game company can not only make money from producing games, but they can also take the time to build the game in such a way that it&#8217;s guts (engine) can be distributed to other gaming companies that perhaps do not want to spend the time and effort (not to mention expertise) in writing a gaming framework that a creative set of people can take and produce something wonderful with (Return to Castle Wolfenstein comes to mind).</p>
<p>John is one of those types who literally eats, sleeps and breathes problem solving. &#8220;Our map files aren&#8217;t compressing well using libraryX, I&#8217;ll write my own algorithm&#8221;  That&#8217;s the type of guy he is.  I guess you can say he would be a person in the computer field I look up to. Not only can he talk for 45 minutes and at the end of it you have no idea what he said (because he&#8217;s at *that* level) but he&#8217;s not just a one trick pony. I remember watching the <a href="http://media.pc.gamespy.com/media/014/014934/vids_1.html">QuakeCon 2004 Keynote speech</a> about Doom 3 (Carmack couldn&#8217;t attend because of the birth of his child, hence why he is on a video screen). In this keynote he kind of humbley mentions that he took over the sound engine code and re-wrote it.  I was blown away.  Not only does he make this revolutionary graphics engine, he also does the sound engine.  To put that into perspective, that would be like if when your cable guy setup your digital receiver he turned around and said &#8220;That faucet looks a bit leaky, I&#8217;ll re-do your plumbing&#8221;.<br />
The thing I like most about John is his ability to think like an application developer. He doesn&#8217;t just one-off a game, he creates an eco-system (a framework+tools) that can be utilized by both hard-core programmers and his high-level designers, not to mention his fan base through the release of modding tools. That&#8217;s what I (and many)&amp;nbsp;like doing in software projects. Framework+tools == teh win (&#8220;the&#8221; intentionally misspelled).<br />
Sadly John&#8217;s .plan is gone, he has switched to a <a href="http://www.armadilloaerospace.com/n.x/johnc/">blog</a> over at his side-project website (side project being: trying to launch a person into space). He hasn&#8217;t updated it since 2006.  Sad.  You can <a href="http://twitter.com/#!/ID_AA_Carmack">find him on Twitter</a> though, and every once in a while he throws out some random graphics prose.</p>
]]></content:encoded>
			<wfw:commentRss>http://tataryn.net/2011/09/first/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
<!-- This Quick Cache file was built for (  tataryn.net/feed/ ) in 0.25824 seconds, on May 20th, 2012 at 9:59 am UTC. -->
<!-- This Quick Cache file will automatically expire ( and be re-built automatically ) on May 20th, 2012 at 10:59 am UTC -->
<!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
<!-- Quick Cache Is Fully Functional :-) ... A Quick Cache file was just served for (  tataryn.net/feed/ ) in 0.00027 seconds, on May 20th, 2012 at 10:18 am UTC. -->
