<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Coding-Design on dlow's blog</title><link>https://blog.dlow.me/tags/coding-design/</link><description>Recent content in Coding-Design on dlow's blog</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Wed, 14 Oct 2020 06:34:42 +0100</lastBuildDate><atom:link href="https://blog.dlow.me/tags/coding-design/index.xml" rel="self" type="application/rss+xml"/><item><title>Objects and Data Structure/Container</title><link>https://blog.dlow.me/programming/object-and-data-structure/</link><pubDate>Wed, 14 Oct 2020 06:34:42 +0100</pubDate><guid>https://blog.dlow.me/programming/object-and-data-structure/</guid><description>The differences and how it affects code maintenance.</description></item><item><title>Anti pattern: turning struct into interface</title><link>https://blog.dlow.me/programming/golang/anti-pattern-turning-struct-into-interface/</link><pubDate>Sun, 24 May 2020 12:44:55 +0100</pubDate><guid>https://blog.dlow.me/programming/golang/anti-pattern-turning-struct-into-interface/</guid><description>&lt;p>&lt;a href="../struct-embedding">Previously I wrote about using struct embedding&lt;/a> to achieve pseudo inheritance for code reuse. In this post, I want to explore an example of what I think is an anti pattern as a result of the lack of inheritance and how I think it could be better.&lt;/p>
&lt;p>In the absence of inheritance in golang, it is very tempting to turn fields into functions of an interface.&lt;/p>
&lt;p>As an motivating example, consider the following:&lt;/p></description></item><item><title>Struct embedding as implicit composition</title><link>https://blog.dlow.me/programming/golang/struct-embedding/</link><pubDate>Fri, 22 May 2020 11:26:33 +0100</pubDate><guid>https://blog.dlow.me/programming/golang/struct-embedding/</guid><description>&lt;p>In programming, the principle &amp;ldquo;don&amp;rsquo;t repeat yourself&amp;rdquo;, DRY for short, recommends that we try to reuse the same implementation where possible. In this post, I will cover what it&amp;rsquo;s like to do it in golang through struct embedding and my personal thoughts on that pattern.&lt;/p>
&lt;h1 id="golang-struct-embedding">Golang struct embedding &lt;a class="anchor" href="#golang-struct-embedding">&lt;span>&lt;/span>&lt;/a>&lt;/h1>&lt;p>One common way of code reuse is inheritance. In Java, we can do this by implementing a method on the parent class, then subclasses will inherit this implementation.&lt;/p></description></item><item><title>Monadic Boolean 2</title><link>https://blog.dlow.me/programming/scala/monadic-boolean-2/</link><pubDate>Thu, 04 Jul 2019 23:04:54 +0100</pubDate><guid>https://blog.dlow.me/programming/scala/monadic-boolean-2/</guid><description>&lt;p>&lt;a href="../monadic-boolean">Previously&lt;/a>, I concluded that using &lt;code>Either[String, Unit]&lt;/code> is quite a nice way to compose predicates. Since then I have noticed some other patterns in my code that I would argue is an improvement.&lt;/p>
&lt;p>We will still keep the &amp;ldquo;for-comprehension&amp;rdquo; style as I find that it makes the code more readable due to the shorter indentation compared to nested if else blocks.&lt;/p>
&lt;p>The main difference is rather than defining functions that take the input and produce the monad, we use implicits to turn a boolean (or any other types as we shall see) into the monad we want for composition.&lt;/p></description></item><item><title>Monadic Boolean</title><link>https://blog.dlow.me/programming/scala/monadic-boolean/</link><pubDate>Mon, 25 Mar 2019 21:42:51 +0000</pubDate><guid>https://blog.dlow.me/programming/scala/monadic-boolean/</guid><description>&lt;p>The following task is quite common: do something if all given predicates are satisfied and if some predicate fails, make it obvious which failed.&lt;/p>
&lt;p>Consider the contrived scenario where we are asked to save a number to the database if it satisfy all our conditions:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#f8f8f2;background-color:#272822;-moz-tab-size:4;-o-tab-size:4;tab-size:4;">&lt;code class="language-scala" data-lang="scala">&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> isGood&lt;span style="color:#f92672">(&lt;/span>x&lt;span style="color:#66d9ef">:&lt;/span> &lt;span style="color:#66d9ef">Int&lt;/span>&lt;span style="color:#f92672">)&lt;/span>&lt;span style="color:#66d9ef">:&lt;/span> &lt;span style="color:#66d9ef">Boolean&lt;/span> &lt;span style="color:#f92672">=&lt;/span> isConditionA&lt;span style="color:#f92672">(&lt;/span>x&lt;span style="color:#f92672">)&lt;/span> &lt;span style="color:#f92672">&amp;amp;&amp;amp;&lt;/span> isConditionB&lt;span style="color:#f92672">(&lt;/span>x&lt;span style="color:#f92672">)&lt;/span> &lt;span style="color:#f92672">...&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span>&lt;span style="color:#66d9ef">def&lt;/span> usage&lt;span style="color:#f92672">(&lt;/span>x&lt;span style="color:#66d9ef">:&lt;/span> &lt;span style="color:#66d9ef">Int&lt;/span>&lt;span style="color:#f92672">)&lt;/span>&lt;span style="color:#66d9ef">:&lt;/span> &lt;span style="color:#66d9ef">Unit&lt;/span> &lt;span style="color:#f92672">=&lt;/span> &lt;span style="color:#66d9ef">if&lt;/span> &lt;span style="color:#f92672">(&lt;/span>isGood&lt;span style="color:#f92672">(&lt;/span>x&lt;span style="color:#f92672">))&lt;/span> saveNumber&lt;span style="color:#f92672">(&lt;/span>x&lt;span style="color:#f92672">)&lt;/span> &lt;span style="color:#66d9ef">else&lt;/span> doNothing&lt;span style="color:#f92672">()&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Some time later, we notice that some numbers are being rejected, and we have no idea why, so we decide to add logging when a number is rejected. How should we go about it?&lt;/p></description></item></channel></rss>