<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Simon Taddiken</title>
    <description>The latest articles on DEV Community by Simon Taddiken (@skuzzle).</description>
    <link>https://dev.to/skuzzle</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F38188%2Fa29697bc-f9e4-4491-938c-857f5cbe442f.png</url>
      <title>DEV Community: Simon Taddiken</title>
      <link>https://dev.to/skuzzle</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/skuzzle"/>
    <language>en</language>
    <item>
      <title>What do you think about Mozilla Firefox weakening TLS security?</title>
      <dc:creator>Simon Taddiken</dc:creator>
      <pubDate>Tue, 02 Jul 2019 18:26:08 +0000</pubDate>
      <link>https://dev.to/skuzzle/what-do-you-think-about-mozilla-firefox-weakening-tls-security-23b1</link>
      <guid>https://dev.to/skuzzle/what-do-you-think-about-mozilla-firefox-weakening-tls-security-23b1</guid>
      <description>&lt;p&gt;Mozilla Firefox is about to add an option (which is enabled by default!) that will make it easier for anti virus software to act as man in the middlle in order to decrypt and analyze encrypted HTTPS connections.&lt;/p&gt;

&lt;p&gt;More information can be found in the mozilla &lt;a href="https://support.mozilla.org/de/kb/how-disable-enterprise-roots-preference"&gt;knowledgebase&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I think this fundamentally subverts the whole purpose of TLS and trusted CAs. It is sad that browser manufacturers have to add security loopholes in order to gain acceptance of clueless users who expect higher security through running useless anti virus software.&lt;/p&gt;

&lt;p&gt;Maybe there is a security expert around who can reasonable justify and explain this step?&lt;/p&gt;

</description>
      <category>browser</category>
      <category>security</category>
      <category>discuss</category>
      <category>https</category>
    </item>
    <item>
      <title>Duck typing in java</title>
      <dc:creator>Simon Taddiken</dc:creator>
      <pubDate>Tue, 09 Apr 2019 20:12:04 +0000</pubDate>
      <link>https://dev.to/skuzzle/duck-typing-in-java-6gh</link>
      <guid>https://dev.to/skuzzle/duck-typing-in-java-6gh</guid>
      <description>&lt;p&gt;Disclaimer: please don't take this too serious. It's just fun and games.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Duck typing&lt;/em&gt; is an idiom known mostly from dynamically typed languages. It states that you can treat unrelated Objects of type X as Objects of type Y as long as both have the same public interface.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If it looks like duck, moves like duck and makes sounds like a duck then it must be a duck!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Java's static type system on the other hand, imposes strong rules on type compatibility. Just having the same methods doesn't qualify two classes to be &lt;em&gt;"type compatible"&lt;/em&gt;. Consider this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"Quak Quak"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"woof woof"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Both classes have the same interface, so can we assign instances to the respective other type?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// fails&lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"woof woof"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Of course we can't. Java's type system needs explicit definitions of compatible classes using inheritance. &lt;/p&gt;

&lt;p&gt;Let's now make our example work using the easiest imaginable change to our code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="s"&gt;"woof woof"&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Besides everything you learned in biology classes our &lt;code&gt;Dog&lt;/code&gt; now &lt;em&gt;is a&lt;/em&gt; duck and is thereby assignable to variables with type &lt;code&gt;Duck&lt;/code&gt;. This change should have made the test above turn green.&lt;/p&gt;

&lt;p&gt;Ok, now our Dog is a Duck, but what about all the other classes in our code base that also have a &lt;code&gt;makeNoise&lt;/code&gt; method? Should they all extend the &lt;code&gt;Duck&lt;/code&gt; class? That seems impractical and confusing (&lt;code&gt;class Train extends Duck&lt;/code&gt; is even stranger than a Dog being a Duck). &lt;/p&gt;

&lt;p&gt;Maybe we can build a more dynamic Duck by using &lt;em&gt;reflection&lt;/em&gt;. During runtime the JVM has pretty extensive type information about every object. Besides accessing type information, reflection also allows us to dynamically retrieve and call methods on arbitrary objects.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DynamicDuck&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nf"&gt;DynamicDuck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notADuck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="nf"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// try block is needed because the reflection stuff will &lt;/span&gt;
        &lt;span class="c1"&gt;// throw all kind of horrible exceptions if you use it wrong&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="c1"&gt;// retrieve dynamic type information of the non-duck object&lt;/span&gt;
            &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;?&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;notADuckType&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;

            &lt;span class="c1"&gt;// find the method of the class that is not a duck by name&lt;/span&gt;
            &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Method&lt;/span&gt; &lt;span class="n"&gt;delegate&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notADuckType&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getMethod&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"makeNoise"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

            &lt;span class="c1"&gt;// invoke the method on the object that is not a duck&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;delegate&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nf"&gt;IllegalStateException&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"DynamicDuck error"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using our &lt;code&gt;DynamicDuck&lt;/code&gt; we can get a "duck-view" on every non-duck Object. Let's adjust our initial test accordingly (and also remove the &lt;code&gt;extends&lt;/code&gt; clause in the &lt;code&gt;Dog&lt;/code&gt; class):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DynamicDuck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"woof woof"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's a little more dynamic but still doesn't scale well. If the &lt;code&gt;Duck&lt;/code&gt; class had more public methods we'd have to delegate &lt;em&gt;every&lt;/em&gt; method to the non-duck Object like we did above. And if we decided not to view everything as a Duck, but as a Goose, we need to additionally create a &lt;code&gt;DynamicGoose&lt;/code&gt; class. That's all way too cumbersome.&lt;/p&gt;

&lt;p&gt;Luckily, Java has another useful concept that comes in handy for our use case. Every class that is available during runtime will be loaded from its byte code representation using a &lt;code&gt;ClassLoader&lt;/code&gt;. As this is a pure runtime feature you can make up artificial classes during runtime that did not exist during compile time.&lt;/p&gt;

&lt;p&gt;We can dynamically generate the byte code of a class that &lt;code&gt;extends Duck&lt;/code&gt; and which automatically delegates every called method to the equivalent method of an arbitrary non-duck Object. The following code uses the popular libraries &lt;em&gt;cglib&lt;/em&gt; (for byte code generation) and &lt;em&gt;Objenesis&lt;/em&gt; (for instantiating the dynamically generated classes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;net.sf.cglib.core.DefaultNamingPolicy&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;net.sf.cglib.proxy.CallbackFilter&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;net.sf.cglib.proxy.Enhancer&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;net.sf.cglib.proxy.Factory&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;net.sf.cglib.proxy.InvocationHandler&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.objenesis.Objenesis&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;org.objenesis.ObjenesisStd&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kn"&gt;import&lt;/span&gt; &lt;span class="nn"&gt;java.lang.reflect.Method&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;NotADuck&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="cm"&gt;/**
     * The unique callback index to which each method in the proxy object is mapped.
     */&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="no"&gt;CALLBACK_INDEX&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="cm"&gt;/**
     * Maps all methods to index {@link #CALLBACK_INDEX}.
     */&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;CallbackFilter&lt;/span&gt; &lt;span class="no"&gt;ZERO_CALLBACK_FILTER&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;CALLBACK_INDEX&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// with caching&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Objenesis&lt;/span&gt; &lt;span class="no"&gt;OBJENESIS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;ObjenesisStd&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="nf"&gt;asDuck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Enhancer&lt;/span&gt; &lt;span class="n"&gt;enhancer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Enhancer&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;InvocationHandler&lt;/span&gt; &lt;span class="n"&gt;invocationHandler&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DelegateDuckToNonDuckMethod&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

        &lt;span class="n"&gt;enhancer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setSuperclass&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;type&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;enhancer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setUseFactory&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;enhancer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setNamingPolicy&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;DefaultNamingPolicy&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
        &lt;span class="n"&gt;enhancer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCallbackFilter&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;ZERO_CALLBACK_FILTER&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="n"&gt;enhancer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCallbackType&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;invocationHandler&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Class&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;proxyClass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;enhancer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;createClass&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;duckInstance&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;OBJENESIS&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getInstantiatorOf&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;proxyClass&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;newInstance&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
        &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Factory&lt;/span&gt; &lt;span class="n"&gt;factory&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Factory&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="n"&gt;duckInstance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;factory&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;setCallback&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;CALLBACK_INDEX&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;invocationHandler&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;duckInstance&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;DelegateDuckToNonDuckMethod&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;InvocationHandler&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

        &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nf"&gt;DelegateDuckToNonDuckMethod&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;notADuck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;

        &lt;span class="nd"&gt;@Override&lt;/span&gt;
        &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="nf"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Object&lt;/span&gt; &lt;span class="n"&gt;proxy&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Method&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Object&lt;/span&gt;&lt;span class="o"&gt;[]&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Throwable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Method&lt;/span&gt; &lt;span class="n"&gt;nonDuckMethod&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getClass&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;getMethod&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getName&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;method&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getParameterTypes&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nonDuckMethod&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;invoke&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;notADuck&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;args&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Using this class, we can create a dynamic, duck-view of a Dog instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;final&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NotADuck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asDuck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Dog&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="nc"&gt;Duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"woof woof"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;duck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;makeNoise&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The code is no longer restricted to only create &lt;code&gt;Duck&lt;/code&gt; objects:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;List&lt;/span&gt; &lt;span class="n"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;NotADuck&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;asDuck&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"I'm a String"&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;List&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="c1"&gt;// both List and String have an isEmpty method&lt;/span&gt;
&lt;span class="n"&gt;assertFalse&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;isEmpty&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This was just a little experiment. There are likely bugs, uncovered edge cases, performance issues and general lack of practical use cases in this implementation. Nevertheless it's fun to see how far you can go with Java's dynamic type information and materialization of arbitrary byte code.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Don't do this at home!&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>experiment</category>
      <category>fun</category>
    </item>
    <item>
      <title>Should I write a library?</title>
      <dc:creator>Simon Taddiken</dc:creator>
      <pubDate>Thu, 21 Mar 2019 09:01:39 +0000</pubDate>
      <link>https://dev.to/skuzzle/should-i-write-a-library-2o33</link>
      <guid>https://dev.to/skuzzle/should-i-write-a-library-2o33</guid>
      <description>&lt;p&gt;Every mid-sized project likely has experienced a point in time where its members were to decide whether or wether not to outsource common code into its own library artifact. The main reason for this is mostly to save on duplicated code and to &lt;em&gt;DRY&lt;/em&gt; (don't repeat yourself). Sadly, I have seen this to backfire so dramatically in some projects, that duplicated code might have been the better solution. That is because:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;DRY&lt;/em&gt; has one major tradeoff: it introduces dependencies. &lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Say you have identical code parts in Service &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt;. These parts are completely independent. In case the requirements for the implementation in Service &lt;strong&gt;A&lt;/strong&gt; changes, Service &lt;strong&gt;B&lt;/strong&gt; can remain untouched. That is a pretty comfortable situation because it gives you great freedom in applying changes.&lt;/p&gt;

&lt;p&gt;If you had moved the common code into its own library artifact and the requirements for service &lt;strong&gt;A&lt;/strong&gt; are changing &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;you update the library code and release it as new version&lt;/li&gt;
&lt;li&gt;you update the library dependency in Service &lt;strong&gt;A&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my experience, what happens next is either of the following:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;you forget to update the library dependency in Service &lt;strong&gt;B&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;you update the library dependency in Service &lt;strong&gt;B&lt;/strong&gt; to find out that your changes broke the implementation in &lt;strong&gt;B&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;you update the library dependency in Service &lt;strong&gt;B&lt;/strong&gt; to find out your latest changes work just fine in &lt;strong&gt;B&lt;/strong&gt;. Unfortunately you broke an unrelated part, because the latest version of your library also contains earlier changes for which you forgot to update the dependency&lt;/li&gt;
&lt;li&gt;you update the library dependency in &lt;strong&gt;B&lt;/strong&gt; and everything works as expected (yeah, sure)&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;These problems obviously get worse with every further Service that uses your library. You now need to update &lt;em&gt;every&lt;/em&gt; client of your library, although you needed a change only affecting a single client. Or you choose to not actively update the dependencies in other services and perform a large migration later on (or never).&lt;/p&gt;

&lt;p&gt;Before carelessly extracting common code, apply some thoughts to the following topics.&lt;/p&gt;

&lt;h2&gt;
  
  
  Selecting the scope
&lt;/h2&gt;

&lt;p&gt;Before deciding to move common code into a library, you have to understand &lt;em&gt;why&lt;/em&gt; you have the same code in different locations to begin with. It might be pure coincidence because of similar (but actually &lt;em&gt;unrelated&lt;/em&gt;) business requirements in Service &lt;strong&gt;A&lt;/strong&gt; and &lt;strong&gt;B&lt;/strong&gt;. Business requirements are likely to change in a unforseen and incompatible way. So it is probably not the best idea trying to unify your business logic into a library.&lt;/p&gt;

&lt;p&gt;On the other hand, if you have code that serves purely technical purposes like managing database connections, logging, serving REST endpoints and the alike, it can be really worthwhile considdering to refactor these stuff into a reusable library. If such a technical aspect changes, it is likely that these changes are beneficial for other users of this code as well.&lt;/p&gt;

&lt;p&gt;When deciding to use a shared artifact for commonly used code, do not make the mistake of mixing different concerns into this artifact. Don't put your database connection management together with your logging code. There should only ever be a single reason of why you would apply a change to your library. This also mitigates bullet 3. from above as we are reducing the probability of breaking unrelated parts.&lt;/p&gt;

&lt;p&gt;Don't &lt;em&gt;start&lt;/em&gt; your project with writing a library that might later be used by multiple clients. You don't know the actual requirements yet and are darned to emphasize on the wrong parts. Simply put:&lt;/p&gt;

&lt;p&gt;&lt;iframe class="tweet-embed" id="tweet-988571579536826368-818" src="https://platform.twitter.com/embed/Tweet.html?id=988571579536826368"&gt;
&lt;/iframe&gt;

  // Detect dark theme
  var iframe = document.getElementById('tweet-988571579536826368-818');
  if (document.body.className.includes('dark-theme')) {
    iframe.src = "https://platform.twitter.com/embed/Tweet.html?id=988571579536826368&amp;amp;theme=dark"
  }



&lt;/p&gt;

&lt;h2&gt;
  
  
  Managing change
&lt;/h2&gt;

&lt;p&gt;If you extracted parts of your code base into a library, you have to plan for upcoming changes and how you want to handle them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Design your library code to be extensible from the "outside"&lt;/li&gt;
&lt;li&gt;Have a migration strategy in case you need to change the library&lt;/li&gt;
&lt;li&gt;Have a deprecation strategy to be able to remove code from the library in the long term&lt;/li&gt;
&lt;li&gt;Have a notification strategy to announce new releases&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Designing for extensibility can be exhausting. You have to think about valid extension points and need to predict future requirements. There might always be some obvious interfaces that can easily be made customizable, but there &lt;em&gt;will&lt;/em&gt; be the interfaces you didn't know you need until the actual requirement comes up. Chances are high that you invest a lot of time in planning ahead and to over engineer your code in order to be prepared for every possible change to come. Or, you don't, and end up applying changes and releasing new library versions every other day.&lt;/p&gt;

&lt;p&gt;You have given up the freedom of freely applying changes to every part of your code base. Now, whenever you have to change even the &lt;em&gt;smallest&lt;/em&gt; part in your library, you have to keep in mind that you might break code that depends on this part. According to &lt;a href="http://www.hyrumslaw.com/" rel="noopener noreferrer"&gt;Hyrums Law&lt;/a&gt; you not only &lt;em&gt;might&lt;/em&gt; break, but you actually &lt;em&gt;will&lt;/em&gt; break code if there are enough users of your library:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;With a sufficient number of users of an API,&lt;br&gt;
it does not matter what you promise in the contract:&lt;br&gt;
all observable behaviors of your system&lt;br&gt;
will be depended on by somebody.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Without a deprecation strategy you end up carrying around old code for ages. If you have no way of informing clients for classes and methods that are no longer to be used, you will either end up maintaining this code for the lifetime of this library or you remove it without announcing and hit your clients unprepared when they migrate to the next version.&lt;/p&gt;

&lt;p&gt;It now starts to feel more like building a &lt;em&gt;product&lt;/em&gt; than doing the simple refactoring of moving code around. The more users your library has the more weight has to be put on those single topics. On the other hand, the less users your library has, the less is the effect of saved efforts. It is a constant tradeoff between maintaining duplicated code and maintaining changes to the library.&lt;/p&gt;

&lt;h1&gt;
  
  
  Summary
&lt;/h1&gt;

&lt;p&gt;The overhead of managing a library can be intimidating, especially if you need to do it in parallel to an actual project. On the other hand, if your overall goal is to write, ship and maintain a library, these topics give you a good start at what to take care of.&lt;/p&gt;

&lt;p&gt;I tried to emphasize the tradeoffs you have to consider when introducing shared code to your project. &lt;/p&gt;

&lt;p&gt;So, Should you write a library?&lt;/p&gt;

&lt;p&gt;As often in software development, there is no binary &lt;em&gt;yes&lt;/em&gt; or &lt;em&gt;no&lt;/em&gt; (which is kind of ironic, isn't it?) but only the good ol' &lt;em&gt;it depends&lt;/em&gt;.&lt;/p&gt;

</description>
      <category>architecture</category>
      <category>dry</category>
    </item>
    <item>
      <title>The beginner's guide to Spring Boot</title>
      <dc:creator>Simon Taddiken</dc:creator>
      <pubDate>Wed, 13 Mar 2019 16:39:45 +0000</pubDate>
      <link>https://dev.to/skuzzle/the-beginners-guide-to-spring-boot-22jb</link>
      <guid>https://dev.to/skuzzle/the-beginners-guide-to-spring-boot-22jb</guid>
      <description>&lt;p&gt;&lt;em&gt;Disclaimer:&lt;/em&gt; This article probably seems shorter than you have expected regarding its title. In the end you will have learned that it's not.&lt;/p&gt;

&lt;p&gt;Spring Boot is the &lt;em&gt;one&lt;/em&gt; big player in the market of available java application frameworks. Everybody is supposed to know Spring Boot and how to write applications using it. Yet, you see so many Spring Boot applications that have gotten out of control and are a nightmare to maintain. Why is that?&lt;/p&gt;

&lt;p&gt;In my opinion, this is because it is so easy to get started - you put an annotation here, you put an annotation there and voilà you have your REST service running. But then, it is not quite as easy to evolve and structure your application correctly if you are not aware of some key concepts and some of the stuff that is going on under the hoods. Without proper understanding, it is way too easy to screw things up. Examples?&lt;/p&gt;

&lt;p&gt;Application context configuration seems easy at a first glance but can get rather complex while your application grows. Do I use component scan or java based configuration? Can I mix them? Where do I put those config classes? Is a single &lt;code&gt;@Configuration&lt;/code&gt; class enough? What is this 'auto configuration' actually? If you can not properly answer these questions in context of your application's requirements you will end up creating an unmaintainable &lt;code&gt;ApplicationContext&lt;/code&gt; hell.&lt;/p&gt;

&lt;p&gt;There are other examples like scopes, logging configuration, property sources, AOP proxies, dependency management, bundling, profiles, web security and whatnotelse.&lt;/p&gt;

&lt;p&gt;In short: If you thought that writing an application using Spring Boot is as easy as putting &lt;code&gt;@SpringBootApplication&lt;/code&gt; on your main class, you are wrong. It is just as easy to screw everything up if you have no idea what you are actually doing.&lt;/p&gt;

&lt;p&gt;So here is my advice if you want to get serious with writing a Spring Boot application: &lt;a href="https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/"&gt;RTFM&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(Really, that is the single most important resource to learn how Spring Boot works)&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>java</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How a great package structure can make a great application</title>
      <dc:creator>Simon Taddiken</dc:creator>
      <pubDate>Wed, 10 Oct 2018 07:38:46 +0000</pubDate>
      <link>https://dev.to/skuzzle/how-a-great-package-structure-can-make-a-great-application-22ng</link>
      <guid>https://dev.to/skuzzle/how-a-great-package-structure-can-make-a-great-application-22ng</guid>
      <description>&lt;p&gt;In this post I will give you some &lt;em&gt;simple&lt;/em&gt; advice that, when followed carefully, will immediately lead to an easy to maintain and scalable architecture of your Java application. While this post is dedicated to Java the concepts will equally apply to other languages that allow to arrange code in packages.&lt;/p&gt;

&lt;p&gt;One could write books of how to achieve good architecture (and people did) but you need a better architecture right now and are short of time - and so am I - so let's jump into it. Everything I'm going to tell you now boils down to this single most important advice:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Use &lt;em&gt;package private&lt;/em&gt; scope liberally&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you do, this solves so many problems at once that, once you got used to it, you will hopefully be as amazed as I was when I realized how valuable this advice is. Let me explain this in detail.&lt;/p&gt;

&lt;p&gt;In bigger applications the key to maintainability is to keep &lt;em&gt;coupling&lt;/em&gt; of the &lt;br&gt;
application's single components under control. This assumes that your application must actually be structured into components in the first place. If it is not, you probably alreay experienced some kind of &lt;em&gt;inter-package-dependency-hell&lt;/em&gt; with package cycles and the alike (the good ol' &lt;em&gt;big ball of mud&lt;/em&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Dependency is the key problem in software development at all scales (&lt;em&gt;Kent Beck&lt;/em&gt;)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Java provides the concept of &lt;em&gt;packages&lt;/em&gt; to structure your code into components. But experience taught me that most times, a single component is spread across multiple packages. This happens when programmers choose their packages under &lt;em&gt;technical&lt;/em&gt; aspects. For example if you have a package &lt;code&gt;org.domain.app.controllers&lt;/code&gt; where all your controllers live and another package &lt;code&gt;org.domain.app.services&lt;/code&gt; where all your services live you have a technical package structure. In this scenario, possibly unrelated classes are located near to each other in the same package while related classes are located far away. &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Locality of related code is a key aspect of having code that is easy to reason about&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Because your related classes, which together solve a certain domain specific problem are located in different packages, they all have to be &lt;em&gt;public&lt;/em&gt; in order to work together. By choosing to not place related classes into the same package you open yourself up to all the evil of having certain classes used in places where you don't want them to be used.&lt;/p&gt;

&lt;p&gt;If upfront you chose a &lt;em&gt;domain&lt;/em&gt; or &lt;em&gt;feature&lt;/em&gt; related package structure there were an easy soultion to avoid this problem in the first place:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Give as many as possible classes the &lt;em&gt;package private&lt;/em&gt; scope&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Actually you should, by default, make every class &lt;em&gt;package private&lt;/em&gt; and only broaden its scope if necessary. After some time you will develop a bad feeling whenever you see something beeing &lt;em&gt;public&lt;/em&gt; - and this is a good thing. It makes you think explicitly about the public interface of your package. Following this advice you will automatically create packages with following characteristics:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;high cohesion: everything in the package is kind of related to the other things. The locality of related code will make it easy to reason about.&lt;/li&gt;
&lt;li&gt;low coupling to other packages: because there is only a minimal public interface of your package&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While these are good things you will also experience that your packages grow bigger and bigger up to a size that make you feel uncomfortable. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8947z3c5fy44tilgge0t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8947z3c5fy44tilgge0t.jpg" alt="No packages - No cycles"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you feel your package is growing too big that is a clear sign that there might be a better domain specific cut. In this situation you should review and question the whole purpose of the current package. In my experience you run into the following cases:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Your original feature got undermined by unrelated code which actually serves a different feature.&lt;/li&gt;
&lt;li&gt;As time passed, the requirements for the original feature changed that much that the original structure does not fit sufficiently anymore. It might also well be possible that the feature did not change, but your understanding of it did while you were coding it.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Most times the first case is simple to solve. If you are really in a situation where mostly unrelated code slipped into your feature package it should be pretty easy to isolate it and move it into its own package. &lt;/p&gt;

&lt;p&gt;If isolating smaller parts is not that easy you might be subject to the second case. This is where the sorcery of &lt;em&gt;package private&lt;/em&gt; reached its limits and you will have to do actual work. You are now up to some refactoring on package level - that is you need to split your current package into multiple new ones which better suit the requirements of the domain problems you are trying to solve. This is most likely not solvable by only moving some classes between packages but might require actual code changes. &lt;/p&gt;

&lt;p&gt;But because everything related is so near together and you know that most things are not used outside the package this gives you great confidence while refactoring.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh7cthlq5opoouugfl85v.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh7cthlq5opoouugfl85v.jpg" alt="unit tests"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever wondered what the &lt;em&gt;unit&lt;/em&gt; in unit testing is? You might have come across test suites where a test class exists for every production class. If so you most likely experienced that your unit tests are not very robust. They break easily if you adjust the implementation class even if you actually preserved behavior correctly. That is because &lt;em&gt;unit&lt;/em&gt; does not mean &lt;em&gt;class&lt;/em&gt;. &lt;em&gt;Unit&lt;/em&gt; pertains to all the code you want to test in isolation in order to solve a domain specific problem. &lt;em&gt;Unit&lt;/em&gt; means &lt;em&gt;feature&lt;/em&gt; and feature, in our case, means the public interface of your package.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Don't write a test class for every production class. Write a test class for every feature&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Doing so loosely couples your test code to the production code in the same way your production code is loosely coupled to other packages. You can freely change all the implementation details (like, even delete classes) behind the public interface without breaking your tests. That is the very meaning of &lt;em&gt;information hiding&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;To summarize, by using package private scope liberally you get (for free!)&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;high cohesion within your packages&lt;/li&gt;
&lt;li&gt;low coupling between packages&lt;/li&gt;
&lt;li&gt;a durable architecture because of well defined interfaces between components/packages&lt;/li&gt;
&lt;li&gt;code that is easier to reason about&lt;/li&gt;
&lt;li&gt;a good hint of what you should test in order to achieve more robust unit tests&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;And again: you get this for simply leaving out the &lt;code&gt;public&lt;/code&gt; modifier in you class declarations! Whenever you feel the need to create a new class, make it package private and place it into the package where, from a domain or feature point of view, you think it fits best. This puts you in a position where you can evolve your package layout carefully over time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Addendum&lt;/strong&gt;&lt;br&gt;
The attentive reader might have noticed the following: the above tips on structuring your packages is actually the same advice we followed along for decades on class level. It is undisputed that it is best practice to keep helper methods &lt;em&gt;private&lt;/em&gt; in order to have a well defined public interface. If &lt;em&gt;"dependency is a problem at all scales"&lt;/em&gt; it is likely and expectable that the solutions for managing dependency look similar at all scales too.&lt;/p&gt;

&lt;p&gt;More information:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Simon Brown on 'package by component': &lt;a href="http://www.codingthearchitecture.com/2015/03/08/package_by_component_and_architecturally_aligned_testing.html" rel="noopener noreferrer"&gt;http://www.codingthearchitecture.com/2015/03/08/package_by_component_and_architecturally_aligned_testing.html&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;a href="https://github.com/olivergierke/moduliths" rel="noopener noreferrer"&gt;https://github.com/olivergierke/moduliths&lt;/a&gt; (A spring Boot extension which tries to enforce this way of structring your application)&lt;/li&gt;
&lt;li&gt;Somthing about to DDD&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>architecture</category>
      <category>informationhiding</category>
      <category>packagebycomponent</category>
    </item>
  </channel>
</rss>
