<?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: Amol</title>
    <description>The latest articles on DEV Community by Amol (@amol).</description>
    <link>https://dev.to/amol</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%2F172654%2Fd4b1ebc1-8916-467f-99c0-2eb2b036a341.jpg</url>
      <title>DEV Community: Amol</title>
      <link>https://dev.to/amol</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/amol"/>
    <language>en</language>
    <item>
      <title>Clear and honest code</title>
      <dc:creator>Amol</dc:creator>
      <pubDate>Fri, 11 Aug 2023 18:30:00 +0000</pubDate>
      <link>https://dev.to/amol/clear-and-honest-code-3l1a</link>
      <guid>https://dev.to/amol/clear-and-honest-code-3l1a</guid>
      <description>&lt;p&gt;When working with a huge code base, it's common to forget how a specific code block (function, method, or class) is implemented. However, the IDE suggests using a signature. Sometimes calling a code block can come with unpleasant shocks. By creating honest and clear code blocks, this can be prevented.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// (account: Account, amount : BigDecimal) -&amp;gt; Account&lt;/span&gt;
&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;credit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nc"&gt;ZERO&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="nc"&gt;NegativeAmountError&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;balance&lt;/span&gt; &lt;span class="p"&gt;+&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By looking at the signature, caller simply does't know that it's going to blow up. This kind of code blocks affect engineer in 2 ways&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Increase cognitive overload of reading and understanding each branch of the code block before calling it&lt;/li&gt;
&lt;li&gt;hampers trust of code block.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Honest signature could have been something like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;NonNegativeCurrency&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;
&lt;span class="c1"&gt;// or&lt;/span&gt;
&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;account&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;BigDecimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Either&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;NegativeAmountError&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;Account&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These 2 signatures appear quite straightforward, yet they use 2 different techniques.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The first signature prevents entry into an incorrect state by forbidding negative input.&lt;/li&gt;
&lt;li&gt;While allowing erroneous input, the second signature communicates output more precisely. It informs the caller that it may provide an error or credit to their account.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;References&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Treat Type as set Read &lt;a href="https://blog.ploeh.dk/2021/11/15/types-as-sets/" rel="noopener noreferrer"&gt;here&lt;/a&gt; and &lt;a href="https://guide.elm-lang.org/appendix/types_as_sets.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;&lt;a href="https://en.wikipedia.org/wiki/Algebraic_data_type" rel="noopener noreferrer"&gt;Algebraic data type&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>programming</category>
      <category>adt</category>
      <category>kotlin</category>
      <category>design</category>
    </item>
    <item>
      <title>Code that changes together, stays together</title>
      <dc:creator>Amol</dc:creator>
      <pubDate>Mon, 01 Aug 2022 18:30:00 +0000</pubDate>
      <link>https://dev.to/amol/code-that-changes-together-stays-together-3p53</link>
      <guid>https://dev.to/amol/code-that-changes-together-stays-together-3p53</guid>
      <description>&lt;p&gt;In traditional clean/layered/onion architecture code is organized in layers and so as the abstractions such as controllers, services, repositories.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Famol.me%2Fassets%2Fblog%2Fclean-architecture.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Famol.me%2Fassets%2Fblog%2Fclean-architecture.jpeg" alt="Clean Architecture" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What I have seen most of people do is tried to organize code per layer by structuring code around those layer.&lt;br&gt;
For example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;src&lt;/span&gt;
 &lt;span class="s"&gt;- controllers&lt;/span&gt;
 &lt;span class="s"&gt;- services&lt;/span&gt;
 &lt;span class="s"&gt;- use case&lt;/span&gt;
 &lt;span class="s"&gt;- entities&lt;/span&gt;
 &lt;span class="s"&gt;- repositories&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In some projects, where I see team wanted to build modular monolith, they started to group based on some heuristic, that these pieces goes together probably we should modularize it.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;src&lt;/span&gt;
 &lt;span class="s"&gt;- module#1&lt;/span&gt;
  &lt;span class="s"&gt;- controllers&lt;/span&gt;
  &lt;span class="s"&gt;- services&lt;/span&gt;
  &lt;span class="s"&gt;- use case&lt;/span&gt;
  &lt;span class="s"&gt;- entities&lt;/span&gt;
  &lt;span class="s"&gt;- repositories&lt;/span&gt;
 &lt;span class="s"&gt;- module#2&lt;/span&gt;
  &lt;span class="s"&gt;- controllers&lt;/span&gt;
  &lt;span class="s"&gt;- services&lt;/span&gt;
  &lt;span class="s"&gt;- use case&lt;/span&gt;
  &lt;span class="s"&gt;- entities&lt;/span&gt;
  &lt;span class="s"&gt;- repositories&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I consider this way of modularizing code base as premature as it suffers from forcing you to make decision when you the least knowledge about the system. Sometimes this kind of modularizing felt so artificial.&lt;/p&gt;

&lt;p&gt;So I was wondering 🤔&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;how we could defer decision of making modularizing &lt;code&gt;code structure&lt;/code&gt; as much as I can?&lt;/li&gt;
&lt;li&gt;How can I let &lt;code&gt;code structure&lt;/code&gt; evolve itself organically?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There was another problem, If I want to make any changes, I have to make changes in upon 4-5 different packages such as controller, services, use case. In lot of cases I felt that it's lot of ceremony and dancing around so many files. There was urge of keeping things together.&lt;/p&gt;

&lt;p&gt;Suddenly something clicked&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;"Neurons that fire together wire together" or "Family thats eats together, stays together"&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;which leads to&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;**Code&lt;/em&gt;* thats &lt;strong&gt;changes&lt;/strong&gt; together, stays together*&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Famol.me%2Fassets%2Fblog%2Fvertical-slice.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Famol.me%2Fassets%2Fblog%2Fvertical-slice.png" alt="vertical slice" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Famol.me%2Fassets%2Fblog%2Fvertical-slice-detail.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Famol.me%2Fassets%2Fblog%2Fvertical-slice-detail.png" alt="vertical slice" width="800" height="400"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I move code which was changing together closer, 2 things happened&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Code thats was coming closer, coupling between them increased(on vertical access).&lt;/li&gt;
&lt;li&gt;Coupling between different slices started to drop&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After spending more time with this approach, I learned that code thats shared across slices bubbled up and moved into core of the feature or is a domain concept. or it's just cross cutting concern. which then could moved up and finds it own place not just in code base but also conceptually.&lt;/p&gt;

&lt;p&gt;In summary this approach provide me following flexibilities&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ Code structure can evolve as organically as with our understanding of the domain concepts.&lt;/li&gt;
&lt;li&gt;✅ Provide a way be more pragmatic per slice.&lt;/li&gt;
&lt;li&gt;✅ Provide way of grouping which can scale with growing feature sets.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Final code structure was similar to 👇🏼&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight yaml"&gt;&lt;code&gt;&lt;span class="pi"&gt;-&lt;/span&gt; &lt;span class="s"&gt;src&lt;/span&gt;
 &lt;span class="s"&gt;- todo&lt;/span&gt;
  &lt;span class="s"&gt;- api&lt;/span&gt;
   &lt;span class="s"&gt;- dto&lt;/span&gt;
   &lt;span class="s"&gt;- controller&lt;/span&gt;
  &lt;span class="s"&gt;- find-todos&lt;/span&gt;
  &lt;span class="s"&gt;- complete-todo&lt;/span&gt;
  &lt;span class="s"&gt;- new-todo&lt;/span&gt;
  &lt;span class="s"&gt;- todo&lt;/span&gt;
 &lt;span class="s"&gt;- notes&lt;/span&gt;
  &lt;span class="s"&gt;- api&lt;/span&gt;
   &lt;span class="s"&gt;- dto&lt;/span&gt;
   &lt;span class="s"&gt;- controller&lt;/span&gt;
  &lt;span class="s"&gt;- new-notes&lt;/span&gt;
  &lt;span class="s"&gt;- recent-notes&lt;/span&gt;
  &lt;span class="s"&gt;- note&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>programming</category>
      <category>architecture</category>
      <category>codebase</category>
    </item>
    <item>
      <title>Nature, Impact and Value of Change</title>
      <dc:creator>Amol</dc:creator>
      <pubDate>Tue, 15 Feb 2022 10:00:00 +0000</pubDate>
      <link>https://dev.to/amol/nature-impact-and-value-of-change-38ke</link>
      <guid>https://dev.to/amol/nature-impact-and-value-of-change-38ke</guid>
      <description>&lt;p&gt;&lt;em&gt;In this article, I'm sharing lesson and learning about change. How understanding &lt;strong&gt;nature, impact and value of change&lt;/strong&gt; resulted into more clarity and confidence and helped me to prepare release plan.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Situation 🤺
&lt;/h2&gt;

&lt;p&gt;Like my other colleagues, I also have been part of such &lt;em&gt;Task force&lt;/em&gt; couple of times. But recently in my task force, it was becoming very hard to come up with proper release plan. We're struggling to make a release plan which can be delivered &lt;em&gt;incrementally&lt;/em&gt; and &lt;em&gt;iteratively&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Background 📜
&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is task force ? 🤔
&lt;/h3&gt;

&lt;p&gt;At &lt;a href="https://golayer.io/about/" rel="noopener noreferrer"&gt;layer&lt;/a&gt; we form a triplet of &lt;em&gt;Tech, Product and Business&lt;/em&gt; in order to refine features. Mostly one person from each area. Sometimes Product owner represents expertise in business or act as proxy for business. For each epic people rotates from each area. This helps to create opportunities to learn, grow and avoid biases in the company. We called such triplet as &lt;em&gt;Task force&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Task force has following responsibility 💪&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Refine feature aka epic&lt;/li&gt;
&lt;li&gt;Be a point of contact for all the QA during crunch time.&lt;/li&gt;
&lt;li&gt;Prepare release plan&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Still picture was not very clear in 🧠
&lt;/h2&gt;

&lt;p&gt;We had few brainstorming meetings but still picture was not very clear in our head; so I decided to analyse this epic critically and come up some options.&lt;/p&gt;

&lt;h2&gt;
  
  
  What we did 🤞
&lt;/h2&gt;

&lt;p&gt;I decided to start from clean slate and look at this feature from users point of view. To do that I asked following question for each concepts and change that we wanted to bring in.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;em&gt;What problem it solves for user?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;&lt;em&gt;What problem it might create for user?&lt;/em&gt;&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;What is the nature of this changes?&lt;/em&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;em&gt;Breaking change&lt;/em&gt; - if it &lt;em&gt;breaks or changes existing concept/feature drastically&lt;/em&gt; and &lt;em&gt;changes affects user flow&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;New change&lt;/em&gt; - there was &lt;em&gt;no such concepts exist before&lt;/em&gt; into the system and it &lt;em&gt;may or may not  affect user flow&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;em&gt;Enhancement&lt;/em&gt; - changes &lt;em&gt;refines existing concepts&lt;/em&gt; but not so drastically and may  not affect user flow.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;After doing this analysis and spending few hours, I realised more than 70 % changes were either enhancement and new changes and ~ 30 % were breaking changes. Just doing analysis unblocked very big chunk of feature and it bring ton of clarity in my head.&lt;/p&gt;

&lt;p&gt;For breaking changes, I did one extra step I asked same 3 questions for existing feature to gain more clarity. After that, I knew exactly what we're taking out and giving back to the user. Now it was very easy to spot and fill the gaps.&lt;/p&gt;

&lt;p&gt;In summary&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I learned that understanding &lt;em&gt;nature&lt;/em&gt;, &lt;em&gt;impact&lt;/em&gt; and &lt;em&gt;value&lt;/em&gt; it brings to the user are very crucial aspects.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Picture credits goes to &lt;a href="https://unsplash.com/photos/PUvPZckRnOg" rel="noopener noreferrer"&gt;Aziz Acharki&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;NOTE: If you have similar learning, please share your experience with me. There is always something new about every experience.&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>change</category>
      <category>development</category>
      <category>lesson</category>
    </item>
    <item>
      <title>Fine control over execution in kotlin</title>
      <dc:creator>Amol</dc:creator>
      <pubDate>Tue, 12 Oct 2021 10:00:00 +0000</pubDate>
      <link>https://dev.to/amol/fine-control-over-execution-in-kotlin-3h</link>
      <guid>https://dev.to/amol/fine-control-over-execution-in-kotlin-3h</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.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%2Foerbhan7oirwzudhoyt9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.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%2Foerbhan7oirwzudhoyt9.jpg" alt="Alt Text" width="640" height="427"&gt;&lt;/a&gt;&lt;br&gt;
Programing language that supports concurrency and parallelism needs to provide cancellation mechanism as well. Well thought cancellation mechanism also gives space to clean up resources.&lt;/p&gt;

&lt;p&gt;In this blog post we’ll take see how to cancel coroutine and impact of suspend functions on it.&lt;/p&gt;
&lt;h2&gt;
  
  
  Basics
&lt;/h2&gt;

&lt;p&gt;The &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html" rel="noopener noreferrer"&gt;&lt;code&gt;Job&lt;/code&gt;&lt;/a&gt; interface has a method called &lt;a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/cancel.html" rel="noopener noreferrer"&gt;&lt;code&gt;cancel&lt;/code&gt;&lt;/a&gt;, which allows to cancel the job.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runBlocking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;job&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"printing $i"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancelAndJoin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cancelled successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// printing 0&lt;/span&gt;
&lt;span class="c1"&gt;// printing 1&lt;/span&gt;
&lt;span class="c1"&gt;// printing 2&lt;/span&gt;
&lt;span class="c1"&gt;// printing 3&lt;/span&gt;
&lt;span class="c1"&gt;// printing 4&lt;/span&gt;
&lt;span class="c1"&gt;// cancelled successfully&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Calling cancel has following effects on job.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ends execution job at first suspension point&lt;/li&gt;
&lt;li&gt;if job has some children, they are also canceled at first suspension point&lt;/li&gt;
&lt;li&gt;Once job is canceled, it can not used as a parent for any new coroutines.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Lets verify our first assumption by replacing &lt;code&gt;delay&lt;/code&gt; call by &lt;code&gt;Thread.sleep&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runBlocking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;job&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt;
      &lt;span class="c1"&gt;// delay(200)&lt;/span&gt;
      &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
      &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"printing $i"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancelAndJoin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
  &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cancelled successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// what will be the outcome?&lt;/span&gt;

&lt;span class="c1"&gt;// outcome:&lt;/span&gt;
&lt;span class="c1"&gt;// printing 0&lt;/span&gt;
&lt;span class="c1"&gt;// printing ..&lt;/span&gt;
&lt;span class="c1"&gt;// printing 99&lt;/span&gt;
&lt;span class="c1"&gt;// cancelled successfully&lt;/span&gt;

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

&lt;/div&gt;



&lt;h2&gt;
  
  
  Thread blocking code
&lt;/h2&gt;

&lt;p&gt;Once we removed delay function job has no suspension point and hence it continues to work until the end of computation. &lt;code&gt;Thread.sleep&lt;/code&gt; is thread blocking function let’s extract repeat code block into suspended function.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runBlocking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;job&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;threadBlockingFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"job completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancelAndJoin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cancelled successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;suspend&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;threadBlockingFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Int&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;coroutineScope&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;200&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"printing $i"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;msg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;with&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Thread&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;currentThread&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;kotlin&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;io&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"$id:$name\t=&amp;gt; $msg"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt; 
&lt;span class="c1"&gt;// what will be the outcome?&lt;/span&gt;

&lt;span class="c1"&gt;// outcome:&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 0&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing .. 🧐&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 99 🤔&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; job completed&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; cancelled successfully&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;job&lt;/code&gt; is running on single thread and even we have suspended function &lt;code&gt;threadBlockingFn&lt;/code&gt; &lt;code&gt;main&lt;/code&gt; thread does not get unblocked to observe cancellation request. So &lt;em&gt;how to create suspension point in this case?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;In order to have more fine control, lets create coroutine for every call of &lt;code&gt;threadBlockFn&lt;/code&gt; and join it back with parent job&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runBlocking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;job&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;threadBlockingFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}.&lt;/span&gt;&lt;span class="nf"&gt;join&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="c1"&gt;// also produces same output&lt;/span&gt;
        &lt;span class="c1"&gt;// repeat(1_00) { i -&amp;gt; async { threadBlockingFn(i) }.await() }&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"job completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancelAndJoin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cancelled successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// output: &lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 0&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 1&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 2&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 3&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 4&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; printing 5&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; cancelled successfully&lt;/span&gt;

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

&lt;/div&gt;



&lt;p&gt;Lets use default dispatcher to delegate &lt;code&gt;threadBlockFn&lt;/code&gt; execution&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;runBlocking&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;job&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;launch&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nf"&gt;repeat&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_00&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;i&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;withContext&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Dispatchers&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Default&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nf"&gt;threadBlockingFn&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;i&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"job completed"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="nf"&gt;delay&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1_150&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;job&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;cancelAndJoin&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="nf"&gt;println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"cancelled successfully"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// output: &lt;/span&gt;
&lt;span class="c1"&gt;// 14:DefaultDispatcher-worker-1    =&amp;gt; printing 0&lt;/span&gt;
&lt;span class="c1"&gt;// 14:DefaultDispatcher-worker-1    =&amp;gt; printing 1&lt;/span&gt;
&lt;span class="c1"&gt;// 14:DefaultDispatcher-worker-1    =&amp;gt; printing 2&lt;/span&gt;
&lt;span class="c1"&gt;// 14:DefaultDispatcher-worker-1    =&amp;gt; printing 3&lt;/span&gt;
&lt;span class="c1"&gt;// 14:DefaultDispatcher-worker-1    =&amp;gt; printing 4&lt;/span&gt;
&lt;span class="c1"&gt;// 14:DefaultDispatcher-worker-1    =&amp;gt; printing 5&lt;/span&gt;
&lt;span class="c1"&gt;// 1:main   =&amp;gt; cancelled successfully&lt;/span&gt;

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

&lt;/div&gt;



&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Coroutine, dispatcher along with suspended function provides very powerful mechanism to have full control over execution of code blocks. Default function such as &lt;code&gt;delay&lt;/code&gt; are cancel aware functions. To summarize, I would say use following thumb rules to achieve fine controlled code&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Have more suspension points in code base&lt;/li&gt;
&lt;li&gt;Avoid thread blocking code areas&lt;/li&gt;
&lt;li&gt;coroutines are very lightweight, use them more frequently&lt;/li&gt;
&lt;li&gt;use &lt;code&gt;async/await&lt;/code&gt;, &lt;code&gt;withContext&lt;/code&gt; around appropriate spaces&lt;/li&gt;
&lt;li&gt;Break down computation heavy operation into smaller pieces with more suspension points&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Dispatchers
&lt;/h3&gt;

&lt;p&gt;&lt;em&gt;Coroutine has different kind of dispatchers available as mentioned in the doc&lt;/em&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Dispatchers.Default&lt;/code&gt; — is used by all standard builders if no dispatcher or any other ContinuationInterceptor is specified in their context. It uses a common pool of shared background threads. This is an appropriate choice for compute-intensive coroutines that consume CPU resources.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dispatchers.IO&lt;/code&gt; — uses a shared pool of on-demand created threads and is designed for offloading of IO-intensive blocking operations (like file I/O and blocking socket I/O).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Dispatchers.Unconfined&lt;/code&gt; — starts coroutine execution in the current call-frame until the first suspension, whereupon the coroutine builder function returns. The coroutine will later resume in whatever thread used by the corresponding suspending function, without confining it to any specific thread or pool. &lt;strong&gt;The Unconfined dispatcher should not normally be used in code.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Private thread pools can be created with &lt;code&gt;newSingleThreadContext&lt;/code&gt; and &lt;code&gt;newFixedThreadPoolContext&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;An arbitrary Executor can be converted to a dispatcher with the &lt;code&gt;asCoroutineDispatcher&lt;/code&gt; extension function.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>kotlin</category>
      <category>coroutine</category>
    </item>
    <item>
      <title>Modelling value types in kotlin</title>
      <dc:creator>Amol</dc:creator>
      <pubDate>Wed, 04 Nov 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/amol/modelling-value-types-in-kotlin-1pp8</link>
      <guid>https://dev.to/amol/modelling-value-types-in-kotlin-1pp8</guid>
      <description>&lt;p&gt;In this article, you’ll see how to model value object&lt;sup id="fnref1"&gt;1&lt;/sup&gt; particularly in kotlin.&lt;/p&gt;

&lt;p&gt;Imagine we want to improve below model:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Customer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Order&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="o"&gt;..&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I had 2 use reason for modelling value type&lt;/p&gt;

&lt;h2&gt;
  
  
  Type safety
&lt;/h2&gt;

&lt;p&gt;lots of time I want to distinguish between let’s say &lt;code&gt;customerId&lt;/code&gt; and &lt;code&gt;orderId&lt;/code&gt;. so that accidental passing of wrong Id can be avoided. the best way is use inline class&lt;sup id="fnref2"&gt;2&lt;/sup&gt; concept but be careful with it’s stability status.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="c1"&gt;// option #1&lt;/span&gt;
&lt;span class="k"&gt;inline&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;CustomerId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// option #2&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;CustomerId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="c1"&gt;// option #3&lt;/span&gt;
&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;CustomerId&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;override&lt;/span&gt; &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;asString&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;asUUID&lt;/span&gt;&lt;span class="p"&gt;():&lt;/span&gt; &lt;span class="nc"&gt;UUID&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Option 1 and 2 are more recommended where as option #3 is example of over modelling.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I often have tendency to make props as private by default. but when we’re modelling immutable data type. You don’t need to mark props as private.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Represent a domain concept with constraint
&lt;/h2&gt;

&lt;p&gt;Let's take an example of &lt;code&gt;email&lt;/code&gt; which need to match some fancy regex.&lt;/p&gt;

&lt;p&gt;This is bit complicated one especially with kotlin because of language design. But we have 2 options here:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;p&gt;Use constructor to throw violations 😟&lt;br&gt;
&lt;/p&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;data class&lt;/span&gt; &lt;span class="nc"&gt;Email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;value&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;init&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="c1"&gt;// if (!isValid(value)) then throw InvalidEmail()&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;


&lt;p&gt;This option is not bad but it has few limitations and such as&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;constructor is not honest enough because it can blew on your face&lt;/li&gt;
&lt;li&gt;it can hijack your program’s execution&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;don't use kotlin since well-known design flaw &lt;sup id="fnref3"&gt;3&lt;/sup&gt; 😅&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://github.com/AhmedMourad0/no-copy#nocopy-compiler-plugin----" rel="noopener noreferrer"&gt;Use NoCopy plugin 🎯&lt;/a&gt;&lt;br&gt;
There is really nice article about NoCopy &lt;a href="https://medium.com/swlh/value-based-classes-and-error-handling-in-kotlin-3f14727c0565" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;Kotlin has few limitations but those can be fixed. Always go for more transparent and honest representation.&lt;/p&gt;




&lt;ol&gt;

&lt;li id="fn1"&gt;
&lt;p&gt;&lt;a href="https://martinfowler.com/bliki/ValueObject.html" rel="noopener noreferrer"&gt;https://martinfowler.com/bliki/ValueObject.html&lt;/a&gt; "Value object" ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn2"&gt;
&lt;p&gt;&lt;a href="https://kotlinlang.org/docs/reference/evolution/components-stability.html#current-stability-of-kotlin-components" rel="noopener noreferrer"&gt;https://kotlinlang.org/docs/reference/evolution/components-stability.html#current-stability-of-kotlin-components&lt;/a&gt; "inline class is at Alpha stability level" ↩&lt;/p&gt;
&lt;/li&gt;

&lt;li id="fn3"&gt;
&lt;p&gt;&lt;a href="https://youtrack.jetbrains.com/issue/KT-11914" rel="noopener noreferrer"&gt;https://youtrack.jetbrains.com/issue/KT-11914&lt;/a&gt; "Confusing data class copy with private constructor" ↩&lt;/p&gt;
&lt;/li&gt;

&lt;/ol&gt;

</description>
      <category>kotlin</category>
      <category>design</category>
    </item>
    <item>
      <title>Introduction to Http4k</title>
      <dc:creator>Amol</dc:creator>
      <pubDate>Mon, 27 Jan 2020 00:00:00 +0000</pubDate>
      <link>https://dev.to/amol/introduction-to-http4k-ef2</link>
      <guid>https://dev.to/amol/introduction-to-http4k-ef2</guid>
      <description>&lt;p&gt;Couple of weeks ago, I was looking for very simple way to create web server. I found &lt;a href="https://www.http4k.org/" rel="noopener noreferrer"&gt;http4k&lt;/a&gt; which is a lightweight but fully-featured HTTP toolkit written in pure Kotlin that enables the serving and consuming of HTTP services in a functional and consistent way.&lt;/p&gt;

&lt;h4&gt;
  
  
  Some of Features of http4k
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;small, written in functional kotlin with zero dependencies.&lt;/li&gt;
&lt;li&gt;very simple, no magic, no reflections.&lt;/li&gt;
&lt;li&gt;immutable http model, which make it very easy to test/debug.&lt;/li&gt;
&lt;li&gt;serverless / server independent&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  WebServer as Function
&lt;/h2&gt;

&lt;p&gt;http4k is designed as application as function. Web server can be looked as one big function which handles requests.so it’s literally represented as typealias&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;typealias&lt;/span&gt; &lt;span class="nc"&gt;HttpHandler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpRequest&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;HttpResponse&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An endpoint which echos request body can be created just by 2 simple lines.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;app&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpHandler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; 
    &lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;body&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;body&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;server&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;asServer&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SunHttp&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8000&lt;/span&gt;&lt;span class="p"&gt;)).&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Request and Response are immutable objects. SunHttp is container which only meant for development. For production use Netty, Jetty, ApacheServer etc.Because &lt;code&gt;app&lt;/code&gt; is just kotlin function, it can be composed very easily.&lt;/p&gt;

&lt;h2&gt;
  
  
  Filters
&lt;/h2&gt;

&lt;p&gt;http4k provides a Filter function which is basically intercept the Request/Response pipeline and&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="k"&gt;typealias&lt;/span&gt; &lt;span class="nc"&gt;Filter&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;HttpHandler&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;HttpHandler&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Below code snippet shows typical composition of HttpHandlers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;setContentType&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Filter&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;nextHandler&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; 
        &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="n"&gt;request&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nf"&gt;nextHandler&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;request&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
            &lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;header&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"text/plain"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;composedApp&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="n"&gt;setContentType&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;then&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;app&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Routing and composition
&lt;/h2&gt;

&lt;p&gt;http4k provides some high level functions which helps to compose application which handles each request differently depending upon url, method etc. These functions accepts multiple httpHandlers and return one handler which is composed of them.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight kotlin"&gt;&lt;code&gt;&lt;span class="kd"&gt;val&lt;/span&gt; &lt;span class="py"&gt;app&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;HttpHandler&lt;/span&gt; &lt;span class="p"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="s"&gt;"/api"&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="nc"&gt;GET&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;apiApp&lt;/span&gt;
    &lt;span class="s"&gt;"/other"&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="nf"&gt;routes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"/get"&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="nc"&gt;GET&lt;/span&gt; &lt;span class="nf"&gt;to&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="nc"&gt;Request&lt;/span&gt; &lt;span class="p"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;Response&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;OK&lt;/span&gt;&lt;span class="p"&gt;)}&lt;/span&gt;
        &lt;span class="s"&gt;"/post"&lt;/span&gt; &lt;span class="n"&gt;bind&lt;/span&gt; &lt;span class="nc"&gt;POST&lt;/span&gt; &lt;span class="n"&gt;to&lt;/span&gt; &lt;span class="n"&gt;otherPostHandlerFn&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;http4k really stands with the promises they made. API from http4 are written very thoughtfully and are not opinionated. They are easy to reason and work with. Only downside can be no support for coroutine.&lt;/p&gt;

&lt;p&gt;To know more about http4k rationale visit &lt;a href="https://www.http4k.org/rationale/" rel="noopener noreferrer"&gt;http4k/rationale&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;PS: This is my very first post. For correction please raise pull request at &lt;a href="https://gitlab.com/amolrv/amolrv.gitlab.io" rel="noopener noreferrer"&gt;gitlab&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

</description>
    </item>
  </channel>
</rss>
