<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
    <title type="text" xml:lang="en">Software Craft Blog</title>
    <subtitle type="text" xml:lang="en">About programming and software craftsmanship.</subtitle>
    <link type="application/atom+xml" href="https://blog.thesoftwarecraft.com/index.xml" rel="self"/>
    <link type="text" href="https://blog.thesoftwarecraft.com" rel="alternate"/>
    <updated>2026-02-17T22:42:58+00:00</updated>
    <author>
        <name>Bart Bakker</name>
    </author>
    <id>https://blog.thesoftwarecraft.com</id>
    
    
    <entry>
        <title>Global Day of Coderetreat 2014</title>
        <link href="https://blog.thesoftwarecraft.com/global-day-of-coderetreat-2014.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-11-28:/global-day-of-coderetreat-2014</id>
        <updated>2014-11-28T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;&lt;img src=&quot;https://blog.thesoftwarecraft.com/img/posts/gdcr2014.png&quot; alt=&quot;Global Day of Coderetreat&quot; title=&quot;Global Day of Coderetreat&quot; width=&quot;250&quot; class=&quot;img-responsive img-thumbnail main-img pull-right&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Saturday November 15 set the stage for Global Day of Coderetreat 2014. On this day developers from all over the world joined together to improve their craft.&lt;/p&gt;

&lt;p&gt;Together with &lt;a href=&quot;https://www.linkedin.com/in/stefanhendriks&quot; rel=&quot;external&quot;&gt;Stefan Hendriks&lt;/a&gt; I facilitated the event in Utrecht, The Netherlands. In this post I sum up the day and provide a few extra links for the participants at out location. Special thanks to Stefan for proofreading this post.&lt;/p&gt;

&lt;p&gt;I have to admit, after the Coderetreat we did for 45 people in September this year, having 13 attendees for Global Day is really relaxing and fun! Much less intensive on the facilitators, so we even got to coding ourselves :)&lt;/p&gt;

&lt;h2 id=&quot;introduction&quot;&gt;Introduction&lt;/h2&gt;

&lt;p&gt;At every Coderetreat we start with a short introduction why we facilitate Coderetreats, and briefly discuss the &lt;a href=&quot;/2014/04/book-review-understanding-4-rules-of.html&quot;&gt;4 rules of simple design&lt;/a&gt;. Also we ask attendees to quickly introduce themselves, what languages they are interested in doing and what languages they are proficient in. This helps people in chosing their pair partners.&lt;/p&gt;

&lt;p&gt;Each session is about an hour; we code for 45 minutes, and then have a little retrospective. The &lt;a href=&quot;/2014/02/code-katas.html&quot;&gt;kata&lt;/a&gt; used in these sessions is &lt;a href=&quot;http://en.wikipedia.org/wiki/Conway%27s_Game_of_Life&quot; rel=&quot;external&quot;&gt;Conway’s Game of Life&lt;/a&gt;. This is a great fit for the Coderetreat format because everyone can understand it, yet there are a lot of interesting aspects to coding it up. Also it is hard to finish the full kata in only 45 minutes, which helps people to refrain from the pressure of finishing it. Coderetreat is not at all about finishing a kata, it’s about practice.&lt;/p&gt;

&lt;p&gt;In every session different constraints are applied to the way people can program. I’ll explain the constraints we used below. Remember that there are a lot more constraints, we don’t use the same ones on every coderetreat. This keeps the sessions interesting, also for coderetreat veterans.&lt;/p&gt;

&lt;p&gt;If you’re interested in learning more about the format, please see &lt;a href=&quot;http://coderetreat.org&quot; rel=&quot;external&quot;&gt;coderetreat.org&lt;/a&gt;. And of course feel free to drop me a question.&lt;/p&gt;

&lt;h2 id=&quot;session-1-warming-up&quot;&gt;Session 1: Warming Up&lt;/h2&gt;

&lt;p&gt;The first session is to get familiar with the problem domain, and to get everyone’s environment up and running.&lt;/p&gt;

&lt;p&gt;On a Coderetreat you don’t need much; your favorite language and a single &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;assert_true&lt;/code&gt; function will do. Sometimes I am still amazed at how much effort people want to put into setting up “&lt;em&gt;the perfect&lt;/em&gt;” test library.&lt;/p&gt;

&lt;h2 id=&quot;session-2-no-conditionals&quot;&gt;Session 2: No Conditionals&lt;/h2&gt;

&lt;p&gt;Programming without using a form of conditionals is tough. This is one of my favorite constraints, especially to start with. It might be mind breaking, and truly opens your mind to find other ways of coding.&lt;/p&gt;

&lt;p&gt;When I attended a Coderetreat given by &lt;a href=&quot;http://articles.coreyhaines.com/&quot; rel=&quot;external&quot;&gt;Corey Haines&lt;/a&gt; he explained this constrained as being about polymorphism. Conditionals are procedural polymorphism. You can substitute conditionals with either inheritance (in Object Oriented languages) or polymorphic types and function parameters (in Functional languages). Explaining the constraint like so helps people to grow a better understanding and gives them some direction. It turns out I’m not alone on this, Cory Foy wrote a &lt;a href=&quot;http://blog.8thlight.com/cory-foy/2013/06/26/procedural-polymorphism.html&quot; rel=&quot;external&quot;&gt;blog post&lt;/a&gt; about this on the &lt;a href=&quot;http://blog.8thlight.com&quot; rel=&quot;external&quot;&gt;8th Light blog&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The common feedback, as usual, was that it is really hard not to use if-statements and to start thinking completely different. Most pairs started finding solutions late in the session.&lt;/p&gt;

&lt;p&gt;If you’re truly fanatic about not using if-statement, perhaps the &lt;a href=&quot;http://antiifcampaign.com/&quot; rel=&quot;external&quot;&gt;Anti-IF Campaign&lt;/a&gt; is for you.&lt;/p&gt;

&lt;h2 id=&quot;session-3-functional-core-imperative-shell-ala-gary-bernhardt&quot;&gt;Session 3: Functional Core, Imperative Shell ala Gary Bernhardt&lt;/h2&gt;

&lt;p&gt;After lunch we first watched Gary Bernhardt’s &lt;a href=&quot;https://www.destroyallsoftware.com/talks/boundaries&quot; rel=&quot;external&quot;&gt;Boundaries&lt;/a&gt; talk. As the topic is relatively unknown. And what’s a better introduction than the original talk anyway?! After watching the talk we shared some thoughts on it and then started the session.&lt;/p&gt;

&lt;p&gt;In case you haven’t watched the talk, in a nutshell this constraint comes down to the following. The Functional Core is about keeping all your objects immutable. The imperative shell is a thin layer that manages state (hence is imperative) using the functional core.&lt;/p&gt;

&lt;p&gt;The common feedback was that using immutable objects helped understanding and testing the code.&lt;/p&gt;

&lt;p&gt;If you’re interested in learning more on this topic, I highly recommend watching Gary’s &lt;a href=&quot;https://www.destroyallsoftware.com/screencasts/catalog/functional-core-imperative-shell&quot; rel=&quot;external&quot;&gt;screencast&lt;/a&gt;. As Gary shows in Ruby you don’t have to use a fully functional language to apply some of the concepts. Jessica Kerr’s talk &lt;a href=&quot;http://www.infoq.com/presentations/fp-principles-oop&quot; rel=&quot;external&quot;&gt;Functional Principles for Object-Oriented Developers&lt;/a&gt; explains some functional principles through Java and C# code samples.&lt;/p&gt;

&lt;h2 id=&quot;session-4-mute-pairs-with-evil-coder&quot;&gt;Session 4: Mute Pairs with Evil Coder&lt;/h2&gt;

&lt;p&gt;Another one of my favorite constraints is the mute session. By not allowing pairs to talk and to only communicate through code (not comments!) we simulate pairing with future-you, say in half a year from now. How understandable is the test you write, does it actually express intent the way you think it does?&lt;/p&gt;

&lt;p&gt;Add the evil coder role and you’ll see where your tests can be more strict. The goal of the evil coder is to find loop holes in the tests and try to push the implementation in a different direction.&lt;/p&gt;

&lt;h2 id=&quot;closing-circle&quot;&gt;Closing Circle&lt;/h2&gt;

&lt;p&gt;The overal mood in the closing circle was that we had a great day. I couldn’t agree more. The group got more intimate throughout the day, and got a lot more open. What surprised me was how eager this group was.&lt;/p&gt;

&lt;p&gt;Again I met some great people, and had an awesome time. Thank you all for that!&lt;/p&gt;

&lt;h2 id=&quot;software-is-eating-the-world&quot;&gt;“Software is eating the world”&lt;/h2&gt;

&lt;p&gt;Throughout the day we mentioned Erik Meijer’s recent talk &lt;a href=&quot;http://reaktor.fi/blog/erik-meijer-software-eating-world/&quot; rel=&quot;external&quot;&gt;Software is eating the world&lt;/a&gt;. And the &lt;a href=&quot;http://blog.cleancoder.com/uncle-bob/2014/11/12/PutItInProduction.html&quot; rel=&quot;external&quot;&gt;One Hacker Way!&lt;/a&gt; response of Uncle Bob (Robert C. Martin).&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Moved to Github Pages</title>
        <link href="https://blog.thesoftwarecraft.com/moved-to-github-pages.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-08-09:/moved-to-github-pages</id>
        <updated>2014-08-09T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;As of this post I moved my blog to Github Pages from Google’s Blogger. As a consequence I built this blog from scratch. As a frequent reader you may notice some changes, hopefully all to your liking.&lt;/p&gt;

&lt;p&gt;Github Pages uses Jekyll to serve content. Jekyll generates a static site from your articles (I write these in markdown) and templates. My main goal was to move my blog to Github Pages, with all articles and a similar layout. Of course on the way I came up with a ton more things I could do (now that I entangled myself from the constraints of Blogger). Most didn’t make it because they were not important to move the blog. It’s interesting to be your own customer.&lt;/p&gt;

&lt;p&gt;Even comments were not important enough to me. I do much appreciate your comments. It’s just that there are great alternatives such as &lt;a href=&quot;http://reddit.com&quot; rel=&quot;external&quot;&gt;Reddit&lt;/a&gt;, rendering my own comments feature unimportant. As a result, comments from my old blog are not available at this time. Sorry about that. I might support commenting later and move existing comments (if the tools allow me to). Of course you can also &lt;a href=&quot;/about/&quot;&gt;contact me&lt;/a&gt; directly.&lt;/p&gt;

&lt;h2 id=&quot;github-pages&quot;&gt;Github Pages&lt;/h2&gt;

&lt;p&gt;&lt;img src=&quot;https://blog.thesoftwarecraft.com/img/posts/github.png&quot; alt=&quot;Github Pages&quot; title=&quot;Github Pages&quot; width=&quot;150&quot; class=&quot;img-responsive img-thumbnail img-left&quot; /&gt;&lt;/p&gt;

&lt;p&gt;So why Github Pages?&lt;/p&gt;

&lt;p&gt;Well, I want to write my blogs in text using a decent editor. Hence I don’t use rich text formatting in an editor. Rich text editors are broken and it highly unlikely for them to be be fixed anytime soon. For me the Blogger editor caused all kinds of issues, from data loss to broken layouts (because the HTML generator changed again). Others (like WordPress) are just as bad.&lt;/p&gt;

&lt;p&gt;Then I also wanted to be able to build some decent templates. Believe it or not, my Blogger template counted almost 2400 lines. At this point all my templates and css combined counts about 400 lines. As a result it’s easy to change templates and add new stuff. Also I now have full control over the HTML, which is alright with me.&lt;/p&gt;

&lt;p&gt;Starting a new site with Jekyll was easy and writing the initial templates was fun. As I write posts in markdown it caused some trouble to set attribute values on tags (like rel=nofollow on ancors). I ended up using the Kramdown markdown engine, which supports most of the things I need.&lt;/p&gt;

&lt;p&gt;But then there comes that point where you have to start migrating content. I could grab the HTML of my articles easily from the atom feed. Then I had to convert that into the markdown I want. That is not fun to do. So rather than converting all of it myself I hacked up a Vim script with a bunch of regex substitutions to do most of the tedious conversion stuff for me.&lt;/p&gt;

&lt;p&gt;So far I pretty much enjoyed Jekyll. It has built-in support for things like pagination and code highlighting. It is kind of a do-it-yourself tool, which is a good thing. It keeps things simple. Yet the powerful templates make many things possible (like creating your own atom feed, as Jekyll doesn’t come with one).&lt;/p&gt;

&lt;p&gt;A bit of a challenge when moving existing content is to keep the exact same URL structure. Normally I would 301 redirect the old URLs to the new structure. Given the nature of Jekyll this is not possible. The use of 301 redirects (in my case) is mainly important to preserve ranking in a search engine. Luckily Google (and likely others) also treat a meta-refresh with a canonical link like a 301.&lt;/p&gt;

&lt;p&gt;All in all this was an interesting experience. For any static site I now would consider Jekyll first, especially if content needs not to be written in a rich text editor. And for many dynamic features of a site you can always use Javascript and cloud service.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Java's Volatile Modifier</title>
        <link href="https://blog.thesoftwarecraft.com/2014/07/javas-volatile-modifier.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-07-15:/2014/07/javas-volatile-modifier</id>
        <updated>2014-07-15T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;A while ago I wrote a Java servlet Filter that loads configuration in its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;init&lt;/code&gt; function (based on a parameter from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;web.xml&lt;/code&gt;). The filter’s configuration is cached in a private field. I set the volatile modifier on the field.&lt;/p&gt;

&lt;p&gt;When I later checked the company &lt;a href=&quot;http://www.sonarsource.com/&quot; rel=&quot;external&quot;&gt;Sonar&lt;/a&gt; to see if it found any warnings or issues in the code I was a bit surprised to learn that there was a violation on the use of volatile. The explanation read:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Use of the keyword ‘volatile’ is generally used to fine tune a Java application, and therefore, requires a good expertise of the Java Memory Model. Moreover, its range of action is somewhat misknown. Therefore, the volatile keyword should not be used for maintenance purpose and portability.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I would agree that volatile is misknown by many Java programmers. For some even unknown. Not only because it’s never used much in the first place, but also because it’s definition changed since Java 1.5.&lt;/p&gt;

&lt;p&gt;Let me get back to this Sonar violation in a bit and first explain what volatile means in Java 1.5 and up (until Java 1.8 at the time of writing).&lt;/p&gt;

&lt;h2 id=&quot;what-is-volatile&quot;&gt;What is Volatile?&lt;/h2&gt;
&lt;p&gt;While the volatile modifier itself comes from C, it has a completely different meaning in Java. This may not help in growing an understanding of it, googling for volatile could lead to different results. Let’s take a quick side step and see what volatile means in C first.&lt;/p&gt;

&lt;p&gt;In the C language the compiler ordinarily assumes that variables cannot change value by themselves. While this makes sense as default behavior, sometimes a variable may represent a location that can be changed (like a hardware register). Using a volatile variable instructs the compiler not to apply these optimizations.&lt;/p&gt;

&lt;p&gt;Back to Java. The meaning of volatile in C would be useless in Java. The JVM uses native libraries to interact with the OS and hardware. Further more, it is simply impossible to point Java variables to specific addresses, so variables actually won’t change value by themselves.&lt;/p&gt;

&lt;p&gt;However, the value of variables on the JVM can be changed by different threads. By default the compiler assumes that variables won’t change in other threads. Hence it can apply optimizations such as reordering memory operations and caching the variable in a CPU register. Using a volatile variable instructs the compiler not to apply these optimizations. This guarantees that a reading thread always reads the variable from memory (or from a shared cache), never from a local cache.&lt;/p&gt;

&lt;h2 id=&quot;atomicity&quot;&gt;Atomicity&lt;/h2&gt;
&lt;p&gt;Further more on a 32 bit JVM volatile makes writes to a 64 bit variable atomic (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;long&lt;/code&gt; or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;double&lt;/code&gt;). To write a variable the JVM instructs the CPU to write an operand to a position in memory. When using the 32 bit instruction set, what if the size of a variable is 64 bits? Obviously the variable must be written with two instructions, 32 bits at a time.&lt;/p&gt;

&lt;p&gt;In multi-threaded scenarios another thread may read the variable half way through the write. At that point only first half of the variable is written. This race-condition is prevented by volatile, effectively making writes to 64 bit variables atomic on 32 bit architectures.&lt;/p&gt;

&lt;p&gt;Note that above I talked about &lt;em&gt;writes&lt;/em&gt; not &lt;em&gt;updates&lt;/em&gt;. Using volatile won’t make updates atomic. E.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;++i&lt;/code&gt; when &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; is volatile would read the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; from the heap or L3 cache into a local register, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;inc&lt;/code&gt; that register, and write the register back into the shared location of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt;. In between reading and writing &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;i&lt;/code&gt; it might be changed by another thread. Placing a lock around the read and write instructions makes the update atomic. Or better, use non-blocking instructions from the atomic variable classes in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;concurrent.atomic&lt;/code&gt; package.&lt;/p&gt;

&lt;h2 id=&quot;side-effect&quot;&gt;Side Effect&lt;/h2&gt;
&lt;p&gt;A volatile variable also has a side effect in memory visibility. Not just changes to the volatile variable are visible to other threads, but also any side effects of the code that led up to the change are visible when a thread reads a volatile variable. Or more formally, a volatile variable establishes a happens-before relationship with subsequent reads of that variable.&lt;/p&gt;

&lt;p&gt;I.e. from the perspective of memory visibility writing a volatile variable effectively is like exiting a synchronized block and reading a volatile variable like entering one.&lt;/p&gt;

&lt;h2 id=&quot;choosing-volatile&quot;&gt;Choosing Volatile&lt;/h2&gt;

&lt;p&gt;Back to my use of volatile to initialize a configuration once and cache it in a private field.&lt;/p&gt;

&lt;p&gt;Up to now I believe the best way to ensure visibility of this field to all threads is to use volatile. I could have used &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;AtomicReference&lt;/code&gt; instead. Since the field is only written once (after construction, hence it cannot be final) atomic variables communicate the wrong intent. I don’t want to make updates atomic, I want to make the cache visible to all threads. And for what it’s worth, the atomic classes use volatile too.&lt;/p&gt;

&lt;h2 id=&quot;thoughts-on-this-sonar-rule&quot;&gt;Thoughts on this Sonar Rule&lt;/h2&gt;
&lt;p&gt;Now that we’ve seen what volatile means in Java, let’s talk a bit more about this Sonar rule.&lt;/p&gt;

&lt;p&gt;In my opinion this rule is one of the flaws in configurations of tools like Sonar. Using volatile can be a really good thing to do, if you need shared (mutable) state across threads. Sure thing you must keep this to a minimum. But the consequence of this rule is that people who don’t understand what volatile is follow the recommendation to not use volatile. If they remove the modifier effectively they introduce a race-condition.&lt;/p&gt;

&lt;p&gt;I do think it’s a good idea to automatically raise red flags when misknown or dangerous language features are used. But maybe this is only a good idea when there are better alternatives to solve the same line of problems. In this case, volatile has no such alternative.&lt;/p&gt;

&lt;p&gt;Note that in no way this is intended as a rant against Sonar. However I do think that people should select a set of rules that they find important to apply, rather than embracing default configurations. I find the idea to use rules that are enabled by default, a bit naive. There’s an extremely high probability that your project is not the one that tool maintainers had in mind when picking their standard configuration.&lt;/p&gt;

&lt;p&gt;Furthermore I believe that as you encounter a language feature that you don’t know, you should learn about it. As you learn about it you can decide if there are better alternatives.&lt;/p&gt;

&lt;h2 id=&quot;java-concurrency-in-practice&quot;&gt;Java Concurrency in Practice&lt;/h2&gt;
&lt;p&gt;The de facto standard book about concurrency in the JVM is &lt;a href=&quot;https://amzn.to/4nM97Tw&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Java Concurrency in Practice&lt;/a&gt; by Brain Goetz. It explains the various aspects of concurrency in several levels of detail. If you use any form of concurrency in Java (or impure Scala) make sure you at least read the former three chapters of this brilliant book to get a decent high-level understanding of the matter.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Book Review: Understanding the 4 Rules of Simple Design</title>
        <link href="https://blog.thesoftwarecraft.com/2014/04/book-review-understanding-4-rules-of.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-04-17:/2014/04/book-review-understanding-the-4-rules-of-simple-design</id>
        <updated>2014-04-17T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;&lt;a href=&quot;https://leanpub.com/4rulesofsimpledesign&quot; rel=&quot;nofollow&quot; class=&quot;main-img pull-right&quot;&gt;&lt;img src=&quot;https://blog.thesoftwarecraft.com/img/posts/understanding-the-4-rules-of-simple-design-cover.png&quot; alt=&quot;cover&quot; title=&quot;Understanding the 4 Rules of Simple Design by Corey Haines&quot; width=&quot;150&quot; class=&quot;img-responsive img-thumbnail&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;It’s about time that a book is written about the 4 rules of simple design. These rules are possibly the most powerful yet least understood software design practices out there.&lt;/p&gt;

&lt;h2 id=&quot;tldr&quot;&gt;TL;DR&lt;/h2&gt;

&lt;p&gt;If you care about your software being easy to adapt to changing requirements and a continuously evolving environment, you should probably know about the 4 rules of simple design. &lt;a href=&quot;https://leanpub.com/4rulesofsimpledesign&quot; rel=&quot;nofollow&quot;&gt;Understanding the 4 Rules of Simple Design&lt;/a&gt; helps you grow a decent understanding of these rules. Or at least enough so that you can practice them yourself (at the next Coderetreat, maybe). So, go buy this wonderful book.&lt;/p&gt;

&lt;h2 id=&quot;slightly-longer-review&quot;&gt;Slightly Longer Review&lt;/h2&gt;

&lt;p&gt;The 4 rules of simple design were originally codified by Kent Beck in the late 90’s. Kent Beck also writes about these rules in his book Extreme Programming Explained. The rules that will lead you to a simple design are&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Tests pass&lt;/li&gt;
  &lt;li&gt;Express intent&lt;/li&gt;
  &lt;li&gt;DRY&lt;/li&gt;
  &lt;li&gt;Small&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;By themselves they are simple rules. Maybe this is why I find people underestimate them. But following these 4 simple rules does lead to better designs. That’s what makes them so interesting. The rules of simple design aren’t usually taught at trainings. The only training format, that I know of, that practices these rules is &lt;a href=&quot;http://coderetreat.org/&quot; rel=&quot;external&quot;&gt;Coderetreat&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The author of &lt;a href=&quot;https://leanpub.com/4rulesofsimpledesign&quot; rel=&quot;nofollow&quot;&gt;Understanding the 4 Rules of Simple Design&lt;/a&gt;, Corey Haines, is to Coderetreat what Kent Beck is to &lt;a href=&quot;http://www.extremeprogramming.org/&quot; rel=&quot;external&quot;&gt;Extreme Programming&lt;/a&gt;. Corey is like the father of Coderetreat. The past 5 years he has traveled the world to give trainings using the Coderetreat format. If someone can write a book about the rules of simple design, no doubt it’s him.&lt;/p&gt;

&lt;p&gt;In the introductory part of the book Corey introduces himself, the Coderetreat format, and explains what Coderetreat is about and why it matters. He then discusses the 4 rules and explains what they mean.&lt;/p&gt;

&lt;p&gt;These introductions lead to the examples chapter. This is the larger part of the book. The examples are based on the patterns that Corey noticed while facilitating Coderetreat sessions. They include how test names should influence the object’s API, what DRY actually means, how to replace procedural polymorphism, and much more. For each example Corey presents the case and then leads you through the thought process of improving the design.&lt;/p&gt;

&lt;p&gt;Of course there are many other design principles that should not be forgotten. Some of these, the SOLID principles and Law of Demeter, are explained in this book as well. Corey mentions how focussing on the 4 rules of simple design naturally leads to satisfying most of the SOLID principles as well. I share this experience.&lt;/p&gt;

&lt;p&gt;In only a hundred pages Corey has managed to describe the 4 rules of simple design with some great examples that no doubt show you the true power of these 4 simple rules.&lt;/p&gt;

&lt;p&gt;However, simple design is hard and after reading this book everything won’t just fit like magic. Corey has done an excellent job writing this book. Software design is hard and in my experience the way to get better at is through practice. Reading this book is an excellent start,&lt;/p&gt;

&lt;p&gt;The style of writing makes this book easy to read, from cover to cover. If you like a preview of some of Corey’s writing you should definitely read his blog post &lt;a href=&quot;http://articles.coreyhaines.com/posts/thoughts-on-pair-programming/&quot; rel=&quot;external&quot;&gt;Some thoughts on pair-programming styles&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Completed with a further reading list, this book is most definitely worth it’s money. As it’s published through Leanpub you can set a price yourself. Although you really shouldn’t want to pay less than $20.&lt;/p&gt;

&lt;p&gt;I would definitely recommend this book to programmers with all sorts of experience; from beginner to expert, from apprentice to master craftsman.&lt;/p&gt;

&lt;p&gt;For a little critical note, I found it a bit disturbing that the first release didn’t include a picture of Zak the cat. It’s awesome that he had fixed that, so this wonderful book now comes with Corey’s trademark! ;-)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: what I’ve written here is my own personal opinion. I do not have any stakes in selling this book. I don’t benefit financially from Corey selling more copies, nor books sold to visitors of this blog.&lt;/em&gt;&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Method References in Ruby</title>
        <link href="https://blog.thesoftwarecraft.com/2014/04/method-references-in-ruby.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-04-10:/2014/04/method-references-in-ruby</id>
        <updated>2014-04-10T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Today I was writing some Ruby code for filtering arrays when I recalled that Ruby has a shorthand notation to pass a method by its name (as a symbol). I’m writing this post to help me remember, while I figure out how it really works.&lt;/p&gt;

&lt;p&gt;Say, for example, that we need to select the odd numbers from a sequence. Of course we could pass a block to do that.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;|&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;odd?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [1, 3, 5, 7, 9]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;But we can also pass a symbol, prefixed with an ampersand. While not actually accurate (in a few minutes you’ll see why), this is what I call a method reference.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:odd?&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [1, 3, 5, 7, 9]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Personally I prefer the latter form as it’s a better representation of the essence that I want my code to do: &lt;em&gt;from range 1 to 9 select only odd numbers&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;how-does-it-work&quot;&gt;How does it work?&lt;/h2&gt;
&lt;p&gt;When Googling for information on the ampersand unary operator in Ruby, I first found a few blog posts and &lt;a href=&quot;http://stackoverflow.com/&quot; rel=&quot;external&quot;&gt;Stack Overflow&lt;/a&gt; answers that told me that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_proc&lt;/code&gt; on a given object. That would be convenient, although it implies that this would probably only work for methods that accept both a block or a proc. Let’s worry about that later.&lt;/p&gt;

&lt;p&gt;First try and see if this works. If &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;&lt;/code&gt; would call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_proc&lt;/code&gt; on the symbol I should be able to pass a proc myself, too.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:foo?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_proc&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;ArgumentError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrong&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Whoops! That didn’t work too well. What’s even more interesting is that if I just put the ampersand in front, the code runs.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:foo?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_proc&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [1, 3, 5, 7, 9]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;So maybe these first hits weren’t actually correct about things. At this point it seems more likely that ampersand turns a proc into a block. And given any object, it will get a proc by calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_proc&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;To see how my new theory works out I use a custom object that answers to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_proc&lt;/code&gt;. Because Ruby’s blocks are special things that cannot be created or assigned to variables, I cannot simply capture the resulting value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;&amp;amp;:foo&lt;/code&gt;. Let’s give it a try.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;odds&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;new&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;odds&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_proc&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;odd?&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;9&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;select&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odds&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; [1, 3, 5, 7, 9]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Awesome! However, this doesn’t really confirm that the ampersand in fact turns the proc into block, only that actually &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;to_proc&lt;/code&gt; is called on the object.&lt;/p&gt;

&lt;p&gt;To check that the ampersand turns the proc into a block I use a function that yields.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;yielder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;yield&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;yielder&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odds&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; true&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;yielder&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odds&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;#=&amp;gt; false&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While passing a proc obviously won’t work.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span class=&quot;n&quot;&gt;yielder&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;odds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;to_proc&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;ArgumentError&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;wrong&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;number&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;of&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arguments&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Tentatively my conclusion is that the ampersand unary operator takes a duck-type of a proc (i.e. an object that responds to to_proc) and returns a block. If you see any flaws in this, don’t hesitate to let me know.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Is your Scrum team Agile?</title>
        <link href="https://blog.thesoftwarecraft.com/2014/04/is-your-scrum-team-agile.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-04-03:/2014/04/is-your-scrum-team-agile</id>
        <updated>2014-04-03T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Many software development teams use Scrum. Indeed Scrum is a highly popular process. In this post I talk about some of the problems I’ve noticed in some Scrum teams. I don’t believe these problems are related to Scrum, but are a direct consequence of the mindset of these teams.&lt;/p&gt;

&lt;p&gt;To stop this from becoming a &lt;a href=&quot;http://en.wikipedia.org/wiki/No_true_Scotsman&quot; rel=&quot;external&quot;&gt;No True Scotsman&lt;/a&gt; story, I’ll talk about the way I’ve seen how some teams have adopted Scrum.&lt;/p&gt;

&lt;p&gt;Of the 12 practices the &lt;a href=&quot;http://agilemanifesto.org/&quot; rel=&quot;external&quot;&gt;Agile Manifesto&lt;/a&gt; is built on, Scrum is mainly about the planning activities. This doesn’t mean the other practices aren’t valuable in Scrum teams, but it’s not the main focus. Most Scrum teams use the planning game for each sprint, do daily stand-ups, use small releases, and have a customer on-site. That should make them agile, right?&lt;/p&gt;

&lt;h2 id=&quot;agile-manifesto&quot;&gt;Agile Manifesto&lt;/h2&gt;
&lt;p&gt;Let’s take a look at the &lt;a href=&quot;http://agilemanifesto.org/principles.html&quot; rel=&quot;nofollow&quot;&gt;Principles behind the Agile Manifesto&lt;/a&gt;. The number one priority is “to satisfy the customer through early and continuous delivery of valuable software”. Closely followed by the importance to (even late in development) embrace changing requirements and the notion that working software is the primary measure of progress.&lt;/p&gt;

&lt;p&gt;While valuable software obviously is highly important, I think the key is in the early and continuous delivery of software. We add value to software by changing it and extending it. The planning game will help you towards that road. But then what?&lt;/p&gt;

&lt;p&gt;This is where the rest of the agile practices pop in. Practices like automation of acceptance tests, TDD, pair programming, simple design and refactoring (not in any specific order). These are practices that actively guide you to more continuous delivery.&lt;/p&gt;

&lt;h2 id=&quot;keeping-up&quot;&gt;Keeping up&lt;/h2&gt;
&lt;p&gt;That Scrum itself focuses on planning aspects doesn’t mean that these other practices aren’t valuable in Scrum teams. It’s just not the main focus of Scrum. In fact, I wonder how well teams can ever keep up the agile principles when they only use planning. I’ve seen teams that respond to new requirements by demanding some refactor sprint to “prepare” for what’s coming.&lt;/p&gt;

&lt;p&gt;I’ve been at such a team. One sprint turned into two sprints. Our biggest problem to me was that we’d only knew if we screwed up when QA told us so. It was seen as the job of QA to tell whether the developers made a mistake. Oh yes, we were a whole team. We had an on-site customer. We had QA inside the team. We did daily stand-ups. We did planning sessions for each sprint. But we didn’t use the other agile practices.&lt;/p&gt;

&lt;p&gt;Now I won’t say that it’s impossible for teams to keep up their delivery of valuable software without following these practices. But I’ve never seen it work well. I wonder how it could. I mean, without simple design and constantly keeping the code clean, how well can code be changed, even in a few months from creating it? What raises a flag for a broken feature when lacking stable unit and acceptance test suites?&lt;/p&gt;

&lt;h2 id=&quot;scrum&quot;&gt;Scrum&lt;/h2&gt;

&lt;p&gt;It’s not fair to blame any of this this on Scrum. Scrum works well in a wide variety of teams and organizations.&lt;/p&gt;

&lt;p&gt;Scrum mostly embodies the planning and management rules of Extreme Programming (XP). I believe it’s thanks to Scrum that much of the agile planning and management practices have made their way into mainstream today.&lt;/p&gt;

&lt;p&gt;It’s just because Scrum doesn’t include the other Agile practices many folks doing Scrum think that those are somehow not valuable. The most successful teams I’ve seen are all doing most, if not all, of the other Agile practices as well. This is also totally possible for a Scrum team.&lt;/p&gt;

&lt;h2 id=&quot;youre-mileage-may-vary&quot;&gt;You’re mileage may vary&lt;/h2&gt;
&lt;p&gt;Over the past years I’ve been experimenting with other ways to write software. This is especially hard without TDD and simple design. While it’s good to experiment, every time I got back to the agile practices as they work best for me.&lt;/p&gt;

&lt;p&gt;You’re mileage may vary, obviously. If you use different practices that even better support the continuous delivery of valuable software I would love to hear about them and give them a try myself.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Lineman.js</title>
        <link href="https://blog.thesoftwarecraft.com/2014/03/linemanjs.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-03-15:/2014/03/linemanjs</id>
        <updated>2014-03-15T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;I think it’s fair to say that JavaScript is no longer that “thing” we used to enhance our otherwise static web apps by adding some dynamic elements to it. Most users use a JavaScript enabled browser and users that lack JavaScript support are simply outnumbered. Without JavaScript you cannot use a large number of online services and this number is growing.&lt;/p&gt;

&lt;p&gt;From time to time I’ve been playing around with JavaScript frameworks like Angular and Backbone. When you want to start an app with any of these frameworks - or in fact, any JavaScript app for that matter - you’ll quickly run into tools like &lt;a href=&quot;http://gruntjs.com/&quot; rel=&quot;external&quot;&gt;Grunt&lt;/a&gt; and &lt;a href=&quot;http://bower.io/&quot; rel=&quot;external&quot;&gt;Bower&lt;/a&gt;. If you don’t know what they are, go ahead an see what they’re about. I’ll be here, waiting patiently for your return. On the other hand, you don’t need full knowledge of these tools, so you might as well read on.&lt;/p&gt;

&lt;p&gt;The problem with starting an app like this is that it’s pretty harsh to set up. You may need configuration for compiling CoffeeScript and Less, a unit test library, something to run the tests, minimize JavaScript, run JSLint. The list may seem almost infinite.&lt;/p&gt;

&lt;h2 id=&quot;enter-lineman&quot;&gt;Enter Lineman&lt;/h2&gt;
&lt;p&gt;&lt;a href=&quot;http://linemanjs.com/&quot; rel=&quot;external&quot;&gt;Lineman&lt;/a&gt; is a thin wrapper around tools like Grunt and Test’em to take away the initial configuration burden from developers and let them focus on what they love most: building great apps.&lt;/p&gt;

&lt;p&gt;Justin Searls, the creator of Lineman did a &lt;a href=&quot;http://youtu.be/KERJkJNV5nI&quot; rel=&quot;external&quot;&gt;brilliant talk&lt;/a&gt; on what’s been wrong for years with building JavaScript apps. In this talk he gives an overview of some of the major problems you face with JavaScript development and how Lineman helps out. I’d recommend you to watch his talk.&lt;/p&gt;

&lt;p&gt;The difference with &lt;a href=&quot;http://yeoman.io/&quot; rel=&quot;external&quot;&gt;Yeoman&lt;/a&gt; (a competing tool) is that Lineman is not based on code generation. Where Yeoman generates your initial project with a full Grunt configuration and example code (which you don’t need) and some other stuff, Lineman installs itself as your Grunt configuration. The benefit of this is that you can update your tools without breaking the configuration (or manually updating it).&lt;/p&gt;

&lt;p&gt;With Lineman all you need to do to start building a new app is tell it to create one.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;lineman new &lt;span class=&quot;s2&quot;&gt;&quot;app-name&quot;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That’s it. It provides much like a walking skeleton for your application that you can run and test. Immediately you can enter your regular build cycle of changing code and have it automatically built and tested each time you save a file. If that’s not all, it’s blazingly fast. One of the goals the creators of Lineman set is for each incremental build and test run to take no more than a few hundred millis, independent of the size of the codebase. Now if that doesn’t stimulate TDD I don’t know what will.&lt;/p&gt;

&lt;p&gt;To start the build cycle all you need to do is to fire up &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lineman run&lt;/code&gt; in one shell and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lineman spec&lt;/code&gt; in another.&lt;/p&gt;

&lt;p&gt;Both of these watch your sources so your app is build and tested whenever you save a change. You need to keep both running because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lineman run&lt;/code&gt; compiles your sources and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lineman spec&lt;/code&gt; watches the built files for running your specs. It took me a little help to find this out (much thanks to Justin for helping out).&lt;/p&gt;

&lt;h2 id=&quot;framework-support&quot;&gt;Framework support&lt;/h2&gt;
&lt;p&gt;So what about framework support? Lineman creates a vanilla flavored app. This is the walking skeleton, but includes nothing like the framework support you can get from Yeoman.&lt;/p&gt;

&lt;p&gt;Personally this is the way I prefer to start an app. Any libraries and frameworks I need I will include myself. Not only gives this full control to what vendor libs I actually use, it also gives me full control to when I want to use them and gives me the opportunity to defer their inclusion if I don’t need them (yet).&lt;/p&gt;

&lt;p&gt;If you like to have some popular framework already included that’s okay, Lineman offers that too. All you need to do is to clone one of the Lineman template projects from Github and you’ll get a walking skeleton app with that framework. At the moment templates are provided for &lt;a href=&quot;https://github.com/linemanjs/lineman-backbone-template&quot; rel=&quot;external&quot;&gt;Backbone&lt;/a&gt;, &lt;a href=&quot;https://github.com/linemanjs/lineman-ember-template&quot; rel=&quot;external&quot;&gt;Ember&lt;/a&gt;, and &lt;a href=&quot;https://github.com/linemanjs/lineman-angular-template&quot; rel=&quot;external&quot;&gt;Angular&lt;/a&gt;, for building a &lt;a href=&quot;https://github.com/linemanjs/lineman-lib-template&quot; rel=&quot;external&quot;&gt;library&lt;/a&gt; or a &lt;a href=&quot;https://github.com/linemanjs/lineman-blog-template&quot; rel=&quot;external&quot;&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;whats-next&quot;&gt;What’s next?&lt;/h2&gt;
&lt;p&gt;At this point I just want to start building things. The lineman website covers a lot of the configuration that’s possible.&lt;/p&gt;

&lt;p&gt;Next to build and test configuration one of the first things I need in most apps is to be able to run end-to-end tests. I suspect Lineman can help me out with this too.&lt;/p&gt;

&lt;p&gt;I hope I’ll keep enjoying Lineman as much as I do in the first few hours of using it.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Fast Remote Service Tests</title>
        <link href="https://blog.thesoftwarecraft.com/2014/02/fast-remote-service-tests.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-02-25:/2014/02/fast-remote-service-tests</id>
        <updated>2014-02-25T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Testing code that interacts with remote services is often pretty hard. There are a lot of tradeoffs that influence what tests you can write and the amount of tests to write. Most of the times you have zero control over the data you get from the service, which makes assertions tough to say the least.&lt;/p&gt;

&lt;p&gt;A while ago I used the VCR library to write some Ruby tests against a remote service. VCR addresses the above problems. It records your test suite’s HTTP interactions to replay them during future runs. The obvious benefits are fast and repeatable tests.&lt;/p&gt;

&lt;p&gt;This week I was wondering whether that’s a thing for Java as well. As it turns out there’s &lt;a href=&quot;http://freeside.co/betamax/&quot; rel=&quot;external&quot;&gt;Betamax&lt;/a&gt; to do that. Actually Betamax is a Groovy port of VCR that can be used with any JVM language.&lt;/p&gt;

&lt;p&gt;Betamax installs a proxy in between you and the target host, records each request and response on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tape&lt;/code&gt; and replays the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tape&lt;/code&gt; for known requests. It works for any HTTP client that respects Java’s proxy settings, and for a bunch that don’t such as Apache HttpClient and WSLite.&lt;/p&gt;

&lt;h2 id=&quot;example&quot;&gt;Example&lt;/h2&gt;
&lt;p&gt;In a JUnit test you can use Betamax as a method-level &lt;a href=&quot;http://junit-team.github.io/junit/javadoc/latest/org/junit/rules/TestRule.html&quot; rel=&quot;external&quot;&gt;&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;TestRule&lt;/code&gt;&lt;/a&gt;. On each test-method that should record and replay you put an &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Betamax&lt;/code&gt; recorder and set a tape.&lt;/p&gt;

&lt;p&gt;Consider the following example where I use the &lt;a href=&quot;https://developer.spotify.com/technologies/web-api/search/&quot; rel=&quot;external&quot;&gt;Spotify Metadata API&lt;/a&gt; to get the popularity of an artist. In this example I use the Apache HttpClient library and configure it for Betamax.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;SpotifyTest&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;@Rule&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Recorder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recorder&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Recorder&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;kd&quot;&gt;private&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultHttpClient&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;http&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;DefaultHttpClient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Betamax&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;tape&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;fixtures/popularity&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;get_popularity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;Spotify&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;spotify&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Spotify&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;assertThat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;spotify&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;popularity&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;The Beatles&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;is&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(.&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;55&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@Before&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;setUp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;BetamaxRoutePlanner&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;http&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;At the moment of writing this code the popularity of The Beatles is .55 but as this number is based on user opinion it is highly likely to change. Using a Betamax tape gets the same response (as long as the request does not change) and allows to assert .55 for popularity.&lt;/p&gt;

&lt;h2 id=&quot;https&quot;&gt;HTTPS&lt;/h2&gt;
&lt;p&gt;As I’ve shown you Betamax properly records and replays any HTTP communication using either a proxy or a wrapper class (as in the example). HTTPS is also supported but may be a bit more interesting as you use Betamax in a proxy-based setup. Using a wrapper will work just fine.&lt;/p&gt;

&lt;p&gt;The problem with HTTPS and a proxy-based setup obviously is that the proxy cannot intercept data on standard HTTPS communication. This is why we trust HTTPS.&lt;/p&gt;

&lt;p&gt;Betamax has its way around this. You can enable &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;sslSupport&lt;/code&gt; on the Betamax &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Recorder&lt;/code&gt;. When your client code is okay with a broken SSL certificate chain you can make this work.&lt;/p&gt;

&lt;p&gt;Again this is only really a problem as you use a proxy-based setup. Using a client wrapper enables Betamax directly on API calls easing HTTPS communication.&lt;/p&gt;

&lt;h2 id=&quot;try-it-yourself&quot;&gt;Try it yourself&lt;/h2&gt;
&lt;p&gt;Betamax can help you to write fast and repeatable unit tests for clients of remote services. The most beneficial to me is that the tests are really fast because remote communication is eliminated. Asserting on specific values can be helpful although personally I like a property-based style for these tests (e.g. popularity must be a number &amp;gt;= 0 and &amp;lt;= 5).&lt;/p&gt;

&lt;p&gt;Give Betamax a try the next time you interact with a remote service.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Code Katas</title>
        <link href="https://blog.thesoftwarecraft.com/2014/02/code-katas.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-02-10:/2014/02/code-katas</id>
        <updated>2014-02-10T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;In this post I want to talk about code katas. Most of you have heard of code katas before, many of you  probably have done some katas. A while ago code katas were getting a fair bit of attention. Including of course some skepticism.&lt;/p&gt;

&lt;p&gt;Just in case you haven’t heard of code kata yet, let me explain briefly. The term code kata is coined by Dave Thomas, co-author of &lt;a href=&quot;https://amzn.to/4ojlAya&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;The Pragmatic Programmer&lt;/a&gt;. It’s a bow to the kata concept in martial arts. Kata is a Japanese word and literally translates to form. Originally kata was a training method to help a student master a specific move or technique.&lt;/p&gt;

&lt;p&gt;The intention of code katas is to help programmers to do just that: master a specific move or technique. To practice these we use simple algorithms that we can dream up at any moment. Think of sorting and prime factor calculation.&lt;/p&gt;

&lt;p&gt;So what’s effective about these code katas? I mean coding up the same algorithm time after time just doesn’t seem like you practice any technique. Except maybe your typing skills. This is why skepticists will tell you that katas are not effective and no more than a waste of time. Solving the same set of programming problems doesn’t make you a master programmer.&lt;/p&gt;

&lt;p&gt;And they are spot on. Solving the same set of problems with (almost) the exact same code time after time doesn’t help a bit towards mastery.&lt;/p&gt;

&lt;p&gt;Yet I totally believe that katas can be a very effective way to practice. Just not by coding up a solution to a problem in the same way over and over again. On the path towards mastery you will have to find many ways of solving problems, not just one. A kata is not about the solution to the problem. It’s about the path of how you get to the solution.&lt;/p&gt;

&lt;p&gt;How many ways can you think of to calculate a Fibonacci sequence? In what forms can you code a bubble sort? And a quick sort? There’s no need to learn these algorithms, you probably can dream them already. Instead you focus on the code you use to get you there. You’ll walk along a certain path until you master it. You make little tweaks to the way you code up the solution along the way. You carefully consider every step you take.&lt;/p&gt;

&lt;p&gt;To me this is what katas are really about. I often use katas to practice and learn. Again, not about the solution, but about the path. My path. The way I code. Katas help me to challenge each of my steps along the way. Is this next step the best I can think of? Why this step? Why not another? By challenging every step on my way it’s almost like pairing with myself. As a navigator I can pick up important feedback.&lt;/p&gt;

&lt;p&gt;Then I find code katas also particularly useful for learning a new language. The famous Hello World asserts that my installation works and I can write a main. Next I want to feel the language. By doing a few katas I can get a decent grasp on its syntax. Then I want to explore some of the language’s unique features. Because I’ve seen many solutions to the katas in various languages I can get a feeling of what the language has to offer.&lt;/p&gt;

&lt;p&gt;That said, katas are not the only way to practice and to learn. Many programming languages now have a set of koans available, to help you learn the language. I find these are very useful, too. And most katas are useless for learning more about a particular library of framework. Which is not the point of katas anyway.&lt;/p&gt;

&lt;p&gt;As you can read, in my experience code katas help me practice and learn. Yet I completely agree that doing katas by coding up the same solution over and over again, is a waste of energy. I want to reiterate that it’s not about the solution here, it’s about the path. Most of the time I delete my code directly after I finish a kata. The lessons I learn from walking the path remain in my head and I won’t ever look back at the solution. The exception here is the first few katas I do in a completely new language. Before I have the syntax in my fingertips, sometimes I find it useful to look back at the previous exercises.&lt;/p&gt;

&lt;p&gt;If you’re interested there are a lot different katas out there. Google is your friend. To get started you can use this &lt;a href=&quot;http://codingdojo.org/cgi-bin/wiki.pl?KataCatalogue&quot; rel=&quot;external&quot;&gt;Kata Catalog&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Acceptance Test Driven Development</title>
        <link href="https://blog.thesoftwarecraft.com/2014/01/acceptance-test-driven-development.html"/>
        <id>tag:blog.thesoftwarecraft.com,2014-01-21:/2014/01/acceptance-test-driven-development</id>
        <updated>2014-01-21T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;In this short series about acceptance testing I previously wrote about collaboration between testers and developers and how acceptance tests help to define clear requirements of a system.&lt;/p&gt;

&lt;p&gt;Where the previous article were on the theoretical side, let’s now take a look at using acceptance testing as a practice in our development process. I prefer using Acceptance Test Driven Development, or ATDD.&lt;/p&gt;

&lt;h2 id=&quot;in-a-nutshell&quot;&gt;In a Nutshell&lt;/h2&gt;
&lt;p&gt;With ATDD you use acceptance tests to guide the implementation path of a feature. You write a failing test scenario and the required glue code. When the scenario fails with the correct message (i.e. the test tool can run the acceptance test and the message correctly describes the missing feature) you start implementing the feature.&lt;/p&gt;

&lt;p&gt;Like in TDD use the simplest way possible to make the scenario pass, but on a feature level. In TDD you would pass a test in the simplest possible way and then improve from there. In ATDD you just implement the feature in the simplest way possible to pass the scenario. Simply stated, if you’ve got the right set of scenarios the feature is implemented when the last one passes.&lt;/p&gt;

&lt;p&gt;[Note that I deliberately chose not to use the word &lt;em&gt;done&lt;/em&gt;, as after implementing the feature often we need to fiddle with the UI a bit more.]&lt;/p&gt;

&lt;h2 id=&quot;example-problem&quot;&gt;Example Problem&lt;/h2&gt;
&lt;p&gt;For an example we’re going to build a shopping cart for Harry Potter book sale kata using ATDD. Customers get a discount based on the number of books in the series they buy. This exercise is taken from a list of kata from the Coding Dojo website. For a full description you can read: &lt;a href=&quot;http://codingdojo.org/cgi-bin/wiki.pl?KataPotter&quot; rel=&quot;external&quot;&gt;http://codingdojo.org/cgi-bin/wiki.pl?KataPotter&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The first 5 books from the Harry Potter series are on sale. One copy of any of the books costs $8. If you buy two different books from the series you get a 5% discount, if you buy three different books you get a 10% discount, if you buy 4 different books your discount is 20%, and if you buy all 5 you get a 25% discount.&lt;/p&gt;

&lt;p&gt;Let’s pair up and build this feature together.&lt;/p&gt;

&lt;h2 id=&quot;discuss-and-distill&quot;&gt;Discuss and Distill&lt;/h2&gt;
&lt;p&gt;The cycle for building a new feature goes through a few phases. Before we think about writing production code we have to get ourselves some scenarios. Remember how we talked about the team meeting with a business expert in the &lt;a href=&quot;/2013/12/acceptance-tests-as-specifications.html&quot;&gt;previous article&lt;/a&gt;? These activities form the first phases in the development cycle: discuss and distill.&lt;/p&gt;

&lt;p&gt;Our team arranges a meeting with a business expert to discuss the shopping cart. We have prepared some questions, so this meeting can be very effective. Our team knows better than to discuss technical details in this meeting. We do not want to waste any of the business expert’s valuable time. At the end of the meeting we have taken note of examples describing the behavior of the shopping cart. For clarity we decide to list the books using roman numerals.&lt;/p&gt;

&lt;table class=&quot;table table-striped&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;I&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;II&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;III&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;IV&lt;/th&gt;
      &lt;th style=&quot;text-align: center&quot;&gt;V&lt;/th&gt;
      &lt;th style=&quot;text-align: left&quot;&gt;Formula&lt;/th&gt;
      &lt;th style=&quot;text-align: right&quot;&gt;$&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;1 * 8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;8.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2 * 8 * 0.95&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;15.20&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;3 * 8 * 0.9&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;21.60&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;4 * 8 * 0.8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;25.60&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;5 * 8 * 0.75&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;30.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2 * 8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;16.00&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt; &lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;2 * 8 * 0.95 + 1 * 8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;23.20&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;2&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: center&quot;&gt;1&lt;/td&gt;
      &lt;td style=&quot;text-align: left&quot;&gt;4 * 8 * 0.8 + 4 * 8 * 0.8&lt;/td&gt;
      &lt;td style=&quot;text-align: right&quot;&gt;51.20&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Most of these lines are pretty much self-explanatory. The last line shows that there are 2 sets of 4 books. This might be surprising because you’d expect a set of 5 books plus a set of 3 books. The reason for this is that the discount must be optimized for the customer. The best possible discount in this scenario is to purchase 2 sets of 4 books as that is $0.40 cheaper than a set of 5 books and a set of 3 books.&lt;/p&gt;

&lt;p&gt;After the meeting we distill acceptance tests from these examples and save them as Cucumber scenarios. The scenario for the first example reads:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-gherkin&quot; data-lang=&quot;gherkin&quot;&gt;&lt;span class=&quot;kn&quot;&gt;Scenario&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; buy single book without discount

  &lt;span class=&quot;nf&quot;&gt;When &lt;/span&gt;I buy 1 copy of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter I&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Then &lt;/span&gt;I must pay $8&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The scenario for the second example turns out to be much alike the first one:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-gherkin&quot; data-lang=&quot;gherkin&quot;&gt;&lt;span class=&quot;kn&quot;&gt;Scenario&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; buy two distinct books

  &lt;span class=&quot;nf&quot;&gt;When &lt;/span&gt;I buy 1 copy of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter II&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Then &lt;/span&gt;I must pay $15.20&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Obviously the remaining scenarios can be added in the same way. But a pattern is emerging. It would be pretty tedious and repetitive to have to copy and paste the exact same scenarios only to use different values. Besides, it would be nice if the example table we took from meeting with the business expert could be used in the acceptance test as well. That would make our scenarios more useful as documentation.&lt;/p&gt;

&lt;p&gt;Cucumber provides a Scenario Outline feature to help out. Scenario Outlines more concisely express these examples through the use of a template with placeholders and an example table. We try a scenario outline for the former two scenarios and then decide to add the rest of the scenarios using the outline.&lt;/p&gt;

&lt;p&gt;Scenario Outline: buy discounted Harry Potter books&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-gherkin&quot; data-lang=&quot;gherkin&quot;&gt;&lt;span class=&quot;nf&quot;&gt;When &lt;/span&gt;I buy &lt;span class=&quot;nv&quot;&gt;&amp;lt;I&amp;gt;&lt;/span&gt; copies of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter I&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;And &lt;/span&gt;I buy &lt;span class=&quot;nv&quot;&gt;&amp;lt;II&amp;gt;&lt;/span&gt; copies of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter II&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;And &lt;/span&gt;I buy &lt;span class=&quot;nv&quot;&gt;&amp;lt;III&amp;gt;&lt;/span&gt; copies of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter III&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;And &lt;/span&gt;I buy &lt;span class=&quot;nv&quot;&gt;&amp;lt;IV&amp;gt;&lt;/span&gt; copies of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter IV&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;And &lt;/span&gt;I buy &lt;span class=&quot;nv&quot;&gt;&amp;lt;V&amp;gt;&lt;/span&gt; copies of &lt;span class=&quot;s&quot;&gt;&quot;Harry Potter V&quot;&lt;/span&gt;
  &lt;span class=&quot;nf&quot;&gt;Then &lt;/span&gt;I must pay $&lt;span class=&quot;nv&quot;&gt;&amp;lt;Total&amp;gt;&lt;/span&gt;

&lt;span class=&quot;nn&quot;&gt;Examples&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;I&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;II&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;III&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;IV&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;V&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Total&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;8.00&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;15.20&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;21.60&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;25.60&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;30.00&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;16.00&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;0&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;23.20&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;   &lt;span class=&quot;n&quot;&gt;2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;  &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;1&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;51.20&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;|&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That’s more like it. Using the outline our examples read just like the table we draw earlier.&lt;/p&gt;

&lt;p&gt;Obviously we’re not done at this stage, although we have successfully discussed the feature and distilled acceptance scenarios that discussion. We now have a complete set of acceptance scenarios. Our scenarios are a complete representation of the problem according to the business expert. The notation format of the scenarios is simple enough for the business expert to review them.&lt;/p&gt;

&lt;p&gt;Now our team is ready for the implementation phase.&lt;/p&gt;

&lt;h2 id=&quot;implement&quot;&gt;Implement&lt;/h2&gt;
&lt;p&gt;First thing we need is the glue code so that examples can be run and fail with a reasonable error message. Luckily Cucumber helps us creating the necessary glue functions by generating stubs. After a first run without glue code we can copy and paste to get to pending steps.&lt;/p&gt;

&lt;p&gt;An example of such a generated function for Java is listed below. This is the glue code for a single step in a Cucumber scenario, also known as a step definition.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;When&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;^I buy (\\d+) copies of \&quot;([^\&quot;]*)\&quot;$&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;I_buy_copies_of&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arg2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Throwable&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;// Express the Regexp above with the code you wish you had&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;throw&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;PendingException&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;();&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now that all steps are pending we start implementing the steps to make the first scenario fail with the appropriate messages. To get these steps to compile we need to add some skeleton production code, which we’ll later enhance with the implementation. This is interesting because at this time the API &lt;a href=&quot;/2013/11/wishful-programming.html&quot;&gt;we wish we’d have&lt;/a&gt; flows rather natural from the acceptance tests.&lt;/p&gt;

&lt;p&gt;When the first scenario fails with a message that describes the missing feature, we implement the feature. Then we move on to the next, and so on, until all features pass. We keep our code clean by using TDD.&lt;/p&gt;

&lt;p&gt;The actual implementation of the step definitions and the code isn’t that interesting for the sake of this article. As I don’t wish to withold these details as well I did the exercise and put the &lt;a href=&quot;https://github.com/bjpbakker/harry-potter-kata-atdd&quot; rel=&quot;external&quot;&gt;code on Github&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;review&quot;&gt;Review&lt;/h2&gt;
&lt;p&gt;The last phase of building a new feature is to review it. I’m not talking about code reviews here, these take place in the implementation phase.&lt;/p&gt;

&lt;p&gt;To wrap up our new feature we do some exploratory testing. Exploratory testing is finding out how the system actually works by using it freestyle. I.e. there are not test scripts to follow. Instead test cases are invented on the fly and immediately executed against the system. If you wish to learn more about exploratory testing I can recommend you to read Elisabeth Hendrickson her fantastic book &lt;a href=&quot;https://amzn.to/42GuD3Q&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Explore It!&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our review phase we write down any inconsistencies we find through exploratory testing.&lt;/p&gt;

&lt;p&gt;To finish our feature we demo it to the business experts and collect their feedback. Combined with any results from exploratory testing, this is important input for business experts. Only they can decide how the system should behave.&lt;/p&gt;

&lt;h2 id=&quot;more-examples&quot;&gt;More Examples&lt;/h2&gt;
&lt;p&gt;Personally I find ATDD a very pleasant way for the entire team to work. While having the benefits of clear requirements you enjoy the fact that you grow an automated regression suite that you can (and should) run every build. This regression suite gains the team a lot of confidence not to be afraid to make changes anywhere. As soon as something breaks, a test will fail, and the team knows what to fix.&lt;/p&gt;

&lt;p&gt;If you like to see more examples of ATDD you should read &lt;a href=&quot;https://amzn.to/3KLvBpi&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;ATDD by Example&lt;/a&gt;, in which Markus Gärtner pairs up with you to practice ATDD while working example problems.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Acceptance Tests as Specifications</title>
        <link href="https://blog.thesoftwarecraft.com/2013/12/acceptance-tests-as-specifications.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-12-18:/2013/12/acceptance-tests-as-specifications</id>
        <updated>2013-12-18T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;In my previous &lt;a href=&quot;/2013/11/acceptance-testing-blaming-tools.html&quot;&gt;article about acceptance testing&lt;/a&gt; I wrote about how tools like FitNesse and Cucumber focus on collaboration between developers and testers. Of course that’s not all these tools help us with. In this post we’ll take a deeper look at acceptance testing as a practice and see what the tools gain us.&lt;/p&gt;

&lt;p&gt;Acceptance Testing is an agile practice that refers to functional testing a user story. Sometimes it is referred to as Agile Acceptance Testing or Specification by Example. Acceptance testing addresses one of the most significant problems in software development: the requirements.&lt;/p&gt;

&lt;h2 id=&quot;requirements&quot;&gt;Requirements&lt;/h2&gt;
&lt;p&gt;Often requirements don’t come across to the team members the way business people intended them to. There are many reasons for this to happen. Often these reasons can be reduced down to the team having a different context than the business people. Because the team often has only a basic understanding of the business world, the team easily misinterprets the ambiguous requirements.&lt;/p&gt;

&lt;p&gt;This problem isn’t limited to agile methodologies. The problem is more persistent, for a few decades already. Remember those classical waterfall projects? For sure the requirements weren’t communicated briefly like user stories. They were however written from the perspective of business people and often got misinterpreted further down the production chain. By the time business people could see the system and verify that it didn’t match what they had in mind, it was too late. Many of these projects were canceled as a result, with lots of wasted money as a result. Some of these projects even caused bankruptcy.&lt;/p&gt;

&lt;p&gt;The agile planning game (today known as Scrum) helped reducing the impact of this problem. Its short feedback loops ensure that business people in an early stage will become aware of discrepancies between their world and the system as it gets build. In case the team got it wrong, only one sprint of work is wasted. Now that’s still ridiculously expensive. Nobody wants to throw away an entire sprint’s work only because the requirements were poorly communicated. But on the plus side, most projects will survive one such a sprint.&lt;/p&gt;

&lt;p&gt;Another approach to fixing the requirements problem is to reduce the level of ambiguity in the requirements. We could use a formal language to write a specification of the requirements. Before you tell me that won’t work or is simply not practical, let me explain. We don’t need math precision. We need to reduce the level of ambiguity just enough so that humans will understand. And conveniently enough on most projects such a formal specification is already created. It’s called the &lt;em&gt;test plan&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;test-plan&quot;&gt;Test Plan&lt;/h2&gt;
&lt;p&gt;The test plan defines the inputs to the system, the actions on the system, and the expected outputs in a rather formal manner. Thereby reducing the level of ambiguity enough so that humans will understand.&lt;/p&gt;

&lt;p&gt;By executing the test plan against the system QA gets feedback about any gaps between the behavior of the system (the interpretation of the developers) and their own interpretations. Agile reduces the amount of differences by putting developers and testers in the same team, but still QA writes the test plan while developers implement the feature. At best the team finds certain differences in interpretation &lt;em&gt;while&lt;/em&gt; implementing.&lt;/p&gt;

&lt;p&gt;Compared to business people finding that the team didn’t implement ‘the right thing’ after a sprint, having the team to find this herself within the sprint is much, much better. But it causes rework nonetheless and that takes up some of our valuable time.&lt;/p&gt;

&lt;p&gt;What if we could learn these gaps &lt;em&gt;before&lt;/em&gt; implementing a feature? That could reduce the rework even further and get the feature even closer to right from the start. Note that I am definitely not claiming that the team shouldn’t make any mistakes, they will. But given an opportunity to prevent certain mistakes by early learning, we should take it.&lt;/p&gt;

&lt;h2 id=&quot;specification-by-example&quot;&gt;Specification by Example&lt;/h2&gt;
&lt;p&gt;So business people write a bunch of stories. The team does some form of estimation of each story’s size and takes some stories in sprint. At this point the team doesn’t know all the details yet. To find out they can organize a meeting with a business expert and ask her any questions to learn these details. This is where the team discusses for example a complex business algorithm or details about the UI. Together with the business expert the team creates a set of examples and acceptance criteria.&lt;/p&gt;

&lt;p&gt;[Note that it is helpful if all team members attends these meetings to gain a shared understanding of the feature. If not, make sure to form a delegation of at least one developer and one tester.]&lt;/p&gt;

&lt;p&gt;The team records the examples and acceptance criteria as test scenarios. Preferably the business expert reviews the scenarios afterwards, to assert the outcome of their meeting. This is the point where the tools pop back in. Tools like FitNesse and Cucumber use a simple yet formal language for defining test scenarios and example tables. At this stage the benefit of using a tool would be that you don’t have to invent such a language yourself.&lt;/p&gt;

&lt;p&gt;The entire team now can read the what the feature is supposed to do and is on the same page. The first thing the developers (or sometimes the testers) do after the scenarios are written is to create some glue code. This code binds (glues) the scenarios to the system to allow the test tool to run the scenarios against the system and verify the outputs.&lt;/p&gt;

&lt;h2 id=&quot;power-of-tools&quot;&gt;Power of Tools&lt;/h2&gt;
&lt;p&gt;This makes these test tools really powerful. The test scenarios can be executed any time we wish, at high speed. Writing test scenarios and glue code are typically tasks for humans, but humans are terrible at consistently repeating the same tasks, making them extremely inefficient test execution platforms. Computers on the other hand are very effective at consistently repeating tasks, which makes them well-suited as an execution platform for our test scenarios.&lt;/p&gt;

&lt;p&gt;Not only the test scenarios help building a shared understanding of a feature before the implementation. Every team member can (and should) read the scenarios, as can the business stakeholders. The team is not done implementing until all scenarios pass.&lt;/p&gt;

&lt;p&gt;Because the scenarios can be executed by computers, they make for an excellent regression suite. We can have the computer run our scenarios every build, or at least a few times a day. As the team gets feedback from the test suite quickly after they change something, they can catch regression bugs in an early stage.&lt;/p&gt;

&lt;p&gt;This may all sounds just too theoretical. Therefore I will continue on acceptance testing with a &lt;a href=&quot;/2014/01/acceptance-test-driven-development.html&quot;&gt;more practical post&lt;/a&gt; about using the practices in our development process.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Acceptance Testing: Blaming the Tools</title>
        <link href="https://blog.thesoftwarecraft.com/2013/11/acceptance-testing-blaming-tools.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-11-29:/2013/11/acceptance-testing-blaming-tools</id>
        <updated>2013-11-29T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;About 5 years ago I was on a project to build a system for collateral management. The system was connected to a large financial network, and got its instructions through standardized financial messages.&lt;/p&gt;

&lt;p&gt;This project was run in waterfall style, with lots of restrictions on collaboration between disciplines. Without to digress in details on this, I do still think it’s interesting that those project managers, even the experienced ones, believe that collaboration is bad. Anyhow, a team of business analysts wrote down the detailed specifications, then handed them over to the developers. The developers implemented the system according to the specifications and then delivered the software to the team of testers.&lt;/p&gt;

&lt;h2 id=&quot;testing-the-system&quot;&gt;Testing the system&lt;/h2&gt;
&lt;p&gt;Because this particular system was operated through all these financial messages, it was much harder to test than a system with a regular user interface. The developers created a testing tool to interact with the system using a command line interface. By the nature of its command line interface the tool was also fit for scripting tests for entire business flows. The tool was low-level, just as the developers needed it to be.&lt;/p&gt;

&lt;p&gt;The test team also needed a tool to interact with the system. The tool used by the developers was too low-level for the testers. One of the testers knew a tool he had used before to describe tests in a language that is more friendly to most people, and then having that tool link the friendly language to code. This tool was &lt;a href=&quot;http://fitnesse.org/&quot; rel=&quot;external&quot;&gt;FitNesse&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To be able to interact with the main system from FitNesse, an adapter was added to the test tool the developers used. This was a fixture to get FitNesse to run the test tool. Some additional features were added to the test tool as well, to allow the testers to create template messages and then set specific values using xpath expressions in their FitNesse test tables.&lt;/p&gt;

&lt;h2 id=&quot;making-changes&quot;&gt;Making changes&lt;/h2&gt;
&lt;p&gt;This approach went well for about 2 months. Within this time the test team had added a FitNesse plugin to connect to the system’s database to verify some of the system’s intermediate state, and had added a few layers of indirection to the FitNesse tests so it wasn’t really clear what operations were performed on the system from the ‘friendly’ specifications in the test tables.&lt;/p&gt;

&lt;p&gt;And then the type of financial messages to support changed. This influenced all the message templates and all of the xpaths that were used so far. You can imagine that this touched almost every test, causing tons of rework on the test team. Because they couldn’t get it done fast enough, they got help from the development team.&lt;/p&gt;

&lt;p&gt;A short while after the tests were all ‘migrated’ to use the new message structure, the database schema changed. And it didn’t change subtle either. Again most of the acceptance tests failed and needed lots of rework.&lt;/p&gt;

&lt;h2 id=&quot;blaming-the-tool&quot;&gt;Blaming the tool&lt;/h2&gt;

&lt;p&gt;Boy, we really started to hate this FitNesse tool. Every time the system evolved all the tests started to fail. And to fix them, developers were to help the testers rework those tests. We could think of a nicer task or two.&lt;/p&gt;

&lt;p&gt;It was not until a few weeks after I left the project that I realized that FitNesse was not to blame for the problems we faced. We were to blame ourselves! It wasn’t the tool that was wrong, it were the people who used it wrong. It was on the Advanced Test-Driven Development masterclass by Uncle Bob (Robert Martin) where I learned that the purpose of FitNesse isn’t to provide a textual abstraction to running your acceptance tests.&lt;/p&gt;

&lt;h2 id=&quot;collaboration&quot;&gt;Collaboration&lt;/h2&gt;

&lt;p&gt;Don’t get me wrong, that textual abstraction is really nice and all. But it shouldn’t be the primary goal.&lt;/p&gt;

&lt;p&gt;FitNesse is a tool for automated acceptance testing, with the focus being collaboration between business people, developers and testers. Through collaboration a team of developers and testers can together learn all about the features, each bringing their own skills and qualities to the discussion. Together they register their learnings as a test table or brief sentences.&lt;/p&gt;

&lt;p&gt;As I mentioned earlier collaboration wasn’t one of the focal points on this project. Instead it was something you ‘do not waste your time upon’.&lt;/p&gt;

&lt;p&gt;Where in this particular project the problems appeared to be around FitNesse, I believe it’s the same thing for many of the Cucumber stories I read. On the internet there are a lot of opinions about how Cucumber (and likewise tools) add no value to a project. I do believe that this is true in many projects, but for different reasons than projected in most of those articles.&lt;/p&gt;

&lt;p&gt;Tools like Cucumber and FitNesse are not developer tools. If no-one but the developers read and write these scripts, using such tools is no more than a waste of time. Developers can faster automate acceptance tests using their regular test framework (e.g. rspec or junit).&lt;/p&gt;

&lt;p&gt;But these tools neither are QA tools. Only when working together, testers and developers can get most out of these tools and use them for real good. Both Cucumber and FitNesse are collaboration tools. They belong in the toolbelt of agile teams.&lt;/p&gt;

&lt;h2 id=&quot;to-be-continued&quot;&gt;To be continued&lt;/h2&gt;
&lt;p&gt;Of course collaboration is only the beginning of automated acceptance testing. In the next weeks I will get back to other aspects of automated acceptance testing.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Wishful Programming</title>
        <link href="https://blog.thesoftwarecraft.com/2013/11/wishful-programming.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-11-20:/2013/11/wishful-programming</id>
        <updated>2013-11-20T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Wishful Programming (or: Programming by Wishful Thinking) is something I heard about for the first time a couple of years ago. It is a very powerful programming practice, but I found many programmers are unaware of it.&lt;/p&gt;

&lt;p&gt;Wishful thinking is a general technique to help you think in abstractions. As far as I know it was first mentioned in the context of programming in &lt;a href=&quot;https://amzn.to/4n1v5AY&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Structure and Interpretation of Computer Programs&lt;/a&gt;. This brilliant book suggests to defer the decision about how to represent your data by imagining you have any function you wish you had in order to solve your problem in the simplest way possible.&lt;/p&gt;

&lt;p&gt;In other words, you write some code that &lt;em&gt;uses&lt;/em&gt; a component, before you &lt;em&gt;implement&lt;/em&gt; the component. This helps to discover what functions and data you need, and following this discovery will lead you to more simple and useful APIs.&lt;/p&gt;

&lt;p&gt;I find that this technique is especially valuable with an &lt;strong&gt;outside-in&lt;/strong&gt; approach for implementing a feature. To start with, it helps writing acceptance tests in terms of business functions. Details about how the system implements these functions are hidden from the context of the acceptance tests and implemented in a fixture.&lt;/p&gt;

&lt;p&gt;Once the acceptance test reads pretty much like the high-level steps you would take to execute the feature and fails with a decent error message it’s time to implement the feature. I drive out the design using TDD, starting at the point where the acceptance test fixture integrates with the production code. This allows me to design any collaborators right from the tests in the form I want them to collaborate, without worrying about creating them first.&lt;/p&gt;

&lt;p&gt;In other words, I write acceptance tests using a fixture I wish I had, and then implement the feature using collaborators I wish I had.&lt;/p&gt;

&lt;p&gt;That is wishful programming. I’ve seen how this helped my code to evolve and be more structured in terms of the solution domain, rather than a sequence of technical steps. I hope being aware of this technique will help you in the same way.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Lambdas for Fluent and Stable APIs</title>
        <link href="https://blog.thesoftwarecraft.com/2013/11/lambdas-for-fluent-and-stable-apis.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-11-19:/2013/11/lambdas-for-fluent-and-stable-apis</id>
        <updated>2013-11-19T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;A few weeks ago I wrote an &lt;a href=&quot;/2013/10/introduction-to-java-lambdas.html&quot;&gt;introduction on Java 8 lambdas&lt;/a&gt;. In this introduction I explained what a lambda is and how to use them in conjunction with the new Stream API, that is also introduced in Java 8.&lt;/p&gt;

&lt;p&gt;The Stream API provides a more functional interface to collections. This interface heavily depends on lambdas. But there’s much more to lambdas than improved collection processing&lt;/p&gt;

&lt;p&gt;Lambdas offer you a chance to build much more fluent APIs. To show this, as an example I like to use a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserStore&lt;/code&gt; which facilitates fetching and saving users using a database. Its public API commonly looks like the following.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserStore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;findByLastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;findByCompany&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;company&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;findBy&lt;/code&gt; methods often is longer than just the two I’ve included here. As the system grows it’s likely that there will be others. While that works, the thing is, all these methods basically do the same thing. They return all the users with a property that matches a specific value.&lt;/p&gt;

&lt;p&gt;Some frameworks offer work-arounds for this mess. If you’ve used Hibernate you’re probably aware that they provide a work-around to this with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;findByExample&lt;/code&gt; where you provide a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt; as an example object that provides the properties and values to query by. Any value that is set in this example object is used to query by, while any field that is &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; is excluded from the query. You can tweak this behavior a little, but there are a number of problems with this approach. Think of default values, required fields (i.e. fields that cannot be made &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;), and immutability. iBatis, MyBatis, and also Spring Data, use code generation to save you time implementing all these methods, leaving the API bloated with a list of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;findBy&lt;/code&gt; methods.&lt;/p&gt;

&lt;p&gt;These work-arounds might come a long way, but they do leave their own specific problems behind. A different approach would be to use lambdas.&lt;/p&gt;

&lt;p&gt;Lambdas can help us to decouple the query part from the filter specification. Let’s change the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;findBy&lt;/code&gt; functions to a single function that accepts a lambda.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserStore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;findBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Predicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That’s a much better API. Obviously it’s a bit naive as the predicate checks a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;User&lt;/code&gt; object; you generally want to filter using your database query. Nonetheless it serves the purpose of this example pretty well, and you can experiment with your own lambdas to filter using database queries. [Note: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Predicate&lt;/code&gt; comes with Java 8 and is located in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.util.function&lt;/code&gt; package.]&lt;/p&gt;

&lt;p&gt;Before you might want to get mad because at least with the previous API the predicates were bundled in one place, we can still bundle (common) predicates. For example, by creating a utility class &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserPredicates&lt;/code&gt; that contains them.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;final&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserPredicates&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Predicate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;lastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;candidate&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;matcher&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;equals&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;candidate&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getLastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;());&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Using the new &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserStore&lt;/code&gt; API has become pretty simple.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;static&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;UserPredicates.lastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;..&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;userStore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;findBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;lastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;lastname&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;There one thing left in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;UserStore&lt;/code&gt; that really bothers me, though. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find(id)&lt;/code&gt; function returns a user. But what if there’s no such user?&lt;/p&gt;

&lt;h2 id=&quot;optional&quot;&gt;Optional&lt;/h2&gt;
&lt;p&gt;To improve on this we can (and should) look at another new feature of Java 8, Optional. This is sort of the Java implementation of the maybe monad. It looks a lot like Scala’s &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Option&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;With &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; we can better express that a function may return a value but not necessarily, and prevent using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;. In our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find(id)&lt;/code&gt; example returning &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; clearly expresses that we might find a user with the requested ID, but maybe no such user exists.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;UserStore&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Long&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;findBy&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Predicate&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;p&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Not only does the API now document the fact that you might get a user, it also &lt;em&gt;never&lt;/em&gt; returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;. I consider an API that never returns &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; much safer. One day a new programmer might not realize that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;find&lt;/code&gt; can return &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt; and a null-pointer exception is the result. Just hopefully the team catches it before production. Null-pointer exceptions are really easy to prevent, as long as you never use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;null&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We can use functions on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; to get a value from user if available, or to get a default value otherwise. For example, to safely get the user’s lastname we write the following.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userStore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;lastname&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;User:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getLastname&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;orElse&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This code is pretty expressive and doesn’t require a lot of explaination. If there’s a user, get it’s lastname. Otherwise, get an empty string.&lt;/p&gt;

&lt;p&gt;What if we need to send the user a password-reset email, if it is found?&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;User&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;userStore&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;find&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;id&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;ifPresent&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;passwordReset:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The password-reset is sent if a user is found, otherwise nothing happens.&lt;/p&gt;

&lt;p&gt;Since Java does not support destructuring like many other languages that provide maybe (e.g. Haskell, Clojure, and Scala), we are limited to the functions of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt;. This makes &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; weaker than its equivalent in any of these languages.&lt;/p&gt;

&lt;h2 id=&quot;builder&quot;&gt;Builder&lt;/h2&gt;
&lt;p&gt;Of course not only the APIs of repositories can benefit from lambdas. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Optional&lt;/code&gt; is also a good example of an API that benefits from lambdas. Personally I also find lambdas particulary useful to replace passing builders around. Rather than passing a specific builder to a function, often it improves decoupling by yielding a builder from the function. Let me show you an example for sending email to clarify this idea.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mailer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sendTextMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;TextMessageBuilder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sendMimeMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MimeMessageBuilder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mailer&lt;/code&gt; we need to pass a specific builder into it. These builders have a common interface but are different in the kind of message they build. And the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mailer&lt;/code&gt; has separate methods because it must add different information based on the type being used. Because of this any client-code is tightly coupled to pass the correct builder.&lt;/p&gt;

&lt;p&gt;As you probably suspect, this is where lambdas are helpful. Rather than requiring the client to create a builder and pass it in, the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mailer&lt;/code&gt; functions can create the builder they need and yield it to a lambda.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Mailer&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sendTextMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MessageConfigurator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configurator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sendMimeMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MessageConfigurator&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;configurator&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;

  &lt;span class=&quot;nd&quot;&gt;@FunctionalInterface&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;MessageConfigurator&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;MessageBuilder&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;configure&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;MessageBuilder&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To use the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Mailer&lt;/code&gt; all we need to do is provide a lambda to build the message.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;mailer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;sendTextMessage&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;message&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;recipients&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;subject&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;APIs&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Lambdas can make for more fluent and stable APIs&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The API now is much more stable. Client-code is decoupled from any changes in specific builders, and won’t break as long as functions on the builder remain compatible.&lt;/p&gt;

&lt;p&gt;As the examples helped me show, lambdas can help you build more fluent and stable APIs that are more intention revealing. These APIs require not much documentation for other programmers to get going because it’s actually hard for them to get it wrong. As a general guideline I prefer clear and intention revealing code over documentation. Fix, don’t document.&lt;/p&gt;

&lt;p&gt;Of course I’ve only shown a few examples in this article. Lambdas are much more widely applicable than only with the examples here. I hope this article provided you with some new insights in what lambdas can help you with, and that you can think of ways how they can improve your code.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Introduction to Java Lambdas</title>
        <link href="https://blog.thesoftwarecraft.com/2013/10/introduction-to-java-lambdas.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-10-26:/2013/10/introduction-to-java-lambdas</id>
        <updated>2013-10-26T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;The main theme of Java 8 is lambdas. I have noticed that for many Java programmers lambdas are pretty tough material. So let’s try to get a basic understanding of them.&lt;/p&gt;

&lt;p&gt;First of all, what exactly is a lambda? A lambda is an anonymous function that is, unlike a regular function, not bound to an identifier (i.e. it has no name). These functions can be passed as arguments to other functions (known as higher-order functions).&lt;/p&gt;

&lt;p&gt;Suppose our application must write to a bunch of files from different places of the system. We don’t want to handle the checked exceptions every time [see &lt;a href=&quot;/2013/03/exceptions-checked-and-unchecked.html&quot;&gt;Exceptions: checked and unchecked&lt;/a&gt; for more about checked exceptions]. So we decide to write a low-level &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeToFile&lt;/code&gt; function that opens a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileWriter&lt;/code&gt; and passes it to a function that can then safely write to the file.&lt;/p&gt;

&lt;p&gt;Using this low-level function we write the following code.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;writeToFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;todo.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;FileWriteFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;nd&quot;&gt;@Override&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Writer&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;IOException&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;learn about lambdas\n&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;learn stream API\n&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The object we pass into &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;writeToFile&lt;/code&gt; is an anonymous implementation of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FileWriteFunction&lt;/code&gt; [anonymous because we didn’t name it as a class]. It has a single function making this effectively passing an anonymous function. In the world of Java these are sometimes referred to as &lt;em&gt;callbacks&lt;/em&gt;. Probably you’ve used this at least a few times before, maybe without taking notice.&lt;/p&gt;

&lt;p&gt;This anonymous object is effectively a lambda. But obviously this doesn’t look like passing a function. The syntax is very unwieldy. And this is exactly what changes in Java 8.&lt;/p&gt;

&lt;p&gt;With the syntactic support for lambdas in Java 8 the code reads like we pass a function. Using a Java 8 lambda we rewrite the above code as follows.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;writeToFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;todo.txt&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;learn about lambdas\n&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;learn stream API\n&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;})&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;That’s better. It emphasizes the code that matters and hides most of the unwieldy parts.&lt;/p&gt;

&lt;h2 id=&quot;closures&quot;&gt;Closures&lt;/h2&gt;
&lt;p&gt;Often lambdas are used interchangeably with lexical functions (closures). While they are both anonymous functions, the definition of a closure is that it is a function containing bound variables. I.e. the closure includes a referencing table that contains references to local variables.&lt;/p&gt;

&lt;p&gt;For example, if we accept a parameter &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;data&lt;/code&gt; that we want to write to a file, we use a closure.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;save&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;writeToFile&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;file.db&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;file&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;write&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;data&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;While anonymous inner-classes restrict access to final variables, closures provide access to any variable. However, the variable is effectively final to the closure, so it cannot be reassigned.&lt;/p&gt;

&lt;h2 id=&quot;compilation&quot;&gt;Compilation&lt;/h2&gt;
&lt;p&gt;What about compilation of lambdas? Does Java 8 only provide a spoonful of syntactic sugars upon anonymous inner-classes with only one method?&lt;/p&gt;

&lt;p&gt;Not really, no. It’s true that it allows the lambda syntax for any single-method anonymous inner-class. But lambdas are not compiled into inner-classes. Instead the compiler outputs &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;lambda$&lt;/code&gt; methods in the defining class and uses &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;invokedynamic&lt;/code&gt; to dispatch the call.&lt;/p&gt;

&lt;p&gt;So now you know how to use a lambda in Java 8. While by themselves lambdas are pretty useful, they are even more so when applying them to collections.&lt;/p&gt;

&lt;h2 id=&quot;stream-api&quot;&gt;Stream API&lt;/h2&gt;
&lt;p&gt;The new Stream API provides an alternative to iterators by offering a more functional API to collections: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;java.util.stream.Stream&lt;/code&gt;. The most noteworthy functions on a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stream&lt;/code&gt; are: &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To start with a simple example, here’s how to sum all the numbers in a list.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;asList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;gt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;value&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// =&amp;gt; 15&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This reduces the sequence by adding each value to an accumulator, starting at zero. For comparison, classically you’d write a loop.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;asList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;))&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// =&amp;gt; 15&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Moving on to sum only odd numbers. First we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt; odd numbers, then we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reduce&lt;/code&gt;.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;n&quot;&gt;asList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;3&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;filter&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Predicates:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;odd&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;reduce&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;acc&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;n&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// =&amp;gt; 9&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The argument to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt; is a function reference to a static &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;odd&lt;/code&gt; function in a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Predicates&lt;/code&gt; class I used. It is a boolean function that, as the name suggests, tests if a number is odd.&lt;/p&gt;

&lt;p&gt;So far, so good. Now suppose we want to transform a list of centimeter sizes to their equivalent size in inches. We use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; for this.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nc&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Inch&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;inches&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;centimeters&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;stream&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nl&quot;&gt;Centimeter:&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;toInches&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;collect&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Collectors&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;toList&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;())&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The centimeters are mapped to inches by applying the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;toInches&lt;/code&gt; function to every item in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;centimeters&lt;/code&gt; collection.&lt;/p&gt;

&lt;p&gt;By its nature, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Stream&lt;/code&gt; is continuous. It is used to describe the operations to apply on a collection. But to acquire the results the data must be collected. This is what the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;collect&lt;/code&gt; function is for. It reduces the elements of the stream to a mutable container [e.g. a list].&lt;/p&gt;

&lt;p&gt;Using the Stream API and lambdas can greatly simplify the code you use to work with collections, and make the code much more expressive. Preferring using non-destructive operations (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;filter&lt;/code&gt;) over destructive operations (e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;forEach&lt;/code&gt;) makes the code more easy to reason about.&lt;/p&gt;

&lt;p&gt;That’s it! These are the basics you need to know about lambdas (and closures) in Java 8. Of course there’s much more to write about lambdas, but that’s something for &lt;a href=&quot;/2013/11/lambdas-for-fluent-and-stable-apis.html&quot;&gt;another post&lt;/a&gt;.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>End of the Journey</title>
        <link href="https://blog.thesoftwarecraft.com/2013/10/end-of-journey.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-10-09:/2013/10/end-of-journey</id>
        <updated>2013-10-09T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Wow! Those were seven exciting weeks following a wonderful journey through seven fantastic languages, each with their own specialties. To three of the languages I was completely blank, with the other four I was (a bit) familiar.&lt;/p&gt;

&lt;p&gt;The journey started with &lt;a href=&quot;/2013/08/week-1-ruby.html&quot;&gt;Ruby&lt;/a&gt;, a language that adopts today’s mainstream programming paradigm of object orientation, and with so much syntactic sugar that every programmer is able to understand what’s going on.&lt;/p&gt;

&lt;p&gt;Through the &lt;a href=&quot;/2013/08/week-2-io.html&quot;&gt;Io language&lt;/a&gt; that uses the prototype based programming style (also the object oriented paradigm) the book went on with logic programming using Prolog. This is completely different from the first two languages, and lays a firm foundation for some of the languages to come. It also drags you into recursion.&lt;/p&gt;

&lt;p&gt;From &lt;a href=&quot;/2013/08/week-3-prolog.html&quot;&gt;Prolog&lt;/a&gt; the journey took a turn for a more functional style of programming. First stop was &lt;a href=&quot;/2013/09/week-4-scala.html&quot;&gt;Scala&lt;/a&gt;, the object oriented and functional hybrid. From thereon the destinations were &lt;a href=&quot;/2013/09/week-5-erlang.html&quot;&gt;Erlang&lt;/a&gt;, &lt;a href=&quot;/2013/09/week-6-clojure.html&quot;&gt;Clojure&lt;/a&gt;, and finally the purely functional &lt;a href=&quot;/2013/10/week-7-haskell.html&quot;&gt;Haskell&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The author, Bruce Tate, kicks off every language with an introduction and some background information on the language. By the order of the languages you can notice that Bruce did his best with each language to provide a foundation for the next one. I am not sure if Bruce intended it this way, but for me it felt like everything came together in Haskell.&lt;/p&gt;

&lt;p&gt;I really enjoyed this vast journey through these seven languages. It’s very interesting how they all have their own strengths and weaknesses, each of them being great at what it was created for. Some of them are applicable to more general purpose programming, others solve a very specific set of problems.&lt;/p&gt;

&lt;p&gt;In the book Bruce sometimes speculates a bit about the future of languages and whether or not languages can become mainstream. Below are some of my thoughts about this.&lt;/p&gt;

&lt;p&gt;There is no longer any doubt that functional programming techniques become increasingly important as our hardware gets more concurrent. Using threads to do concurrent programming just won’t do if programmers must be productive. Moore’s law these day’s no longer applies to the speed or our hardware, but it does seem to nearly apply to parallelism. Our hardware doesn’t get twice as fast every year, but it gets twice as parallel every few years.&lt;/p&gt;

&lt;p&gt;That said, I think that the best candidate to become a mainstream language is Scala, simply because it offers a smooth transition from object orientation to functional programming. Personally I doubt whether this is actually helpful. It’s much like C++ transitioned many programmers from procedural programming in C to the same way of thinking with objects that is still much applied today in mainstream languages as Java and C#. [Of course this wasn’t all C++ did and no offense towards C++ is meant in any way.]&lt;/p&gt;

&lt;p&gt;At the moment I believe Ruby is becoming a mainstream language, but without many programmers who use it actually knowing it. Tools like Puppet or Chef offer a configuration DSL written in Ruby, where programmers can fallback on Ruby for doing more complicated and exotic configurations. As a scripting language that is available on all major platforms it is great for building system administration tools and configurations. It is rather ironic how offensive many Java programmers are against Ruby (or all dynamic languages for that matter) and at the same time telling how much they like the configurations of Puppet.&lt;/p&gt;

&lt;p&gt;To anyone who is passioned about programming I would strongly suggest reading this well-crafted piece of work. Especially if you haven’t learned a new language in a while, let me remind you Andy Hunt’s advice in &lt;a href=&quot;https://amzn.to/4n5z27L&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;The Pragmatic Programmer&lt;/a&gt; to learn a new language every year.&lt;/p&gt;

&lt;p&gt;As I mentioned in the articles, Ruby and Clojure belong to my favorite languages today. And for Erlang and Haskell there’s much to learn, which I intend to pick up next year.&lt;/p&gt;

&lt;p&gt;Thank you for joining me on my journey. I had a great time, I hope you did too.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 7: Haskell</title>
        <link href="https://blog.thesoftwarecraft.com/2013/10/week-7-haskell.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-10-02:/2013/10/week-7-haskell</id>
        <updated>2013-10-02T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Haskell is the seventh language in the book. It is a pure functional language, so functions have no side effects. This makes it easier to reason about the code.&lt;/p&gt;

&lt;p&gt;In contrast to most other languages Haskell is not created by a single person. When in the late 1980s a large number of people were designing and implementing functional languages, a committee formed to work together so they’d be stronger. The original goals of the language were to form a foundation for research that was suitable for teaching and industrial usages.&lt;/p&gt;

&lt;p&gt;Haskell exploits a static type system, like Scala. It has strong support to infer types. Its type system is build to clearly express the intent of the code. It can prevent much error handling and argument validation. Its type system is much more powerful than Java’s (and so Scala’s).&lt;/p&gt;

&lt;p&gt;Bruce compares Haskell with Spock from Star Trek. Like Spock, Haskell is completely predictable (because of its pure functions), and always logical.&lt;/p&gt;

&lt;p&gt;This week of Haskell was my first experience with the language. It took me a little while to get started. On day 2 things got better because the exercises got more difficult. I liked the exercise for generating a lazy list with prime numbers. I used a simple algorithm (Siege of Eratosthenes) that is not really fast but easier to implement than many other algorithms.&lt;/p&gt;

&lt;p&gt;On day 3 Bruce introduced monads. In short, using a monad we can compose functions that have certain properties. This is not an accurate explanation of monad, and if you wish to really understand them you will have to invest a bit of time reading through example monads and the explanations. Monads are not specific to Haskell, lots of (partly) functional languages support them. Every other functional language in the book (Scala, Erlang, and Clojure) does.&lt;/p&gt;

&lt;p&gt;I enjoyed the exercise on day 3 of representing a maze using Haskell’s type system and solving it using the List monad. The exercise of implementing the List monad in a non-functional language was interesting. I chose Ruby and Io.&lt;/p&gt;

&lt;p&gt;My solutions to the most interesting exercises in the book are available in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Github profile&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;One of the major strengths of Haskell is of course its type system. Personally I prefer duck typing over static typing, but that’s mostly because all major static type systems (C, Java, Scala) force so much redundancy. Haskell changes this by understanding that static typing is a tool that is very useful in some situations while not so much in others. Like I said earlier, the compiler can infer types if you don’t provide them.&lt;/p&gt;

&lt;p&gt;Other great strengths of Haskell include its lazy computation strategies, the expressiveness of the language, and the purity of its programming model. Also Haskell is the primary language for teaching functional programming techniques.&lt;/p&gt;

&lt;p&gt;A definite weakness of Haskell is the very steep learning curve. To get the most out of the language and fully embrace its strengths you have to understand many functional concepts (monads is one of them). As Haskell is a pure functional language, there’s no cheating in using a few non-pure functions.&lt;/p&gt;

&lt;p&gt;Personally I find Haskell a particularly interesting language, one that is important to get a good grasp on. As I am not yet comfortable doing some real work with Haskell yet, I definitely want to learn more about it. I believe that getting more proficient in Haskell will strengthen my understanding of functional techniques.&lt;/p&gt;

&lt;p&gt;Finishing this week of Haskell only leaves me to wrap up my &lt;a href=&quot;/2013/10/end-of-journey.html&quot;&gt;final thoughts&lt;/a&gt; about the book and learning new programming languages. I’ll leave that for next week.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 6: Clojure</title>
        <link href="https://blog.thesoftwarecraft.com/2013/09/week-6-clojure.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-09-23:/2013/09/week-6-clojure</id>
        <updated>2013-09-23T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Clojure is one of my favorite languages of the moment. Although I haven’t used it for real work yet, I enjoy the simplicity of the language and syntax. So with a week full of Clojure exercises I am in for a joyful week!&lt;/p&gt;

&lt;p&gt;Clojure is a dialect of Lisp. It’s a functional language but not a pure functional one. One of the most interesting aspects of Lisp (in my opinion) is that the language is homoiconic. Simply stated this means that both code and data share the same internal representation. This makes the language ideal for metaprogramming.&lt;/p&gt;

&lt;p&gt;Rich Hickey created Clojure because he wanted a functional, dynamic language for the industry dominant platforms of the JVM (Java’s virtual machine) and CLR (common language runtime, a virtual machine for the .NET platform).&lt;/p&gt;

&lt;p&gt;Clojure is the first Lisp that is designed to be a tool for general-purpose programming. Its syntax is a little easier on the parentheses than other Lisps, so that less programmers are put off by them. Moving from &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;foo(bar, baz)&lt;/code&gt; to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;(foo bar baz)&lt;/code&gt; should not be difficult for programmers.&lt;/p&gt;

&lt;p&gt;Bruce chose Yoda as a character for Clojure. His communication style is often inverted and in the early beginning hard to understand. Especially if you’ve never read Lisps before, this is true for Clojure. Where the Jedi master has an inner power that others can’t seem to master, Lisp has macros and higher-order constructs that most no other language is offering.&lt;/p&gt;

&lt;p&gt;Like I said I haven’t used Clojure for real work yet but my previous experience was enough to get me through the first days quick. On day 3 the &lt;em&gt;sleeping barber&lt;/em&gt; exercise (by Edsger Dijkstra in 1965) was interesting and fun. I got to the average of ~475 haircuts in 10 seconds, whereas the theoretical maximum is 500.&lt;/p&gt;

&lt;p&gt;My solutions to the exercises in the book are available in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Github profile&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Clojure has a lot to offer. It is Lisp and therefore is extremely flexible and very powerful. Being on the JVM it can take advantage of the Java ecosystem and its libraries. Lazy evaluation features can simplify many problems by delaying execution until it is actually needed.&lt;/p&gt;

&lt;p&gt;Clojure has an interesting approach to concurrency. STM (shared transactional memory) protects you from many race conditions by forcing state mutations to happen within appropriately protected functions. This really pays off for solving problems like the &lt;em&gt;sleeping barber&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;One of the greatest weaknesses of Clojure comes from being on the JVM. Optimization for tail recursion is limited to loop/recur. We can only hope that the JVM will fix this some day.&lt;/p&gt;

&lt;p&gt;I like Clojure a lot. Once you get past the awkward feeling of the syntax it actually starts making sense. It is highly expressive and its functional nature allows for much more clear and beautiful abstractions that Java ever can. I believe that better software can be built faster with Clojure.&lt;/p&gt;

&lt;p&gt;Next week is the last language of this book: &lt;a href=&quot;/2013/10/week-7-haskell.html&quot;&gt;Haskell&lt;/a&gt;. See you in a week.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 5: Erlang</title>
        <link href="https://blog.thesoftwarecraft.com/2013/09/week-5-erlang.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-09-14:/2013/09/week-5-erlang</id>
        <updated>2013-09-14T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Erlang! The language of concurrency, and much more. It’s the first actual functional language from the book, although not purely functional.&lt;/p&gt;

&lt;p&gt;Erlang is acronym for Ericsson Language and was originally developed by Joe Armstrong in 1986. What started as finding a better way of writing control software for a telephone exchange, is now the foundation of many highly reliable systems like CouchDB and Facebook’s chat.&lt;/p&gt;

&lt;p&gt;Bruce characterizes Erlang as Agent Smith, the artificial intelligence program in the Matrix. It has the ability to take any form and bend the rules of reality to be in many places at once. He was unavoidable.&lt;/p&gt;

&lt;p&gt;Erlang is the second language in the book that is completely new to me (the other was Prolog). After a week of Prolog, Erlang isn’t as much of a shock. They both heavily use pattern matching (for the good) and their syntax is much alike. Although a big advantage in Erlang is the ability to use return values. It might be because Prolog was still fresh in my memory, but I had not much trouble with Erlang’s syntax.&lt;/p&gt;

&lt;p&gt;The first exercises would be rather trivial with Erlang’s core libraries, but of course that would spoil the exercise. Therefore I didn’t use any of the core library functions and wrote any functions I needed, myself. This helped getting a better grip on the language.&lt;/p&gt;

&lt;p&gt;On day 3 the exercises were all about concurrency. To explore the concurrency features introduced on day 3 I went a bit beyond the exercise of monitoring a translation process and restart it in case is dies. The doctor I wrote can monitor and restart any process, and I attached translation services for two languages.&lt;/p&gt;

&lt;p&gt;Further exercises on day 3 were to set up two-way process monitoring, so that both processes are automatically restarted in case of failure. Erlang makes this pretty easy.&lt;/p&gt;

&lt;p&gt;My solutions to the exercises in the book are available in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Github profile&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;All in all I liked Erlang a lot. Once you get used to the syntax (which might take a little while, but again it is definitely simpler than Prolog) you have access to an amazingly powerful language. I believe that the most power in Erlang comes from its unique approach to concurrency. It uses very lightweight processes to handle messages. It’s extremely easy to monitor processes and restart them might they fail. This complements Erlang’s &lt;em&gt;let it fail&lt;/em&gt; strategy, which makes error handling so much easier.&lt;/p&gt;

&lt;p&gt;What’s more is that the Open Telecom Platform (OTP) provides standard ways for creating and monitoring processes. With only a few lines of code you build reliable concurrent processes. Erlang provides what many other languages cannot: scalable concurrency and reliability.&lt;/p&gt;

&lt;p&gt;Compare that to Java’s heavy threading model! Java might be the enterprise technology of today, but Erlang has industrial strength ready for adoption.&lt;/p&gt;

&lt;p&gt;I definitely want to come back to Erlang to release its true power and added &lt;a href=&quot;https://amzn.to/4h9jFJV&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Programming Erlang&lt;/a&gt; to my reading list. For now this is where I leave Erlang and move on to next week’s language: &lt;a href=&quot;/2013/09/week-6-clojure.html&quot;&gt;Clojure&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 4: Scala</title>
        <link href="https://blog.thesoftwarecraft.com/2013/09/week-4-scala.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-09-07:/2013/09/week-4-scala</id>
        <updated>2013-09-07T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;It’s a while ago since I last wrote some Scala code so this week was fun picking it back up.&lt;/p&gt;

&lt;p&gt;Scala is not a full object-oriented language nor a functional language, but it supports both paradigms. In fact, the intension of Scala’s creator, Martin Odersky, was to form a bridge between the object-oriented and functional paradigms, much like C++ was a bridge between the procedural and object-oriented paradigms.&lt;/p&gt;

&lt;p&gt;Scala gets the character of Edward Scissorhands: often awkward, sometimes amazing, but always a unique expression. This is absolutely true for Scala, with it’s academic-like syntax.&lt;/p&gt;

&lt;p&gt;Scala fits the role of being a bridge like a glove. It has a strong affinity with Java; it runs on the Java virtual machine, it allows you to call Java libraries directly, and as mentioned before, it supports the ideas of object orientation.&lt;/p&gt;

&lt;p&gt;Personally I find its static typing still pretty intrusive and still found myself adding redundant type information just to satisfy the compiler. Also the Scala language looks much more complicated because of its static typing. But it definitely eases the static typing burden of the Java language. Because the syntax is simpler and the code is less polluted with type information, I also found it easier to express ideas in Scala than in Java.&lt;/p&gt;

&lt;p&gt;On the up-side, Scala’s actor model is a welcome improvement in concurrent programming. Combined with immutable (shared) state it makes it much more easy to reason about concurrency. Scala is also very powerful for creating DSLs, and its built-in XML support is a great example of that.&lt;/p&gt;

&lt;p&gt;My solutions to the more interesting exercises in the book (a Tic-tac-toe game and a text censor trait) are available in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Github profile&lt;/a&gt;. For day 3 I didn’t include the exercise as it was rather trivial.&lt;/p&gt;

&lt;p&gt;I do believe that there’s a future for Scala. Its support for object-orientation makes it easier to get started for programmers who don’t know functional programming. And for programmers who do understand a bit about functional programming, Scala has excellent support for that too - especially with the Scalaz extension.&lt;/p&gt;

&lt;p&gt;I would like to get back to Scala and definitely prefer it over Java. For now this is where I leave Scala and focus on the next language: &lt;a href=&quot;/2013/09/week-5-erlang.html&quot;&gt;Erlang&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 3: Prolog</title>
        <link href="https://blog.thesoftwarecraft.com/2013/08/week-3-prolog.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-08-31:/2013/08/week-3-prolog</id>
        <updated>2013-08-31T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Prolog is the first language that completely gets me out of my comfort zone and brought me back to noob. Prolog is a pretty amazing language that at first makes you wonder how it knows so much.&lt;/p&gt;

&lt;p&gt;Bruce picked the Rain Man character for Prolog. I can see this resemblance. They both are a great source of knowledge, if you get your questions right. Many times you’re left wondering ‘how did he know that?!’.&lt;/p&gt;

&lt;p&gt;In the beginning I was in a continuous struggle to get to my questions right. The first hour or so I had to get used to thinking only in terms of rules and recursion. My brain seems adjusted for being able to return values. At first this made it more difficult to reason about the code and the problems at hand.&lt;/p&gt;

&lt;p&gt;After some fooling around and getting not much done without some help, I took a different approach. I started evolving the code in really small increments, trying each increment in the prolog repl. This really helped and got me gain a better understanding of the language.&lt;/p&gt;

&lt;p&gt;Simple algorithms like fibonacci and bubblesort that were hard at first (writing them in a single attempt) were suddenly easier to get done.&lt;/p&gt;

&lt;p&gt;The fibonacci algorithm gave me a little deeper insight into Prolog. I implemented a tail recursive calculation of the fibonacci sequence that gave me the fibonacci number at a certain point in the index (e.g. the fibonacci of index 6 is 8) and I wondered why Prolog couldn’t reverse the rules and give me the index of a provided fibonacci number (e.g. the index of 8 is 6).&lt;/p&gt;

&lt;p&gt;That’s when I saw the difference between functions and rules. The fibonacci algorithm is a function and in fact there is no way for Prolog to revert the algorithm like it can with rules. After some googling if fibonacci can be done with rules, it seems to be solvable by applying the Constraint Logic Programming library (or CPL).&lt;/p&gt;

&lt;p&gt;The Sudoku solver was surprisingly simple. I took the solution from the book a bit further by generalizing it for mutiple valid Sudoku puzzle sizes (4x4, 6x6, 9x9). As I choose to stick with GNU prolog I had to write some utility functions to make for a more general solver (like partition, transpose, take, first, etc).&lt;/p&gt;

&lt;p&gt;I wrote these utility functions using actual TDD [by which I mean here having a set of tests I can run every time I want to, rather than using the repl]. This made it a lot easier. Interestingly, I ended up taking somewhat larger increments than I would normally use.&lt;/p&gt;

&lt;p&gt;My solutions to the more interesting exercises in the book (like the Sudoku solver) are available in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Github profile&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Prolog is a very interesting language that makes solving the right problem a breeze. Basically it provided all-or-nothing solutions. If it cannot find a solution to the proposed problem it just prints ‘no’. This is where you have to adjust your questioning and retry.&lt;/p&gt;

&lt;p&gt;After a bit of a puzzling start I had great fun using Prolog. It has an interesting way of thinking about the problem and describing the problem domain itself rather than a recipe for the solution. If I again come across a problem that is well suited for Prolog, I will take this advantage.&lt;/p&gt;

&lt;p&gt;For now this is where I leave Prolog and move to my next destination: &lt;a href=&quot;/2013/09/week-4-scala.html&quot;&gt;Scala&lt;/a&gt;. See you in a week!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 2: Io</title>
        <link href="https://blog.thesoftwarecraft.com/2013/08/week-2-io.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-08-24:/2013/08/week-2-io</id>
        <updated>2013-08-24T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;After I had some fun with the Ruby exercises, week 2 brings a language that’s new to me. Or at least it was new to me as I started reading about the Io language about half a year ago. Like I wrote in the introduction post I spent some more time with Io and came to write &lt;a href=&quot;https://github.com/bjpbakker/iospec&quot; rel=&quot;external&quot;&gt;iospec&lt;/a&gt;, a small BDD-style test library that is inspired by Ruby’s RSpec.&lt;/p&gt;

&lt;p&gt;Io is a prototype language, like Lua or JavaScript. Hence every thing is an object, and every object is a clone of another object. You interact with an object through messages. Io is an interesting language that provides a lot of flexibility. It’s syntax is simple enough so that you can learn it in about 15 minutes.&lt;/p&gt;

&lt;p&gt;The character Bruce chose to represent the Io language is Ferris Bueler. Let him take you for a day and you’re in for a real treat, and end up seeing things you’ve never considered before. Io uses normal order evaluation, meaning that it delays evaluation of method arguments until they are required (i.e. lazy evaluation).&lt;/p&gt;

&lt;p&gt;Being a homoiconic language (meaning that program structure is, like data, represented in a primitive data structure of the language) Io offers lots of flexibility, making Io a great language for implementing DSLs. It’s this flexibility that made it possible to create the iospec test library. Io is almost like an object-oriented Lisp.&lt;/p&gt;

&lt;p&gt;Io is relatively new with a small community. Development started in 2002 by Steve Dekorte, to help him better understand how interpreters work. It is inspired by Smalltalk, Self and Lisp. Io is used in different environments, like router configuration language and a scripting language for video games. Its small footprint, rich concurrency (it supports the actor model) and simple integration with C makes it highly suitable for embedded systems.&lt;/p&gt;

&lt;p&gt;I loved spending time with Io, doing the exercises of the book and creating iospec. Unlike Ruby there are not much syntactic sugars, but because of its awesome flexibility adding some yourself is fairly simple.&lt;/p&gt;

&lt;p&gt;My solutions to the more interesting exercises in the book are available in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Github profile&lt;/a&gt;. These exercises include a Fibonacci sequence (both recursive and destructive), a two-dimensional array including transpose, and an XML builder DSL.&lt;/p&gt;

&lt;p&gt;Io is an amazing language. Maybe it will have a lasting impact, maybe not. Learning Io gained me many insights in how JavaScript actually works.&lt;/p&gt;

&lt;p&gt;Now it’s time to move on with the next language: &lt;a href=&quot;/2013/08/week-3-prolog.html&quot;&gt;Prolog&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Week 1: Ruby</title>
        <link href="https://blog.thesoftwarecraft.com/2013/08/week-1-ruby.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-08-17:/2013/08/week-1-ruby</id>
        <updated>2013-08-17T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;My journey through these seven different programming languages starts with a language I am already familiar with: Ruby. Even so the chapter was an interesting read and provided some new background information.&lt;/p&gt;

&lt;p&gt;The character that Bruce chose to represent Ruby is Mary Poppins, the British nanny. With a spoonful of sugar she made the household more efficient by making it fun. Ruby does the same thing to programming, with more than just a spoon of sugar. Syntactic sugar. These little language features break the basic rules of the language to make the code a little easier to understand.&lt;/p&gt;

&lt;p&gt;The creator of Ruby, Matz (Yukihiro Matsumoto), doesn’t worry much about the runtime efficiency, he optimizes Ruby for programmer efficiency. He makes the language simpler to read and to program in by adding more complexity on the parsing and interpretation side.&lt;/p&gt;

&lt;p&gt;Ruby is a strongly typed dynamic language. Dynamic type systems rely on duck typing, where the features of an object are based on what it can support rather than its class hierarchy. Duck typing, in general, will gain you cleaner abstractions with more concise, readable code. The cost of duck typing is less comprehensive tool support (e.g. the IDEs for static languages). The fact that Ruby is a very dynamic language is proven by its extensive support for meta-programming.&lt;/p&gt;

&lt;p&gt;The book provides some nice exercises to get hands on with Ruby, like writing a number-guess game, a basic grep implementation, and an interesting API to the information from a CSV file using meta-programming. I put my solutions to these exercises in the &lt;a href=&quot;https://github.com/bjpbakker/7-languages-in-7-weeks&quot; rel=&quot;external&quot;&gt;7-languages-in-7-weeks&lt;/a&gt; repository on my &lt;a href=&quot;https://github.com/bjpbakker&quot; rel=&quot;external&quot;&gt;Gibhub profile&lt;/a&gt;. The rest of the exercises from the book where much smaller and more trivial. I did these in the Repl [&lt;a href=&quot;http://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop&quot; rel=&quot;external&quot;&gt;Read-eval-print loop&lt;/a&gt;].&lt;/p&gt;

&lt;p&gt;The strengths of Ruby lie in its pure object orientation. Ruby is a great language for web development. Because the language is so productive it is awesome for bringing a viable product to marked quickly. Ruby is also ideal for scripting since it is present on most major operating systems.&lt;/p&gt;

&lt;p&gt;The main weakness of Ruby is its runtime performance. Some major performance improvements have been done in Ruby 1.9 and there are some attempts at bringing JIT compilation to Rubinius. And as with any object oriented language the programming model also is a weakness. It depends on the idea of wrapping behavior around (usually mutable) state. This model often leads to serious concurrency problems, that are almost impossible to test and debug.&lt;/p&gt;

&lt;p&gt;As I mentioned at the beginning of this article, Ruby and I have met before. In fact, Ruby is one of my favorite languages at the moment. After the long ruling of languages from the C family it’s a breath of fresh air. It’s syntactic sugars help you to write very clean and readable code.&lt;/p&gt;

&lt;p&gt;Time to continue my travel to the destination for next week: &lt;a href=&quot;/2013/08/week-2-io.html&quot;&gt;Io&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Back to Noob</title>
        <link href="https://blog.thesoftwarecraft.com/2013/08/back-to-noob.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-08-13:/2013/08/back-to-noob</id>
        <updated>2013-08-13T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;A while ago I bought the book &lt;a href=&quot;https://amzn.to/4odi7Rr&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Seven Languages in Seven Weeks&lt;/a&gt; by &lt;a href=&quot;http://en.wikipedia.org/wiki/Bruce_Tate&quot; rel=&quot;external&quot;&gt;Bruce Tate&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The goal of this book is to take you (the reader) on an enlightening journey that will challenge the way you look at programming. Using this book alone you won’t become an expert on any of the languages. But the book will teach you a lot more than just “Hello, world”.&lt;/p&gt;

&lt;p&gt;In the introduction Bruce explains the method he uses to learn a new programming language and how he needs a deep, fast dive into a language to taste its true flavor. In the book he guides you through seven such experiences.&lt;/p&gt;

&lt;p&gt;The languages that are covered in the book are: Ruby, Io, Prolog, Scala, Erlang, Clojure, and Haskell. Of each of these languages the book helps you to study, amongst other things:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;the type model - i.e. strong or weak, static of dynamic&lt;/li&gt;
  &lt;li&gt;the programming model - i.e. object oriented, functional, procedural, logic, or hybrid&lt;/li&gt;
  &lt;li&gt;the core features that make the language unique&lt;/li&gt;
  &lt;li&gt;and more, that I won’t spoil for you&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The introduction comes with the warning that the book won’t be your installation guide. Personally I welcome this as I strongly dislike such instructions in a book. They’ll become out of date and there are at least a number of platforms irrelevant to your situation. There’s plenty of online documentation to cover installation details and there’s &lt;a href=&quot;http://google.com/&quot; rel=&quot;external&quot;&gt;Google&lt;/a&gt; to find it.&lt;/p&gt;

&lt;p&gt;Throughout the book each language is assigned its very own character that matches its distinct personality. As an example of such characterization, Bruce compares Java with having a rich lawyer for a brother. He was fun when he was younger, but now he’s a black hole that sucks away all the joy.&lt;/p&gt;

&lt;p&gt;The book is in my collection for about half a year now. The moment I received my copy I started reading the first chapters about Ruby and Io. I played a while with Io and got to write a small BDD-style test library for it.&lt;/p&gt;

&lt;p&gt;Last week I picked up the book again, from the start. In addition to working through the languages by the exercises from the book, I’ll write an article on this blog about how I experienced the language. Also I’ll try to stick with book’s schedule of doing 7 languages in 7 weeks.&lt;/p&gt;

&lt;p&gt;Some of the languages I’m already familiar with, others are completely new to me. I am excited to get cracking at the languages and experience how it is again to be a noob, and learn all about them.&lt;/p&gt;

&lt;p&gt;I will see you in a week with a post about the first language, Ruby.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Mockito</title>
        <link href="https://blog.thesoftwarecraft.com/2013/07/mockito.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-07-26:/2013/07/mockito</id>
        <updated>2013-07-26T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Yesterday I send out a &lt;a href=&quot;https://twitter.com/bjpbakker/status/360391093726089216&quot; rel=&quot;external&quot;&gt;tweet&lt;/a&gt; in which I claimed that Mockito and clean tests seem not to go hand in hand. It triggered some reactions on which I’ll elaborate in this post a bit more than in the Twitter conversations.&lt;/p&gt;

&lt;p&gt;First of all, in no way I intend to say that your tests are a mess by definition if you use Mockito. I am saying that Mockito doesn’t really help keeping your tests clean. Rather it puts that burden on you.&lt;/p&gt;

&lt;p&gt;The responses were from &lt;a href=&quot;https://twitter.com/sdudzin&quot; rel=&quot;nofollow&quot;&gt;@sdudzin&lt;/a&gt; and &lt;a href=&quot;https://twitter.com/jeroenrosenberg&quot; rel=&quot;nofollow&quot;&gt;@jeroenrosenberg&lt;/a&gt;, for which I like to thank them. Their reactions included that it’s kind of a bold statement, what is my definition of &lt;em&gt;clean tests&lt;/em&gt;, why Mockito breaks that, Mockito provides feedback about design, and that verifying that an invocation did &lt;em&gt;not&lt;/em&gt; happen is over specification.&lt;/p&gt;

&lt;p&gt;So, yes, my tweet was a bit of a bold statement. It even might be exaggerated a bit. But that doesn’t invalidate it - it is my opinion on Mockito.&lt;/p&gt;

&lt;p&gt;The context in which I tweeted this is that I was implementing a dispatch function. Based on some input, a single method must be invoked on a collaborator. As I TDD-ed my way in soon I refactored to a switch statement. I’m not sure why but in one of the branches I forgot to break. The result is a bug - which luckily I caught while writing the next test.&lt;/p&gt;

&lt;p&gt;In an after-method I added a call to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;verifyNoMoreInteractions&lt;/code&gt; causing these types of bugs to show up for any test, polluting the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tearDown&lt;/code&gt; method with this incoherent statement.&lt;/p&gt;

&lt;p&gt;Which brings me to my definition of clean tests. In my perception, a clean test simply stated is a test that clearly communicates its intent. It’s name clearly states what is tested, and the implementation clearly describes how it’s tested.&lt;/p&gt;

&lt;p&gt;In my opinion adding these statements adds noise to the tests. These statements are incoherent with the tests and are only necessary to work around strange behavior of a test library. Because of this you have to work hard to keep your tests clean.&lt;/p&gt;

&lt;p&gt;I don’t really agree on the statement that Mockito gives you feedback on the design. Test libraries don’t provide feedback. Tests do that. Test libraries only provide some convenience. Therefore I like test libraries to be as simple as possible, so the focus always lies on the test itself. Messy tests often tell you the design is messy. However, if a test library makes your tests just a little messier, it takes away this focus making it easier to ignore this feedback.&lt;/p&gt;

&lt;p&gt;Obviously I do agree that having many mocks in one test indicates a design problem. In fact, if you need more than one mock in a test, you probably need to refactor. Also I completely agree that verifying for an interaction &lt;em&gt;not&lt;/em&gt; to take place is over specification and should not be necessary. This actually leads to fragile tests.&lt;/p&gt;

&lt;p&gt;That still leaves the fact that I don’t want my test doubles to ignore calls that didn’t specify myself. As Mockito’s ignorance cannot be switched off, the verification for no other interactions sadly is necessary. It’s exactly this kind of behavior that I don’t like about Mockito. Other examples include that I find setting up doubles with Mockito gets complicated much earlier than with a library like JMock.&lt;/p&gt;

&lt;p&gt;This article is no way meant as a rant against Mockito. If you happen to like Mockito, that’s okay. If you don’t know about other libraries, it might be time to investigate some. Personally, at this moment I prefer JMock for test doubles in Java. I find JMock to enable me to clearly express the role of collaborators, while Mockito just makes it possible to setup test doubles. JMock also is much more consistent with other libraries, like RSpec, Jasmine and Sinon.&lt;/p&gt;

&lt;p&gt;To wrap up I send my thanks to Siarhei and Jeroen for the conversations we had.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Partial Functions in JavaScript</title>
        <link href="https://blog.thesoftwarecraft.com/2013/05/partial-functions-in-javascript.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-05-18:/2013/05/partial-functions-in-javascript</id>
        <updated>2013-05-18T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;JavaScript (both the language itself and libraries such as &lt;a href=&quot;http://underscorejs.org/&quot; rel=&quot;external&quot;&gt;Underscore&lt;/a&gt;) provides many functions that use closures. A closure is a block of code that is passed as an argument to a function or returned from a function. In JavaScript you pass in that code in the form of a function.&lt;/p&gt;

&lt;p&gt;Consider the following example for calculating the 20% tip of an array of checks.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt; function accepts a closure that in the example returns a 20% tip.&lt;/p&gt;

&lt;p&gt;Rather than using an inline &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tip&lt;/code&gt; function it makes sense to pass a reference to a defined function, so we can reuse the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tip&lt;/code&gt; function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nf&quot;&gt;tip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;100&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;20&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Probably you don’t always want to tip 20%. Sometimes you want to tip a little more, other times you may want to tip a little less. Therefore you want to pass in the percentage to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tip&lt;/code&gt; as an argument rather than tipping 20% all the time.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;tip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;percentage&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;check&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;check&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;percentage&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Adding the percentage as argument is of course simple. But now we can no longer pass the tip function into map since it needs an extra argument that map ain’t going to provide. Rather than using an inline function that calls &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tip&lt;/code&gt; with 20%, let’s explore another possibility.&lt;/p&gt;

&lt;h2 id=&quot;partial-functions&quot;&gt;Partial Functions&lt;/h2&gt;
&lt;p&gt;Partial functions is a functional programming concept. A partial function takes a function and fewer arguments than normal. It returns a function that takes the remaining arguments. When called the returned function call the original function with both sets of arguments.&lt;/p&gt;

&lt;p&gt;As JavaScript is a dynamic language that can create functions on the fly, it should be possible to implement this feature.&lt;/p&gt;

&lt;p&gt;We need a function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partial&lt;/code&gt; that takes a function and a variable number of arguments for its input. That’s pretty easy considering that &lt;em&gt;any&lt;/em&gt; function in JavaScript takes a variable number of arguments. These  arguments can be accessed from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arguments&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;[&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arguments&lt;/code&gt; is not an actual array but an object that supports array lookups. It lacks most array functions (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;map&lt;/code&gt;), but we can call the functions from the array prototype using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;arguments&lt;/code&gt; as their context.]&lt;/p&gt;

&lt;p&gt;Then &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partial&lt;/code&gt; must return a function that takes the remaining arguments and, as invoked, calls the original function with the combined argument arrays. Since &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partial&lt;/code&gt; can only access arguments in form of an array we cannot call the original function (i.e. using parentheses or &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;call&lt;/code&gt;) but must &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; it instead. This way the array of arguments we pass to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;apply&lt;/code&gt; is mapped to arguments of the original function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;partial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;remainingArgs&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;slice&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;call&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;arguments&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;apply&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kc&quot;&gt;null&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;args&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;concat&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;remainingArgs&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Using &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partial&lt;/code&gt; the general tip function can be passed into map as a 20%-tip function like this.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;120&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;90&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;180&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;].&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;map&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;partial&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;tip&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mf&quot;&gt;0.2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;24&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;36&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;partial&lt;/code&gt; function only works for pure functions and not for a function that gets needs some state of the object it is in. This is due to the function being applied without a context.&lt;/p&gt;

&lt;p&gt;Partial functions can offer a great deal of flexibility and can prevent a lot of adapter functions. Pay attention to the order of your arguments as JavaScript binds arguments by the order they are passed in. The example with the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;tip&lt;/code&gt; function obviously wouldn’t have worked out if the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;percentage&lt;/code&gt; argument was last.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>JavaScript: Prototypal Inheritance</title>
        <link href="https://blog.thesoftwarecraft.com/2013/05/javascript-prototypal-inheritance.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-05-13:/2013/05/javascript-prototypal-inheritance</id>
        <updated>2013-05-13T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;In my previous article about &lt;a href=&quot;/2013/05/javascript-objects-prototypes-inheritance.html&quot;&gt;JavaScript: Objects, Prototypes and Inheritance&lt;/a&gt; I wrote using objects and constructor functions to create object hierarchies in JavaScript. I also over-simplified things a little, to make the whole article a bit more comprehensible.&lt;/p&gt;

&lt;p&gt;In the article I stated that JavaScript’s behavior for copying objects is different from other prototype-based programming languages, like Self and Io. And it is. But the article is constrained to JavaScript’s classical object inheritance using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword.&lt;/p&gt;

&lt;h2 id=&quot;prototypal-inheritance&quot;&gt;Prototypal Inheritance&lt;/h2&gt;

&lt;p&gt;JavaScript also supports prototypal inheritance. By this I mean inheritance by copying (or cloning) or objects that is typical for prototype-based languages. In JavaScript this practically means inheriting from any object rather than limiting to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function&lt;/code&gt; derivatives.&lt;/p&gt;

&lt;p&gt;Furthermore, for prototypal inheritance you won’t need the strange (class-like) prototype of a function. You inherit from any object, much like the first example of inheritance I used in my previous article. To refresh your memory, here it is again.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Bart&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, Bart here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;By changing the object’s internal prototype (i.e. its parent) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; it inherits all of its attributes. This is prototypal inheritance.&lt;/p&gt;

&lt;p&gt;From its early beginning JavaScript supported classical inheritance (i.e. inheriting from a prototype using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; operator). But as a prototype-based programming language it is somewhat awkward to lack prototypal inheritance. Back in 2006 Douglas Crockford wrote &lt;a href=&quot;http://javascript.crockford.com/prototypal.html&quot; rel=&quot;external&quot;&gt;an article&lt;/a&gt; about this.&lt;/p&gt;

&lt;p&gt;His article manifested itself into the core language as the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.create&lt;/code&gt; function. Taking the above example you can make a copy of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; using this function.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Bart&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;The object is identical to the one in the former example and nearly identical to an object created using a constructor.&lt;/p&gt;

&lt;p&gt;Indeed, &lt;em&gt;nearly&lt;/em&gt; identical. One difference is that JavaScript keeps track of the constructor function of an object. An object created by classical inheritance has its constructor point to the function that created it, while an object created by prototypal inheritance has its constructor point to the constructor of its parent object.&lt;/p&gt;

&lt;p&gt;I.e. the following statements would be true given that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; is a constructor function and that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; is the object from the first example respectively.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;create&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;constructor&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;Object&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;browser-support&quot;&gt;Browser Support&lt;/h2&gt;
&lt;p&gt;Browser support for prototypal inheritance is pretty good, meaning that Internet Explorer is last of the major browsers to support it (as of version 10). The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.create&lt;/code&gt; function is part of version 5 of the ECMAScript specification and implemented as of JavaScript 1.8.5.&lt;/p&gt;

&lt;p&gt;And if you need to support a browser that runs an earlier JavaScript version you can always monkey patch &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt;, which obviously is one of the major advantages of using a dynamic language.&lt;/p&gt;

&lt;h2 id=&quot;performance&quot;&gt;Performance&lt;/h2&gt;
&lt;p&gt;But what about performance? JavaScript has been optimized for years on using the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword and constructor functions. How does prototypal inheritance relate to that? There one way really to find that out and that would be by testing it.&lt;/p&gt;

&lt;p&gt;So I’ve prepared some performance testing and was somewhat surprised by the (very early) results. Not that overall prototypal inheritance is slower than classical inheritance. Like I said, JavaScript has been optimizing the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; keyword for years. What surprised me was that this assumption failed for Firefox. It’s also interesting to see how different browsers relate to others, performance wise.&lt;/p&gt;

&lt;p&gt;To get a decent overall view it is of course important to get a wide range of different browsers and operating systems. So please run the performance tests on all your browsers, it only takes a few seconds.&lt;/p&gt;

&lt;p&gt;The tests and intermediate results are available at: &lt;a href=&quot;http://jsperf.com/javascript-classical-versus-prototypal-inheritance&quot; rel=&quot;external&quot;&gt;http://jsperf.com/javascript-classical-versus-prototypal-inheritance&lt;/a&gt;&lt;/p&gt;

&lt;h2 id=&quot;so-now-what&quot;&gt;So now what?&lt;/h2&gt;

&lt;p&gt;I believe that both classical and prototypal inheritance have their place in JavaScript development. If you need the speed (i.e. you create many, many objects) you should probably choose using the new keyword. For most other cases I would at least seriously consider prototypal inheritance.&lt;/p&gt;

&lt;p&gt;I also believe that most JavaScript code is written in a strict class-based approach. If you find yourself choosing for classical inheritance just because &lt;em&gt;‘you are used to that’&lt;/em&gt;, start practicing with prototypal inheritance only to learn its benefits.&lt;/p&gt;

&lt;p&gt;JavaScript has dynamic types, weak types, and &lt;em&gt;no&lt;/em&gt; classes. No matter the inheritance approach you choose this does not change. Don’t attempt to change these rules in your code for you will fail. Value duck typing and prototype objects over type checking and class-like object definitions.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>JavaScript: Objects, Prototypes and Inheritance</title>
        <link href="https://blog.thesoftwarecraft.com/2013/05/javascript-objects-prototypes-inheritance.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-05-09:/2013/05/javascript-objects-prototypes-inheritance</id>
        <updated>2013-05-09T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;JavaScript is a prototype-based object-oriented programming language. The prototype-based programming paradigm characterizes itself by the absence of classes. Reuse of behavior is performed by cloning existing objects, known as prototypes.&lt;/p&gt;

&lt;p&gt;Examples of other prototype-based programming languages include Self and Io, and of course other ECMAScript implementations. [As JavaScript is an implementation of ECMAScript most things in this article are true for other ECMAScript implementations too.]&lt;/p&gt;

&lt;p&gt;Because there are no classes you get an object by simply creating one. For example, the following creates an object that has a single method.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, Bart here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This allows me to introduce myself:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, Bart here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Of course this is nice and all but not of too much help. If I want to create yet another person who can introduce herself I have to duplicate the introduce function. Like class-based programming languages group behavior in classes, we want to group the behavior of a person in an object prototype.&lt;/p&gt;

&lt;p&gt;The prototype would look something like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
  &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To reuse the behavior of this prototype in JavaScript we must manually set its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__proto__&lt;/code&gt;. [This is &lt;em&gt;not&lt;/em&gt; recommended and I’ll get to that in a second.]&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;na&quot;&gt;name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Bart&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, Bart here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Like I said, this is not recommended. I included this example because I want to show that there’s not much magic going on, it’s just all about the objects. The reason for this not being recommended is that &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__proto__&lt;/code&gt; is the internal prototype used for implementing JavaScript’s inheritance.&lt;/p&gt;

&lt;p&gt;In fact, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;__proto__&lt;/code&gt; had the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; before manually changing it to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt;. [This is not exactly true, but consider it the truth for now. I’ll reveal the lie towards the end of this article.] JavaScript tracks the internal prototype on object construction.&lt;/p&gt;

&lt;p&gt;To get JavaScript to automatically track the prototype, the Person object must be rewritten as a constructor object.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;introduce&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Hi, &lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;this&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt; here&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;new&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;Bart&lt;/span&gt;&lt;span class=&quot;dl&quot;&gt;&apos;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;introduce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;[Normally I would pass in &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;name&lt;/code&gt; as a constructor argument on &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt;, but that discussion is beyond the scope of how objects work in JavaScript.]&lt;/p&gt;

&lt;p&gt;Beside defining &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; as a function now, something else changed here. Something important. The &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;introduce&lt;/code&gt; function is no longer an attribute of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Person&lt;/code&gt; but instead of its &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;prototype&lt;/code&gt;. This is necessary because when copying from an object, JavaScript limits access to the object’s prototype, not to its own attributes.&lt;/p&gt;

&lt;p&gt;The way to copy from objects is by using keyword &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;new&lt;/code&gt; on a function. This implies that you can only copy from objects that are actually a constructor and thus objects that have &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function&lt;/code&gt; in their prototype-chain (and most of the time are direct descendants of it). Because &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Function&lt;/code&gt; is an object itself you can use it like any other object and put attributes on it. But these attributes are put on the constructor and are not inherited by descendant objects. Copies only get access to the prototype. That is due to how JavaScript manages the object’s internal prototype. In the above example, the following is true.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-javascript&quot; data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;nx&quot;&gt;bart&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;__proto__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;===&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;Person&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;prototype&lt;/span&gt; &lt;span class=&quot;c1&quot;&gt;// =&amp;gt; true&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This might seem a bit strange, but that is only because it &lt;em&gt;is&lt;/em&gt; a bit strange. JavaScript’s behavior for copying objects is different from other prototype-based programming languages (like Self and Io). In Self an Io objects have constructors themselves and you can copy or clone (respectively) an object.&lt;/p&gt;

&lt;p&gt;I started this article by showing how to manually set up the prototype-chain for an object and that there’s not much special to it. While JavaScript’s management of the internal prototype-chain may seem much like the work of a sorcerer, it’s basically all objects (and some internal state).&lt;/p&gt;

&lt;p&gt;In the beginning I also told you a lie. It should be clear by now that the internal prototype of the object was not &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object&lt;/code&gt; but rather &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Object.prototype&lt;/code&gt;. This is also where the prototype-chain ends.&lt;/p&gt;

&lt;p&gt;Explaining JavaScript objects this detailed helped me to really understand how it works. I hope that writing this down helps you getting a better understanding of JavaScript objects, too.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Exceptions: Checked and Unchecked</title>
        <link href="https://blog.thesoftwarecraft.com/2013/03/exceptions-checked-and-unchecked.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-03-28:/2013/03/exceptions-checked-and-unchecked</id>
        <updated>2013-03-28T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;In the Java community there’s a still this disagreement about whether to use statically checked exceptions versus their unchecked opponent. In this article I provide some facts about exceptions and my personal view on using them.&lt;/p&gt;

&lt;h2 id=&quot;exceptions&quot;&gt;Exceptions&lt;/h2&gt;

&lt;p&gt;An exception is an event that disrupts the normal flow of execution in a program.&lt;/p&gt;

&lt;p&gt;Nowadays almost any major programming language support the raising and handling of exceptions. These languages allow for registering an exception handler (i.e. a catch/rescue block). If an exception is raised the first exception handler up the call stack is invoked.&lt;/p&gt;

&lt;p&gt;Because handling an exception always was optional, OCaml introduced exception checkers. This was an external tool that could be used during compilation to fail when no handler for a particular exception was registered.&lt;/p&gt;

&lt;p&gt;Growing on this idea compile-time checks upon exception handling were added to CLU and Modula-3. These languages check for each exception that is listed in the throws declaration of a function whether a handler is registered up the call stack. Any exception not listed was not checked at compilation time. C++ has a likewise feature known as &lt;em&gt;exception specifications&lt;/em&gt;.&lt;/p&gt;

&lt;h2 id=&quot;checked-exceptions&quot;&gt;Checked exceptions&lt;/h2&gt;

&lt;p&gt;Then checked exceptions as we know them today were added in the first version of Java. The difference with earlier attempts is that Java decides to check an exception based on its class hierarchy, while others decide based on function signatures. Therefore a checked exception is an exception that is checked on compile time. A function that might raise it &lt;em&gt;must&lt;/em&gt; declare it in its signature. A calling function &lt;em&gt;must&lt;/em&gt; either register a handler or declare the exception it its signature as well.&lt;/p&gt;

&lt;p&gt;At the time of introduction the strictness of checked exceptions seemed like a good idea. By having to list all the exceptions that are possibly raised in a functions signature, you could never lack a handler.&lt;/p&gt;

&lt;p&gt;The problem with checked exception wasn’t visible until late after a project entered maintenance. (Remember, at the time agile wasn’t a big thing and most projects were done waterfall style.) As it turned out any change to a function that might raise a checked exception forces its callers to change too (possibly multiple layers of them). And that’s a straight violation of the Open-Closed Principle [OCP]. Micheal Feathers provides more details on this in an excellent chapter on error handling in &lt;a href=&quot;https://amzn.to/4ndxHfi&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Clean Code&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Because of this checked exceptions are a bad idea. After Java no major language adopted the concept (not even C#). And a few years ago C++ deprecated exception specifications in its recent C++11 standard.&lt;/p&gt;

&lt;h2 id=&quot;express-intent&quot;&gt;Express intent&lt;/h2&gt;

&lt;p&gt;As unchecked exceptions don’t violate the OCP, does that mean they should always be used over checked exceptions? In &lt;a href=&quot;https://amzn.to/3KOcJWC&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Effective Java&lt;/a&gt; Joshua Bloch says that if you can reasonably expect your client to recover from the exception, you should consider using a checked exception. Hence any client is &lt;em&gt;forced&lt;/em&gt; to deal with the exception.&lt;/p&gt;

&lt;p&gt;Although this might be considered a good reason to use checked exceptions (especially at the time this great book was written), personally I see their use more as a design smell.&lt;/p&gt;

&lt;p&gt;As Bertrand Meyer writes in his book &lt;a href=&quot;https://amzn.to/4hbzvUH&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Object-Oriented Software Construction&lt;/a&gt; there are only two meaningful ways that a routine can react when an exception occurs: by failure or by retry.&lt;/p&gt;

&lt;p&gt;Using unchecked exceptions a function automatically handles a failure. The failure then is normally passed to some more general handler up in the call stack. Practically this handler logs the failure and display a friendly message to the user.&lt;/p&gt;

&lt;p&gt;If a function should opt a retry (in which case you might doubt to use a checked exception) it would help if this was expressed somehow different from expecting a failure reaction. An exception (checked or unchecked) won’t express this intention quite well.&lt;/p&gt;

&lt;p&gt;In such case I want to communicate the event through the API. Adding a dependency to the target object (whose member function must communicate the error) helps me reason about what the error means in term of the caller. By doing this I can descriptively name the relationship between the caller and the target, and make it explicit that someone must handle the error.&lt;/p&gt;

&lt;p&gt;I hope this might be of help to you and provided you some new insights.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Signing Git Tags</title>
        <link href="https://blog.thesoftwarecraft.com/2013/03/signing-git-tags.html"/>
        <id>tag:blog.thesoftwarecraft.com,2013-03-14:/2013/03/signing-git-tags</id>
        <updated>2013-03-14T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;One of Git’s interesting features is definitely the ability to sign tags. Using your GPG key-pair you can create a signed tag. Anyone with your public key can verify that you are the true creator of that tag.&lt;/p&gt;

&lt;h2 id=&quot;why-you-should-sign-your-tags&quot;&gt;Why you should sign your tags&lt;/h2&gt;

&lt;p&gt;As Git is a distributed version control system (DVCS) you need a to verify the source of a release. Just like you verify checksums of releases you download to make sure it’s the release you expect it to be (you do verify your downloads, don’t you?!), you want to prove that the tag is created by the expected trusted source.&lt;/p&gt;

&lt;p&gt;While signing tags is more important in open-source projects, it’s generally also a good idea for your private projects. See the signing as one of the core team members placing stamp in your repository, as they release a version of their software.&lt;/p&gt;

&lt;p&gt;Also signing tags is dead easy as I’ll show you next.&lt;/p&gt;

&lt;h2 id=&quot;create-a-signed-tag&quot;&gt;Create a signed tag&lt;/h2&gt;

&lt;p&gt;To create a signed tag all you need is a Git repository and a GPG private key you want to use to sign the tag. Don’t worry if you don’t have GPG set up yet, I’ll cover this a little later.&lt;/p&gt;

&lt;p&gt;To make things really easy set your GPG signing key as a global configuration setting. Obviously you don’t need to do this - especially if you have more than one account (e.g. work and private) you want multiple signing keys so set them up per repository (discard the global option).&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git config &lt;span class=&quot;nt&quot;&gt;--global&lt;/span&gt; user.signingkey &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;gpg-key-id]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Now all you need to do is create a tag and sign it.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git tag &lt;span class=&quot;nt&quot;&gt;--sign&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;signed-tag-name]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;h2 id=&quot;verify-a-tag&quot;&gt;Verify a tag&lt;/h2&gt;

&lt;p&gt;Verifying tags is as easy as creating one.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git tag &lt;span class=&quot;nt&quot;&gt;--verify&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;signed-tag-name]&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;However, you need the signer’s public key to be able to verify the commit. Most maintainers put their public key in a special object in the repository - an object that can only be reached from a specific tag (or of course its own SHA).&lt;/p&gt;

&lt;p&gt;You can make your public key available in a tag as follows.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gpg &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--export&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;gpg-key-id] | git hash-object &lt;span class=&quot;nt&quot;&gt;-w&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;--stdin&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;object SHA]

&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git tag &lt;span class=&quot;nt&quot;&gt;-a&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;[&lt;/span&gt;object SHA] maintainer-pgp-pub&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;To import a maintainer’s public key into your keyring you can show the tag.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;git show maintainer-pgp-pub | gpg &lt;span class=&quot;nt&quot;&gt;--import&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Generally it is also a good idea to leave instructions on importing your public key and verifying tags in the tag message.&lt;/p&gt;

&lt;h2 id=&quot;create-a-gpg-key&quot;&gt;Create a GPG key&lt;/h2&gt;

&lt;p&gt;If you don’t have a GPG key pair yet it’s about time to create one. If you’re in doubt most likely you don’t have any but it’s always a good idea to check your list of GPG keys.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gpg &lt;span class=&quot;nt&quot;&gt;--list-keys&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;If you need to, generate a key. Choose the RSA algorithm if you can as DSA only supports up to 1024 bits.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-sh&quot; data-lang=&quot;sh&quot;&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;gpg &lt;span class=&quot;nt&quot;&gt;--gen-key&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you see using GPG and signing Git tags is really simple, so there’s actually no reason not to sign your tags.&lt;/p&gt;

&lt;p&gt;So go and sign and verify!&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Coderetreat: Global Day 2012</title>
        <link href="https://blog.thesoftwarecraft.com/2012/12/coderetreat-global-day-2012.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-12-09:/2012/12/coderetreat-global-day-2012</id>
        <updated>2012-12-09T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Yesterday, Saturday December 8th, the Global Day of Coderetreat 2012 was finally there. As facilitator of the event in Haarlem (The Netherlands) I was quite excited about this event, and think we it was great and much fun.&lt;/p&gt;

&lt;p&gt;Even though we had an event with only 5 people, we really had an awesome day. Throughout the day we did 5 implementations of Conway’s Game of Life, each with different constraints. The constraint for all session was that the universe is infinite (so two-dimensional arrays are probably not the best option).&lt;/p&gt;

&lt;p&gt;The constraints in a coderetreat are extreme. This is the purpose of a coderetreat. By taking practices into the extreme you’ll learn when they can be useful and when to avoid them. On each coderetreat people are strongly suggested &lt;em&gt;not&lt;/em&gt; to write code like that on the job.&lt;/p&gt;

&lt;p&gt;Each session is 45 minutes. When a session ends the code &lt;em&gt;must&lt;/em&gt; be deleted. After each session there’s a short retrospective.&lt;/p&gt;

&lt;h2 id=&quot;warming-up&quot;&gt;Warming up&lt;/h2&gt;

&lt;p&gt;To get everyone started the warming up didn’t have much constraints. The Game of Life must be implemented using clean code and TDD - like usual. This allowed people to get set and into the mood for the rest of the day.&lt;/p&gt;

&lt;p&gt;After 40 minutes I communicated a little change in the requirements. The universe must be 3 dimensional rather than 2d. The remaining few minutes in the session were for each pair to figure out and discuss in how many places they would need to make a change.&lt;/p&gt;

&lt;h2 id=&quot;baby-steps&quot;&gt;Baby Steps&lt;/h2&gt;

&lt;p&gt;The second session was constrained with taking baby steps. Each pair had to set a timer on 2 minutes, write a failing test and implement it (make it green) before the timer rings. Then they commit, reset the timer, and refactor their code before the timer rings. If they didn’t finish within the timer they had to roll back their changes and start over - taking a smaller step.&lt;/p&gt;

&lt;p&gt;To be able to commit changes but still delete the code after the session ends each pair set up a local git repository. As it turned out the pairs struggled a little with git as they weren’t familiar with it. Next time I’ll give more introduction into git, if needed.&lt;/p&gt;

&lt;h2 id=&quot;silent-ping-pong&quot;&gt;Silent Ping-Pong&lt;/h2&gt;

&lt;p&gt;After a nice lunch, the third session focused on how you document your code using tests. Future-you may not fully understand your test while current-you think it’s fine.&lt;/p&gt;

&lt;p&gt;As pairing with future-you is rather hard - or probably impossible if you don’t own a Delorean (and none of the attendees did) - we used the ping-pong pairing technique. One writes a test (tester) and the other implements it (implementer). As you cannot communicate with future-you other than through code, the pairs may only communicate through code in this session. To make the test suite a little more interesting the implementer had the role of evil coder (try to find loop-holes in the tests).&lt;/p&gt;

&lt;p&gt;It’s interesting how the pairs had the same experience I had when I did this session on a coderetreat. You get really far without talking to each other. You won’t be discussing design. You just code. One drives the design, the other implements and refactors.&lt;/p&gt;

&lt;h2 id=&quot;no-return-values&quot;&gt;No return values&lt;/h2&gt;

&lt;p&gt;The fourth session constrains the use of return values: it’s lambda’s all the way down.&lt;/p&gt;

&lt;p&gt;People were using Java all day long, which is really quite awful at this exercise because of Java’s lack of lambda’s. For those programming only in Java this really is a great exercise as it helps them understand what callbacks are about and when they can be useful.&lt;/p&gt;

&lt;p&gt;On a side note: if you only know how program in Java, learn another language.&lt;/p&gt;

&lt;h2 id=&quot;cooling-down&quot;&gt;Cooling Down&lt;/h2&gt;

&lt;p&gt;For the cooling down we did a fish-bowl session. One pair was programming on the big screen, the others were watching. Each 2 minutes one of the pair was replaced. No constraints were applied in this session.&lt;/p&gt;

&lt;p&gt;This session was a really fun and interesting way to finish the day.&lt;/p&gt;

&lt;h2 id=&quot;closing-circle&quot;&gt;Closing Circle&lt;/h2&gt;

&lt;p&gt;At the end of the day we all joined together in the closing circle to each tell briefly about their view on the day. For me it was really interesting to hear how people experienced the day, what they learned, what surprised them and what they’ll take with them.&lt;/p&gt;

&lt;h2 id=&quot;next-year&quot;&gt;Next year&lt;/h2&gt;

&lt;p&gt;Global Day 2012 was an awesome day that takes place in an amazing community. Special thanks goes out to Corey Haines and Jim Hurne for all their hard work on raising the bar for coderetreat events and making sure every location in the world could host an awesome event!&lt;/p&gt;

&lt;p&gt;The most important thing I’ll take with me to next year is to get more people to attend. Even though coderetreat scales down well it is even better with more people.&lt;/p&gt;

&lt;p&gt;That said, yes, I plan to facilitate Global Day 2013 too. Hope to see you next year!&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>Coderetreat</title>
        <link href="https://blog.thesoftwarecraft.com/2012/12/coderetreat.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-12-02:/2012/12/coderetreat</id>
        <updated>2012-12-02T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;The Sunday after the &lt;a href=&quot;/2012/11/scna.html&quot;&gt;SCNA conference&lt;/a&gt; a code retreat was held at 3 locations in Chicago. I attended the one at the 8th Light office, hosted by Corey Haines.&lt;/p&gt;

&lt;p&gt;Coderetreats are really awesome! With global day being in a week from now I like to tell a little about coderetreats and why you want to join us on global day.&lt;/p&gt;

&lt;h2 id=&quot;so-what-is-a-coderetreat&quot;&gt;So what is a coderetreat?&lt;/h2&gt;

&lt;p&gt;Besides being just really awesome, a coderetreat is a day of intensive practice, focusing on fundamentals of programming and software design.&lt;/p&gt;

&lt;p&gt;The essence is in &lt;em&gt;practice&lt;/em&gt;. A coderetreat offers you a safe environment to practice a day long together with other programmers, without having to worry about getting things done.&lt;/p&gt;

&lt;p&gt;By doing the same kata multiple times with different constraints, you’ll learn when these constraints can be useful and when to avoid them. Examples of constraints are to not use if-statements, use at most 2 lines of code in a function, or not to use return values.&lt;/p&gt;

&lt;h2 id=&quot;why-we-need-practice&quot;&gt;Why we need practice&lt;/h2&gt;

&lt;p&gt;In most professions regular practice is mandatory. For example, would you want to see a band that only plays together when they perform live on stage? You would probably only go see them once, and find them rather disappointing.&lt;/p&gt;

&lt;p&gt;Programmers also need constant practice to improve their skills. Programming is hard. Becoming good at it takes a lot of practice. And as part of becoming better, we’ll learn about even more things we want to practice. Like a band we also need a safe environment to practice. We cannot experiment in our live performance -our every day job- as it would hurt our customers.&lt;/p&gt;

&lt;p&gt;A coderetreat session provides you this environment. Besides practice being important, practicing in a coderetreat is really fun too; program all day with other people, without the urge to deliver!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update&lt;/strong&gt; obviously there’s no way you’ll make it to Global Day of Coderetreat 2012, so I deleted the registration information. If you like programming, follow the annual &lt;a href=&quot;http://gdcr.coderetreat.org/&quot; rel=&quot;external&quot;&gt;Global Day of Coderetreat&lt;/a&gt; event and attend a location near to you.&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>SCNA 2012</title>
        <link href="https://blog.thesoftwarecraft.com/2012/11/scna.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-11-25:/2012/11/scna-2012</id>
        <updated>2012-11-25T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;The &lt;a href=&quot;http://scna.softwarecraftsmanship.org/&quot;&gt;Software Craftsmanship North America&lt;/a&gt; (SCNA) 2012 conference was held at November 9 and 10 on the top floor of the AON Center in Chicago, IL.&lt;/p&gt;

&lt;p&gt;Proud to be one of the few Europeans to attend this great conference, in this post I write about my personal highlights.&lt;/p&gt;

&lt;p&gt;As this post is becoming pretty large I just have to skip over some talks. As the conference was amazing with great speakers, the talks I skip I still found very interesting.&lt;/p&gt;

&lt;h2 id=&quot;opening&quot;&gt;Opening&lt;/h2&gt;
&lt;p&gt;Micah Martin -co-founder of 8th Light- opens the conference with a brief chat about software craftsmanship and a short introduction to the conference.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&quot;This conference is not about specific technologies or programming languages. This conference is about attitude.&quot;
&lt;small&gt;Micah Martin&lt;/small&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2 id=&quot;cory-foy&quot;&gt;Cory Foy&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;When Code Cries: Listening to What Your Code is Saying&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The honor of having the first talk of the conference was for Cory Foy. In this great talk he discussed code quality and communication.&lt;/p&gt;

&lt;p&gt;A really interesting point that he made is that the quality of code is not something static. Code quality happens at the interaction point of the code base: when we try to understand it, modify it, or when the code is used by people. I.e. quality code is code that actually gets used and is a joy to work with. Code cries when it is not understood at the interaction point.&lt;/p&gt;

&lt;p&gt;Cory made a comparison between the course of the Mississippi river and the course of design. We tend to feel the need to control things. We want to control the course of the Mississippi river even though it is natural for a river to change course over time. We just happen to like how it flows next to Orleans and we do anything we can to prevent it to naturally flow another course. We fear the natural change of the river.&lt;/p&gt;

&lt;p&gt;Much in the same way we tend to control software design. The first time you use TDD to drive the design is quite fearful. After you get used to it and allow letting design flow more naturally, you learn that tests drive towards a design that is fully applicable to the situation on hand.&lt;/p&gt;

&lt;p&gt;Further Cory explained the Foy-Z technique, a model that captures the way people learn to program in a simple yet elegant way. The model moves from cognitively undemanding (i.e. without clients, dead lines, etc) with context embedded to a cognitively demanding with context reduced, in a few steps.&lt;/p&gt;

&lt;p&gt;With a few quotes from Christopher Alexander’s classic &lt;a href=&quot;https://amzn.to/4n6sc1V&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;The Timeless Way of Building&lt;/a&gt;, this fantastic talk by Cory belongs to the best of the conference in my opinion.&lt;/p&gt;

&lt;h2 id=&quot;sarah-allen&quot;&gt;Sarah Allen&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Crafting Software Products&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This talk is about product development. Sarah Allen told the story of one of her customers asking for a mobile app. Rather than coding up the MVP (Minimum Viable Product) she took a much different approach.&lt;/p&gt;

&lt;p&gt;Together with her customer she examined the target audience of the mobile app and what they would expect of the app. Using ads to draw attention to prototypes (mainly static images and web pages) and various measurement techniques help to learn the target audience and their expectations of the app.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&quot;Potential users should not be the target audience of an app, but those who will love and crave using the product&quot;
&lt;small&gt;Sarah Allen&lt;/small&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;She referred to this as test-first product development: to start with a hypothesis, then test it, analyze the test results and learn from it. Then adapt to what you learned and reiterate, leading from a belief (the hypothesis) to knowledge (learned from the tests).&lt;/p&gt;

&lt;h2 id=&quot;adewale-oshineye&quot;&gt;Adewale Oshineye&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;What Happens When Software Craftsmanship Meets User Experience?&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Adewale Oshineye talked about usability of the tools and APIs developers use. Even though most tools are well designed, well made, very useful, they are not very usable.&lt;/p&gt;

&lt;p&gt;Many tools developers use on a daily basis -like Git, Vim, Emacs- suffer usability. We use them anyway because they are the least worst tools out there.&lt;/p&gt;

&lt;p&gt;Using the example of a glass door with the text ‘push’ on the one side and the text ‘pull’ on the other he makes the case to fix usability rather than document how to use it.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&quot;A poor workman blames his tools&quot;
&lt;small&gt;Adewale Oshineye&lt;/small&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It’s very interesting because if anyone should be able to fix these problems in software it should be the people creating these products. Yet most APIs (and also tools) are designed from the perspective of its creator, not of its user.&lt;/p&gt;

&lt;h2 id=&quot;robert-martin&quot;&gt;Robert Martin&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;The Reasonable Expectations of Your CTO&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Uncle Bob goes over the attitude of professionals. Simply stated, professional programmers:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;don’t ship shit&lt;/li&gt;
  &lt;li&gt;know when to say no&lt;/li&gt;
  &lt;li&gt;don’t lie about estimates&lt;/li&gt;
  &lt;li&gt;are eager to learn&lt;/li&gt;
  &lt;li&gt;invest time to improve their skills and work on their craft&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The most reasonable thing for a CTO to expect of programmers is for them to behave professional.&lt;/p&gt;

&lt;p&gt;In addition, a CTO can reasonably expect work to be done at a stable speed. Don’t start very fast and slow down along the way by making a mess. Also be fearless of the code you create; fearfulness is unacceptable. This means you can make changes to code easily without breaking it, which implies good design, clean code, and a healthy safety net. The reason we call this SOFTware is that it is easy to change.&lt;/p&gt;

&lt;p&gt;Another thing is that QA should not find anything. And when they do the team should find out how that happened, learn from it, and prevent it to happen the next time.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&quot;QA is not your debugger&quot; &lt;small&gt;Uncle Bob&lt;/small&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Having read his book on professionalism - &lt;a href=&quot;https://amzn.to/3W3kKcE&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;The Clean Coder&lt;/a&gt; - the attitude that can be expected of professionals was not new to me. Still talks from Robert Martin are always great. He’s a very experienced speaker, and you’ll learn something more about science every time.&lt;/p&gt;

&lt;h2 id=&quot;jen-myers&quot;&gt;Jen Myers&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Developers Can’t Understand Design&lt;/em&gt;&lt;/strong&gt;&lt;span style=&quot;color: red;&quot;&gt;*&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: red;&quot;&gt;*&lt;/span&gt;
&lt;em&gt;and other completely mistaken design myths&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Jen Myers tried to clear many of the common misconceptions about the work of a designer. Using a lot of quotes and great stories she made clear that designers do not &lt;em&gt;just&lt;/em&gt; make things pretty, they make things work.&lt;/p&gt;

&lt;p&gt;Using their own set of tools (like fonts, color theories, ui patterns, user testing, etc) designers make a product work for the intended audience. Therefore it is important for designers and developers to work together and have much interaction, unlike the common task handed over to a designer by a developer: “it works, please make it look pretty”.&lt;/p&gt;

&lt;p&gt;She introduces the concept &lt;em&gt;digital-native designer&lt;/em&gt;, who knows how to do visual and user experience design, and can implement it. For developers who want to learn about design, David Kadavy’s book &lt;a href=&quot;https://amzn.to/46Rxc5u&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Design for Hackers: Reverse Engineering Beauty&lt;/a&gt; could be good start.&lt;/p&gt;

&lt;h2 id=&quot;michael-feathers&quot;&gt;Michael Feathers&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Testing Revisited&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this wonderful talk Michael Feathers revisits testing. He starts with some sweetness.&lt;/p&gt;

&lt;p&gt;Tests are sweet. They help us to think, precisely detect change, serve as documentation, and to get to done. Tests give us a hard line, 0 or 1.&lt;/p&gt;

&lt;p&gt;Tests that serve more than one purpose may become harder to change. As with all code, a test should focus on one thing. This is where the SRP (Single Responsibility Principle) is applied in tests too.&lt;/p&gt;

&lt;p&gt;He steps through some common problems with larger test suites.&lt;/p&gt;

&lt;p&gt;Some teams phase that they are held hostage by their tests. Instead of supporting them to change the system these teams suffer from many failing tests if they try to make a change. This makes modifications to the system extremely hard.&lt;/p&gt;

&lt;p&gt;Also tests can cause systems to increase heavily in build time. At first this does not seem like a big deal but as the system grows it becomes more and more time consuming to run all tests, to end with not running the tests anymore as it would take too long.&lt;/p&gt;

&lt;p&gt;To the build time problem Michael proposes an interesting solution: to set a build budget. This forces the improvement of tests to not run out of the budget. It also makes a good case to do this to your customer.&lt;/p&gt;

&lt;p&gt;Further he talks about deleting tests that are no longer of use or not really necessary. And to option of maintaining multiple suites and run only some of them.&lt;/p&gt;

&lt;h2 id=&quot;gary-bernhardt&quot;&gt;Gary Bernhardt&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Boundaries&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Gary Bernhardt kicked off the second day of SCNA with his talk about boundaries and isolation.&lt;/p&gt;

&lt;p&gt;Code is often tested in isolation by using stubs and mocks. While these serve isolation, they might be different from the real world. Using tools to check test doubles against a contract (such as rspec-fire, static typing, or integration tests) allows for writing tests that help catch these differences.&lt;/p&gt;

&lt;p&gt;In this talk Gary shows a natural isolation of code: through value boundaries. In his presentation he uses examples from a Twitter client he wrote and talked about in a few &lt;a href=&quot;http://www.destroyallsoftware.com/&quot; rel=&quot;external&quot;&gt;DAS&lt;/a&gt; episodes. If you haven’t seen his screencasts, make sure to watch them soon, they are really awesome!&lt;/p&gt;

&lt;p&gt;He separated the program into a functional core that contains the intelligence, surrounded by an imperative shell that brings in the dependencies.&lt;/p&gt;

&lt;p&gt;After going through the following table of known paradigms, he introduces the name for this approach that combined functional and OO: Faux-O.&lt;/p&gt;

&lt;table class=&quot;table&quot; style=&quot;width: 50%&quot;&gt;
  &lt;thead&gt;
    &lt;tr&gt;
      &lt;th class=&quot;text-right&quot;&gt;&lt;/th&gt;
      &lt;th class=&quot;text-center&quot;&gt;mutation&lt;/th&gt;
      &lt;th class=&quot;text-center&quot;&gt;data &amp;amp; code&lt;/th&gt;
    &lt;/tr&gt;
  &lt;/thead&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td class=&quot;text-right&quot;&gt;procedural&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;yes&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;separated&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class=&quot;text-right&quot;&gt;OO&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;yes&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;combined&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class=&quot;text-right&quot;&gt;functional&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;no&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;separated&lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
      &lt;td class=&quot;text-right&quot;&gt;faux-O&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;no&lt;/td&gt;
      &lt;td class=&quot;text-center&quot;&gt;combined&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;Functional code is much easier to test than imperative code as it has no side effects. You can test a function by its values, which are the natural boundaries of a function. Values also afford shifting process boundaries, of which the actor model is an example.&lt;/p&gt;

&lt;h2 id=&quot;sarah-gray&quot;&gt;Sarah Gray&lt;/h2&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;Visualizing Enumerable: Own Abstract Concepts Through Physicalization&lt;/em&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Sarah talked about beginners, and how they require different kinds of help based on their background. She shared her stories of mentoring different beginners, and how often she’s reminded of the difficulties beginners face.&lt;/p&gt;

&lt;p&gt;What help for example is it to point a beginner to the Ruby Docs? She used the example of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Enumerable&lt;/code&gt; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select&lt;/code&gt; method. In this definition of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select&lt;/code&gt; is so much context implied, that it would probably blows a beginners mind.&lt;/p&gt;

&lt;p&gt;Sarah’s solution to this was to visualize these concepts by drawing pictures of what the lines of code actually do. She showed a picture explaining &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;select&lt;/code&gt; with her select-cat, selecting balls by color and dropping everything else in the ocean.&lt;/p&gt;

&lt;p&gt;Even though select-cat is not really friendly to the environment by dropping stuff in the ocean, she really uses a great way to visualize concepts for beginners.&lt;/p&gt;

&lt;p&gt;Sarah also talked about how people learn in different ways, and that some require more visualization than others. I found her talk one of the most inspiring of the conference.&lt;/p&gt;

&lt;h2 id=&quot;kata-battles&quot;&gt;Kata Battles&lt;/h2&gt;

&lt;p&gt;On each day a kata battle was organized. This is a contest where both contestants would perform the Coin Changer kata live in front of all conference attendees. The winner was determined by the audience based on speed, style, creativity, etc.&lt;/p&gt;

&lt;p&gt;The battles were a lot of fun to spectate, and I can only imagine also fun to perform (although under the huge pressure of the watching audience).&lt;/p&gt;

&lt;h2 id=&quot;fish-bowl&quot;&gt;Fish bowl&lt;/h2&gt;

&lt;p&gt;On day one during lunch a fish bowl was organized.&lt;/p&gt;

&lt;p&gt;Programmers rotate in as pair working on a software exercise. The exercise was the Clojure version of the  Gilded Rose kata. The audience can observe, heckle, or help solve the problem.&lt;/p&gt;

&lt;p&gt;Fish bowls are both fun and instructive.&lt;/p&gt;

&lt;h2 id=&quot;jeopardy&quot;&gt;Jeopardy&lt;/h2&gt;

&lt;p&gt;On day two during lunch a Jeopardy of software trivia was organized. The contestants were Cory Foy, Corey Haines, and Gary Bernhardt.&lt;/p&gt;

&lt;h2 id=&quot;in-conclusion&quot;&gt;In conclusion&lt;/h2&gt;

&lt;p&gt;When I went to Chicago to attend SCNA I had high expectations of the conference. The schedule showed really experienced speakers, and some great entertainment. And SCNA managed to completely beat my expectations.&lt;/p&gt;

&lt;p&gt;I find this an amazing conference, with very experienced speakers, great entertainment - I didn’t even mention the brewery trip, which was lots of fun! - and very nice, like-minded people.&lt;/p&gt;

&lt;p&gt;I’ll try very hard to attend next year, so hope to see you there!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Continuous Delivery Workshop</title>
        <link href="https://blog.thesoftwarecraft.com/2012/09/continuous-delivery-workshop.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-09-28:/2012/09/continuous-delivery-workshop</id>
        <updated>2012-09-28T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;On Saturday September 15, I attended a free workshop about continuous delivery by Robert Chatley, in Amsterdam. This workshop was organized by Ugly Duckling.&lt;/p&gt;

&lt;p&gt;Continuous delivery is most about getting feedback much faster than with traditional (i.e. conservative) delivery strategies. Traditionally releasing is error-prone and time consuming, and often quite stressful. Teams and project managers want to release as few times as possible because it is so hard. The agile mindset teaches that if something is this hard you should it more often (practice it) so that you get better at it. Continuous delivery adopts this mindset and is about removing the bottlenecks from the delivery process, so that you can deliver in minutes rather than hours (or sometimes even days), at any time.&lt;/p&gt;

&lt;p&gt;The workshop was really fun and introduced the concepts of continuous delivery in a playful manner. We were given a small Java application, containing a web server, HTML interface and API interface (which I’ll come back to a little later). As a good workshop befits we worked in pairs.&lt;/p&gt;

&lt;p&gt;About the first thing you always need is some sort of source code management system. We used GitHub, created an empty repository and pushed the provided source code as first commit.&lt;/p&gt;

&lt;p&gt;Then we set up continuous integration using &lt;a href=&quot;http://www.jetbrains.com/teamcity/&quot; rel=&quot;external&quot;&gt;TeamCity&lt;/a&gt;, on a provided virtual server. I hadn’t seen much of TeamCity before this workshop and it’s ease of use really quite surprised me. Much more so than Jenkins ever did. TeamCity lives up to the high standards of its creator JetBrains (known from tools like IntelliJ and RubyMine). To make it ever better TeamCity comes with a free professional license, allowing up to 20 build configurations per server.&lt;/p&gt;

&lt;p&gt;To deploy the application we used &lt;a href=&quot;http://www.heroku.com/&quot; rel=&quot;external&quot;&gt;Heroku&lt;/a&gt;. With Heroku you can easily deploy a Ruby, Clojure, Python, Scala, Java, or Node.js application. To deploy something you push your code to Heroku using Git. I.e. you add your Heroku deployment as a remote and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push&lt;/code&gt; to it. If you don’t know Git that well, see &lt;a href=&quot;http://www.kernel.org/pub/software/scm/git/docs/git-remote.html&quot; rel=&quot;external&quot;&gt;git-remote(1)&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;To make deployment even more easy (and to allow for automatic deploys later on), we set up a deployment build in TeamCity. We cloned the GitHub repository on the continuous integration server, added the Heroku remote and set up a TeamCity build configuration to run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git pull&lt;/code&gt; and &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git push heroku master&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Now this may seem like a lot of work, it actually is quite trivial to set up and use. But continuous delivery is about doing this continuous and the way to do something continuous is to automate it.&lt;/p&gt;

&lt;p&gt;To automate deployment our application when something is changed (i.e. changes are pushed to GitHub) we used TeamCity to trigger the deployment build after a successful continuous integration build. This continuous integration build was already set up to trigger on SCM changes.&lt;/p&gt;

&lt;p&gt;Now only a few minutes after pushing changes to the GitHub repository the changes were deployed in production.&lt;/p&gt;

&lt;p&gt;When you have a deployment chain like this it is a good idea to have the possibility to rollback a deployment. With Heroku you can perform rollback from the command line (&lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;heroku rollback&lt;/code&gt;). To make rollbacks more accessible a TeamCity build configuration can be created to perform the rollback.&lt;/p&gt;

&lt;p&gt;After we did all this, it was about time to see the continuous delivery of added value in action. The organizers used a nice way to show this. The application we were handed earlier had an API that allowed to register the application as player in a game. Once the game started your application got lots of different questions to answer. You scored on each right answer, lost points on each wrong answer, and neither scored nor lost when your app didn’t know (yet).&lt;/p&gt;

&lt;p&gt;To play the game we needed to implement the answering of certain questions. To get the questions we watched the application log from Heroku and read the requested URLs from it (also some logging could be added to the application by printing to stdout, but we didn’t need that).&lt;/p&gt;

&lt;p&gt;We choose to push changes after each implemented answer, so that we gained points (i.e. increased our business value) quickly. We also used TDD to implement the answers. We wanted to go fast, really fast, to score as much as we could, so we couldn’t afford &lt;em&gt;not&lt;/em&gt; to use TDD. As we went, we wrote a failing test, fixed the test, refactored, and committed, and repeated this this cycle until we got a feature fully implemented. Then we pushed the changes causing a production deployment within a few minutes. At the end of the game we were ranked second.&lt;/p&gt;

&lt;p&gt;After discussing the different approaches to the game it was nice to see that almost everybody used some form of tests to implement the features, and that most of those who didn’t write tests were ranked very low.&lt;/p&gt;

&lt;p&gt;While playing this game we used the deployment pipeline to go all the way into production. This is useful to get as much feedback as possible, but not a deal breaker for using continuous delivery. Once you’ve automated the deployment pipeline, you control where you want automatic delivery (on changes) to stop. This should be an environment from which you get feedback (i.e. no development environment) but depending on the project might be some test environment for your customer rather than production. This can even put the control of production deployments at your customer. If he or she approves of the changes he or she can push a button to instantly deploy the changes to production. And if the changes are not well-received, you’ll get that valuable feedback.&lt;/p&gt;

&lt;p&gt;The workshop made me really enthusiast on continuous delivery. To learn more I bought the book &lt;a href=&quot;https://amzn.to/4h8urjR&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Continuous Deployment&lt;/a&gt; by Jez Humble and David Farley.&lt;/p&gt;

&lt;p&gt;As a last note here I like to thank to &lt;a href=&quot;http://uglyduckling.nl/&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Ugly Duckling&lt;/a&gt; for doing this great workshop. They were very well-prepared, including providing a virtual server for each team. Thank you for doing this free workshop it was a free Saturday well spent!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Professionalism</title>
        <link href="https://blog.thesoftwarecraft.com/2012/07/professionalism.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-07-12:/2012/07/professionalism</id>
        <updated>2012-07-12T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Last night I read a tweet from Alan Cooper, the father of Visual Basic. His tweet read:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;IMHO, the problem is that people (managers) keep looking for shortcuts. THERE ARE NO SHORTCUTS IN SOFTWARE!!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Very good point on the shortcuts. Shortcuts in software always turn out to be detours. By taking a shortcut -no matter how small- you always delay your team and almost always in the very near future. You don’t want to be responsible for your team’s delay, do you?!&lt;/p&gt;

&lt;p&gt;But this tweet should not only mention managers as the people looking for shortcuts. Isn’t the programmer the one that takes the actual shortcut? Sure thing it’s more convenient to blame someone else. But the programmers are the ones taking the shortcuts, not the managers.&lt;/p&gt;

&lt;p&gt;Though managers can be quite pushy, it’s not fair to only blame them for any taken shortcuts. When a manager tries to push for a shortcut, the programmers have to say no. And they must stick with it, as a team.&lt;/p&gt;

&lt;p&gt;A professional programmer knows not to take shortcuts. He or she would try to find out the reason why the manager is pushing for early delivery and what he want it for. And a professional manager would openly discuss his needs with the programmers. Together they can work out a solution that is mutually agreeable.&lt;/p&gt;

&lt;p&gt;If you want to learn more on becoming a professional programmer -and you should- make sure to read &lt;a href=&quot;https://amzn.to/436Xm1O&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;The Clean Coder&lt;/a&gt;, an excellent work by Robert C. Martin.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Building Web Services with Haste</title>
        <link href="https://blog.thesoftwarecraft.com/2012/06/building-web-services-with-haste.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-06-12:/2012/06/building-web-services-with-haste</id>
        <updated>2012-06-12T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;A few weeks ago a customer needed two web services. One of them needed to return an image, the other some data in XML document form. The contract of these services didn’t really matter. And of course, haste was required. They had other companies who wanted to start their integration with these services.&lt;/p&gt;

&lt;h2 id=&quot;haste&quot;&gt;Haste&lt;/h2&gt;

&lt;p&gt;It’s quite common that haste is required when adding value to software. Most of the time customers are in a hurry, meaning that they want to add the value as soon as possible.&lt;/p&gt;

&lt;p&gt;This shouldn’t be thought of as annoying. In fact it can be rather helpful to know that haste is required. It means that there is something to focus on first.&lt;/p&gt;

&lt;p&gt;You must never deal with haste by rushing into an implementation. Take the time to interact with your customer and end users. That allows you to do what’s necessary, and doing it right the first time.&lt;/p&gt;

&lt;p&gt;We started on these web services with an attempt to define their contracts. These contracts are a great start for communication with the end users, the parties waiting to integrate with the services.&lt;/p&gt;

&lt;p&gt;Then we left the web service contract part for what is was, and implemented the use cases. We didn’t bother to look at the web service view once. This put focus on the use cases, while we kept all options open for feedback on the contract.&lt;/p&gt;

&lt;h2 id=&quot;feedback-first&quot;&gt;Feedback-First&lt;/h2&gt;

&lt;p&gt;After a few days the use cases were implemented. So we could continue with the view layer. In these few days we got the first feedback on the contract. We adjusted the contract and implemented the web services view on top of the use case implementations. We implemented these web services feedback-first.&lt;/p&gt;

&lt;p&gt;Sure, I know the style we used to build the services is called contract-first. But defining a contract first, is all about communication and early feedback. It well-supports the lean principle “&lt;em&gt;everybody, all together, from early on&lt;/em&gt;”. To emphasize this I like &lt;em&gt;feedback-first&lt;/em&gt; in this post.&lt;/p&gt;

&lt;p&gt;By the way, you should use contract-first development style for any service you build. I don’t elaborate on all the benefits here - use your Google skills to learn about this style.&lt;/p&gt;

&lt;h2 id=&quot;deferring-the-view&quot;&gt;Deferring the view&lt;/h2&gt;

&lt;p&gt;Now it’s easy to say that you should first define a web service contract, then leave it to generate feedback, and later on implement it on top of a web service agnostic application, &lt;em&gt;after&lt;/em&gt; you got the feedback. But in order to do this, your architecture must allow you to defer the view. That means that your view must not be an integral part of the application, but instead, a plugin.&lt;/p&gt;

&lt;p&gt;If you defer the view until you &lt;em&gt;know&lt;/em&gt; how to build it, you can focus on getting the functionality done. And after you have sufficient feedback on the view, you implement it and plug it into the application.&lt;/p&gt;

&lt;p&gt;In fact, deferring the view is only the technical consequence of deferring the implementation details for a particular stakeholder. And to be able to do that, you must make sure that the various stakeholders in your system are separated from each other. E.g. the use cases are driven by a different stakeholder than the web service view, so they must be decoupled.&lt;/p&gt;

&lt;h2 id=&quot;architecture&quot;&gt;Architecture&lt;/h2&gt;

&lt;p&gt;So the architecture must partition the system so that different stakeholders are separated. Taking the image service as example here, on forehand I saw three stakeholders:
our customer, who drives the use case,
the parties integrating with the services, who drive the view,
a virtual data stakeholder
Hence they served the primary partitioning of the system. For the sake of this post, the virtual data stakeholder is left out - in the actual implementation it is decoupled by an image entity gateway.&lt;/p&gt;

&lt;p&gt;The next picture gives you an idea of the partitioning.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;http://yuml.me/9d1f8de6&quot; alt=&quot;Image Service Partitioning&quot; title=&quot;Image Service Partitioning&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The ImageWebService does all the translation of the web request into a vanilla data structure and passes it to the control object, Viewer. The Viewer performs all the use case interactions, and displays the image on the interface object, Display, which in fact is implemented by the ImageWebService (or some class close to it).&lt;/p&gt;

&lt;p&gt;The partitioning allows to defer the implementation of the view and the data layer. It allows you to focus on use cases first, without bothering about the data or the view. Plus any part of the system whose implementation can be deferred, can also be changed or even replaced without affecting the rest of the system.&lt;/p&gt;

&lt;h2 id=&quot;learn-more&quot;&gt;Learn more&lt;/h2&gt;

&lt;p&gt;Uncle Bob did a &lt;a href=&quot;http://www.cleancoders.com/codecast/clean-code-episode-7/show&quot; rel=&quot;nofollow&quot;&gt;Clean Coders episode&lt;/a&gt; on architecture; watch it if you haven’t. The architecture he talks about is much inspired by the work of &lt;a href=&quot;http://en.wikipedia.org/wiki/Ivar_Jacobson&quot; rel=&quot;external&quot;&gt;Ivar Jacobson&lt;/a&gt;, which he wrote about in his book &lt;a href=&quot;https://amzn.to/4h6cZwa&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Object Oriented Software Engineering: A Use Case Driven Approach&lt;/a&gt;.&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>About Code Coverage</title>
        <link href="https://blog.thesoftwarecraft.com/2012/06/about-code-coverage.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-06-05:/2012/06/about-code-coverage</id>
        <updated>2012-06-05T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;The discussion about the number of code coverage you must have, seems to gain more and more attention lately. On the one side there are the people who write their tests after the fact and their managers, claiming numbers that vary more or less in the range of 60-80%. On the other side there are the people who do TDD, who wonder how you can get anything less than about 100%.&lt;/p&gt;

&lt;p&gt;Sure thing I am from the TDD camp. But I’m not going to write another post that claims that you must have 100% code coverage. Well, I might just a bit, but I’ll provide good reasoning. Moreover I want to make the point that putting down any number of coverage that is sufficient, does not improve the actual coverage.&lt;/p&gt;

&lt;p&gt;A project I recently worked on, the goal was set, of course after a lot of discussion, to 80%. Project managers interpreted anything less as lack of quality, and anything more as waste (or as they called it, ‘overkill’). As a result I was asked why my code was about 95% covered by tests. In their eyes I was ‘wasting time by writing more tests than necessary’. I am anything but unwilling to cut waste, and I asked what 20% of the features didn’t matter, so I could cut even more waste by skipping the tests and the production code.&lt;/p&gt;

&lt;p&gt;As you might expect, the project manager didn’t like any bit of that question. ‘Of course the features matter, it’s just that testing them is overkill’, he said.&lt;/p&gt;

&lt;p&gt;So why do I think discussing a target number of code coverage (goal) for a project, or worse, in general, is nonsense? First of all, the goal is not something to discuss about. The only possible goal is 100%! Sure, 60% is a milestone, and so is 80%, as is 90%. But the only goal you can ever set is 100%.&lt;/p&gt;

&lt;p&gt;And how could it be anything else? Setting the goal to anything lower is an obvious statement that a part of your code doesn’t matter. A vicious statement that for a part of the customer’s or end users’ problems, it doesn’t matter if they get solved. If that’d be true, why waste time on solving these problems in the first place? Everything you build adds value to the system and therefore the goal is to test it, or you shouldn’t be building it in the first place.&lt;/p&gt;

&lt;p&gt;You probably won’t ever reach 100% code coverage, though. On a class level you will, but not for an entire project. In fact, I find projects with 100% overall coverage quite suspicious. Such a project smells like the focus was on gaining test coverage rather than using tests to drive the system.&lt;/p&gt;

&lt;p&gt;I use TDD for a while now. And I use code coverage to look for problems in my tests. Yes, that’s right, I don’t care about the number. Normally my coverage is about 95-99%, but again, I don’t care. What I care about is the feasible coverage, and make sure that I covered everything feasible. And I use code coverage tools to look for any weak spots. But that I’ll discuss in another post.&lt;/p&gt;

&lt;p&gt;With feasible coverage I refer to the code that you can possibly test. There will be some lines of code in any project that you cannot get under test. An example is I/O in java. Any interaction with I/O requires you to handle IOException. You handle it and throw a properly named runtime exception instead of the checked one (and of course include its cause). You cannot test any of the possible I/O failures, and you should not. Oh sure, there are some ways to throw these exceptions; you can use one of the nifty mocking frameworks to apply all the reflection necessary to be able to throw that exception. But again, you should not.&lt;/p&gt;

&lt;p&gt;It doesn’t matter that you cannot get every line of code under test. What matters is that you maintain a constant drive to improve the tests and the production code. Therefore the only sensible goal of code coverage is 100%, and again, we’ll never reach that goal.&lt;/p&gt;

&lt;p&gt;And this is why I find the discussion about a level of coverage to be wrong. The behavior you stimulate by setting a minimum amount of code coverage, is to reach that level no matter what. This leads to writing bad tests - e.g. tests without asserts. These tests will only get in the way when you make changes to the system rather than helping you to make changes. In an earlier post I wrote about &lt;a href=&quot;/2012/04/art-of-testing.html&quot;&gt;the art of testing&lt;/a&gt; and how the primary purpose of tests is to help you refactor. As soon as tests stop helping you modify the system, you should rethink your tests!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Life's not about Frameworks</title>
        <link href="https://blog.thesoftwarecraft.com/2012/05/lifes-not-about-frameworks.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-05-24:/2012/05/lifes-not-about-frameworks</id>
        <updated>2012-05-24T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Last week I came across a post on Java Code Geeks about how “ORM haters don’t get it”. The author rambles on about how the arguments of the so-called &lt;em&gt;haters&lt;/em&gt; are wrong, and he explains his understanding of ORM and how he claims that without using ORM from the start, every project eventually ends up with a home-grown ORM-like framework.&lt;/p&gt;

&lt;p&gt;I don’t place myself in a group of ORM &lt;em&gt;haters&lt;/em&gt;, and frankly I have to agree with the author that a lot of bashing against ORM is going on lately. Partly this is because other, more simple, techniques became available to interact with the database, such as the Active Record. Another reason is the trend of using NoSQL over relational databases.&lt;/p&gt;

&lt;p&gt;Whether or not you use ORM, Active Record, NoSQL, or any other way to serialize data, is up to you. This post is not about data access specifically. What I want to talk about is the fanatic embracing of frameworks. We tend to put frameworks and other tools (such as the database) at the center of our application. Uncle Bob &lt;a href=&quot;http://blog.8thlight.com/uncle-bob/2012/05/15/NODB.html&quot;&gt;blogged&lt;/a&gt; about how the database is just a detail, last week. I want to include frameworks in this discussion.&lt;/p&gt;

&lt;p&gt;Frameworks - most of them anyway - ease the implementation of application components and features. Sticking to the ORM example, ORM helps us to interact with the database by communicating with it in type-safe data structures. Another example is Google Guice that, though not in all cases, eases applying the Dependency Inversion Principle. These frameworks are indeed of great help to us, programmers. If you need ORM you should use an existing framework; it makes no sense to implement ORM yourself.&lt;/p&gt;

&lt;p&gt;But that doesn’t mean these frameworks should be at the center of our application. An application is always about its use cases. Neither the customer nor its end users care about frameworks. Frameworks are implementation details, and use cases are at the center of our application.&lt;/p&gt;

&lt;p&gt;How many times have you told a customer or end user, that the changes they want are not feasible because the architecture or a framework does not support it? The answer, most likely, is too many! Customers and end users do not care about implementation details, and they shouldn’t. They care about their own needs. The end users care about their processes and how to ease them, while the customer cares about its market.&lt;/p&gt;

&lt;p&gt;So, then how must we work with frameworks? The answer to that is simple; do not embrace them! We should not trust them with our lives, nor with the life of the application! We must keep all frameworks at arms-length, keep them as details to the implementation.&lt;/p&gt;

&lt;p&gt;If we keep those frameworks at arms-length, we can replace them when they no longer support our customer and end users. And I mean all frameworks, no matter how unlikely to change. If the customer or end user wants changes that the framework is not capable of, we replace the implementation with one that supports those changes. We should never have to tell the customer that something is not possible because of the frameworks we chose. It’s our responsibility to keep the options open, support and embrace future change, by deferring decision as long as we can. Any decision that you can defer, is a decision that you can change!&lt;/p&gt;

&lt;p&gt;I strongly suggest you to watch &lt;a href=&quot;http://www.cleancoders.com/&quot;&gt;Clean Coders&lt;/a&gt; episode 7, about &lt;a href=&quot;http://www.cleancoders.com/codecast/clean-code-episode-7/show&quot; rel=&quot;nofollow&quot;&gt;Architecture, Use Cases, and High Level Design&lt;/a&gt;. In this episode, Uncle Bob explains why a good architecture maximizes the number of decisions not made.&lt;/p&gt;

&lt;p&gt;From now on, be very careful with the frameworks you use, don’t trust them with your life, and make sure you can replace them whenever necessary!&lt;/p&gt;

</content>
    </entry>
    
    <entry>
        <title>The Art of Testing</title>
        <link href="https://blog.thesoftwarecraft.com/2012/04/art-of-testing.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-04-30:/2012/04/the-art-of-testing</id>
        <updated>2012-04-30T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;In a few previous posts I talked about the importance of unit testing and Test-Driven Development (TDD).&lt;/p&gt;

&lt;p&gt;TDD helps us to ensure we’ve covered everything in our system by tests. Sure thing if you never write more production code than to satisfy a failing test, you’ve always covered all your production code by tests.&lt;/p&gt;

&lt;p&gt;But how do we write a good suite of tests? How do we ensure that our tests help us maintaining the system? And remember, in agile development maintenance starts after the first successful compilation.&lt;/p&gt;

&lt;h2 id=&quot;purpose-of-tests&quot;&gt;Purpose of tests&lt;/h2&gt;

&lt;p&gt;To know how to create a good suite of tests, we first must understand our expectations of these tests. What is the primary reason we need a good suite of tests?&lt;/p&gt;

&lt;p&gt;The primary reason is not to prove that our code works. There are many ways to prove this, including an army of QA staff. Sure, it’s unprofessional to not know if all your code works, but it’s not the primary reason for our suite of tests. Why do we need this suite of tests? What do we expect from it?&lt;/p&gt;

&lt;p&gt;The primary reason for a suite of tests is to allows us to refactor! The suite of tests eliminates the fear of changing the code. With a good suite of tests we can refactor the code while having continuous feedback if we broke something.&lt;/p&gt;

&lt;h2 id=&quot;good-tests&quot;&gt;Good tests&lt;/h2&gt;

&lt;p&gt;A good unit test is Fast, Independent, Repeatable, Self-Validating, and Timely. For details on this F.I.R.S.T. acronym, read &lt;a href=&quot;https://amzn.to/46ObgIv&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Clean Code&lt;/a&gt; by Uncle Bob, Robert C. Martin. Seriously, if you haven’t read it yet, order a copy now and come back after to finish reading this post.&lt;/p&gt;

&lt;p&gt;Tests that follow this acronym, support refactoring the production code quite well. But there’s more to create a sustainable suite of good tests. Not only is a unit test independent of other unit tests, it also validates something that no other test validates. In order words, a single feature of the system (under test) is unit tested once.&lt;/p&gt;

&lt;p&gt;Furthermore, tests must be stable, meaning that things that things that commonly change should not be asserted. E.g. asserting text on a screen leads to fragile tests; whenever the text is changed, the test fails. It’s better not to assert this text, but stub it or only assert that some text is printed.&lt;/p&gt;

&lt;h2 id=&quot;design-feedback&quot;&gt;Design feedback&lt;/h2&gt;

&lt;p&gt;Tests not given feedback about the behavior of the system, they also provide feedback about its design. Let me clarify this with an example.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;shouldGetWeekOfYear&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;kt&quot;&gt;int&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;weekOfYear&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Calendar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;getInstance&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;().&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;get&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;Calendar&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;na&quot;&gt;WEEK_OF_YEAR&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;);&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;assertThat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getWeekOfYear&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equalTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;weekOfYear&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;As you can see the test determines the current week of the year, and then asserts the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getWeekOfYear(..)&lt;/code&gt; function returning the same value. Most likely this function determines the week of the year pretty much the same way.&lt;/p&gt;

&lt;p&gt;Quite often I see constructions like this; tests that repeat the production code to assert its behavior. Most of these are written after the fact, but either way this is a clear symptom of bad design.&lt;/p&gt;

&lt;h2 id=&quot;understand-design-feedback&quot;&gt;Understand design feedback&lt;/h2&gt;

&lt;p&gt;To find a better way to test this function, we must understand the feedback from this test. We can hear it scream bad design at us, but what’s wrong with the design for this function? Isn’t the function too trivial to be burdened by bad design?&lt;/p&gt;

&lt;p&gt;Remember the Single Responsibility Principle? It teaches us that each function in a system must do one thing, and one thing only. This function &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getWeekOfYear()&lt;/code&gt; in fact has two responsibilities; it determines the week of the year, of the current system date. Let’s see what the test would look like when we extract this second responsibility.&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Test&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;shouldGetWeekOfYear&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;throws&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Exception&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;assertThat&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;getWeekOfYear&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;date&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2012&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;APRIL&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;30&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;)),&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;equalTo&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;18&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;));&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;}&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;This is much better. We fixed the function’s design by extracting the extra responsibility from the function, and the test now is much more expressive. Note that I used a function date(..) to get a specific date, to improve expressiveness somewhat more.&lt;/p&gt;

&lt;h2 id=&quot;dont-fix-the-symptom&quot;&gt;Don’t fix the symptom&lt;/h2&gt;

&lt;p&gt;But wait a minute. It’s nice and all that the test now is more expressive, and that this specific function is left with only one responsibility. But now it became responsibility of the caller to determine the current date. How’s that better than having a low-level function to do two things?&lt;/p&gt;

&lt;p&gt;Indeed. Our test helped finding a much more serious design problem: the concept of date/time is not properly separated in this particular system. Now that we’ve got this design flaw clear, we can fix it.&lt;/p&gt;

&lt;p&gt;By the way, I’m not suggesting that you create your own date and time abstractions, but access to the current date/time is a separate concern that must not be spread throughout the system. We can introduce a Clock that gets the current date/time, and use this clock to pass the current date into the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;getWeekOfYear(..)&lt;/code&gt; function.&lt;/p&gt;

&lt;h2 id=&quot;conclusion&quot;&gt;Conclusion&lt;/h2&gt;

&lt;p&gt;When you have trouble creating the scenario for a function to be tested, listen to this feedback from your test, take the time to understand what exactly is wrong, and fix it. This way tests help you from early on to separate concerns by constantly providing feedback about the system design.&lt;/p&gt;

&lt;p&gt;Because tests given you loads of design feedback, writing tests after the fact is not really useful. If you get this feedback after you wrote the production code, you either have to rework the production code, or worse, ignore the design feedback. Writing tests after the fact therefore makes tests more likely to fail even on minor changes to the system, and thus they support but our primary purpose of tests: the ability to refactor!&lt;/p&gt;
</content>
    </entry>
    
    <entry>
        <title>Legacy Code: Treat it with Silk Gloves</title>
        <link href="https://blog.thesoftwarecraft.com/2012/02/legacy-code-treat-it-with-silk-gloves.html"/>
        <id>tag:blog.thesoftwarecraft.com,2012-02-12:/2012/02/legacy-code-treat-it-with-silk-gloves</id>
        <updated>2012-02-12T00:00:00+00:00</updated>
        <content type="html">&lt;p&gt;Legacy code is tough to deal with. It may be the toughest thing we have to deal with in our everyday jobs.&lt;/p&gt;

&lt;p&gt;Before we start, let’s align our definition of what legacy code is. Legacy code is untested code. Legacy code may be old, but not necessarily. Unfortunately some projects are actively producing legacy code.&lt;/p&gt;

&lt;p&gt;Most programmers have to work with legacy code frequently. Even worse, they often must make changes in the legacy code. They must fix bugs in the legacy code, or change it to implement new features. At the end of last October I started working on a project that at the time, fully consisted of legacy code. I won’t share any specific details about that client and their projects, but I like to share how we work with the legacy code.&lt;/p&gt;

&lt;p&gt;First of all, as a general rule, we try hard not to change the legacy code. If a feature can be implemented without changing the legacy code, implement it with completely new code. And of course, write it using TDD.&lt;/p&gt;

&lt;p&gt;Only if this first rule does not apply - i.e. when we must change the legacy code - we continue to think about making some changes. And before making any changes we bring the legacy code under test - or at least the parts where the changes will be. This is the hardest part. I won’t go into any details about how to bring it under test here, but I strongly advise reading Michael Feathers’ excellent book &lt;a href=&quot;https://amzn.to/46S4SA8&quot; rel=&quot;external nofollow noreferrer noopener&quot;&gt;Working Effectively with Legacy Code&lt;/a&gt;. In his book Michael presents a large set of strategies that will help you to get your legacy system under test.&lt;/p&gt;

&lt;p&gt;Testing the legacy code mostly is time consuming. Still it must be done, there’s no alternative. We can’t fiddle in some changes and only make things worse, we’re professionals. And besides, it wouldn’t help at all. I’ve seen lots of changes fiddled into untested code, almost all with the same result: the introduction of some or even many defects. But like I said, we’re professionals, we don’t fiddle around in untested code.&lt;/p&gt;

&lt;p&gt;When the legacy code is under test and we have a sufficient amount of coverage, we can refactor. Because the code is under test we are confident that when we break something, the tests tell us so we can undo our last change. We are no longer afraid of changing the code.&lt;/p&gt;

&lt;p&gt;After the code is refactored, the changes can be implemented using TDD. Focusing on not changing the legacy code, doesn’t mean that it’s better to duplicate code or features. It means that legacy code should not be changed if you can avoid it. If you must reuse some feature already implemented in legacy code, you bring it under test and refactor to reusable components.&lt;/p&gt;

&lt;p&gt;Never ever try to fix unreported issues in legacy code, even if the change seems so trivial. Sometimes code can be disguised like a bug and even smell like one, but in the end it may turn out to be a feature of the legacy code.&lt;/p&gt;

&lt;p&gt;A good example of such a feature in disguise is one I heard in a chat with a colleague last week. He told me that on his project they face a lot of nasty legacy code, and how one of his coworkers changed a string comparison by reference (==) to compare the strings using equals. The change made it to production and, as you may expect by this point, ended up to cause a production defect. The comparison by reference, no matter how much of a code smell, was a feature in disguise and shows how dangerous it is to change any untested code based on assumptions.&lt;/p&gt;

&lt;p&gt;To fix the production defect, they reverted the change and put a comment in the code to tell others not to change the comparison. By doing this, they left the code in worse shape then they found it. They added a comment to express their fear of changing it.&lt;/p&gt;

&lt;p&gt;Instead, by the time the defect was reported, they should have started writing tests. Clearly they were changing code they didn’t understand. Whenever you must change code you don’t understand, you must refactor first. No matter how trivial the change! And to refactor legacy code, you must get it under test first.&lt;/p&gt;

&lt;p&gt;This is how I would like everybody to treat legacy code: with extreme care. I hope this post will be of help to you, and remember: never be the one that creates the legacy code! There is no reason not to test your code, ever.&lt;/p&gt;
</content>
    </entry>
    

    <rights>Copyright (c) 2012-2019, Bart Bakker; all rights reserved.</rights>
</feed>

