Things to Code2018-09-16T21:06:47+00:00http://www.thingstocode.comTolga OnbayLocale-Independent String Sorting in Java2014-12-15T00:00:00+00:00http://www.thingstocode.com/2014/12/locale-independent-string-sorting-in-java<p>In Java programming language, <code class="highlighter-rouge">Arrays.sort</code> or <code class="highlighter-rouge">Collections.sort</code>
methods from the Class Library are used to sort a list of strings. These
methods use the <code class="highlighter-rouge">compareTo</code> method of the <code class="highlighter-rouge">Comparable</code> interface for
objects. The standard <code class="highlighter-rouge">compareTo</code> implementation of the String class uses
ASCII codes of the characters, and this works great in English.</p>
<p>When you need to sort strings in languages other than English, or more commonly
you need to write a software for global use, then you can use the
<a href="https://docs.oracle.com/javase/7/docs/api/java/text/Collator.html">Collator</a>
class.</p>
<p>Below is the Turkish alphabet in order.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">abc</span><span class="err">ç</span><span class="n">defg</span><span class="err">ğ</span><span class="n">h</span><span class="err">ı</span><span class="n">ijklmno</span><span class="err">ö</span><span class="n">prs</span><span class="err">ş</span><span class="n">tu</span><span class="err">ü</span><span class="n">vyz</span></code></pre></figure>
<p>Let’s sort the characters of the alphabet with <code class="highlighter-rouge">Arrays.sort</code> method.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">String</span><span class="o">[]</span> <span class="n">alphabet</span> <span class="o">=</span> <span class="s">"abcçdefgğhıijklmnoöprsştuüvyz"</span><span class="o">.</span><span class="na">split</span><span class="o">(</span><span class="s">""</span><span class="o">);</span>
<span class="n">Arrays</span><span class="o">.</span><span class="na">sort</span><span class="o">(</span><span class="n">alphabet</span><span class="o">);</span></code></pre></figure>
<p>The result is not ordered in Turkish, as expected.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">abcdefghijklmnoprstuvyz</span><span class="err">çöüğış</span></code></pre></figure>
<p>Let’s try the Collator class.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">Arrays</span><span class="o">.</span><span class="na">sort</span><span class="o">(</span><span class="n">alphabet</span><span class="o">,</span> <span class="n">Collator</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="n">Locale</span><span class="o">.</span><span class="na">forLanguageTag</span><span class="o">(</span><span class="s">"tr"</span><span class="o">)));</span></code></pre></figure>
<p>This works just in Turkish unsurprisingly. When you need to make your code work
in other languages too, you can use <code class="highlighter-rouge">getDefault</code> instead of
<code class="highlighter-rouge">forLanguageTag</code>.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="n">Arrays</span><span class="o">.</span><span class="na">sort</span><span class="o">(</span><span class="n">alphabet</span><span class="o">,</span> <span class="n">Collator</span><span class="o">.</span><span class="na">getInstance</span><span class="o">(</span><span class="n">Locale</span><span class="o">.</span><span class="na">getDefault</span><span class="o">()));</span></code></pre></figure>
<p>I will write about internationalization in detail, but until then, have a look
at <a href="http://blog.codinghorror.com/whats-wrong-with-turkey/">What’s wrong with
Turkey?</a> by Jeff Atwood</p>
Null Object Pattern2014-06-10T00:00:00+00:00http://www.thingstocode.com/2014/06/null-object-pattern<p>A couple of months ago I needed to write a logging mechanism where logs weren’t
always necessary to be persisted and log objects had to be transferred between
methods, so each method assigns some specific attributes of the log object
before being persisted. I know the latter seems like a terrible design, but not
everybody can work on a state-of-the-art system like some lucky guys do!</p>
<p>You can get a feeling of the log class below:</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">AuditLog</span> <span class="o">{</span>
<span class="kd">private</span> <span class="n">String</span> <span class="n">id</span><span class="o">;</span>
<span class="o">...</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">id</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="n">String</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">save</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">//Persist to database</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure>
<p>Standard procedure is of course passing null reference to the methods when
there is no need to log and afterwards checking if the object is not null, so
you can invoke its methods without getting a null reference exception.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TransactionHandler</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">handle</span><span class="o">(</span><span class="n">Transaction</span> <span class="n">transaction</span><span class="o">,</span> <span class="n">AuditLog</span> <span class="n">log</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">log</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span>
<span class="n">log</span><span class="o">.</span><span class="na">setId</span><span class="o">(</span><span class="n">id</span><span class="o">);</span>
<span class="o">...</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">log</span><span class="o">(</span><span class="n">AuditLog</span> <span class="n">log</span><span class="o">)</span> <span class="o">{</span>
<span class="k">if</span> <span class="o">(</span><span class="n">log</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span>
<span class="n">log</span><span class="o">.</span><span class="na">save</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure>
<p>If you are an Objective-C programmer, then you are lucky, you don’t need to
worry about checking if the object is null. But if you are using a mainstream
object oriented programming language like me, you need to keep in mind that the
objects can be null.</p>
<p>When you need to check null reference multiple times in more than a
few methods, then your code is going to start smelling. You should start
thinking about something else. Null Object Pattern can be your way out.</p>
<h2 id="null-object-pattern">Null Object Pattern</h2>
<p>Null Object Pattern is actually a special form of <a href="http://martinfowler.com/eaaCatalog/specialCase.html">Special
Case</a> pattern. It gives a
null meaning to your object and prevents getting null reference error and the
best part is: invoking its methods does nothing and there’s no need to check if
it is null. It will cost you a method call instead of an if statement, but you
will get a cleaner code.</p>
<p>Let’s get back to the design. We can transform the AuditLog class to use the
Null Object pattern like below.</p>
<p><img src="http://www.thingstocode.com/assets/2014/04/nullobjectpattern.png" alt="null object pattern" /></p>
<p>The code will simply be changed to:</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">interface</span> <span class="nc">AuditLog</span> <span class="o">{</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">();</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="n">String</span> <span class="n">id</span><span class="o">);</span>
<span class="o">...</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">save</span><span class="o">();</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">AuditLogImpl</span> <span class="kd">implements</span> <span class="n">AuditLog</span> <span class="o">{</span>
<span class="kd">private</span> <span class="n">String</span> <span class="n">id</span><span class="o">;</span>
<span class="o">...</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">id</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="n">String</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">id</span> <span class="o">=</span> <span class="n">id</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">save</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">//Persist to database</span>
<span class="o">}</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">NullAuditLog</span> <span class="kd">implements</span> <span class="n">AuditLog</span> <span class="o">{</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="kc">null</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setId</span><span class="o">(</span><span class="n">String</span> <span class="n">id</span><span class="o">)</span> <span class="o">{</span>
<span class="c1">//Do nothing</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="nd">@Override</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">save</span><span class="o">()</span> <span class="o">{</span>
<span class="c1">//Do nothing</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure>
<p>Invoking the save method with a NullAuditLog instance will do nothing and you
don’t need to add a null reference check anymore.</p>
<figure class="highlight"><pre><code class="language-java" data-lang="java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TransactionHandler</span> <span class="o">{</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">handle</span><span class="o">(</span><span class="n">Transaction</span> <span class="n">transaction</span><span class="o">,</span> <span class="n">AuditLog</span> <span class="n">log</span><span class="o">)</span> <span class="o">{</span>
<span class="n">log</span><span class="o">.</span><span class="na">setId</span><span class="o">(</span><span class="n">id</span><span class="o">);</span>
<span class="o">...</span>
<span class="o">}</span>
<span class="o">...</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">log</span><span class="o">(</span><span class="n">AuditLog</span> <span class="n">log</span><span class="o">)</span> <span class="o">{</span>
<span class="n">log</span><span class="o">.</span><span class="na">save</span><span class="o">();</span>
<span class="o">}</span>
<span class="o">}</span></code></pre></figure>
<p>Actually using an interface is the optimal usage. But in my design, I simply
extended AuditLog class as NullAuditLog and made it a private static
class inside of the AuditLog class. With this way null instance may have been
retrieved from the NULL constant defined in AuditLog class. You can have a look
at it on
<a href="https://github.com/tolgaonbay/examples/blob/master/src/com/thingstocode/examples/nullobject/AuditLog.java">github</a>.</p>
<p>There are some downsides of this usage, like when you add new methods to the
class, you should remember to add them into your null class. With an interface
it is clearer to add method definitions into your interface and implement them
in the derived classes.</p>
<h2 id="conclusion">Conclusion</h2>
<p>Even if it came out because of a <a href="http://www.infoq.com/presentations/Null-References-The-Billion-Dollar-Mistake-Tony-Hoare">billion dollar
mistake</a>,
Null Object Pattern is very handy and makes your code clean. This doesn’t mean
that you should apply Null Object Pattern to every single class in your design</p>
<ul>
<li>
<p>it can make things even more complicated and error-prone. It may be
used when:</p>
</li>
<li>It is possible to have a null reference and assigning null reference has a
meaning in the design. For example we used it to not persist a log into the
database</li>
<li>There are many null comparisons for the object across the system</li>
</ul>
<p>Take a look at the Special Case pattern if you didn’t before. I’m sure you will
find it very useful.</p>
Source Code for MS-DOS and Word for Windows2014-03-30T00:00:00+00:00http://www.thingstocode.com/2014/03/source-code-ms-dos-word-windows<p><a href="http://blogs.technet.com/b/microsoft_blog/archive/2014/03/25/microsoft-makes-source-code-for-ms-dos-and-word-for-windows-available-to-public.aspx">Microsoft released source code for MS-DOS and Word for
Windows</a> this
week with the Computer History Museum.</p>
<blockquote>
<p>On Tuesday, we dusted off the source code for early versions of MS-DOS and
Word for Windows. With the help of the Computer History Museum, we are making
this code available to the public for the first time.</p>
</blockquote>
<p>Without reading the whole press release, you might think of it as Microsoft’s
new move towards getting into the open source community. Actually the intent is
good, but it’s not making them open source:</p>
<blockquote>
<p>Thanks to the Computer History Museum, these important pieces of source code
will be preserved and made available to the community for historical and
technical scholarship.</p>
</blockquote>
<p>As I said the intent is good, but this effort might have been more meaningful
maybe 15 years ago. People could have learned many things from their work and
they could have used them in their fields. Who knows, maybe we would have had
better word processors by now.</p>
<p>Of course, this is about their marketing strategy, and I don’t blame them for
this. I don’t really think they are evil. They have some <a href="http://www.microsoft.com/en-us/openness/">good
efforts</a>. Besides <a href="http://www.hanselman.com/blog/MicrosoftKilledMyPappy.aspx">Microsoft didn’t
kill my pappy</a>, not
yet.</p>
<p>You can grab source codes
of <a href="http://www.computerhistory.org/_static/atchm/microsoft-ms-dos-early-source-code/">MS-DOS</a>
and <a href="http://www.computerhistory.org/_static/atchm/microsoft-word-for-windows-1-1a-source-code/">Word for
Windows</a>
from the Computer History Museum’s website.</p>
The Good Enough Software Design2014-03-09T00:00:00+00:00http://www.thingstocode.com/2014/03/good-enough-software-design<p>Designing software is a challenge, especially for a perfectionist programmer.
There are tons of decisions to make and lots of problems with many solutions.
It is very likely that some of them will be hard. You can find yourself
searching best practices online, and asking questions at
<a href="http://www.stackoverflow.com">stackoverflow.com</a>. Even if you find practices
and you get the answers you want, it is quite possible that you will end up
with a design that you are not satisfied with. You doubt whether it is a
perfect design or not. Guess what, there is no perfect design!</p>
<h2 id="mount-of-design">Mount of Design</h2>
<p>It is impossible to solve the design puzzle as a whole. Every requirement will
pop up in your brain and you will see the biggest challenges in the design like
the choices about the data or the presentation layer. Do I need to use MVC or
MVVM? Which ORM tool do I need to use? How am I supposed to handle multiple
database connections? Every question will make you feel more overwhelmed and
you will start to see the design as an ever growing mountain. Every step you
take will seem worthless to the giant mountain in front of you. Feeling
overwhelmed can make you want to escape from the problem and the
procrastination problem begins. But procrastination won’t lead you to the
solution.</p>
<p>The simplest solution to this problem is to <a href="http://en.wikipedia.org/wiki/Divide_and_rule">Divide &
Conquer</a>. You have to stop
thinking about the entire system in your head. You should divide the design
into smaller blocks and see what you can do to those simple blocks one by one.
If possible, choose an iterative development approach like
<a href="http://en.wikipedia.org/wiki/Extreme_programming">XP</a>. This way you can focus
design obstacles iteratively and then you will start to see multiple small
hills in your head.</p>
<p>Also don’t spend too much time on choosing tools or APIs to use like a database
or a rule engine. These are just tools to make our lives easier. You should
spend more time on general design principles like <a href="http://en.wikipedia.org/wiki/Separation_of_concerns">Separation of
Concerns</a>. For example SoC
will allow you to change any layer or any tool in your software without
affecting the others. Apply
<a href="http://en.wikipedia.org/wiki/Test-driven_development">TDD</a>, it will pump up
your courage and let you make radical changes in your software.</p>
<p>Don’t forget; the time spent on design is never enough. As Steve McConnell said
in his masterpiece, <a href="http://www.amazon.com/gp/product/0735619670/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=0735619670&linkCode=as2&tag=thin0f5-20">Code
Complete</a>,</p>
<blockquote>
<p>When are you done [designing]? Since design is open-ended, the most common
answer to that question is “When you’re out of time.”</p>
</blockquote>
<h2 id="requirements-change">Requirements Change</h2>
<p>Your client or your company has to adapt changes in the business to survive.
This means that requirements will change. You can’t write a software to cover
all the possible requirements that could come up in the future. There will
always be some requirements that didn’t even pop up in your brain. So don’t try
to cover future requirements because <a href="http://en.wikipedia.org/wiki/You_aren">You Aren’t Gonna Need
It</a>. It is like reserving and decorating
a room for a baby when you aren’t even expecting one. Try to write a software
that covers the current requirements.</p>
<h2 id="dont-overengineer">Don’t Overengineer</h2>
<p>If you work on a specific feature for days that will reduce the work of your
client just 5 minutes in a year, then your work is actually useless. I know
sometimes we can’t stop ourselves making that particular change, because it
might feel us like a better programmer or we might do it just for fun, but you
need to consider the cost/benefit ratio. Your time is precious, spend your time
on something better.</p>
<p><a href="https://xkcd.com/974"><img src="http://imgs.xkcd.com/comics/the_general_problem.png" alt="Overengineering problem" /></a></p>
<h2 id="simplify">Simplify</h2>
<p>When you just need to store the state of an object into the file system, no
time-intensive job or anything special, it is pointless to write a custom
object serializer. I saw some designs that look like masterpieces from an
engineering point of view, but they made everything overcomplicated. It takes
weeks for newcomers to adapt to the system. You shouldn’t make your design
overcomplicated. Sometimes the best solution is the simplest one. So keep in
mind the <a href="http://en.wikipedia.org/wiki/KISS_principle">KISS principle</a>.</p>
<h2 id="courage">Courage</h2>
<p>The design actually is the outcome of the programmer’s experience. It is quite
likely that different programmers can come up with different design ideas. This
might result in you searching for the best possible way to design a feature
since there is no single solution to a design problem. Don’t do that. It
doesn’t mean you should stop learning, listening or reading other people’s
ideas, of course you should. But design phase is not the time to learn all the
best practices in the world that could possibly be applied to your problem.
Have some courage, do your best design and start implementing your design
ideas.</p>
<p>In the design, you might give a class more responsibility than it should have,
or you might not see the inheritance relationship between two classes, or you
might miss a clearly visible pattern. When you start coding, you will see some
similar flaws in your design.
<a href="http://en.wikipedia.org/wiki/Code_refactoring">Refactoring</a> is a really good
practice to make your design better. Don’t try to make a perfect design, start
coding and refactor when your <a href="http://en.wikipedia.org/wiki/Code_smell">code
smells</a>. You will learn from your
mistakes and they will be helpful for your future designs.</p>
<h2 id="no-perfect-sofware">No Perfect Sofware</h2>
<p>Whatever you do you will end up with a piece of software that won’t satisfy
you. As Hunt and Thomas wrote in <a href="http://www.amazon.com/gp/product/020161622X/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=020161622X&linkCode=as2&tag=thin0f5-20">The Pragmatic
Programmer</a>;</p>
<blockquote>
<p>… perfect software doesn’t exist. No one in the brief history of computing
has ever written a piece of perfect software. It’s unlikely that you’ll be
the first. And unless you accept this as a fact, you’ll end up wasting time
and energy chasing an impossible dream.</p>
</blockquote>
<p>I’m sorry, but they are right. Instead of chasing an impossible dream, try to
make your design “good enough” and start coding!</p>
Developing Hadoop Projects in Eclipse2014-01-31T00:00:00+00:00http://www.thingstocode.com/2014/01/developing-hadoop-projects-in-eclipse<p>I’ve started learning the <a href="http://hadoop.apache.org/">Apache Hadoop</a> platform
recently with a great book; <a href="http://www.amazon.com/gp/product/1449311520/ref=as_li_ss_tl?ie=UTF8&camp=1789&creative=390957&creativeASIN=1449311520&linkCode=as2&tag=thin0f5-20">Hadoop: The Definitive
Guide</a>.
I had written some examples in terminal, but the projects were getting bigger
and bigger. Eventually, I decided to switch to an IDE and started using
Eclipse. Below I will explain two different ways to start your Hadoop Eclipse
project in standalone mode.</p>
<p>##Importing Libraries</p>
<p>Create a new project and add these libraries as a reference to your project.</p>
<ul>
<li>commons-configuration-1.6.jar</li>
<li>commons-httpclient-3.0.1.jar</li>
<li>commons-lang-2.4.jar</li>
<li>commons-logging-1.1.1.jar</li>
<li>hadoop-core-1.0.0.jar</li>
<li>jackson-core-asl-1.0.1.jar</li>
<li>jackson-mapper-asl-1.0.1.jar</li>
</ul>
<p>Add your source folder and you are ready to start your MapReduce application!</p>
<p><img src="/assets/2014/02/hadoop-project.png" alt="Hadoop project" /></p>
<p>##Using Maven</p>
<p>First you need to create a pom.xml file. You can get it from my repository on
github.</p>
<p>After getting the pom.xml, you can create your Eclipse project with the
following command.</p>
<p><code class="highlighter-rouge">mvn eclipse:eclipse</code></p>
<p>Alternatively you can start a Java project in Eclipse, add pom.xml file to your
project, right click your pom.xml and select Run as > Maven Build. Then you
should write eclipse:eclipse to the Goals section and hit Run.</p>
<p><img src="/assets/2014/02/hadoop-maven-build.png" alt="Hadoop maven build" /></p>
<p>After the build is completed, right click on your project and Refresh your
project. You should see the added libraries in the Referenced Libraries
section.</p>
<p><img src="/assets/2014/02/hadoop-maven-build.png" alt="Hadoop project maven" /></p>
<p>Now, you can run your application.</p>
Hello world!2014-01-30T00:00:00+00:00http://www.thingstocode.com/2014/01/hello-world<p>And the blogging began…</p>