<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
  <title><![CDATA[Useful Coding]]></title>
  <subtitle><![CDATA[A practical rant on software enginnering]]></subtitle>
  <link href="/b/atom.xml" rel="self"/>
  <link href="https://gon.al/b/"/>
  <updated>2015-11-17T06:30:41.136Z</updated>
  <id>https://gon.al/b/</id>
  
  <author>
    <name><![CDATA[Gonzalo Alvarez]]></name>
    
  </author>
  
  <generator uri="http://hexo.io/">Hexo</generator>
  
  <entry>
    <title><![CDATA[Annotations are good]]></title>
    <link href="https://gon.al/b/2015/09/20/annotations-are-good/"/>
    <id>https://gon.al/b/2015/09/20/annotations-are-good/</id>
    <published>2015-09-20T17:15:00.000Z</published>
    <updated>2015-11-17T06:30:41.136Z</updated>
    <content type="html"><![CDATA[<p>Using annotations in your code will make it easier to read, hence more maintainable, and far more powerful. Yet, some people try to make us think that are the new evil to avoid.<br><a id="more"></a></p>
<blockquote><p>While people say – it’s easier to read the Code, it’s easier to debug the code with annotations in the mix, they forget what’s it’s not code in code anymore – they have embedded configuration in code. And as far as I remember Configurations were supposed to be externalized. The problem is more severe in cases where we use ORM frameworks like Hibernate and JPA.</p>
<footer><strong>Scratch Pad</strong><cite><a href="http://scratchpad101.com/2015/08/06/annotations-are-evil/" target="_blank" rel="external">scratchpad101.com/2015/08/06/annotations-are-evil</a></cite></footer></blockquote>
<div class="svideo"><video autoplay=" " loop=" " muted=" " poster="https://v.gon.al/patrickstewardfacepalm.jpg"><source type="video/webm" src="https://v.gon.al/patrickstewardfacepalm.webm"><source type="video/mp4" src="https://v.gon.al/patrickstewardfacepalm.mp4"><img src="https://v.gon.al/patrickstewardfacepalm.jpg" data-src="https://v.gon.al/patrickstewardfacepalm.gif" alt="That is mixing apples and oranges"></video><div class="svtitle">That is mixing apples and oranges</div></div>
<p>I get it. You have a lot of frustration when using <strong>hibernate</strong> with annotations, and that’s understandable. But by using annotations you are not mixing configuration with code or making your code less maintainable. If something, you are making it more flexible and readable, hence more maintainable.</p>
<p>Let me show you some great examples of annotations.</p>
<h2 id="Lombok">Lombok</h2><p>Project lombok is a great boilerplate code generator that uses annotations to remove the need to add common methods, such as getters, setters or hash/equals.</p>
<figure class="highlight java"><figcaption><span>Hocus Pocus</span><a href="https://projectlombok.org/" target="_blank" rel="external">Lombok Documentation</a></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="annotation">@Data</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> String username;</span><br><span class="line">  <span class="keyword">private</span> String passwordHash;</span><br><span class="line">  <span class="keyword">private</span> List&lt;Role&gt; roles;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>The <strong>@Data</strong> annotation gives us to have 3 getters (<em>getUsername</em>,<em>getPasswordHash</em>,<em>getRoles</em>), 3 setters (<em>setUsername</em>,<em>setPasswordHash</em>,<em>setRoles</em>), the <em>equals</em> function and the <em>hashCode</em> function with a single line. So you go from 41 lines of code to 6.</p>
<p>You can see the full before and after in the <a href="https://projectlombok.org/features/Data.html" target="_blank" rel="external">@Data</a> documentation. And start using it.</p>
<div class="svideo"><video autoplay=" " loop=" " muted=" " poster="https://v.gon.al/aladdinwow.jpg"><source type="video/webm" src="https://v.gon.al/aladdinwow.webm"><source type="video/mp4" src="https://v.gon.al/aladdinwow.mp4"><img src="https://v.gon.al/aladdinwow.jpg" data-src="https://v.gon.al/aladdinwow.gif" alt="A whole new world of wonders ahead!"></video><div class="svtitle">A whole new world of wonders ahead!</div></div>
<h2 id="@Override">@Override</h2><p>Another key annotation very widely used in the OO programming with Java is the @Override annotation.</p>
<figure class="highlight java"><figcaption><span>@Override</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Vehicle</span> </span>&#123;</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getNumberOfWheels</span><span class="params">()</span></span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Car</span> <span class="keyword">implements</span> <span class="title">Vehicle</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="annotation">@Override</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">int</span> <span class="title">getNumberOfWheels</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> <span class="number">4</span>;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>The @Override annotation here has two important usages. First, it will let us know if we made a mistake while naming the function ‘getNumberOfWheels’ in the class. And second, if the interface changes for whatever the reason, it will notify us of that change, to ensure that we are aware of it.</p>
<p>The @Override annotation is simply critical when you use external 3rd party libraries that you want to keep updated from time to time.</p>
<h2 id="JUnit">JUnit</h2><p>Not sure if you test your code, but if you don’t I don’t wanna know. No, seriously, test your code.</p>
<div class="svideo"><video autoplay=" " loop=" " muted=" " poster="https://v.gon.al/thumbsuppoehler.jpg"><source type="video/webm" src="https://v.gon.al/thumbsuppoehler.webm"><source type="video/mp4" src="https://v.gon.al/thumbsuppoehler.mp4"><img src="https://v.gon.al/thumbsuppoehler.jpg" data-src="https://v.gon.al/thumbsuppoehler.gif" alt="Promise me you will use unit tests"></video><div class="svtitle">Promise me you will use unit tests</div></div>
<p>Anyways, if you test your code with unit tests (please, please, do), you’ve probably stumbled across the @Test annotation. And maybe the @Before. And if you are fairly savvy, you are even aware of the existence of @Ignore.</p>
<p>There are plenty of tutorials on how to use JUnit all over the web (<a href="http://www.vogella.com/tutorials/JUnit/article.html" target="_blank" rel="external">Vogella</a>, <a href="http://www.mkyong.com/tutorials/junit-tutorials/" target="_blank" rel="external">Mkyong</a>). Here a very simple example:</p>
<figure class="highlight java"><figcaption><span>JUnit</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">CarTests</span> </span>&#123;</span><br><span class="line"></span><br><span class="line">  <span class="annotation">@Test</span></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">getNumberOfWheelsTest</span><span class="params">()</span> </span>&#123;</span><br><span class="line">    Car carTest = <span class="keyword">new</span> Car();</span><br><span class="line">    <span class="keyword">int</span> wheels = carTest.getNumberOfWheels();</span><br><span class="line"></span><br><span class="line">    assertEquals(<span class="number">4</span>, wheels);</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h2 id="Spring_JavaConfig">Spring JavaConfig</h2><p>And we get to the core of the issue here: JavaConfig. Spring, as you may know, is (among other things) a dependency injection framework, so we can make use of the inversion of control principle. Anyways, even if you don’t know that, the idea is that Spring allows you to setup some objects in memory that can be instantiated upon need ().</p>
<p>In order to define those objects, called beans, you usually had to write fairly complex XML files pointing to fully qualified class names that would use other class names to work. This is extremely error prone. Let me show you an example of the legacy XML documantion:</p>
<figure class="highlight xml"><figcaption><span>Spring XML Documentation</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="title">beans</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!-- first, define your individual @Configuration classes as beans --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">bean</span> <span class="attribute">class</span>=<span class="value">"com.myapp.config.AppConfig"</span>/&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">bean</span> <span class="attribute">class</span>=<span class="value">"com.myapp.config.DataConfig"</span>/&gt;</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">&lt;!-- be sure to include the JavaConfig bean post-processor --&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="title">bean</span> <span class="attribute">class</span>=<span class="value">"org.springframework.config.java.process.ConfigurationPostProcessor"</span>/&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="title">beans</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>Now, this all changed with Spring 3, where JavaConfig showed up as a game changer. Before you had to start up your application to see if there was an error in your (xml-based) configuration. With annotations, the IDE would tell you if you made a mistake. Let’s see how it looks with annotations:</p>
<figure class="highlight scala"><figcaption><span>Spring XML Documentation</span></figcaption><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="annotation">@Configuration</span></span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">DataSourceConfig</span> &#123;</span></span><br><span class="line">    <span class="annotation">@Bean</span></span><br><span class="line">    public <span class="type">DataSource</span> dataSource() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="type">DriverManagerDataSource</span>(...);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="annotation">@Configuration</span></span><br><span class="line"><span class="annotation">@AnnotationDrivenConfig</span></span><br><span class="line"><span class="annotation">@Import</span>(<span class="type">DataSourceConfig</span>.<span class="keyword">class</span>)</span><br><span class="line">public <span class="class"><span class="keyword">class</span> <span class="title">AppConfig</span> <span class="keyword"><span class="keyword">extends</span></span> <span class="title">ConfigurationSupport</span> &#123;</span></span><br><span class="line">    <span class="annotation">@Autowired</span> <span class="type">DataSourceConfig</span> dataSourceConfig;</span><br><span class="line"></span><br><span class="line">    <span class="annotation">@Bean</span></span><br><span class="line">    public void <span class="type">TransferService</span> transferService() &#123;</span><br><span class="line">        <span class="keyword">return</span> <span class="keyword">new</span> <span class="type">TransferServiceImpl</span>(dataSourceConfig.dataSource());</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>How beautiful is that. You create all your configuration using annotations, so you are sure you don’t make any mistakes because otherwise your IDE will let you know. That is simply amazing, if you ask me. Not to mention that you can now start using @Inject or @Named.</p>
<h2 id="Conclusion">Conclusion</h2><p>Annotations are here to stay. They do a lot of things, and some of them may not be perfect, but there is no reason to hate them, or to claim that they are killing Java. Find out for yourself if you want to be making your code flexible and powerful, or you rather discuss about abstract arguable concepts.</p>
]]></content>
    <summary type="html">
    <![CDATA[<p>Using annotations in your code will make it easier to read, hence more maintainable, and far more powerful. Yet, some people try to make us think that are the new evil to avoid.<br>]]>
    
    </summary>
    
  </entry>
  
  <entry>
    <title><![CDATA[Hello World]]></title>
    <link href="https://gon.al/b/2015/08/21/hello-world/"/>
    <id>https://gon.al/b/2015/08/21/hello-world/</id>
    <published>2015-08-21T03:45:00.000Z</published>
    <updated>2015-11-17T06:30:41.136Z</updated>
    <content type="html"><![CDATA[<p>It’s all about personal branding, <a href="http://chase-seibert.github.io/blog/2014/08/01/why-blogging.html" target="_blank" rel="external">they say</a>. If you plan on having a career in the software industry, you’d better start blogging, <a href="http://emptysqua.re/blog/write-an-excellent-programming-blog/" target="_blank" rel="external">others may argue</a>.<br><a id="more"></a></p>
<div class="svideo"><video autoplay=" " loop=" " muted=" " poster="https://v.gon.al/fineilldoit.jpg"><source type="video/webm" src="https://v.gon.al/fineilldoit.webm"><source type="video/mp4" src="https://v.gon.al/fineilldoit.mp4"><img src="https://v.gon.al/fineilldoit.jpg" data-src="https://v.gon.al/fineilldoit.gif" alt="Fine, Ill do it'"></video><div class="svtitle">Fine, Ill do it'</div></div>
<p>Fine, I’ll do it.</p>
<p>I’m pretty sure I won’t be good at all at this, but I hope to have fun while doing it.</p>
<p>Let’s get started.</p>
]]></content>
    <summary type="html">
    <![CDATA[<p>It’s all about personal branding, <a href="http://chase-seibert.github.io/blog/2014/08/01/why-blogging.html">they say</a>. If you plan on having a career in the software industry, you’d better start blogging, <a href="http://emptysqua.re/blog/write-an-excellent-programming-blog/">others may argue</a>.<br>]]>
    
    </summary>
    
      <category term="gif" scheme="https://gon.al/b/tags/gif/"/>
    
      <category term="rant" scheme="https://gon.al/b/tags/rant/"/>
    
      <category term="rant" scheme="https://gon.al/b/categories/rant/"/>
    
  </entry>
  
</feed>
