<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title>On Programming Well</title>
		<description>My tech and thought blog.</description>
		<link>http://javiersaldana.com</link>
		<atom:link href="http://javiersaldana.com/feed.xml" rel="self" type="application/rss+xml" />
		
			<item>
				<title>Becoming a better software developer</title>
				<description>&lt;p&gt;If you’ve spent enough time programming software, chances are you’ve had quite a few “a-ha” moments where you realize your past efforts suck.&lt;/p&gt;

&lt;p&gt;If you really like programming, you enjoy learning from those &lt;em&gt;a-ha&lt;/em&gt; moments, and then spend a lot of time figuring out how to write better software.&lt;/p&gt;

&lt;p&gt;After a while you’ll start to realize that software development is hard, and it’s tougher to become better if you’re in the “wrong” environment. It’s like trying to grow plants in barren land (not that I ever tried, but it sounds like it wouldn’t have the best results).&lt;/p&gt;

&lt;p&gt;In Mexico, I spent my share of time trying to become a better software developer. But I realized it was not the best environment. Even though I tried (and I did become better), the biggest thing that hindered my growth was &lt;strong&gt;indifference&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;1. The developers didn’t care&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Some did, but most didn’t. Since developers didn’t care there was no one to look up to. In this types of environments, most of the time you’ll be learning the &lt;em&gt;right&lt;/em&gt; lessons on your own. This was also true for the community. I’m sure things are changing (I’m talking about 3 to 5 years ago). I also believe there are companies out there with outstanding development practices, but it’s difficult to know about them all (especially if they don’t foster this culture outside, with the community).&lt;/p&gt;

&lt;p&gt;&lt;em&gt;2. The clients didn’t care&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;And because the clients didn’t care, there was not enough money to fund our self growth. Once I had enough knowledge to say with confidence that there was a better way, and that it was much more valuable, a lot of my effort went towards convincing new clients of this. Changing their mind was not difficult, but the bigger problem I faced was convincing my own co-workers that it mattered. This is essential if we are going to build better software, because one person cannot carry the others if they just don’t get it or don’t care bad enough. I’ve come to the conclusion that a team of professionals can grow exponentially as long as there are no “bad apples” slowing them down.&lt;/p&gt;

&lt;p&gt;Once I had enough of this indifference, I decided not to fight against the current for a change, and come to the US to work first-hand with one of the companies that I admire, and was mentoring me indirectly anyway through books, blog posts, conference talks, etc: &lt;a href=&quot;http://8thlight.com&quot;&gt;8th Light&lt;/a&gt;. This company has a very high regard for well crafted software, and the people who work here care deeply about their craft. We try to improve every day, help each other, and our work environment fosters learning and self growth.&lt;/p&gt;

&lt;p&gt;Some things we do that I wish I was doing back in Mexico:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Lead the industry by example&lt;/li&gt;
  &lt;li&gt;Identify knowledge gaps between newcomers and seasoned developers&lt;/li&gt;
  &lt;li&gt;Mentoring relationships between seasoned employees and newcomers&lt;/li&gt;
  &lt;li&gt;Share our knowledge outside of the company– conference talks, attending and hosting meet ups, writing blog posts, etc.&lt;/li&gt;
  &lt;li&gt;Identify developers who &lt;strong&gt;care&lt;/strong&gt;, and support them to become better&lt;/li&gt;
  &lt;li&gt;Encourage developers to expand their knowledge by attending conferences, having a “self-edification” budget to purchase books, online courses, or anything that helps us improve&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While not every US company is like this, in my opinion there’s a lot of lessons Mexico could learn from the general tech scene here. Leaders of the industry invite people over to their offices to talk about better software practices, or show the solution to hard engineering problems they faced. And they do it for free. Of course this is also a strategy to hire new talent, but anyone who wants to learn can attend these meet ups, and there’s usually a lot of qualified people willing to help. This is a whole different culture that not until recently Mexico started to adopt. I may be wrong because there are many cities in Mexico and I can’t speak for all of them. I’m from Monterrey which I think is a fair representation of other cities that are strong in tech, and I’m just talking about what I experienced there.&lt;/p&gt;

&lt;p&gt;It surprises me that there are claims (like the article in &lt;a href=&quot;http://www.elfinanciero.com.mx/tech/egresados-de-tics-sin-el-perfil-que-piden-las-empresas.html&quot;&gt;this link&lt;/a&gt;) saying there’s a lack of qualified professionals in the tech industry in Mexico. In my experience the reality is that there are not enough qualified tech companies in Mexico. I think there’s a misunderstanding worth having a conversation over.&lt;/p&gt;

&lt;p&gt;The tech leads of big Mexican companies need to improve their software practices, their own teams, and their hiring and training processes. With proper guidance it’s not difficult, but first of all they need to care and take their share of ownership instead of laying the blame on universities or novice developers for “not knowing” what the company needs (which is likely outdated and so specific to &lt;em&gt;that&lt;/em&gt; company that it would be useless elsewhere). On top of that, the compensation these companies offer doesn’t even come close to what is offered in other countries for similar positions. I’m not saying that developers are perfect or that companies “should” pay more just because, my point is that software development does not have the same value in Mexico as in the US or other countries, and the reason, I believe is indifference. We can change that and show its real value by creating software of higher quality. But in order to achieve that we need developers and companies who care, and who are willing to make a joint effort instead of competing against each other for better compensation, or demanding long work hours, cranking out more features, faster, without actually backing them up economically.&lt;/p&gt;

&lt;p&gt;The following statement will not apply to &lt;em&gt;every&lt;/em&gt; company but it will apply to a great majority: until Mexican companies and developers recognize and experience the higher value of well crafted software, their products will not be top of the line, salaries will not be top of the line, and the general market offers will be mediocre (appealing to guess what kind of developers? the ones that don’t care, and companies complain about).&lt;/p&gt;

&lt;p&gt;If you’re a software developer or tech lead of a company, and feel identified with some of what I described, please &lt;a href=&quot;mailto:javier@ my domain&quot;&gt;reach out to me&lt;/a&gt;. If you’re serious about becoming better I’ll do my best to help you out. I can at the very least recommend you some essential books that I really enjoyed and I know helped others. I’m also available twice a week for pair programming sessions.&lt;/p&gt;
</description>
				<pubDate>Wed, 09 Sep 2015 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/personal-values/becoming-a-better-software-developer</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/personal-values/becoming-a-better-software-developer</guid>
			</item>
		
			<item>
				<title>Named Pipes</title>
				<description>&lt;p&gt;A couple of weeks ago I heard about named pipes, a very neat feature of unix-like systems. This feature allowed me to speed up my &lt;a href=&quot;/tech/2014/11/26/refactoring-the-three-laws-of-tdd.html&quot;&gt;TDD&lt;/a&gt; cycle. In general, though, I think named pipes fall into the “good-to-know” category for your developer toolbelt.&lt;/p&gt;

&lt;p&gt;First off all, a unix pipeline allows a set of processes to have their I/O chained together. A typical example that you’ve probably seen or used before is:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep &lt;/span&gt;Documents
drwx------+  3 jassa  staff   102 Feb  6 19:59 Documents
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;table&gt;
  &lt;tbody&gt;
    &lt;tr&gt;
      &lt;td&gt;Each pipe (&lt;/td&gt;
      &lt;td&gt;) connects the output of the left program, to the input of the right program. In this example we pass the output of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ls -l&lt;/code&gt; (a list of files and directories) to &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;grep Documents&lt;/code&gt; (which will return the lines that match our &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;“Documents”&lt;/code&gt; search). Pipes have many useful applications, and named pipes are an extension of them.&lt;/td&gt;
    &lt;/tr&gt;
  &lt;/tbody&gt;
&lt;/table&gt;

&lt;p&gt;A traditional pipe lives as long as the processes involved are running. A named pipe, on the other hand, is persistent and sits in your system like any other file. Whatever you write to it, can be read elsewhere. Here’s an example (we need two terminal sessions because reading/writing to a named pipe is I/O blocking).&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Terminal 1&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;mkfifo &lt;/span&gt;myNamedPipe &lt;span class=&quot;c&quot;&gt;# creates a file named “myNamedPipe”&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; &lt;span class=&quot;s2&quot;&gt;&quot;Foo&quot;&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myNamedPipe &lt;span class=&quot;c&quot;&gt;# send “Foo” to myNamedPipe&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# ..this process hangs until we read the contents of myNamedPipe elsewhere&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Terminal 2&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;myNamedPipe
Foo &lt;span class=&quot;c&quot;&gt;# returns immediately, and the process in terminal 1 also returns&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Reading and writing to named pipes are I/O blocking operations. Both processes depend on each other and will be blocked until you read the contents of your named pipe. Our previous example with an anonymous pipe could become:&lt;/p&gt;

&lt;div class=&quot;language-bash highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Terminal 1&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;ls&lt;/span&gt; &lt;span class=&quot;nt&quot;&gt;-l&lt;/span&gt; | &lt;span class=&quot;nb&quot;&gt;grep&lt;/span&gt; &lt;span class=&quot;si&quot;&gt;$(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;cat &lt;/span&gt;myNamedPipe&lt;span class=&quot;si&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;c&quot;&gt;# hangs…&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Terminal 2&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;$ &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;echo&lt;/span&gt; “Documents” &lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; myNamedPipe

&lt;span class=&quot;c&quot;&gt;# Back in terminal 1 our process returns…&lt;/span&gt;
drwx------+  3 jassa  staff   102 Feb  6 19:59 Documents
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;By now you should start to realize the power of named pipes. I switched to Vim recently (most people I work with use it, so in order to pair effectively I left Emacs for a while), and on top of that I started working on a Java project that uses maven to manage its build. I decided to avoid the typical slow and bloated IDEs, and just try a Vim workflow instead. I value simplicity, not just in my code but also in my tools, and named pipes allowed me to achieve a tight feedback loop without the overhead an IDE and third-party libraries bring into the mix.&lt;/p&gt;

&lt;p&gt;Basically I created a custom autorunner for whenever my tests change, using Vim and maven.&lt;/p&gt;

&lt;p&gt;My “manual” TDD workflow looks like this:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Make and save changes to SomeClassTest.java&lt;/li&gt;
  &lt;li&gt;Run &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mvn -Dtest=SomeClassTest clean test&lt;/code&gt; in a command prompt&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In order to automate the 2nd step, I had to setup a couple of things in my terminal.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Create a named pipe (in my case &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;mkfifo .test-class&lt;/code&gt;)&lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;Run an endless loop that evaluates the name of the desired test class&lt;/p&gt;

    &lt;div class=&quot;language-plaintext highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;  $ while true; do mvn -Dtest=$(cat .test-class) clean test; done
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;    &lt;/div&gt;

    &lt;p&gt;This endless loop will be blocked waiting to read from the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.test-class&lt;/code&gt; pipe. If we &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;echo “SomeClassTest” &amp;gt; .test-class&lt;/code&gt; it will run the given test and block again waiting to read.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The last step is making Vim write the class name to the pipe every time we save our changes. In my project’s local &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.vimrc&lt;/code&gt; (See the &lt;a href=&quot;http://vimdoc.sourceforge.net/htmldoc/options.html#'exrc'&quot;&gt;exrc&lt;/a&gt; option):&lt;/p&gt;

&lt;div class=&quot;language-vim highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;autocmd &lt;span class=&quot;nb&quot;&gt;BufWritePost&lt;/span&gt; *Test&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;java &lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;call&lt;/span&gt; RunTest&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;function&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;!&lt;/span&gt; RunTest&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;call&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;system&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'echo '&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;.&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;expand&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'%:tr'&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;s1&quot;&gt;' &amp;gt; .test-pipe'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;endfunction&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;This function runs every time we save a file that ends with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Test.java&lt;/code&gt;, and passes the current file name to our named pipe. Now my tests run automatically whenever I save my changes. It might not look like much, but consider the seconds saved from every time I had to switch to another tab, hit the up arrow and press enter (or alternatively &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;reverse-i-search&lt;/code&gt; for “test”).&lt;/p&gt;

&lt;p&gt;What I liked the most about this approach is that with a couple tweaks you can reuse it for pretty much any language and environment.&lt;/p&gt;

&lt;p&gt;This is only the first use that I give to named pipes. Like I said, it’s probably one of those features that are useful to know about, but probably not immediately. I don’t think I have any other use for them right now, but I bet if I encounter a problem they can solve, they’ll pop up in my head.&lt;/p&gt;
</description>
				<pubDate>Sun, 08 Mar 2015 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/examples/named-pipes</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/examples/named-pipes</guid>
			</item>
		
			<item>
				<title>Loose coupling in Go lang</title>
				<description>&lt;p&gt;Go lang offers a wide range of features that help us achieve loosely coupled program designs. In this blog post I will talk about &lt;a href=&quot;#interfaces&quot;&gt;interfaces&lt;/a&gt; and &lt;a href=&quot;#higher-order-functions&quot;&gt;higher-order functions&lt;/a&gt;.&lt;/p&gt;

&lt;h2 id=&quot;interfaces&quot;&gt;1. Interfaces&lt;/h2&gt;

&lt;p&gt;In Go we can define a type interface by supplying a set of method definitions:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;c&quot;&gt;/* name of interface */&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
     &lt;span class=&quot;c&quot;&gt;// method definitions, that is without implementation&lt;/span&gt;
     &lt;span class=&quot;c&quot;&gt;// e.g. sum(a, b int) int&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Let’s say we’re working on a chess program, and we want to define an interface to present the game in multiple I/O devices. We need a method to get the input from the user, and another one to present the output.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;message&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&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;/div&gt;&lt;/div&gt;

&lt;p&gt;When we call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Read&lt;/code&gt; we expect a string back, and when we call &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Write&lt;/code&gt; we pass in a message. In Go, any value that implements those two functions satisfies the interface. For example:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CommandLine&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cli&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CommandLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;input&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Scanf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%s&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&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;input&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;n&quot;&gt;input&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;cli&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;CommandLine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&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;fmt&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Printf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;%v&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&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;/div&gt;&lt;/div&gt;

&lt;p&gt;This is especially useful for testing. We can create a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;FakeIO&lt;/code&gt; type and have complete control over its input and output to verify their contents.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FakeIO&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;Output&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FakeIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Read&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&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;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FakeIO&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;output&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;string&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;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Output&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;output&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Now we can stub and spy calls to I/O in our tests. Suppose we want the player to enter his name when we start a new game:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Player&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;GetName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;IO&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;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Write&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;What's your name?&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
     &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Read&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;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;TestGetPlayerName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;testing&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;T&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;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;io&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;FakeIO&lt;/span&gt;

	&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Input&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Javier&quot;&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;GetName&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Name&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;Javier&quot;&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
		&lt;span class=&quot;n&quot;&gt;t&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Errorf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Expected name to be %#v, but was %#v&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
			&lt;span class=&quot;s&quot;&gt;&quot;Javier&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;name&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;/div&gt;&lt;/div&gt;

&lt;p&gt;And it’s just as easy to verify the game output, we’d just check the value of &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;io.Output&lt;/code&gt; when testing functions that use &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;io.Write(string)&lt;/code&gt;.&lt;/p&gt;

&lt;h2 id=&quot;higher-order-functions&quot;&gt;2. Higher-order functions&lt;/h2&gt;

&lt;blockquote&gt;
  &lt;p&gt;In mathematics and computer science, a higher-order function (also functional form, functional or functor) is a function that does at least one of the following:&lt;/p&gt;

  &lt;ul&gt;
    &lt;li&gt;takes one or more functions as an input&lt;/li&gt;
    &lt;li&gt;outputs a function&lt;/li&gt;
  &lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;Go is strongly typed, which can complicate the use of functions as a means of injecting dependencies. Continuing with our chess game, consider the problem of playing against the computer.&lt;/p&gt;

&lt;p&gt;When it’s the &lt;em&gt;human&lt;/em&gt; player’s turn, we have to pass the chess board and the IO interface to get the move input, which could be a mouse click in a GUI, or text entered through the command line, etc.:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The computer on the other hand, has no need for an IO interface to supply its move. It calculates what to do based on the current state of the chess board.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;computer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;An implementation of the game loop could look like this:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&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;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HasCheckMate&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;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;computer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&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;/div&gt;&lt;/div&gt;

&lt;p&gt;As you may notice, the problem with the loop above is that we’re not checking for the end game condition before the computer moves, so if the human wins, the computer will still attempt to move. This can be easily solved if we create an endless loop instead, and guard each player move with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;HasCheckMate()&lt;/code&gt;&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HasCheckMate&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;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chessBoard&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;if&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HasCheckMate&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;computer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&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;/div&gt;&lt;/div&gt;

&lt;p&gt;But I’d rather avoid duplication (consider what happens if we want to add a third player, and a fourth, and so on).&lt;/p&gt;

&lt;p&gt;It’d be nice to store the players in a data structure, and cycle through it to get the player for each turn. Then we’d be able to write something like this:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&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;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HasCheckMate&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;getCurrentPlayer&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;Move&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;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Storing the references to the players wouldn’t be enough, though, because of the different arity in their &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Move&lt;/code&gt; functions. If we really wanted the function above to work there are a few approaches we could try, but I won’t be doing that in this blog post. Instead, I’m going to use Go’s anonymous functions to store the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;Move&lt;/code&gt; functions, not the players.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;playerMove&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Board&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;player&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;io&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chessBoard&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;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;computerMove&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Board&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;computer&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Move&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&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;var&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moves&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;k&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Board&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;playerMove&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;computerMove&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;The anonymous functions receive only the chess board, so arity is no longer an issue and we can store them in an array. Finally our game loop can look like this:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&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;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;HasCheckMate&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;moveCurrentPlayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
	&lt;span class=&quot;n&quot;&gt;changeTurn&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;/div&gt;&lt;/div&gt;

&lt;p&gt;And calling move on the current player is trivial:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;moveCurrentPlayer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;chess&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Board&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;moves&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;m&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;](&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chessBoard&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;/div&gt;&lt;/div&gt;

&lt;p&gt;To change the player turn, simply cycle through the contents of the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;moves&lt;/code&gt; array.&lt;/p&gt;

&lt;p&gt;These are only some of the language features that we can use to achieve loosely coupled designs. In this example we used interfaces and higher-order functions to comply with the language constraints while keeping our code concise and easy to test.&lt;/p&gt;

&lt;p&gt;On an ending note, I find Go features very refreshing. Out of the box it’s simple and powerful. It provides just enough utility functions (through its &lt;a href=&quot;https://golang.org/src/&quot;&gt;src packages&lt;/a&gt;), so you don’t have to reinvent the wheel. Go treats functions as first-class citizens, and that’s my favorite feature of the language so far.&lt;/p&gt;
</description>
				<pubDate>Thu, 22 Jan 2015 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/examples/loose-coupling-in-go-lang</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/examples/loose-coupling-in-go-lang</guid>
			</item>
		
			<item>
				<title>The Craftsman Toolbelt</title>
				<description>&lt;p&gt;This past couple of weeks I had the opportunity to pair with eight different craftsmen and craftswomen. We worked on very different codebases, from small startups to giant companies, on Scala, Ruby, JS, Clojure, Java… I learned a lot from each one of them.&lt;/p&gt;

&lt;p&gt;I’ll try to sum it up in what I’ll call “the Craftsman Toolbelt”. Even though we were working on different environments, with different levels of information (some times we had the big picture, sometimes we had nothing), I saw many skills and disciplines at play when tackling the problem at hand.&lt;/p&gt;

&lt;h3 id=&quot;tdd&quot;&gt;TDD&lt;/h3&gt;

&lt;p&gt;The most evident discipline for me was TDD because there’s a lot of hype around it. When we were dealing with an unknown language, or library, or system, or all three combined, we could rely on TDD to ensure we’re doing what we mean to. This was an eye opener about the importance and utility of test-driven development.&lt;/p&gt;

&lt;h3 id=&quot;communication&quot;&gt;Communication&lt;/h3&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Communicating through code&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Personally this is my favorite type of communication because it’s never out of date. Written documentation outside of the codebase can lie, people can lie, even comments can lie. The best way to know for sure what’s going on is to look at the code, and if it’s written with an audience in mind then you won’t have any troubles understanding it. Craftsmen know this and write their code considering future developers.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Written documentation outside of the codebase&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;In reality, some programmers just won’t care for the next person reading their code and they’ll tackle the problem as quickly as possible, leaving a mess behind. This is were documentation comes in handy, instead of jumping around between incongruent functions across files and servers, you can expect that the documentation is somewhat up to date and use it as a resource to get a better general picture of what you can work with.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Communication with the client&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Sometimes the documentation and the code are working and they’re easy to understand, but they are solving the wrong problem. It’s important to talk with the client to make sure you’re on the same page. There are cheap ways to ensure you keep on the right track, such as weekly meetings, creating user stories, and defining acceptance criteria before you write any code.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Communicating about the project status and road map&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Sometimes after working on a problem for a while, we would find our estimates were off and there was no way we could finish as soon as we expected. The craftsmen I paired with did a fantastic job communicating with the client and the rest of the team, and managed their expectations accordingly. Solving a given problem will take time, &lt;em&gt;how long&lt;/em&gt; depends on the elegance of the solution, a skilled craftsman will consider multiple options, their trade-offs, and do what works best for each client. Even when there are setbacks, honest communication can go a long way. This is why trust is essential in software business relationships, and we gain trust by consistently delivering what we say we will.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;

&lt;h3 id=&quot;curiosity&quot;&gt;Curiosity&lt;/h3&gt;

&lt;p&gt;I don’t think this is a discipline but a quality instead, when we didn’t know much about the tools or the system, craftsmen were curious and explored what they had control over, to see what resources they had at their disposal and solve the problem.&lt;/p&gt;

&lt;p&gt;It was a really great experience to pair with everyone and see how they think, what they value, and how they interpret and apply the principles of software craftmanship.&lt;/p&gt;
</description>
				<pubDate>Thu, 18 Dec 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/personal-values/the-craftsman-toolbelt</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/personal-values/the-craftsman-toolbelt</guid>
			</item>
		
			<item>
				<title>Formatting Patterns Picks: Cascade</title>
				<description>&lt;p&gt;Formatting (done right) makes code easy to read and understand. In a team, formatting code the same way is important because it helps each member feel comfortable working on a shared codebase.&lt;/p&gt;

&lt;p&gt;Kent Beck defines ten Formatting Patterns for Smalltalk. I found the &lt;strong&gt;Cascade Pattern&lt;/strong&gt; particularly interesting. Essentially it’s a clean and simple way to pass multiple messages to one receiver. I haven’t seen anything so simple in another language before (which is odd since in OOP we pass messages all the time).&lt;/p&gt;

&lt;p&gt;A Cascade sends multiple messages to one receiver. Think of it as chaining, but each message goes to the first receiver.&lt;/p&gt;

&lt;p&gt;In the following example, we send three messages to a rectangle instance:&lt;/p&gt;

&lt;div class=&quot;language-smalltalk highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nv&quot;&gt;rectangle&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;color:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;rectangle&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;height:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
&lt;span class=&quot;nv&quot;&gt;rectangle&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;width:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;In Smalltalk, we can write the code above as:&lt;/p&gt;

&lt;div class=&quot;language-smalltalk highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nf&quot;&gt;rectangle&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;color:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;Color&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;red&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;height:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;10&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
    &lt;span class=&quot;nf&quot;&gt;width:&lt;/span&gt; &lt;span class=&quot;m&quot;&gt;20&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Simple, right?&lt;/p&gt;

&lt;p&gt;Cascades can be misleading when used directly on a class, though:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Smalltalk&quot;&gt;Collection new
    add: 1;
    add: 2;
    add: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Remember each message is going to the first receiver, so the code in the example above will return a collection with only one entry (the number “3”).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Use a Cascade to send several messages to the same receiver. Separate each message with a semicolon. Put each message on its own line and indent one tab. Only use Cascades for messages with zero or one argument. (Kent Beck)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Kent recommends that we use Cascades only to send messages with one or zero arguments, otherwise the readability gains are lost because the reader has to figure out which messages are being sent. E.g. in the code below we end up with two entries instead of one.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Smalltalk&quot;&gt;Collection new
    add: 1;
    add: 2
    add: 3
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;All in all I think this is a nice feature of Smalltalk that would be very useful in modern languages.&lt;/p&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://amzn.com/013476904X&quot;&gt;Smalltalk Best Practice Patterns&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
				<pubDate>Tue, 02 Dec 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/design/formatting-patterns-picks-cascade</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/design/formatting-patterns-picks-cascade</guid>
			</item>
		
			<item>
				<title>Behavior Patterns Picks: Reversing Method</title>
				<description>&lt;p&gt;The &lt;strong&gt;Reversing Method&lt;/strong&gt; pattern (as defined in &lt;a href=&quot;http://amzn.com/013476904X&quot;&gt;Smalltalk Best Practice Patterns&lt;/a&gt;) is useful to code a smooth flow of messages.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Problem: a &lt;strong&gt;Composed Method&lt;/strong&gt; may not read right because messages are going to too many receivers. You may have a &lt;strong&gt;Cascade&lt;/strong&gt; that doesn’t look quite right because several different objects need to receive messages.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;A &lt;em&gt;Composed Method&lt;/em&gt; divides functionality into smaller methods. A Composed Method might become hard to read if we’re sending messages to many receivers.&lt;/p&gt;

&lt;p&gt;Consider the following example:&lt;/p&gt;

&lt;div class=&quot;language-smalltalk highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nc&quot;&gt;Point&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;&amp;gt;&amp;gt;printOn:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;aStream&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;printOn:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;aStream&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;aStream&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;nextPutAll:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;' @ '&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;
    &lt;span class=&quot;nv&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;printOn:&lt;/span&gt; &lt;span class=&quot;nv&quot;&gt;aStream&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;We’re sending messages to three different objects; &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;x&lt;/code&gt; receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printOn&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;aStream&lt;/code&gt; receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;nextPutAll&lt;/code&gt;, &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;y&lt;/code&gt; receives &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printOn&lt;/code&gt;. You don’t have to know much Smalltalk to understand from the code above that our intent is to define a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;printOn&lt;/code&gt; method that will print a formatted Point instance into a &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;stream&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The Reversing Method makes sure that all the messages go through a single object, which improves readability (as we’ll see next) and by having the object receive all the messages it can vary without affecting the parameters.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Code a method on the parameter. Derive it’s name from the original message. Take the original receiver as the parameter to the new method.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;&lt;code class=&quot;language-Smalltalk&quot;&gt;&quot;code a method on the parameter (a 'stream'), derive the name from the original message ('printOn')&quot;
Stream&amp;gt;&amp;gt;print ...

&quot;take the original receiver ('x' and 'y') as the parameter of the new method&quot;
Stream&amp;gt;&amp;gt;print: x
Stream&amp;gt;&amp;gt;print: y

&quot;or more succinctly&quot;
Stream&amp;gt;&amp;gt;print: anObject
&lt;/code&gt;&lt;/pre&gt;

&lt;blockquote&gt;
  &lt;p&gt;Implement the method by sending the original message to the original receiver.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;pre&gt;&lt;code class=&quot;language-Smalltalk&quot;&gt;&quot;send the original message ('printOn: aStream') to the original receiver ('x' and 'y', or 'anObject')&quot;
Stream&amp;gt;&amp;gt;print: anObject
    anObject printOn: self
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;Finally:&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Smalltalk&quot;&gt;Point&amp;gt;&amp;gt;printOn: aStream
    aStream print: x.
    aStream nextPutAll: ' @ '.
    aStream print: y
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;This is much easier to follow than the example we started with. Smalltalk takes readability one step further with the use of &lt;em&gt;Cascades&lt;/em&gt; (which I’ll cover on my next post, but here’s an example)&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-Smalltalk&quot;&gt;Point&amp;gt;&amp;gt;printOn: aStream
    aStream
        print: x;
        nextPutAll: ' @ ';
        print: y
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;&lt;em&gt;Great code has a rhythm that makes it easy to understand. Code that breaks the rhythm is harder to read and understand.&lt;/em&gt; (Kent Beck)&lt;/p&gt;
</description>
				<pubDate>Mon, 01 Dec 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/design/behavior-patterns-picks-reversing-method</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/design/behavior-patterns-picks-reversing-method</guid>
			</item>
		
			<item>
				<title>Refactoring the three laws of TDD</title>
				<description>&lt;p&gt;Uncle Bob describes TDD with three rules:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;You are not allowed to write any production code unless it is to make a failing unit test pass.&lt;/li&gt;
  &lt;li&gt;You are not allowed to write any more of a unit test than is sufficient to fail; and compilation failures are failures.&lt;/li&gt;
  &lt;li&gt;You are not allowed to write any more production code than is sufficient to pass the one failing unit test.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Read them carefully and you’ll notice they’re simpler than they look like, but they have a lot of repetition so here’s a “refactored” version:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Write production code only to make a failing unit test pass.&lt;/li&gt;
  &lt;li&gt;Write only enough of a unit test to fail.&lt;/li&gt;
  &lt;li&gt;Write only enough production code to make the failing unit test pass.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;It’s obvious now that rule 3 implies rule 1, so here’s a concise version that I’ve found easier to remember:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Write only enough of a unit test to fail.&lt;/li&gt;
  &lt;li&gt;Write only enough production code to make the failing unit test pass.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This couple of rules also serve me as a checklist when I’m developing, so I just repeat them in order, over and over, to keep me in the TDD loop.&lt;/p&gt;

&lt;h2 id=&quot;reference&quot;&gt;Reference&lt;/h2&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;http://butunclebob.com/ArticleS.UncleBob.TheThreeRulesOfTdd&quot;&gt;The Three Laws of TDD&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;
</description>
				<pubDate>Wed, 26 Nov 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/tech/refactoring-the-three-laws-of-tdd</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/tech/refactoring-the-three-laws-of-tdd</guid>
			</item>
		
			<item>
				<title>Behavior Patterns</title>
				<description>&lt;p&gt;In objects, we model the world around us via &lt;strong&gt;&lt;em&gt;behavior&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;state&lt;/em&gt;&lt;/strong&gt;. The operations performed by the object define it’s behavior, and the objects specify their behavior via methods and messages.&lt;/p&gt;

&lt;p&gt;Objects can hide their representation behind messages:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;div class=&quot;highlight&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;House&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;build&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;install_floor&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;raise_walls&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;add_roof&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;/div&gt;

&lt;p&gt;Code is easier to read when we decompose functionality and use messages. In the example above, we can see that in order to build a house we install the floor, raise the walls, and add a roof. These methods handle the details of the implementation, and might even be broken down into more messages depending on the complexity of the task.&lt;/p&gt;

&lt;p&gt;Decomposing the functionality of your program into smaller, digestible, and carefully named methods clearly communicates your intentions to the reader.&lt;/p&gt;

&lt;p&gt;Centralizing the &lt;em&gt;meat&lt;/em&gt; of the algorithm might make code easier to follow, since you don’t have to jump around from file to file to understand it, but this approach leads to rigid systems. If you break up behavior into many methods and pass messages around to carry out tasks, you increase flexibility by essentially making your objects replaceable. Managing many bits and pieces of code takes time and effort, sometimes bigger methods could be better if the management overhead outweighs the benefits of decentralizing the flow of control.&lt;/p&gt;

&lt;p&gt;In &lt;a href=&quot;http://amzn.com/013476904X&quot;&gt;Smalltalk Best Practice Patterns&lt;/a&gt;, we can find an extensive list of behavior patterns gathered by Kent Beck. He splits behavior patterns into &lt;em&gt;methods&lt;/em&gt; and &lt;em&gt;messages&lt;/em&gt;. Method patterns deal with the decomposition of a program into methods, and how to write those methods throughout your system, while Messages patterns deal with how to compose and decompose your messages, and how to pass those messages between the components of your system in order to achieve complex tasks.&lt;/p&gt;

&lt;p&gt;It’s really interesting to put a name on strategies I’ve used in the past and see why they work. I found a few pretty cool patterns that I didn’t know about, but they’re exclusive to Smalltalk. I’ll write about my favorites on a separate blog post.&lt;/p&gt;
</description>
				<pubDate>Mon, 24 Nov 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/tech/behavior-patterns</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/tech/behavior-patterns</guid>
			</item>
		
			<item>
				<title>Introduction to Smalltalk Best Practice Patterns</title>
				<description>&lt;p&gt;A &lt;em&gt;pattern&lt;/em&gt; is a decision recommended by experts to solve recurring problems in software engineering. For example, delegating work to one of many possible objects is a pattern, and even though the actual objects and messages being passed around will differ on each implementation, they have several constant qualities.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://amzn.com/013476904X&quot;&gt;Smalltalk Best Practice Patterns&lt;/a&gt;, by Kent Beck, is written under the premise that problems in software development are universal: we all have to name variables, functions and classes, relate entities, cope with change, and so on. Patterns offer an approach refined through experience to solve these problems.&lt;/p&gt;

&lt;p&gt;Some of the key benefits of understanding and using them:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Read and understand code more quickly&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;When you identify a pattern in the code you’re working on, it’s easier to understand why it’s written like it is. If you know what to expect you can understand the system faster.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Make everyday development decisions with confidence&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Kent Beck suggests attempting to back up every line of code you write with a pattern. He mentions that at first it will slow you down, but as you practice and internalize them, you’ll gain your old speed with the added benefit of writing much cleaner code.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Common language and values for code reviews&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;If your team is familiar with patterns, you can use them as reference in code reviews and suggest legitimate improvements for existing code.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Describe a system with fewer words&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;Patterns carry meaning. If you communicate with patterns and your audience understands them, you can say more with fewer words. For example, think of how many more words you’d need to describe &lt;em&gt;“a broken computer”&lt;/em&gt; without using the word &lt;em&gt;“computer”&lt;/em&gt;. Each pattern embodies a concept.&lt;/p&gt;
  &lt;/li&gt;
  &lt;li&gt;
    &lt;p&gt;&lt;strong&gt;Clean up complex systems&lt;/strong&gt;&lt;/p&gt;

    &lt;p&gt;When you’re working on a system for the first time, you can use patterns to clean the code to make it readable and more understandable. Kent beck mentions he used this approach to clean code and at the same time understand the system. Once he went through the system and understood what needed to change, the code was already clean to make changes without much effort.&lt;/p&gt;
  &lt;/li&gt;
&lt;/ul&gt;
</description>
				<pubDate>Wed, 19 Nov 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/tech/introduction-to-smalltalk-best-practice-patterns</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/tech/introduction-to-smalltalk-best-practice-patterns</guid>
			</item>
		
			<item>
				<title>Build and validate your own web server</title>
				<description>&lt;p&gt;I’m creating a web server to practice my programming skills/improve my knowledge, I’d say it’s from scratch but I’m using Java sockets which handle a big deal of complexity.&lt;/p&gt;

&lt;p&gt;My challenge then is creating an implementation of the &lt;a href=&quot;https://tools.ietf.org/html/rfc2616&quot;&gt;HTTP protocol&lt;/a&gt; to handle the communication between server and clients.&lt;/p&gt;

&lt;p&gt;Our web server must be prepared to interpret HTTP &lt;a href=&quot;https://tools.ietf.org/html/rfc2616#section-5&quot;&gt;requests&lt;/a&gt; and build a proper &lt;a href=&quot;https://tools.ietf.org/html/rfc2616#section-6&quot;&gt;response&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;A request message from a client to a server includes, within the
   first line of that message, the method to be applied to the resource,
   the identifier of the resource, and the protocol version in use.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The first line of the &lt;strong&gt;request&lt;/strong&gt; message must include:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;Method&lt;/strong&gt; (GET, POST, HEAD, etc.)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Request-URI&lt;/strong&gt; (to be affected by the method)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;HTTP-Version&lt;/strong&gt; (protocol version, in this case HTTP/1.1)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CRLF&lt;/strong&gt; (US-ASCII CR, carriage return (13), US-ASCII LF, linefeed (10), in other words a line terminator or newline)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The elements should be separated by SP characters (US-ASCII SP, space (32)).&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;After receiving and interpreting a request message, a server responds
   with an HTTP response message.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The &lt;strong&gt;response&lt;/strong&gt; should begin with:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;strong&gt;HTTP-Version&lt;/strong&gt; (same as above, HTTP/1.1)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Status-Code&lt;/strong&gt; (3-digit number that represents the result of processing the request, see &lt;a href=&quot;http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html&quot;&gt;full definitions&lt;/a&gt;)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;Reason-Phrase&lt;/strong&gt; (short textual description of the status code)&lt;/li&gt;
  &lt;li&gt;&lt;strong&gt;CRLF&lt;/strong&gt; (line terminator)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And again, the elements must be separated by the defined SP characters.&lt;/p&gt;

&lt;p&gt;These are the basic elements of HTTP requests and responses, the web server implementation gets more complicated as you support different media types, streaming, concurrent connections, etc.&lt;/p&gt;

&lt;p&gt;In order to validate that our web server adheres to the HTTP protocol, we can use a suite of acceptance tests called &lt;a href=&quot;https://github.com/8thlight/cob_spec&quot;&gt;Cob Spec&lt;/a&gt;. The tests were written using a framework called &lt;a href=&quot;http://www.fitnesse.org&quot;&gt;FitNesse&lt;/a&gt; which runs in your browser as a wiki. It’s very easy to use and the installation is fairly straightforward, just go through the instructions in the project &lt;a href=&quot;https://github.com/8thlight/cob_spec/blob/master/README.md&quot;&gt;Read Me&lt;/a&gt;. At the time of writing I just had to clone a couple of GitHub repos, and have Java and Ruby installed.&lt;/p&gt;

&lt;p&gt;Once you run Cob Spec you can access the tests suite in your browser, which lists the different test cases. You’ll have to edit a couple of variables (from the website) in order to run them.&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/images/cob_spec.jpg&quot;&gt;&lt;img src=&quot;/images/cob_spec.jpg&quot; alt=&quot;Cob Spec Web&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To run a test case go to “HttpTestSuite” from the main menu and navigate through the list, then click “Test”. Otherwise click “Suite” to run everything.&lt;/p&gt;

&lt;p&gt;Cob Spec will start your server on each test case (given a command you specify in your variables, e.g. &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;/path/to/server/bin/run&lt;/code&gt;), then it will send different requests to your server and expect the appropriate response.&lt;/p&gt;

&lt;p&gt;Here’s a screenshot of a test passing for a simple &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;GET&lt;/code&gt; request:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;/images/cob_spec-test.jpg&quot;&gt;&lt;img src=&quot;/images/cob_spec-test.jpg&quot; alt=&quot;Cob Spec test example&quot; /&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this example, the test passes when it receives an HTTP response with a status code of 200 (OK).&lt;/p&gt;
</description>
				<pubDate>Mon, 10 Nov 2014 00:00:00 +0000</pubDate>
				<link>http://javiersaldana.com/articles/examples/build-and-validate-your-own-web-server</link>
				<guid isPermaLink="true">http://javiersaldana.com/articles/examples/build-and-validate-your-own-web-server</guid>
			</item>
		
	</channel>
</rss>
