<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">

 <title>Please be careful</title>
 <link href="http://tmichel.github.io/atom.xml" rel="self"/>
 <link href="http://tmichel.github.io/"/>
 <updated>2016-10-09T12:37:01+02:00</updated>
 <id>http://tmichel.github.io</id>
 <author>
   <name>Tamás Michelberger</name>
 </author>

 
 <entry>
   <title>MongoDB replica set tags with mongoose</title>
   <link href="http://tmichel.github.io/2016/07/19/mongodb-replica-set-tags-with-mongoose/"/>
   <updated>2016-07-19T00:00:00+02:00</updated>
   <id>http://tmichel.github.io/2016/07/19/mongodb-replica-set-tags-with-mongoose</id>
   <content type="html">&lt;p&gt;MongoDB replica set tags are a handy way to refer to a subset of nodes. Imagine a scenario where you have three nodes in your replica set and you want to dedicate one server to reporting or background jobs. This can easily be achieved by &lt;a href=&quot;https://docs.mongodb.com/manual/tutorial/configure-replica-set-tag-sets/&quot;&gt;replica set tags&lt;/a&gt; combined with &lt;a href=&quot;https://docs.mongodb.com/manual/tutorial/configure-secondary-only-replica-set-member/&quot;&gt;preventing that node from becoming primary&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After you have set up your MongoDB cluster you also have to configure your clients so they can read from the correct nodes.&lt;/p&gt;

&lt;p&gt;Our production API is a node.js application that uses &lt;a href=&quot;http://mongoosejs.com/&quot;&gt;mongoose&lt;/a&gt; as an ORM or ODM if we want to be fancy with abbreviations. Fortunately mongoose &lt;a href=&quot;http://mongoosejs.com/docs/connections.html#connection-string-options&quot;&gt;supports replica set tags&lt;/a&gt; as &lt;code class=&quot;highlighter-rouge&quot;&gt;readPreferenceTags&lt;/code&gt;. It claims to support them in the connection URI as well. We are a big fan of 12 factor apps so we pass the connection URI as an environment variable.&lt;/p&gt;

&lt;p&gt;Up until this point everything is straightforward and pretty simple. When we configured everything we expected one of the MongoDB nodes to have little to no traffic. To our surprise this was pretty far from the truth. The traffic distribution was the same as before. All nodes got some portion of the traffic. At this point we had to deep dive into the source code of mongoose. I can tell you it wasn’t a nice experience.&lt;/p&gt;

&lt;p&gt;The problem was pretty apparent from the get go. In the underlying package that handles the actual TCP connection to the database there is a &lt;a href=&quot;https://github.com/christkv/mongodb-core/blob/1a3f5aef67a363c1bba4020c352a98b9afd2a277/lib/topologies/replset.js#L985&quot;&gt;function called &lt;code class=&quot;highlighter-rouge&quot;&gt;pickServer&lt;/code&gt;&lt;/a&gt; which does what you expect. The strange thing about this is that the read preference tags were not passed down to this function. Every other relevant configuration was available and set properly. I tried to hunt down how the configuration is passed around and where the read preference tags get lost.&lt;/p&gt;

&lt;p&gt;I ended up with the realization that the configuration is parsed but it is lost somewhere between the 3-5 underlying packages that are used by mongoose. I &lt;a href=&quot;https://github.com/Automattic/mongoose/issues/4311&quot;&gt;filed an issue&lt;/a&gt; on Github, maybe it saves a few hours of debugging for somebody.&lt;/p&gt;

&lt;p&gt;Fortunately there is a quick and pretty painless workaround so we didn’t have to figure out where it gets actually lost. If the read preference tags are set directly and not via the connection URI then it sticks and gets passed the &lt;code class=&quot;highlighter-rouge&quot;&gt;pickServer&lt;/code&gt; function.&lt;/p&gt;

&lt;div class=&quot;language-js highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;var&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;mongoUrl&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;process&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;env&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;MONGO_URL&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;muri&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;mongoUrl&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;;&lt;/span&gt;
&lt;span class=&quot;nx&quot;&gt;mongoose&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;connect&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;mongoUrl&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;na&quot;&gt;db&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;na&quot;&gt;readPreference&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;na&quot;&gt;preference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readPreference&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
      &lt;span class=&quot;na&quot;&gt;tags&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nx&quot;&gt;options&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nx&quot;&gt;readPreferenceTags&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;p&gt;This only gets us half-way there. As it turns out there is another bug in one of the underlying packages. It is a pretty trivial bug so as a good open source citizen I opened up my text editor and &lt;a href=&quot;https://github.com/christkv/mongodb-core/pull/107&quot;&gt;fixed the issue&lt;/a&gt;. This was the easy part. Now I had to somehow force node.js to load the fixed package instead of the canonical one. This would be super simple in Ruby land. I would just declare that xyz dependency comes from a git repo and be done with it. In Ruby every loaded dependency is global so overwriting it in one place would solve it for every other package. This is not the case for node.js…&lt;/p&gt;

&lt;p&gt;npm has a feature called &lt;a href=&quot;https://docs.npmjs.com/cli/shrinkwrap&quot;&gt;shrinkwrap&lt;/a&gt; which is basically a version lock file. We can specify the exact version of one or more packages which will be consistent across all &lt;code class=&quot;highlighter-rouge&quot;&gt;npm install&lt;/code&gt;s. Since npm version 3 we can specify only a subset of packages without specifying versions for all packages. We can use this to hack around node.js’s questionable module system so we can inject the fixed package.&lt;/p&gt;

&lt;p&gt;Our &lt;code class=&quot;highlighter-rouge&quot;&gt;npm-shrinkwrap.json&lt;/code&gt; is the following:&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;dependencies&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;mongoose&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;version&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;4.4.20&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;from&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;mongoose@4.4.20&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;resolved&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://registry.npmjs.org/mongoose/-/mongoose-4.4.20.tgz&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;dependencies&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;mongodb&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;version&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;2.1.18&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;from&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;mongodb@2.1.18&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;resolved&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;https://registry.npmjs.org/mongodb/-/mongodb-2.1.18.tgz&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;dependencies&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;mongodb-core&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
              &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;version&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;1.3.22-alpha4&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
              &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;from&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;git+https://github.com/sspinc/mongodb-core.git#fix-tag-selection&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
              &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;resolved&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;git+https://github.com/sspinc/mongodb-core.git#e1fdf33a031ae653437aac3d419a81191a777bfa&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
          &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
      &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As the time of writing this is the only way to get read preference tags to work with mongoose.&lt;/p&gt;

</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>Object-oriented controversies: Tell Don't Ask vs the Web</title>
   <link href="http://tmichel.github.io/2015/09/14/oo-controversies-tell-dont-ask-vs-the-web/"/>
   <updated>2015-09-14T00:00:00+02:00</updated>
   <id>http://tmichel.github.io/2015/09/14/oo-controversies-tell-dont-ask-vs-the-web</id>
   <content type="html">&lt;p&gt;Lately I have been thinking a lot about object-oriented principles and ways to better apply them in my day-to-day work. The more I think the more questions I have. Principles and best practices contradicts each other more often than not. This post is not a rant but rather me trying to understand some portions of object-oriented programming.&lt;/p&gt;

&lt;p&gt;The mainstream view of OOP seems to focus on classes and inheritance while the original interpretation is more about message passing and the &lt;a href=&quot;https://pragprog.com/articles/tell-dont-ask&quot;&gt;Tell Don’t Ask&lt;/a&gt; principle.&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Procedural code gets information then makes decisions. Object-oriented code tells objects to do things.&lt;/p&gt;

  &lt;p&gt;– Alec Sharp&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically the Tell Don’t Ask principle says that you should tell objects to do things and not query their internal state, make a decision and &lt;em&gt;then&lt;/em&gt; tell them what to do.&lt;/p&gt;

&lt;p&gt;Not so good:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sound_alarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;alarm&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;alarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;enabled?&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;alarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;working?&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;alarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;start_siren&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;p&gt;Better:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&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;Alarm&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;sound&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;enabled?&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;working?&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;sound_siren&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;span class=&quot;c1&quot;&gt;# ...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;alarm&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;sound&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This seems quite natural as one of the core principles of OO is that data and behavior should be co-located: a behavior is “responsible” for the data that it needs to operate on.&lt;/p&gt;

&lt;p&gt;On its own Tell Don’t Ask seems to be very close to the One True Way. Locality, encapsulation and data hiding are very pleasant side effects. Code written this way is very readable and the &lt;a href=&quot;https://robots.thoughtbot.com/tell-dont-ask&quot;&gt;refactoring from the “old” way&lt;/a&gt; is pretty straightforward.&lt;/p&gt;

&lt;p&gt;Things start to go south when you think about how to apply it in the context of web applications or more specifically in the context of Rails. You should put behavior where the data is and in a web application the most important data resides in models. It does make sense to couple data and behavior. For example if I query the user’s name the returning &lt;code class=&quot;highlighter-rouge&quot;&gt;String&lt;/code&gt; object does not know how to make sense of itself. It is trivial that it should not know. The &lt;code class=&quot;highlighter-rouge&quot;&gt;user&lt;/code&gt; object should know what its &lt;code class=&quot;highlighter-rouge&quot;&gt;name&lt;/code&gt; actually means. The Tell Don’t Ask principle says that instead of asking the user for its name I should tell the user things to do with its name.&lt;/p&gt;

&lt;p&gt;Following this logic basically every behavior would end up in the model classes. This is something that we know (by experience) is a bad idea. Hundreds or thousands of lines of code crammed into a single class is a recipe for disaster. &lt;a href=&quot;http://brewhouse.io/blog/2014/04/30/gourmet-service-objects.html&quot;&gt;Service Objects&lt;/a&gt; grew out of this frustration.&lt;/p&gt;

&lt;p&gt;Service objects are great. It takes some time to shift your mind that classes can actually be verbs and instead of representing a piece of the real world they represent actions in the real world. At first I thought service objects (with names like &lt;code class=&quot;highlighter-rouge&quot;&gt;CreateOrder&lt;/code&gt;) were abominations. You get a strange feeling about using verbs for classes, after all real world object and real world actions are different beasts. But with time I grew to appreciate service objects for their simplicity. They nicely obey the &lt;a href=&quot;https://en.wikipedia.org/wiki/Single_responsibility_principle&quot;&gt;Single Responsibility Principle&lt;/a&gt;, they are basically glorified functions with their own internal state.&lt;/p&gt;

&lt;p&gt;Let’s look at a trivial example:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&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;MentionUser&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;attr_reader&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:post&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;initialize&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;vi&quot;&gt;@post&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&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;n&quot;&gt;user&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;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;has_access_to?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;post&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;k&quot;&gt;unless&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;published?&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;content&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;include?&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;@&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;#{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;nick&lt;/span&gt;&lt;span class=&quot;si&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;watchers&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;
      &lt;span class=&quot;no&quot;&gt;UserNotificationMailer&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;mentioned_in_post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;user&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;post&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;).&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;deliver_later&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;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is nice and easy. All the behavior is in one place, we extracted this from the &lt;code class=&quot;highlighter-rouge&quot;&gt;User&lt;/code&gt; model. It is testable and it has a clear responsibility. From the point of Tell Don’t Ask this is really bad. It queries objects for their internal state and then tells them what to do based on the queried data.&lt;/p&gt;

&lt;p&gt;Nowadays the general wisdom is to keep your models thin (to act as a gateway layer to your database) and if possible only contain associations and very trivial derived properties. All the business related logic should go into service objects and kept as a separate layer. Martin Fowler &lt;a href=&quot;http://www.martinfowler.com/bliki/AnemicDomainModel.html&quot;&gt;wrote about this phenomenon&lt;/a&gt; about 12 years ago and called it an anti-pattern. As always, he is quite right. Service objects are a lot closer to procedural programming than object-oriented programming although they work surprisingly well.&lt;/p&gt;

&lt;p&gt;Now we have two concepts that contradict each other. Tell Don’t Ask is really sensible but service objects are based on hard-earned experience. Some people tried to &lt;a href=&quot;http://blog.jasonharrelson.com/blog/2014/03/04/ruby-oop-events-and-the-tell-dont-ask-principle/&quot;&gt;close the gap between them&lt;/a&gt; but it only addresses one level: the controller uses the Tell Don’t Ask principle on service objects but service objects are still querying models for their internal state and make decisions based on them.&lt;/p&gt;

&lt;p&gt;At this point I was really stuck and it seemed that this conflict is impossible to resolve. After quite some thinking I realized that I treated the model as a fixed piece of the puzzle as if it cannot be moved or modified. ActiveRecord models are a very crude view of the data that makes up the domain of the application. It is not just ActiveRecord though, we could also mention JPA and its entity beans but .NET EntityFramework is also guilty as charged.&lt;/p&gt;

&lt;p&gt;A user model is a very general view of our domain’s user concept. When we interact with a user we don’t always need every information that is associated with our user concept. For example for authenticating a user we really only need its username and password. What if we had a model to capture this. A small and very focused model that could hold the data and the behavior at the same time but adhere to SRP. It is certainly not a revolutionary idea but realizing that models should not be tied to database tables gives you a new perspective.&lt;/p&gt;

&lt;p&gt;Suddenly ActiveRecord seems more like a constraint and not a convenient way to access the database. A more flexible mediation layer might be a better option where I can easily map database tables (even from different sources) to my domain models. &lt;a href=&quot;http://rom-rb.org/&quot;&gt;ROM&lt;/a&gt; would be a great candidate here. Splitting up our domain concepts by context is taking us awfully close to &lt;a href=&quot;https://en.wikipedia.org/wiki/Data,_context_and_interaction&quot;&gt;DCI&lt;/a&gt; which is a huge topic that I’m not going to touch on.&lt;/p&gt;

&lt;p&gt;Conclusion? As always there is no silver bullet. Service objects (with their procedural style) could work as well as the Tell Don’t Ask approach. A touch of pragmatism is needed to evaluate the different approaches and use a mixture that fits our needs. Although my gut feeling tells me that mixing too many approaches would lead to the same spaghetti code that we strive to avoid.&lt;/p&gt;

&lt;p&gt;Thanks &lt;a href=&quot;http://blog.vbalazs.me/&quot;&gt;Balazs Varga&lt;/a&gt; and Zsofia Langi for reviewing.&lt;/p&gt;

</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>View components in Rails</title>
   <link href="http://tmichel.github.io/2015/03/01/view-components-in-rails/"/>
   <updated>2015-03-01T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/2015/03/01/view-components-in-rails</id>
   <content type="html">&lt;p&gt;I started poking around with Rails just after the release of 3.1. Since then not much changed in how we approach the view layer. We use the same abstraction or to be more precise we are stuck with the lack of abstractions and doing everything on the lowest possible level of string manipulation.&lt;/p&gt;

&lt;p&gt;The interesting thing about this is how far we can go without noticing the lack of available tools. With templates, partials and myriads of built-in helpers we can get by for a long time. But there always comes a moment when our partials become too complicated and stuffed with logic or coupled to the context so much that  it makes it really hard to reuse them.&lt;/p&gt;

&lt;p&gt;When this moment came I started wishing for view components. The Rails-world is so big, there must have been somebody else who ran into this and came up with an elegant solution. To my surprise I couldn’t find one that fits all my needs.&lt;/p&gt;

&lt;h2 id=&quot;what-are-view-components-anyway&quot;&gt;What are view components anyway?&lt;/h2&gt;

&lt;p&gt;Before I go any further I’d like to take a moment and explain what view components are (at least for me). My first exposure to web application development was a framework called &lt;a href=&quot;https://wicket.apache.org/&quot;&gt;Wicket&lt;/a&gt; and I tend to think this is where my definition of view component comes from. Wicket tries really hard to bridge the gap between desktop and web development, but at the end it fails miserably. It is a pain to use but the component concept it promotes is ingenious.&lt;/p&gt;

&lt;p&gt;A view component should be …&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;completely self-contained, which means it can be rendered anywhere on its own&lt;/li&gt;
  &lt;li&gt;easily extendable (think OO inheritance)&lt;/li&gt;
  &lt;li&gt;reusable across your application (or even between applications)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This means I can have a generic table component to render &lt;code class=&quot;highlighter-rouge&quot;&gt;&amp;lt;table&amp;gt;&lt;/code&gt; elements and I can use that directly in my views. But nothing holds me back from pulling that out into its own component and create an &lt;code class=&quot;highlighter-rouge&quot;&gt;ArticleTable&lt;/code&gt; that can easily render itself anywhere and the only thing it needs is a list of articles. The &lt;code class=&quot;highlighter-rouge&quot;&gt;ArticleTable&lt;/code&gt; could use the template from its parent or provide its own if absolute control is needed over how it should be rendered.&lt;/p&gt;

&lt;h2 id=&quot;the-rails-way&quot;&gt;The Rails way&lt;/h2&gt;

&lt;p&gt;What I described above is simply not possible with Rails. The best you can do is create helpers or &lt;a href=&quot;http://teotti.com/building-intention-revealing-ruby-on-rails-helpers/&quot;&gt;helper classes&lt;/a&gt; which spit out some string. This way you lose the flexibility and clarity of templates because you have to shove pieces of html into your ruby class or use the clunky &lt;code class=&quot;highlighter-rouge&quot;&gt;content_tag&lt;/code&gt; that Rails provides.&lt;/p&gt;

&lt;p&gt;Another option could be a partial with a backing (view)model (or presenter if you like). This is a bit better but there is a huge disconnect between the template and the view model/logic. You have to remember to pass in the right view model every time you want to use your partial + view model combo. Not to mention that the syntax is pretty ugly:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;render&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;partial: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'articles/table'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;locals: &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;articles: &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ArticleTableViewModel&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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;vi&quot;&gt;@articles&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;h2 id=&quot;arbre&quot;&gt;Arbre&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/activeadmin/arbre&quot;&gt;Arbre&lt;/a&gt; basically fell out of &lt;a href=&quot;https://github.com/activeadmin/activeadmin&quot;&gt;ActiveAdmin&lt;/a&gt; for the same reasons this article is being written. Arbre has a few warts and bugs but it is really usable and provides most of the abstractions you need, and also provides a &lt;a href=&quot;https://github.com/activeadmin/arbre#components&quot;&gt;straightforward way to create components&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Unfortunately this is an all-in solution. It comes with its own &lt;code class=&quot;highlighter-rouge&quot;&gt;Arbre::Context&lt;/code&gt; which makes it really hard to integrate with existing template engines. You basically have to drop erb or haml or whatever you’ve been using. This makes it virtually impossible to integrate it into an existing application.&lt;/p&gt;

&lt;p&gt;The closest I could get with embedding it into an erb template was to instantiate a new &lt;code class=&quot;highlighter-rouge&quot;&gt;Arbre::Context&lt;/code&gt; within Rails’ view context.&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# in helpers/application_helper.rb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;arbre&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;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
  &lt;span class=&quot;no&quot;&gt;Arbre&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Context&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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;assigns&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;self&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;block&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;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And then use this in a view:&lt;/p&gt;

&lt;div class=&quot;language-erb highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;container&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;cp&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arbre&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;article_table&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;articles&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;cp&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;But this is where integration ends. The following is not working:&lt;/p&gt;

&lt;div class=&quot;language-erb highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;id=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;container&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;cp&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;arbre&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;
    &lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;panel&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Article panel'&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;cp&quot;&gt;&amp;lt;%&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Arbre is full-on ruby - your templates become ruby scripts that only vaguely resemble html. It takes some time to get familiar with how it works and how to work around its quirks (eg. using Rails’ built-in helpers).&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# in app/views/articles/show.html.arb&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;class: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'container'&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;div&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;class: &lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'row'&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;h3&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;article&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;title&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;text_node&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;link_to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s1&quot;&gt;'Edit'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;edit_article_path&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;article&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;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And it gets a little worse when you write your own components and you have to put the template code into a component’s &lt;code class=&quot;highlighter-rouge&quot;&gt;build&lt;/code&gt; method.&lt;/p&gt;

&lt;p&gt;From the Arbre docs:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&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;Panel&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Arbre&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;Component&lt;/span&gt;
  &lt;span class=&quot;n&quot;&gt;builder_method&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:panel&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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;attributes&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;super&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;attributes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;h3&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;title&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;class: &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;panel-title&quot;&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;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Arbre is a good choice when you use it inside ActiveAdmin but for general purposes it presents you with more challenges than solutions.&lt;/p&gt;

&lt;p&gt;There is a very similar project called &lt;a href=&quot;https://github.com/erector/erector&quot;&gt;erector&lt;/a&gt;. I haven’t looked into it properly but it seems to have the same problem of mixing “template” code into ruby classes. I think a clear distinction between the template and logic is really useful.&lt;/p&gt;

&lt;h2 id=&quot;cells&quot;&gt;Cells&lt;/h2&gt;

&lt;p&gt;&lt;a href=&quot;https://github.com/apotonick/cells&quot;&gt;Cells&lt;/a&gt; describes itself as &lt;em&gt;view components for Rails&lt;/em&gt; but it has a slightly different definition of what a component is. It naturally plays well with existing solutions and really easy to add it to an application. When you have a &lt;code class=&quot;highlighter-rouge&quot;&gt;CommentCell&lt;/code&gt; you can render that in a template with the &lt;code class=&quot;highlighter-rouge&quot;&gt;cell&lt;/code&gt; helper method.&lt;/p&gt;

&lt;div class=&quot;language-erb highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nt&quot;&gt;&amp;lt;div&lt;/span&gt; &lt;span class=&quot;na&quot;&gt;class=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;container&quot;&lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&amp;gt;&lt;/span&gt;
  &lt;span class=&quot;cp&quot;&gt;&amp;lt;%=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;cell&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;:comment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;vi&quot;&gt;@comment&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;cp&quot;&gt;%&amp;gt;&lt;/span&gt;
&lt;span class=&quot;nt&quot;&gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It does a superb job of separating the presentation from the view logic: every cell has its own templates and a cell class which is basically the view model.&lt;/p&gt;

&lt;p&gt;Cells is basically a way to replace Rails’ partials with maintainable and self-contained components. It falls short when you try to create generic components such as a table or a panel. The &lt;code class=&quot;highlighter-rouge&quot;&gt;cell&lt;/code&gt; helper does not accept a block and you cannot &lt;code class=&quot;highlighter-rouge&quot;&gt;yield&lt;/code&gt; from a cell’s template. Nick Sutterer, the author of Cells, has &lt;a href=&quot;http://nicksda.apotomo.de/?s=cells&quot;&gt;lots of great articles about how Cells works&lt;/a&gt;. I especially liked the one which showcases Cells with &lt;a href=&quot;http://nicksda.apotomo.de/2010/11/lets-write-a-reusable-sidebar-component-in-rails-3/&quot;&gt;a sidebar component&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Cells gets so many things right but the ability to easily create generic components is sorely missing. Also there are a few conceptual things that I disagree with. A cell is basically a small MVC stack (in Rails kinda way) so it can have different actions or states (in Cells terminology) and these all have corresponding templates. I think that a view component should have only one representation.&lt;/p&gt;

&lt;p&gt;In cells you could do the following:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&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;CommentCell&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Cells&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;::&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;ViewModel&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;show&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# renders comment/show.erb&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;render&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;

  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;flagged&lt;/span&gt;
    &lt;span class=&quot;c1&quot;&gt;# renders comment/flagged.erb&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;render&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;span class=&quot;c1&quot;&gt;# in a view&lt;/span&gt;
&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;sx&quot;&gt;%= cell(:comment, @comment).call(:flagged) %&amp;gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;I would much rather see two separate classes since I already have to manually decide which state I want to render.&lt;/p&gt;

&lt;h2 id=&quot;the-ideal-solution&quot;&gt;The ideal solution?&lt;/h2&gt;

&lt;p&gt;There is no silver bullet. I couldn’t find an existing gem that would solve all my problems. It seems that at the end I have to get dirty and write my own view components library. In the meantime Cells is the best option I have.&lt;/p&gt;

&lt;p&gt;This post became quite lengthy but I still have some thoughts on the subject. It is very likely I will post a follow up with a wish list for a view components library and some code experiments.&lt;/p&gt;

</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>Sharing code between ActiveAdmin resources</title>
   <link href="http://tmichel.github.io/2015/02/22/sharing-code-between-activeadmin-resources/"/>
   <updated>2015-02-22T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/2015/02/22/sharing-code-between-activeadmin-resources</id>
   <content type="html">&lt;p&gt;Recently I’ve been working with &lt;a href=&quot;https://github.com/activeadmin/activeadmin&quot;&gt;ActiveAdmin&lt;/a&gt; a lot, since we use it as an application framework to build an internal admin-like tool. It serves us very well most of the time but the level of magic it introduces is considerable.&lt;/p&gt;

&lt;p&gt;The other day I had to implement the same functionality for two resources&lt;sup id=&quot;fnref:1&quot;&gt;&lt;a href=&quot;#fn:1&quot; class=&quot;footnote&quot;&gt;1&lt;/a&gt;&lt;/sup&gt; and as every well-groomed Rails developer would want it, I wanted it DRY. Like super dry.&lt;/p&gt;

&lt;p&gt;I created a new &lt;code class=&quot;highlighter-rouge&quot;&gt;ActiveSupport::Concern&lt;/code&gt; and dropped it in &lt;code class=&quot;highlighter-rouge&quot;&gt;app/admin/concerns&lt;/code&gt; and of course it didn’t work. I tried the obvious way to include it but it turns out that ActiveAdmin resources are a special kind of animals.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://i.imgflip.com/hwmw0.jpg&quot; alt=&quot;One does not simply include a module in an ActiveAdmin resource&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code class=&quot;highlighter-rouge&quot;&gt;ActiveAdmin.register&lt;/code&gt; block runs in the context of an instance of &lt;code class=&quot;highlighter-rouge&quot;&gt;ActiveAdmin::ResourceDSL&lt;/code&gt; and somehow the &lt;code class=&quot;highlighter-rouge&quot;&gt;included&lt;/code&gt; block threw a &lt;code class=&quot;highlighter-rouge&quot;&gt;MultipleIncludedBlocks&lt;/code&gt; error. I couldn’t figure out why, but if &lt;code class=&quot;highlighter-rouge&quot;&gt;include&lt;/code&gt; is not working we just try &lt;code class=&quot;highlighter-rouge&quot;&gt;extend&lt;/code&gt; and &lt;em&gt;bingo!&lt;/em&gt; it works like a charm.&lt;/p&gt;

&lt;p&gt;I ended up with something like this:&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# in app/admin/concerns/confirmable.rb&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;module&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;Confirmable&lt;/span&gt;
  &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;extended&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;base&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;instance_eval&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
      &lt;span class=&quot;n&quot;&gt;action_item&lt;/span&gt; &lt;span class=&quot;ss&quot;&gt;:confirm&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;link_to&lt;/span&gt; &lt;span class=&quot;s1&quot;&gt;'Confirm'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;url_for&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;ss&quot;&gt;action: :confirm&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;c1&quot;&gt;# omitting other gory details for brevity&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;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;And then&lt;/p&gt;

&lt;div class=&quot;language-ruby highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c1&quot;&gt;# in app/admin/post.rb&lt;/span&gt;
&lt;span class=&quot;no&quot;&gt;ActiveAdmin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nf&quot;&gt;register&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Post&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;do&lt;/span&gt;
  &lt;span class=&quot;kp&quot;&gt;extend&lt;/span&gt; &lt;span class=&quot;no&quot;&gt;Confirmable&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This worked pretty well on my local machine but then I pushed it to &lt;code class=&quot;highlighter-rouge&quot;&gt;origin&lt;/code&gt; and the CI build blew up. It could not load the &lt;code class=&quot;highlighter-rouge&quot;&gt;Confirmable&lt;/code&gt; module. I was baffled because &lt;a href=&quot;https://github.com/activeadmin/activeadmin/blob/ed1fbca8c64af827616c58f274d7be256ca513b6/lib/active_admin/application.rb#L186&quot;&gt;according to the source code&lt;/a&gt; ActiveAdmin simply loads everything in the &lt;code class=&quot;highlighter-rouge&quot;&gt;app/admin&lt;/code&gt; folder. I thought maybe there is more to code loading in ruby that I know of. So I ran off and read some pretty good articles about the topic:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://practicingruby.com/articles/ways-to-load-code&quot;&gt;Ways to load code&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://urbanautomaton.com/blog/2013/08/27/rails-autoloading-hell/&quot;&gt;Rails autoloading — how it works, and when it doesn’t&lt;/a&gt;&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;http://myronmars.to/n/dev-blog/2012/12/5-reasons-to-avoid-bundler-require&quot;&gt;5 Reasons to Avoid Bundler.require&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;These were very good but ultimately none of them helped. I moved the &lt;code class=&quot;highlighter-rouge&quot;&gt;confirmable.rb&lt;/code&gt; file around but nothing seemed to help&lt;sup id=&quot;fnref:2&quot;&gt;&lt;a href=&quot;#fn:2&quot; class=&quot;footnote&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;. And the most infuriating part was that I could not reproduce it on my local machine. It didn’t matter which environment I used, which gems were installed.&lt;/p&gt;

&lt;p&gt;As my last resort I turned to the GitHub issues page of ActiveAdmin: there must have been somebody else who wanted to do the same thing. And &lt;em&gt;yes!&lt;/em&gt; finally &lt;a href=&quot;https://github.com/activeadmin/activeadmin/issues/3673#issuecomment-65768172&quot;&gt;something usable&lt;/a&gt;. It turns out that you should not put your modules into &lt;code class=&quot;highlighter-rouge&quot;&gt;app/admin&lt;/code&gt;, so I ended up creating an &lt;code class=&quot;highlighter-rouge&quot;&gt;app/admin_shared&lt;/code&gt; folder. It’s not particularly nice but at least it works.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;UPDATE&lt;/strong&gt;: It was brought to my attention that &lt;code class=&quot;highlighter-rouge&quot;&gt;include&lt;/code&gt; does actually work, but it is &lt;a href=&quot;https://github.com/activeadmin/activeadmin/blob/ed1fbca8c64af827616c58f274d7be256ca513b6/lib/active_admin/dsl.rb#L54&quot;&gt;another piece of ActiveAdmin magic&lt;/a&gt;. You cannot use an &lt;code class=&quot;highlighter-rouge&quot;&gt;ActiveSupport::Concern&lt;/code&gt;, you have to roll your own &lt;code class=&quot;highlighter-rouge&quot;&gt;self.included&lt;/code&gt; hook.&lt;/p&gt;

&lt;div class=&quot;footnotes&quot;&gt;
  &lt;ol&gt;
    &lt;li id=&quot;fn:1&quot;&gt;
      &lt;p&gt;If you are not familiar with ActiveAdmin, a resource is basically an autogenerated controller for a model. &lt;a href=&quot;#fnref:1&quot; class=&quot;reversefootnote&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
    &lt;li id=&quot;fn:2&quot;&gt;
      &lt;p&gt;Interestingly the &lt;em&gt;where to put things&lt;/em&gt; question always comes back to haunt you when you work with Rails. &lt;a href=&quot;#fnref:2&quot; class=&quot;reversefootnote&quot;&gt;&amp;#8617;&lt;/a&gt;&lt;/p&gt;
    &lt;/li&gt;
  &lt;/ol&gt;
&lt;/div&gt;
</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>Sending and testing emails in Go</title>
   <link href="http://tmichel.github.io/2014/10/12/golang-send-test-email/"/>
   <updated>2014-10-12T14:00:00+02:00</updated>
   <id>http://tmichel.github.io/2014/10/12/golang-send-test-email</id>
   <content type="html">&lt;p&gt;My last project was to create a notification service for our back-end. It had to
support a few different ways to send out notifications and of course sending
email was among them. Go’s awesome standard library makes it super easy to send
out emails. After some initialization it comes down to one function call. For
example sending email via Gmail would look something like this:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;you@gmail.com&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PlainAuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&amp;lt;your gmail password&amp;gt;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;smtp.gmail.com&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SendMail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;smtp.gmail.com:587&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;               &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;// server address&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;                               &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;// authentication&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;                               &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;// sender's address&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &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;s&quot;&gt;&quot;recipient@example.com&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;// recipients' address&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World!&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;             &lt;/span&gt;&lt;span class=&quot;c&quot;&gt;// message body&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;log&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This is all good but what about testing? It is good practice &lt;em&gt;not to test&lt;/em&gt; your
framework and standard library again, but what about all the other stuff?
Building the email’s body, loading configuration and all the necessary
preparation - that has to be tested somehow.&lt;/p&gt;

&lt;p&gt;You could use an existing SMTP server (such as Gmail) but introducing external
dependencies to your tests are a bad idea. You might think that mocking the SMTP
server by spinning up a local from your test case could solve your problem. It
certainly would but it is unnecessarily difficult and time consuming. Go has two
useful features that really helps with testing. Interfaces and first-class
functions.&lt;/p&gt;

&lt;p&gt;Let’s create an interface for our email sender!&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EmailSender&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;interface&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This way we can mock the entire email sending later when we use it as a
dependency and this approach also provide us a convenient way to test emailing
and well.&lt;/p&gt;

&lt;p&gt;Let’s create a simple implementation for this interface!&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EmailConfig&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &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;x&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailSender&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EmailConfig&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;send&lt;/span&gt;&lt;span class=&quot;x&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;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&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;x&quot;&gt; &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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;func&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Send&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServerHost&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;:&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServerPort&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;PlainAuth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Username&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Password&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;ServerHost&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&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;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;e&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SenderAddr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;There is no call to &lt;code class=&quot;highlighter-rouge&quot;&gt;smtp.SendMail&lt;/code&gt; yet, although in a constructor function we
can easily wire it together. We pass the a function reference to &lt;code class=&quot;highlighter-rouge&quot;&gt;smtp.SendMail&lt;/code&gt;
to the a newly created &lt;code class=&quot;highlighter-rouge&quot;&gt;emailSender&lt;/code&gt; instance - the outside world will never
know the difference.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;NewEmailSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EmailConfig&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EmailSender&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailSender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;conf&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;SendMail&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now we can easily mock out the actual email sending function in our tests.
Creating something similar to &lt;code class=&quot;highlighter-rouge&quot;&gt;httptest.ResponseRecorder&lt;/code&gt; we can actually see
what was passed to the sender function.&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mockSend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errToReturn&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&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;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&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;x&quot;&gt; &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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailRecorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;new&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailRecorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;x&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;addr&lt;/span&gt;&lt;span class=&quot;x&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Auth&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;x&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;error&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailRecorder&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;a&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;return&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;errToReturn&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;

&lt;/span&gt;&lt;span class=&quot;k&quot;&gt;type&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailRecorder&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;struct&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;addr&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;auth&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;smtp&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;Auth&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;from&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;string&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;to&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;   &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;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;  &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Using a handy closure here we can record what was sent and we can emulate an
error for the edge cases.&lt;/p&gt;

&lt;p&gt;Now all we have to do is put everything together in a test:&lt;/p&gt;

&lt;div class=&quot;language-go highlighter-rouge&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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;TestEmail_SendSuccessful&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;x&quot;&gt; &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;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;mockSend&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;emailSender&lt;/span&gt;&lt;span class=&quot;p&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;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;Hello World&quot;&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;:=&lt;/span&gt;&lt;span class=&quot;x&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;n&quot;&gt;Send&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;s&quot;&gt;&quot;me@example.com&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;},&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;&lt;span class=&quot;kt&quot;&gt;byte&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;

    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;no&quot;&gt;nil&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&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;unexpected error: %s&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;err&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt;&lt;span class=&quot;x&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;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;x&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;wrong message body.&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;expected: %&lt;/span&gt;&lt;span class=&quot;se&quot;&gt;\n&lt;/span&gt;&lt;span class=&quot;s&quot;&gt; got: %s&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;body&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;x&quot;&gt; &lt;/span&gt;&lt;span class=&quot;n&quot;&gt;r&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;msg&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;x&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;a href=&quot;https://gist.github.com/tmichel/a57bd4033db3e90f5516&quot;&gt;All the code stiched together&lt;/a&gt;.
Download it and run &lt;code class=&quot;highlighter-rouge&quot;&gt;go test email_test.go&lt;/code&gt; to run the test.&lt;/p&gt;
</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>Math Is Necessary for Software Development</title>
   <link href="http://tmichel.github.io/2014/01/16/math-is-necessary-for-software-development/"/>
   <updated>2014-01-16T17:30:00+01:00</updated>
   <id>http://tmichel.github.io/2014/01/16/math-is-necessary-for-software-development</id>
   <content type="html">&lt;p&gt;&lt;a href=&quot;http://www.mutuallyhuman.com/blog/2014/01/14/math-is-not-necessary-for-software-development/&quot;&gt;Math Is Not Necessary for Software Development&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;Why are computer science and mathematics &lt;em&gt;always&lt;/em&gt; combined? In my day-to-day
as a developer I rarely do what I would call &lt;em&gt;math&lt;/em&gt;. Now admittedly, computer
science is different than software development, but I don’t think laypeople
know that. I don’t think that high school kids who are deciding what do do
with their lives know that. Our industry desperately needs more talented
developers - and it turns out that the skills that make a good mathematician
don’t necessarily line up with the skills that make a good software developer.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Math is always a tricky question. On a daily basis you might not &lt;em&gt;directly&lt;/em&gt; use
higher math, but everything you touch is built upon it. So math is actually
super important, and you should know &lt;em&gt;and&lt;/em&gt; understand the basic concepts. In the
long run you cannot live without knowing how the low level stuff works. You can
get by, but if you want to be a great (not a good) developer, then you must
understand the building blocks that you are using. You cannot choose the
appropriate hash function if you do not know what that is.&lt;/p&gt;

&lt;p&gt;Math is everywhere in software development. Denying it would be completely
foolish. You can try to shield yourself from it, but eventually you will have to
sit down and understand how things really work. It doesn’t matter if you do this
at a university or on your own, but you cannot avoid it.&lt;/p&gt;
</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>On writing C</title>
   <link href="http://tmichel.github.io/2013/12/22/on-writing-c/"/>
   <updated>2013-12-22T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/2013/12/22/on-writing-c</id>
   <content type="html">&lt;p&gt;A few days ago I had to write some C code again. I haven’t written a single line
of C since freshman year. And that was 5 years ago. That is a long time and
this &lt;a href=&quot;https://github.com/tmichel/chatd&quot;&gt;little home assignment&lt;/a&gt; made me realize
a few things.&lt;/p&gt;

&lt;p&gt;In high school I already picked up some programming skills. The first year of
university I lived off of these. Understanding new concepts and algorithms was
easy and C seemed like just like any other programming language I had
encountered up until that point. It had little quirks, but I was not bothered
that much.&lt;/p&gt;

&lt;p&gt;Using C was uncomfortable. I had to type much more, I missed strings and an
infinite standard library. I just couldn’t understand why I had to re-implement
all the string manipulation functions when somebody else have already done it,
it’s all there and ready to be used. I couldn’t understand why I had to rebuild
the building blocks instead of just using them and creating something worthy.
Now I understand.&lt;/p&gt;

&lt;p&gt;Now I know that learning the basics is invaluable. It is boring and tiresome,
but it is a must. It has to be enforced upon you, otherwise an &lt;em&gt;I-already-know-everything&lt;/em&gt;
young titan would simply ignore it and die horribly when he has to
really understand a problem to fix it.&lt;/p&gt;

&lt;p&gt;Diving into C and the linux api after 5 years was interesting. I needed two days
to catch up to speed. I’ve avoided manual memory management, zero terminated
strings and obscure function names for a long time. After I got used to it
again, it seemed like the world opened up. C felt liberating.&lt;/p&gt;

&lt;p&gt;I felt like a god. I could create anything. The computer’s memory is my
playground and I can shape it the way I like it. No one tells me how to allocate
chunks of memory and how to use them. Close to the metal I felt like a child
again. The constraints of higher level languages were all gone. It was like
playing with Lego, but the instruction manual was out of reach and only my
creativity could restrict me.&lt;/p&gt;

&lt;p&gt;C is freedom. It is a burden and a gift at the same time. It’s pure engineering,
when you can lose yourself in the nitty-gritty details of implementing something
instead of focusing on the end goal and user satisfaction. It is the perfect
playground, but it is poorly suited to be a &lt;em&gt;first language&lt;/em&gt; for the CS
students. I almost missed out on C, because of the bad memories and scars that I
have due to not understanding it properly. I needed to spend 5 years writing
high level stuff to really appreciate what C can offer.&lt;/p&gt;
</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>Testing. Testing. Testing.</title>
   <link href="http://tmichel.github.io/2013/12/08/testing-testing-testing/"/>
   <updated>2013-12-08T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/2013/12/08/testing-testing-testing</id>
   <content type="html">&lt;p&gt;I dropped this little pearl in my code the other day.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;while (i &amp;lt; vec-&amp;gt;len &amp;amp;&amp;amp; vec-&amp;gt;data[i] != value) ;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;It is from a simple vector implementation’s remove method. Do you see any
anything wrong with it? I did not for a while and oddly it behaved well. Too
well actually. For a while I did not put more than one element in the vector, so
this little loop never actually happened. The surprise came when there was two
elements in it and I wanted to remove the second one.&lt;/p&gt;

&lt;p&gt;My biggest mistake here is that I did not write unit tests for this, because I
was confident enough I would not screw this up. Unfortunately my confidence was
without merits.&lt;/p&gt;

&lt;p&gt;Testing is always time well spent.&lt;/p&gt;
</content>
   <category term="" />
 </entry>
 
 <entry>
   <title>TIL: initialize an array with uniform values in C</title>
   <link href="http://tmichel.github.io/til/2013/12/06/til-initialize-an-array-with-uniform-values-in-c/"/>
   <updated>2013-12-06T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/til/2013/12/06/til-initialize-an-array-with-uniform-values-in-c</id>
   <content type="html">&lt;p&gt;Sometime I admire C and then in the next moment I want to die out of
frustration. Immutable strings are a gift from the gods by the way. I can only
now appreciate them properly. It’s a shame that C does not have them.&lt;/p&gt;

&lt;p&gt;So back to the topic. This is such a great syntactic sugar.  When you want to
initialize an array with a uniform value you can do the following:&lt;/p&gt;

&lt;div class=&quot;language-c highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;my_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&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;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;

&lt;span class=&quot;cm&quot;&gt;/* Or for pointers */&lt;/span&gt;
&lt;span class=&quot;kt&quot;&gt;char&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;*&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;my_string_array&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;30&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;nb&quot;&gt;NULL&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;};&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
</content>
   <category term="til" />
 </entry>
 
 <entry>
   <title>How lazy is Hibernate's lazy initialization?</title>
   <link href="http://tmichel.github.io/til/2013/12/01/how-lazy-is-hibernates-lazy-initialization/"/>
   <updated>2013-12-01T20:00:00+01:00</updated>
   <id>http://tmichel.github.io/til/2013/12/01/how-lazy-is-hibernates-lazy-initialization</id>
   <content type="html">&lt;p&gt;Originally I was going to give a more catchy title, but then I figured there is
no need to pick on Hibernate. It actually does its job well when you manage to
tame it.&lt;/p&gt;

&lt;hr /&gt;

&lt;p&gt;Part of our application is using AngularJS which talks to a REST API, and the
data travels as JSON. So we needed a way to serialize our entities into JSON
strings, so we can send it back to the client. We are using JAX RS for the REST
API, which uses Jackson by default. It seemed like a straightforward decision to
use Jackson as our JSON serializer library.&lt;/p&gt;

&lt;p&gt;Our domain model is a bit problematic and has lots of bidirectional
associations, which is not best kind to have when you want to serialize it.&lt;/p&gt;

&lt;p&gt;I knew upfront that Hibernate proxies will cause some trouble, but I would have
never imagined how much. When the serializer tried to access a lazily
initialized property without a hibernate session, it threw an exception. There
is no surprise there. Luckily others have encountered this before, so there is a
&lt;a href=&quot;https://github.com/FasterXML/jackson-datatype-hibernate&quot;&gt;handy library&lt;/a&gt;.
Unfortunately there is a &lt;a href=&quot;https://github.com/FasterXML/jackson-datatype-hibernate/issues/25&quot;&gt;known issue&lt;/a&gt;
about this as well.&lt;/p&gt;

&lt;p&gt;This is the point where I made a mistake. I assumed that &lt;code class=&quot;highlighter-rouge&quot;&gt;Hibernate4Module&lt;/code&gt; can
handle Hibernate proxies correctly, so I reattached the entity to a Hibernate
session and hoped for the best. It was like raining blood. Everything blew up.
The whole thing got stuck in an infinite loop because of the circular nature of
the associations in our domain model.&lt;/p&gt;

&lt;p&gt;I went down the debugging rabbit hole to see where all the Hibernate proxies get
handled and it seemed fine. It seemed to return null for the uninitialized
proxies just as it was advertised in the docs. But somehow those proxies got
initialized during serialization despite all of my efforts.&lt;/p&gt;

&lt;p&gt;It seems that Hibernate’s lazy initialization is a bit more greedier than I
thought. I had the impression that it only loaded an entity or a collection when
you accessed one of its properties. So for an &lt;code class=&quot;highlighter-rouge&quot;&gt;Employee ---&amp;gt; Workspace&lt;/code&gt; relation
calling a &lt;code class=&quot;highlighter-rouge&quot;&gt;employee.getWorkspace()&lt;/code&gt; would not fire a query to the database. When
working with Jackson this was not the case. It loaded everything and caused me
some serious headache. It might have been a bug in the &lt;code class=&quot;highlighter-rouge&quot;&gt;Hibernate4Module&lt;/code&gt; that I
did not notice or my setup was somehow broken.&lt;/p&gt;

&lt;p&gt;I ended up using &lt;a href=&quot;http://wiki.fasterxml.com/JacksonMixInAnnotations&quot;&gt;mixins&lt;/a&gt; and
ignored all the properties that caused the trouble. I was lucky because I did
not need those properties on the client.&lt;/p&gt;
</content>
   <category term="til" />
 </entry>
 
 <entry>
   <title>TIL: JSF + Guava not such a good idea</title>
   <link href="http://tmichel.github.io/til/2013/12/01/til-jsf-guava-not-such-a-good-idea/"/>
   <updated>2013-12-01T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/til/2013/12/01/til-jsf-guava-not-such-a-good-idea</id>
   <content type="html">&lt;p&gt;The other day I was working on a page that presents hierarchical data and I
wanted to retrieve each new level with an Ajax request. So far so good, but JSF
makes simple things harder than they should be.&lt;/p&gt;

&lt;p&gt;I created a view-model for each level of data. It was very basic:&lt;/p&gt;

&lt;div class=&quot;language-java highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;EntityViewModel&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;n&quot;&gt;Entity&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;entity&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;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;EntityViewModel&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;children&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;/div&gt;

&lt;p&gt;After querying the database for the entities, I had to transform and insert the
retrieved data into the view-model object tree. The obvious solution is to wrap
every entity in a view-model and set the parent’s &lt;code class=&quot;highlighter-rouge&quot;&gt;children&lt;/code&gt; property to the new
list.&lt;/p&gt;

&lt;p&gt;My mistake was that I used Guava’s fancy &lt;code class=&quot;highlighter-rouge&quot;&gt;Lists#transform&lt;/code&gt; method instead of
a simple &lt;code class=&quot;highlighter-rouge&quot;&gt;foreach&lt;/code&gt;. For the first level, everything worked fine, but when I
wanted to retrieve the second level nothing came back in the response.
Everything seemed to be fine, but somehow JSF did not pick up the changes in the
object-tree and did not render anything. All the responses were empty.&lt;/p&gt;

&lt;p&gt;After a few hours of staring at the screen helplessly and blaming JSF, I
realized that the problem was with the &lt;code class=&quot;highlighter-rouge&quot;&gt;List&amp;lt;&amp;gt;&lt;/code&gt; implementation that Guava
returned. The &lt;code class=&quot;highlighter-rouge&quot;&gt;#transform&lt;/code&gt; method only returns a &lt;em&gt;view&lt;/em&gt; of the original list, so
JSF did not have a chance to notice when the &lt;code class=&quot;highlighter-rouge&quot;&gt;children&lt;/code&gt; property of an
object changed, thus it never rendered anything below the first level.&lt;/p&gt;

&lt;p&gt;The takeaway? Don’t shy away from using the good ol’ &lt;code class=&quot;highlighter-rouge&quot;&gt;foreach&lt;/code&gt;. It will probably
never let you down.&lt;/p&gt;
</content>
   <category term="til" />
 </entry>
 
 <entry>
   <title>First</title>
   <link href="http://tmichel.github.io/meta/2013/11/30/first/"/>
   <updated>2013-11-30T00:00:00+01:00</updated>
   <id>http://tmichel.github.io/meta/2013/11/30/first</id>
   <content type="html">&lt;p&gt;This blog was born out the frustration and pain that I have to endure every day
due to the frameworks that are trying to be too clever. And because of that,
this blog probably will mostly consist of my ramblings and rants about software
and frameworks.&lt;/p&gt;

&lt;p&gt;This is the nth attempt at keeping a blog regularly updated. I did fail most of
the time, but I’ll never give up trying. I’ll probably collect some of the older
posts from my other blogs and move them here and close up shop everywhere else.&lt;/p&gt;
</content>
   <category term="meta" />
 </entry>
 

</feed>
