<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Roman Elizarov on Medium]]></title>
        <description><![CDATA[Stories by Roman Elizarov on Medium]]></description>
        <link>https://medium.com/@elizarov?source=rss-4762e889f8fc------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*LoU1WYCDpbNqNj2MKssqAA.jpeg</url>
            <title>Stories by Roman Elizarov on Medium</title>
            <link>https://medium.com/@elizarov?source=rss-4762e889f8fc------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 14:02:38 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@elizarov/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Programming Language Evolution]]></title>
            <link>https://elizarov.medium.com/programming-language-evolution-ab7d7d2b0d0b?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/ab7d7d2b0d0b</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[language]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Mon, 23 Nov 2020 16:18:08 GMT</pubDate>
            <atom:updated>2020-12-08T13:33:29.977Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PsQdxpXjfSAAUN-Y0lW1ow.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@polarmermaid?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Anne Nygård</a> on <a href="https://unsplash.com/s/photos/evolution?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>The history of programming languages is ripe with evolution. Existing languages constantly evolve and new languages are created to address the emerging needs. Sometimes there are radical, revolutionary breakthroughs, with a complete paradigm shift, but often there are just gradual improvements and refinements. The latter is the topic of this story. The practice of programming at any given era usually goes ahead of capabilities that programming languages provide, while programming language designers recognize it and catch up to fulfill the demand. Let us see some examples to the point.</p><h4>From GOTOs to the structured code</h4><p>In early languages, you had to write a lot of repetitive code just to do a simple loop. The loop was such a common programming <em>pattern</em>, that it was adopted even by the primitive higher-level languages in the era predating structured programming. So, there was a time when you still had GOTOs in your programming language but a significant fraction of the code had structured loops:</p><pre>10 <strong>LET</strong> N=10<br>20 <strong>FOR</strong> I=1 <strong>TO</strong> N<br>30 <strong>PRINT</strong> &quot;Hello, World!&quot;<br>40 <strong>NEXT</strong> I</pre><p>As we know, the subsequent generation of languages not only added structured IF statements but also made the structure explicit in the source and ended up abolishing GOTOs completely. This kind of evolution can be seen in other areas, too.</p><h4>Objects and pointers</h4><p>Let’s take a brief look at OOP. The object-oriented style of programming does not need an object-oriented language. Even nowadays you can find software written in C where methods are just a <em>convention</em> of writing functions whose first parameter is a pointer to the receiver:</p><pre><strong>void </strong>Point_move(Point* self, <strong>int </strong>dx, <strong>int </strong>dy) { ... }</pre><p>Virtual methods are routinely implemented in pure C, too, explicitly keeping a virtual methods table with references to methods somewhere in the object’s structure.</p><p>However, the rising popularity of object-oriented programming back in the day cemented the growth of languages that incorporated these patterns natively with classes and methods as we know them today.</p><p>As hardware became faster, working with objects by reference became so popular in large-scale software systems that even an effort of having to explicitly use Point* or some other syntactic notation to designate references to an object came to be perceived as a <em>boilerplate </em>— a syntactic incantation that you have to always write when working with business objects. So it was abolished, paving the road to the modern approach of having an implicit reference to all object instances that top application-programming languages (Python, JS, Java) use.</p><p>This is also a good example of how a language feature that is ubiquitous today might have a very long history but was not universally adopted by main-stream languages until the right time.</p><h4>The scale of software and extensions</h4><p>The first generations of object-oriented languages were created in the 20th century, well before the massive software projects (in terms of lines of code and 3rd-party libraries used) that we see today. These languages were quickly adopted at a scale hardly envisioned by their creators.</p><p>The original idea that you can simply declare all the methods you’ll ever need on a class together with its declaration quickly crumbled, leading to hundreds StringUtil , FileUtil, and other “utility classes” in a typical project, which were merely collections of “extension methods”. They were essentially written in an old-school pre-OOP style with a convention of having a receiver of the call as the first parameter as we saw with the example in C. History goes in circles! So it is not a surprise that a generation of languages designed in the 21st century for large-scale development typically incorporates this pattern via some form of a 1st-class extension concept.</p><h4>Patterns, Boilerplate, Features</h4><p>I could go on with more examples, like the tenuous malloc/free dance that was replaced by various automated memory-management schemes, unwieldy callback-based programming that gave rise to language support for coroutines, repeated boilerplate of collection-manipulating operations leading to support for standard higher-order functions like filter, map, etc.</p><p>I hope that you already see a common motif here. As programming evolves, various ubiquitous patterns of code emerge. These patterns are boring, error-prone, and are totally not fun to work with. Eventually, they get recognized by programming language designers and are incorporated into languages as new features, increasing the overall level of abstraction, improving developer productivity and job satisfaction. This story repeats with each generation of languages as newer patterns build on top of features that used to be just programming patterns themselves a generation ago.</p><h4>Being modern, staying modern</h4><p>What does it mean for a programming language to be modern and to stay modern? It means to accept the change, this inevitable cycle of evolution, and to evolve with it. When software developers consistently write or have to automatically generate some repeated pattern of code it sends a strong signal that something is not right, that some language feature is missing.</p><p>Take a look at one more example. As OOP design style scaled, decoupling the internal storage of data from its externally-visible representation became the norm, thus giving rise to a convention of getXxx (getter) and setXxx (setter) methods to achieve this encapsulation. It has been going on for 20+ years all over the enterprise codebases with an extensive set of tools helping to generate this boilerplate. So nowadays, simply looking at whether a language has or lacks built-in support for this property pattern, we can see if a language is keeping up with the needs of modern large-scale software development.</p><h4>Removing obsolete features</h4><p>Let’s get back to the example we’ve started with — a transition from programming with GOTOs to modern structured programming. The real breakthrough came not just from adding structured loops and conditionals to languages, but from the complete removal of the GOTO statement.</p><p>A case in point is a notorious “billion-dollar problem” with null pointers. Adding language support for better null-handling is only half a solution that helps developers with writing the null-avoidance boilerplate. The real breakthrough is to completely forbid storing nulls into arbitrary reference types.</p><p>This removal of obsolete features is often the hardest hurdle that prevents languages from adapting to the ever-changing world, leading to their ultimate downfall under the weight of features they add. Of course, it also depends on the inertia of the language, the size of its ecosystem. The more widespread the language is, the more deadweight it can afford to support without crumbling. But ultimately, the general law of evolution, as stated by Charles Darwin, holds for programming languages, too:</p><blockquote>It is not the most intellectual of the species that survives; it is not the strongest that survives; but the species that survives is the one that is able best to adapt and adjust to the changing environment in which it finds itself.</blockquote><h4>Further reading</h4><p>If you are interested in trends that shape the design of modern programming languages, then check out my other related stories:</p><ul><li><a href="https://elizarov.medium.com/dealing-with-absence-of-value-307b80534903">Dealing with absence of value</a></li><li><a href="https://elizarov.medium.com/types-are-moving-to-the-right-22c0ef31dd4a">Types are moving to the right</a></li><li><a href="https://elizarov.medium.com/functional-programing-is-on-the-rise-ebd5c705eaef">Functional Programming is on the rise</a></li><li><a href="https://elizarov.medium.com/application-programming-language-ff7f0063c16">Application programming language</a></li><li><a href="https://elizarov.medium.com/the-end-of-the-semicolon-era-60ab95e669ab">The End of the Semicolon Era</a></li><li><a href="https://elizarov.medium.com/immutability-we-can-afford-10c0dcb8351d">Immutability we can afford</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=ab7d7d2b0d0b" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Shared flows, broadcast channels]]></title>
            <link>https://elizarov.medium.com/shared-flows-broadcast-channels-899b675e805c?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/899b675e805c</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[flow]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[channel]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Mon, 16 Nov 2020 13:16:59 GMT</pubDate>
            <atom:updated>2021-05-18T11:41:31.628Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*Ql7vqk_X9xDxPPvI" /><figcaption>Photo by <a href="https://unsplash.com/@davies_designs?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Davies Designs Studio</a> on <a href="https://unsplash.com/s/photos/share?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Once upon a time coroutines were introduced to Kotlin and they were lightweight. We could launch a multitude of coroutines and we needed a way to communicate between those coroutines without running into a dreaded “mutable shared state” problem.</p><p>Thus <a href="https://kotlinlang.org/docs/reference/coroutines/channels.html">Channel</a> was added as an inter-coroutine communication primitive. The channels are wonderful. Channels support one-to-one, one-to-many, many-to-one, and many-to-many communication between coroutines, and every value that is sent to the channel is received once.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*AKac-NMxGseAkF_jHZ71lw.png" /><figcaption>Diagram of many-to-many channel operation</figcaption></figure><p>You cannot use channels to distribute events or state updates in a way that allows multiple subscribers to independently receive and react upon them.</p><p>Thus the <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-broadcast-channel/">BroadcastChannel</a> interface was introduced with buffered and <a href="http://st-channel/index.html">ConflatedBroadcastChannel</a> as its implementations. They served us well for a while, but they turned out to be a design dead-end. Now, since kotlinx-coroutines version 1.4 we have introduced a better solution — shared flows. Read on for the full story.</p><h4>Flows are simple</h4><p>In the early versions of the library, we had only channels and we tried to implement various transformations of asynchronous sequences as functions that take one channel as an argument and return another channel as a result. It means that, for example, a filter operator would run in its own coroutine.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dOOGNGnfTJ27FQf_CwhAhA.png" /><figcaption>Diagram of filter operator with channels</figcaption></figure><p>The performance of such an operator was far from great, especially compared to just writing an if statement. In a hindsight, it is not surprising, because a channel is a <em>synchronization primitive</em>. Any channel, even an implementation that is optimized for a single producer and a single consumer, must support <em>concurrent</em> communicating coroutines and a data transfer between them needs synchronization, which is expensive in modern multicore systems. When you start building your application architecture on top of the asynchronous data streams, the need to have transformations naturally appears, and the channel costs start to accrue.</p><p><a href="https://medium.com/@elizarov/simple-design-of-kotlin-flow-4725e7398c4c">The simple design of Kotlin Flow</a> allows efficient implementation of transformation operators. In basic cases, values are emitted, transformed, and collected in the same coroutine, without any need for synchronization.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CDzFMiDwvxyBQKDfllDNrA.png" /><figcaption>Diagram of filter operator with flows</figcaption></figure><p>Synchronization is introduced with flows only when it is needed for <a href="https://medium.com/@elizarov/kotlin-flows-and-coroutines-256260fb3bdb">emission and collection of values in different coroutines</a>.</p><h4>Flows are cold</h4><p>However, the flows are typically cold — a Flow&lt;Value&gt; created by <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow.html">flow { … }</a> builder function is a passive entity. Consider the following code:</p><pre><strong>val </strong><em>coldFlow </em>= flow <strong>{<br>    while</strong> (isActive) {<br>        emit(nextEvent)<br>    }<br><strong>}</strong></pre><p>The flow itself is not backed by any kind of computation and does not have any state by itself until it starts to be collected. Every collector coroutine executes its own instance of emitting code. The story on “<a href="https://medium.com/@elizarov/cold-flows-hot-channels-d74769805f9">Cold flow, hot channels</a>” describes the reasoning behind Kotlin Flows and shows use-cases for which they fit better than channels — returning asynchronous streams of values that are computed on demand.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZTlZKrryIWAiKkArIvAfcg.png" /><figcaption>Diagram of cold flow operation</figcaption></figure><p>But how do you handle things like user actions, external device events, state updates, etc? They are operating independently of whether there is any code that is interested in them. They should support multiple observers inside the application. These are so-called <em>hot </em>sources of events.</p><h4>Shared flows</h4><p>That’s where the concept of a<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-shared-flow/">SharedFlow</a> comes in. A shared flow exists regardless of whether it is being collected or not. A collector of the shared flow is called a <em>subscriber</em>. All subscribers of a shared flow receive the same sequence of values. It effectively works like a “broadcast channel”, without most of the channel overhead. It makes the concept of a broadcast channel obsolete.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*GMYcj6j0mN_j2kDDi1WgsQ.png" /><figcaption>Diagram of shared flow operation</figcaption></figure><p>Essentially a shared flow is a lightweight broadcast event bus that you can <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-mutable-shared-flow.html">create</a> and use in your application architecture.</p><pre><strong>class </strong>BroadcastEventBus {<br>    <strong>private val</strong> _events = <em>MutableSharedFlow</em>&lt;Event&gt;()<br>    <strong>val </strong>events = _events.<em>asSharedFlow</em>() <em>// read-only public view</em><br><br>    <strong>suspend fun</strong> postEvent(event: Event) {<br>        _events.emit(event) <em>// suspends until subscribers receive it<br>    </em>}<br>}</pre><p>It has tunable parameters such as the number of old events to keep and <em>replay</em> for new subscribers and the <em>extraBufferCapacity </em>to provide cushion for fast emitters and slow subscribers.</p><p>All subscribers of a shared flow are asynchronously collecting events in their own context. Emitter does not wait until subscribers finish processing the events. However, when the shared flow buffer is full, the emitter suspends until there is room in the buffer. This suspension of emitter on buffer overflow provides back-pressure to slow down emission when collectors cannot keep up. Alternative strategies to deal with buffer overflow are supported via <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-buffer-overflow/index.html">BufferOverlow</a> parameter.</p><h4>State flows</h4><p>A popular way to deal with buffer overflow is to drop the oldest events and retain only the most recent, newest events. In particular, it is a great way to model <em>state</em> variables in an application. It is such a widespread use-case that it has its own specialized <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/-state-flow/">StateFlow</a> type which serves as a replacement for a <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-conflated-broadcast-channel/">ConflatedBroadcastChannel</a>, which became obsolete, too.</p><pre><strong>class</strong> StateModel {<br>    <strong>private val</strong> _state = <em>MutableStateFlow</em>(<em>initial</em>)<br>    <strong>val </strong>state = _state.<em>asStateFlow</em>() <em>// read-only public view<br><br>    </em><strong>fun</strong> update(newValue: Value) {<br>        _state.value = newValue // NOT suspending<br>    }<br>}</pre><p>Think of a val x: StateFlow&lt;T&gt; as an asynchronous and observable counterpart of var x: T . Its most recent value is always available and, in fact, the most recent value is the only one that matters, so updating it is always possible without suspension.</p><p>With the state flow, the performance difference between a complex channel and a simple flow becomes quite apparent. An implementation of a state flow has allocation-free updates, which was not the case with a conflated broadcast channel.</p><h4>A use-case for channels</h4><p>As different kinds of shared flows replaced different kinds of broadcast channels the popular question is what going to happen with plain, regular channels? They are going to stay for many reasons. One reason is that channels are low-level primitives that are used to implement many complex flow operators.</p><p>But channels also have their application use-cases. Channels are used to handle events that must be processed <em>exactly once* (see sidenote below for details)</em>. This happens in a design with a type of event that usually has a single subscriber, but intermittently (at startup or during some kind of reconfiguration) there are no subscribers at all, and there is a requirement that all posted events must be retained until a subscriber appears.</p><pre><strong>class </strong>SingleShotEventBus {<br>    <strong>private val</strong> _events = <em>Channel</em>&lt;Event&gt;()<br>    <strong>val </strong>events = _events.<em>receiveAsFlow</em>() <em>// expose as flow<br><br>    </em><strong>suspend fun</strong> postEvent(event: Event) {<br>        _events.send(event) <em>// suspends on buffer overflow<br>    </em>}<br>}</pre><p>Both BroadcastEventBus , that is written with the SharedFlow in the first example, and this SingleShotEventBus, that is written with the Channel, publicly expose their events as Flow&lt;Event&gt;, yet they have an important difference.</p><p>With the shared flow, events are broadcast to an unknown number (zero or more) of subscribers. In the absence of a subscriber, any posted event is immediately dropped. It is a design pattern to use for events that must be processed immediately or not at all.</p><p>With the channel, each event is delivered to a single subscriber. An attempt to post an event without subscribers will suspend as soon as the channel buffer becomes full, waiting for a subscriber to appear. Posted events are not dropped.</p><blockquote>Note that the SingleShotEventBus implementation with a channel processes each posted event <strong>exactly once</strong> only in the absence of cancellation. When a subscriber to the flow is cancelled, the event may fail to get delivered. See <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.channels/-channel/index.html#undelivered-elements">the documentaion on undelivered elements in channels</a> for details.</blockquote><h4>Bottom line</h4><p>Know the difference and use both shared flows and channels appropriately. They are both useful and are designed to work well together. However, <em>broadcast channels</em> are obsolete artifacts of the past, they will be deprecated and removed in the future.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=899b675e805c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Immutability we can afford]]></title>
            <link>https://elizarov.medium.com/immutability-we-can-afford-10c0dcb8351d?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/10c0dcb8351d</guid>
            <category><![CDATA[immutability]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[programming-languages]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Wed, 22 Jul 2020 09:18:28 GMT</pubDate>
            <atom:updated>2022-01-13T07:23:09.730Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Z4dAFGtQQkg2I6xvOSXTTg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@ly0ns?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Li Yang</a> on <a href="https://unsplash.com/s/photos/luxury?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>At the dawn of software engineering computers were programmed directly in <a href="https://en.wikipedia.org/wiki/Machine_code">machine code</a>, then in assembly, and only later in higher-level languages. Computers are <a href="https://en.wikipedia.org/wiki/Imperative_programming">imperative</a>. They operate by executing instructions that mutate their state, stored in registers and memory. Naturally, the same was true about programming languages. In the old world of expensive computers with limited resources, the primary concern was an efficient translation of higher-level abstractions into low-level code.</p><h4>The past glory of mutable state</h4><p>It used to be a normal practice to write software in a way that mimics the actual computer architecture with thousands of global variables that are being mutated by various pieces of the system. It might be shocking for a modern developer to learn that just recently there were cars on the street, designed as late as 2005, that ran <a href="https://www.safetyresearch.net/toyota-unintended-acceleration-and-the-big-bowl-of-spaghetti-code/">what we would call the “Spaghetti” code</a>.</p><p>The software industry, in general, had firmly moved past the unruly global state before the end of the last century. The rise of the <a href="https://en.wikipedia.org/wiki/Object-oriented_programming">object-oriented programming</a> paradigm had established an orderly approach with the <em>encapsulation</em> of all the mutable state in our software systems inside of objects. It had fueled tremendous growth in the complexity of modern software, layering abstractions above abstractions, while still maintaining a reasonable degree of human’s ability to make sense of it.</p><p>However, any developer who worked on a non-trivial piece of UI using an object-oriented framework, or had programmed in another domain with lots of asynchronously occurring events, can tell you stories of debugging all those cases where mutable state, even encapsulated into objects, continually trips you. You expect this object to be in a such and such state, but due to some rare sequence of events, it turns out to be in a state you did not expect, having been mutated by another piece of code.</p><p>For example, take a popular architectural pattern where a repository class encapsulates a piece of application state and sends updates of this state to other layers of application:</p><pre><strong>class</strong> Repository {<br>    <strong>private val</strong> state = State(&quot;some state&quot;, 42, ...)<br>}</pre><p>If State is represented by a classic object-oriented mutable object, then developers have to be careful when sending it around through any kind of asynchronous processing pipeline or event bus, as a receiving code might discover it in a different state. Even caching such states becomes a challenge. A discipline of defensive copying is needed to work around that.</p><h4>Immutability takes the stage</h4><p>A solution is simple on the surface — embrace immutability. Immutable objects are incapable of surprising a programmer by an unexpected change of their state.</p><p>Yet, historically, immutability has been a prerogative of niche research languages. Mainstream languages provided support for immutability as a 2nd class citizen, if at all. Just look at basic signs. In most programming languages from the past century a declaration of a normal integer field of an object, like int x , means mutable <em>variable</em> by default, while extra effort is needed to make it immutable by adding a modifier like const or final. If you look farther back in time, even text string types used to be mutable, like in C (1972).</p><p>However, many modern languages treat immutable on par with mutable (var vs val/let having the same length by design) at least for simple data, or even prefer immutability by forcing developers to go through extra hoops when declaring common data containers as mutable (like List vs MutableList).</p><p>A picture worth a thousand words. Pick high-level languages that enjoy popularity nowadays, check whether they were designed in the 20th or the 21st century, and arrange them according to their basic degree of respect for immutability:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*4-mFZUgthlx6n4y4vaHHVA.png" /></figure><p>As you can see, among the languages designed in the 20th century, only Haskel (1990) stands apart, being wholly based on ideas of immutability. In the 21st century, immutability gets a lot more love with Scala (2004), F# (2005), Rust (2010), Kotlin (2011), and Swift (2014) treating it at least on par with mutability for variables.</p><h4>The cost of immutability</h4><p>The reluctance in accepting immutability is not surprising. It is a radical and costly leap of abstraction from the underlying mutable machines. Most modern software is interactive. It works in a changing environment. Representing a non-trivial state (beyond a simple type that fits into a machine word) by immutable values means copying these values on each update.</p><p>All data structures and algorithms that we use in day-to-day work (like collections) can be implemented as immutable, yet their most efficient implementations are typically <em>asymptotically</em> <em>slower</em> than their mutable counterparts, meaning that they become progressively slower when they work with more data. Even with a small number of data items they run several times longer than the corresponding algorithms with mutable data structures.</p><h4>We can afford abstractions</h4><p>A lot of developers nowadays still program constrained systems or at such a massive scale where every bit of efficiency is important. This creates a considerable market for zero-cost abstraction, systems programming languages. However, if you look at a larger picture, at the kind of code that millions of developers write every day, you’ll inevitably find that the concern for resource consumption often takes backstage. With so much demand for software in the modern world and with lagging supply of developers to meet it, the frontstage concern becomes developer productivity.</p><p>There is an incessant need to equip programmers with abstractions that let them get their job done faster, with less effort. These abstractions are costly, but it does not worry a majority of people writing code today. In a world with fast and cheap machines, we can afford costly abstractions. In a world with an expensive developer workforce, programming languages are racing to provide convenient abstractions. Immutability removes the whole class of problems and concerns in software and we can afford immutability nowadays.</p><h4>Beyond programming languages</h4><p>A course to immutability is clear even beyond the design of programming languages. Take a look at UI frameworks. They all used to be wholly object-oriented in their philosophy. Developers used to have a tenuous responsibility of carefully writing code that mutates states of UI objects to make them match the data that needs to be displayed. This UI programming style is going away. Reactive UI frameworks that are based on unidirectional data flows are growing like fire everywhere: React (JS, 2013), Compose (Kotlin, 2019), SwiftUI (Swift, 2019) to name a few. A great overview of problems that these UI frameworks solve and the whole class of boilerplate code they remove can be found in <a href="https://www.youtube.com/watch?v=Q9MtlmmN4Q0">Leland Richardson’s talk “Understanding Compose”</a>.</p><p>What is common between those modern UI frameworks is that most of them chose to work with immutable representations of data that is being displayed. Attempts to build better observability and data-binding for changes inside complex mutable objects are being abandoned. This trend also fuels immutable data pipeline abstractions like the <a href="https://medium.com/@elizarov/reactive-streams-and-kotlin-flows-bfd12772cda4">Reactive Streams and Kotlin Flow</a>.</p><h4>Conclusion</h4><p>Immutability is a luxury of programming, making software more comfortable and less error-prone to write in many important cases. Immutability is becoming more popular in software development because we became so rich in machine resources that we can afford it now.</p><h4>Other trends in programming languages</h4><p>If you are interested in trends that shape the design of modern programming languages, then check out my other related stories:</p><ul><li><a href="https://medium.com/@elizarov/dealing-with-absence-of-value-307b80534903">Dealing with absence of value</a></li><li><a href="https://medium.com/@elizarov/types-are-moving-to-the-right-22c0ef31dd4a">Types are moving to the right</a></li><li><a href="https://medium.com/@elizarov/functional-programing-is-on-the-rise-ebd5c705eaef">Functional Programming is on the rise</a></li><li><a href="https://medium.com/@elizarov/application-programming-language-ff7f0063c16">Application programming language</a></li><li><a href="https://medium.com/@elizarov/the-end-of-the-semicolon-era-60ab95e669ab">The End of the Semicolon Era</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=10c0dcb8351d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[With the receiver in scope]]></title>
            <link>https://elizarov.medium.com/with-the-receiver-in-scope-7b52bdcca6e9?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/7b52bdcca6e9</guid>
            <category><![CDATA[context]]></category>
            <category><![CDATA[scopes]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[receiver]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Wed, 01 Jul 2020 14:33:13 GMT</pubDate>
            <atom:updated>2020-07-07T11:04:13.033Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*HkvZ_wM3IvBPEgXY" /><figcaption>Photo by <a href="https://unsplash.com/@profwicks?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Ben Wicks</a> on <a href="https://unsplash.com/s/photos/antenna?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><blockquote>Repetitio est mater studiorum (Latin, repetition is the mother of all learning).</blockquote><p>Repetition is great for study, but a bane of software development. Repetitive code is boring and error-prone. Let’s look at a hypothetical example inspired by imperative UI frameworks that are still being used a lot nowadays, even though their heyday is past. We might find ourselves having to write code like this:</p><pre>applicationWindow.title = &quot;Just an example&quot;<br>applicationWindow.position = FramePosition.AUTO<br>applicationWindow.content = createContent()<br>applicationWindow.show()</pre><p>Referencing applicationWindow object over and over again is very explicit but it is not pretty. We can make this code better using <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/with.html">with</a> scope function from the Kotlin standard library:</p><pre>with(applicationWindow) <strong>{</strong> // this: ApplicationWindow<br>    title = &quot;Just an example&quot;<br>    position = FramePosition.AUTO<br>    content = createContent()<br>    show()<br><strong>}</strong></pre><p>This code now has more lines, but the number of lines is not the key metric you should be looking at when judging the code. The code looks cleaner. It groups all the initialization of the applicationWindow object into a separate, syntactically distinct block. It directly represents the developer’s intent to perform all of these actions on applicationWindow together.</p><p>The block of code inside of with(x) { ... } is easily recognizable by any developer trained in a mainstream object-oriented language — it is similar to the code you encounter inside a method of the corresponding class where <strong>this</strong> refers to the method’s receiver object and all unqualified references like title refer to <em>this</em> object’s properties and methods.</p><p>In the Kotlin programming language writing a method is not the only way to get access to object members without having to repeat the object’s name. Kotlin has support for <a href="https://kotlinlang.org/docs/reference/extensions.html#extension-functions">extension functions</a> that allow us to write a method-like looking code outside of the class body:</p><pre><strong>fun</strong> ApplicationWindow.configure() { // this: ApplicationWindow<br>    title = ... // no need to qualify title reference<br>}</pre><p>The ability to write extension functions naturally extends into the ability to write <a href="https://kotlinlang.org/docs/reference/lambdas.html">lambdas with a receiver</a>. Kotlin’s with is not a keyword nor intrinsic in the language. It is a library function with a trivial implementation:</p><pre><strong>fun</strong> &lt;T, R&gt; with(receiver: T, block: T.() -&gt; R): R =<br><strong>    </strong>receiver.block()</pre><p>When you use it, the argument’s type becomes the receiver in the corresponding block and IDE helpfully annotates the code inside the block with this: ApplicationWindow comment.</p><p><strong>Great feature! Why would not we use it everywhere?</strong> Providing access to the object reference makes our code concise. Concision is the chief Kotlin’s motto, it is the first one mentioned in “Why Kotlin” section on <a href="https://kotlinlang.org">kotlinlang.org</a>, isn’t it?</p><p>However, looking at the Kotlin standard library we will not find a lot of higher-order functions that are using parameters with receivers. For example, collection manipulation functions, like filter, are declared this way:</p><pre><strong>fun</strong> &lt;T&gt; Iterable&lt;T&gt;.filter(predicate: (T) -&gt; Boolean): List&lt;T&gt;</pre><p>A predicate has the type of a regular function (T) -&gt; Boolean, and not a function with the receiver T.() -&gt; Boolean. When we use filter on some collection of domain-specific objects we might end up with code like this:</p><pre>persons.filter <strong>{</strong> <strong>it</strong>.firstName == name <strong>}</strong></pre><p>Here it is perfectly clear that Person.firstName property of each element in the persons collection is being compared with the specific name value.</p><p>If filter was defined with predicate: T.() -&gt; Boolean parameter then the corresponding code would look like this:</p><pre>persons.filter <strong>{</strong> firstName == name <strong>}</strong></pre><p>It is much less clear what is going on here at first sight. Is Person.firstName being compared to name here or isPerson.name being compared to firstName? We’ll need to look around to figure it out. The more code and more context we have around, the harder it will be to see.</p><p>The key quality of the code that we are looking to achieve is not concision, but <em>readability</em>. The goal is to make code clear and unambiguously understandable to a person who is reading the code. There is a fine balance between longer code, that has too much repetition, making it harder to grasp the essence of the code, and shorter code, that relies too much on the context, knowledge, and attention of the reader.</p><p>Don’t abuse this Kotlin language flexibility in your API design. Prefer to be explicit. Lambdas with receivers are a great tool to <a href="https://kotlinlang.org/docs/reference/type-safe-builders.html">design clean-looking DSLs</a>, where the structure of the code mimics the structure of the domain you are modeling. They allow you to provide a context in your lambdas with a carefully picked set of available extensions that you need. Do use them for DSLs and for type-safe builders.</p><p>For general-purpose needs rely on Kotlin scope functions with, run, apply. Their use is idiomatic in Kotlin. However, resist the urge to use them just to make your code shorter. Make sure that their use is consistent with the intent of the code you are writing. Kotlin documentation has a helpful <a href="https://kotlinlang.org/docs/reference/scope-functions.html">page with examples and the intended use for scope functions</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7b52bdcca6e9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin and Exceptions]]></title>
            <link>https://elizarov.medium.com/kotlin-and-exceptions-8062f589d07?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/8062f589d07</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[error-handling]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Wed, 10 Jun 2020 08:18:06 GMT</pubDate>
            <atom:updated>2020-07-06T06:17:53.521Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*b8Kt7jsqSEKt1_To" /><figcaption>Photo by <a href="https://unsplash.com/@johnmarkarnold?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">John Mark Arnold</a> on <a href="https://unsplash.com/s/photos/field?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>What are Kotlin Exceptions and how should you use them? To figure it out let’s look at their origins first. Exceptions came to Kotlin from Java. The story with exceptions in Java is complicated, though. I’ll give a brief overview.</p><h4>The Origin</h4><p>Java has a unique concept of <em>checked exceptions</em> that were designed to solve the problem of verbose and error-prone error-handling (pun intended). In languages predating Java, like in venerable C, you have to write code like shown in this snippet when doing basic input/output:</p><pre>file = fopen(&quot;file.txt&quot;, &quot;r&quot;);<br><strong>if</strong> (file == NULL) {<br>    // handle error &amp; return<br>}<br>// work with file, check for error after each file operation</pre><p>Every time you perform an operation that might fail due to some external circumstance, which happens especially often with files and network, you have to write code that checks the corresponding error condition and handles it. That’s tedious, easy to forget, hard to debug.</p><p>Java set on a noble goal to eliminate this problem. The solution was to use checked exceptions. Every file I/O operation is declared as throws IOException in Java and the compiler checks that you either handle it or declare that you rethrow it. The beauty of this is that you can write exception-handling code once for a whole bunch of I/O operations and you cannot forget writing it since the Java compiler is there to help you.</p><pre>file = FileInputStream(&quot;file.txt&quot;); // throws IOException</pre><p>No error-handling boilerplate, no more missed error checks. It was such a bliss to program in Java… for a while.</p><h4>Problems</h4><p>Problems with checked exceptions accumulated over the years. Memory input/output APIs like ByteArrayInputStream were still declared to throw IOException that you had to handle even though it never happened, people abused checked exceptions in API design leading to long, contagious lists of thrown exceptions, developers routinely caught and ignored checked exceptions just to fit some exception-throwing API under an interface that did not declare any exception, etc.</p><p>Developers stopped using checked exceptions in their APIs. There were wide-spread community calls against checked in Java (see, for example, an article by <a href="https://blog.joda.org/2010/09/checked-exceptions-bijava_9688.html">Stephen Colebourne on Checked Exceptions</a> from as far back as 2010).</p><p>However, the fatal blow to checked exceptions came later when lambda expressions were added to Java 8 in 2014 together with Streams and data-processing operators for the Java collections framework. It was so hard to incorporate checked exceptions support there and would have made it so burdensome to use, that checked exceptions support did not make it into the final design. Checked exceptions simply don’t mix well with functional abstractions, they don’t compose well with <a href="https://kotlinlang.org/docs/reference/lambdas.html">higher-order functions</a>. Moreover, none of the popular successor languages to Java include checked exceptions support. Checked exceptions are now widely accepted to be a language design dead-end.</p><h4>Exceptions in Kotlin</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/0*I-PeIDzX7dSXsUGC" /><figcaption>Photo by <a href="https://unsplash.com/@mreichelt?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Marc Reichelt</a> on <a href="https://unsplash.com/s/photos/kotlin?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Kotlin inherited the concept of exception from Java and supports them to seamlessly interoperate with JVM libraries. Just like other successor languages, Kotlin learned from Java that checked exceptions should not be a thing. This creates a different problem, though.</p><p>It is still called an <em>exception</em> in Kotlin, but it became a different language feature and comes with a different usage style. Moreover, Kotlin developers have to work with Java APIs that use checked exceptions as an alternative way to return a value from a function. How to deal with it and what should exceptions in Kotlin be used for?</p><h4>Handling program logic errors</h4><p><strong>The first and foremost use of exceptions in Kotlin is to handle program logic errors.</strong> Exceptions are used to check preconditions and invariants in code that static type system cannot track down during compilation. For example, if you a have a function that updates order quantity that must be always positive by the business logic of the code, then you might write something like:</p><pre>/** Updates order [quanity], must be positive. */<br><strong>fun</strong> updateOrderQuanity(orderId: OrderId, quantity: Int) {<br>    require(quantity &gt; 0) { &quot;Quantity must be positive&quot; }<br>    // proceed with update<br>}</pre><p>Calling this function with a negative or zero quantity indicates a logic error in some other place in the code and throws an exception. The same happens when you try to get an element by an out of range index from a list, divide by zero, etc.</p><p><strong>As a rule of thumb, you should not be catching exceptions in general Kotlin code.</strong> That’s a code smell. Exceptions should be handled by some top-level framework code of your application to alert developers of the bugs in the code and to restart your application or its affected operation. That’s the primary purpose of exceptions in Kotlin.</p><h4>Dual-use APIs</h4><p>For some APIs, especially non-domain-specific ones, it is not so clearcut whether the error indicates a program logic error or an error in the input that the application must deal with without calling for the developer’s help. It depends on the context of its use.</p><p>Take Kotlin’s <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-int.html">String.toInt()</a> extension function for example:</p><pre><strong>val</strong> number = &quot;123&quot;.toInt()</pre><p>In the above code it is pretty clear that a failure of toInt function is a bug in the code and so it throws a NumberFormatException if the given string does not represent an integer. But what if some string came from a user input field and application logic shall deal with invalid input by replacing it with a default value?</p><p>Technically, you could handle it by writing <a href="https://kotlinlang.org/docs/reference/exceptions.html#try-is-an-expression">try/catch</a> around string.toInt() and it is an idiomatic thing to do in Java. But do not do it in Kotlin. It is a bad style for Kotlin code. There’s a dedicated <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.text/to-int-or-null.html">String.toIntOrNull()</a> extension function specifically created for this purpose. It indicates an error through its result<em> value</em>, using null in this particular case.<em> </em>It nicely interplays with Kotlin null-safety to produce an elegant code that gives a default value in case of malformed input:</p><pre><strong>val</strong> number = string.toIntOrNull() <strong>?:</strong> defaultValue</pre><p>Moreover, the whole Kotlin standard library is designed with this concept in mind. Even operators to <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/-list/get.html">g</a>et an element from lists and arrays have <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.collections/get-or-null.html">getOrNull</a> counterparts just in case an index-of-range condition is not a logic error in your particular piece of code, but an intended special case that needs to be handled.</p><h4>API design</h4><p>You should design your own general-purpose Kotlin APIs in the same way: use exceptions for logic errors, type-safe results for everything else. <strong>Don’t use exceptions as a work-around to sneak a result value out of a function.</strong></p><p>When you face an API that is using exceptions for conditions that are not logic errors for your code, then it is better to write a separate wrapper function that converts its exceptions to the appropriate return value and use this wrapper from your code. This way, your caller will have to handle the error condition right away and you avoid writing try/catch in your general application code.</p><p>If there’s a single error condition and you are only interested in success or failure of the operation without any details, then prefer using null to indicate a failure. If there are multiple error conditions, then create a <a href="https://kotlinlang.org/docs/reference/sealed-classes.html">sealed class</a> hierarchy to represent various results of your function. Kotlin has all those needs covered by design, including a powerful <a href="https://kotlinlang.org/docs/reference/control-flow.html#when-expression">when</a> expression. This way all the application-specific errors become just values that can be easily and conveniently stored and processed using all the power of Kotlin.</p><p>For example, if you’ll ever need to use <a href="https://docs.oracle.com/javase/8/docs/api/java/text/DateFormat.html#parse-java.lang.String-">DateFormat.parse</a> method that throws <a href="https://docs.oracle.com/javase/8/docs/api/java/text/ParseException.html">ParseException</a> with details on <a href="https://docs.oracle.com/javase/8/docs/api/java/text/ParseException.html#getErrorOffset--">errorOffset</a> more than once in your code, then take a bit of time to write the corresponding sealed class and wrapper function and then enjoy Kotlin-style error handling elsewhere:</p><pre><strong>sealed</strong> <strong>class</strong> ParsedDate {<br>    <strong>data</strong> <strong>class</strong> Success(<strong>val</strong> date: Date) : ParsedDate()<br>    <strong>data</strong> <strong>class</strong> Failure(<strong>val</strong> errorOffset: Int) : ParsedDate()<br>}<br><br><strong>fun</strong> DateFormat.tryParse(text: String): ParsedDate =<br>    <strong>try</strong> {<br>        ParsedDate.Success(parse(text))<br>    } <strong>catch</strong> (e: ParseException) {<br>        ParsedDate.Failure(e.<em>errorOffset</em>)<br>    }</pre><p>When you see try/catch in code always eye it with extreme suspicion. Every try/catch in your code has the potential to shuffle a critical programming bug under the rug. Encapsulate them and review them carefully.</p><h4>Input/output</h4><p>Let’s get back to the roots and address the largest elephant in the error-handling room — input/output errors. They represent a special case in the error-handling conundrum because they combine two properties. First of all, input/output errors are not logic errors in code. They typically represent external conditions that the code has no control over but must deal with. However, immediately turning every input/output error into a special result value in input/output APIs gets us back to the C state of affairs that we started with. Any kind of parsing or even business logic that makes multiple network requests becomes too cumbersome to write, obscuring the substance of code by the ceremony of error handling.</p><p>The default in Kotlin is to use exceptions for input/output. This way you can write dense logic of your code, without having to worry about potential network errors that each individual call could produce:</p><pre><strong>fun</strong> updateOrderQuanity(orderId: OrderId, quantity: Int) {<br>    require(quantity &gt; 0) { &quot;Quantity must be positive&quot; }<br>    <strong>val</strong> order = loadOrder(orderId)<br>    order.quantity = quantity<br>    storeOrder(order)<br>}</pre><p>The special thing about input/output errors is that they are handled in a centralized way by reporting an input/output problem to end-users and/or retrying an operation. You should not be writing boilerplate code to handle a potential network error in each individual network request in your application’s logic. <strong>Dedicate a single top-level place in your code to address all network errors uniformly.</strong> This is usually done at a boundary between low-level logic of the code and high-level user-interface or service endpoint.</p><p>Kotlin exceptions are not checked. One can forget to write this kind of top-level code to handle input/output errors, turning them into logic errors that will get reported as bugs to developers, which is a safe and pragmatic default for many applications.</p><h4>Exceptions, asynchronous programming, and coroutines</h4><p>There are already too many words in this story, so we’ll just briefly touch the subject of using exceptions in asynchronous code and coroutines in particular. Kotlin&#39;s <em>suspending functions </em>are sequential and thus the simple fact the code is using suspending functions proves nothing special about error handling approach that the code shall use.</p><p>However, Kotlin Coroutines are used to build highly asynchronous and concurrent applications where a lot of coroutines are running, each with the potential to fail. <a href="https://medium.com/@elizarov/structured-concurrency-722d765aa952">Structured Concurrency</a> in Kotlin is designed with the Kotlin’s view of exceptions. All exceptions should automatically percolate to the top-level of the application to get centrally handled. The key design principle is that no exception should be lost, all bugs should get reported to developers.</p><p>Thus, the same general advice applies to exceptions and coroutines: don’t use exceptions if you need local handling of certain failure scenarios in your code, don’t use exceptions to return a result value, avoid try/catch in general application code, implement centralized exception-handling logic, handle input/output errors uniformly at an appropriate boundary of your code.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8062f589d07" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Phantom of the Coroutine]]></title>
            <link>https://elizarov.medium.com/phantom-of-the-coroutine-afc63b03a131?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/afc63b03a131</guid>
            <category><![CDATA[kotlin-coroutines]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Sun, 10 May 2020 07:53:50 GMT</pubDate>
            <atom:updated>2020-07-06T06:20:49.459Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mzBSREBlYikO_CdUOsgJFg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@linusmimietz?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Linus Mimietz</a> on <a href="https://unsplash.com/s/photos/translucent?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Threads are heavy-weight and have substance. With threads, we can get a reference to some kind of current Thread object, examine its properties, modify its thread-local variables, and otherwise manipulate it. It is no surprise that people with the thread-programming background and education, who are coming to programming with coroutines, are looking for some kind of Coroutine object they can get hold of. However, there is none. Coroutines are phantom, ephemeral, insubstantial. There are good reasons for this state of affairs and some non-trivial consequences. Let’s dig in.</p><h4>The rule of coroutine transparency</h4><p>Consider the following suspending function foo that performs some work and then writes the resulting data to a database and sends a message with it over a message bus (both use network and are suspending, too).</p><pre><strong>suspend fun</strong> foo() {<br>    <strong>val</strong> data = doSomeWork()<br>    writeToDatabase(data)<br>    sendMessage(data)<br>}</pre><p>We can speed foo function up by calling writeToDatabase concurrently with the rest of the code that does sendMessage. It is straightforward— just delimit a scope for this concurrent operation and use <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html">launch</a> function:</p><pre><strong>suspend fun</strong> foo() = coroutineScope <strong>{</strong><br>    <strong>val </strong>data = doSomeWork()<br>    launch <strong>{</strong> writeToDatabase(data) <strong>}</strong><em> </em>// concurrent now<br>    sendMessage(data)<br><strong>}</strong></pre><blockquote>We don’t have to explicity wait for writeToDatabase operation to complete before returning from foo because <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/coroutine-scope.html">coroutineScope</a> builder does this wait automatically.</blockquote><p><strong>The rule of coroutine transparency</strong> states that neither the caller of foo nor the function writeToDatabase should be aware of or be affected by this introduction of concurrency. Having a separate coroutine should be completely transparent. Stated more narrowly, replacing a direct call to writeToDatabase(data) with a call from another coroutine viacoroutineScope { launch { writeToDatabase(data) } } should have as little noticeable effects as possible.</p><p>Of course, <a href="https://www.joelonsoftware.com/2002/11/11/the-law-of-leaky-abstractions/">all software abstractions are leaky</a> and we only have an illusion of true transparency here. If you look carefully, you’ll figure out that the coroutine context of writeToDatabase function has got a new <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html">Job</a> object, as shown in detail in the story on <a href="https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055">Coroutine Context and Scope</a>, but that is all there is to it. In all the other respects, writeToDatabase executes in the same context. If it fails or gets canceled, then those failures and cancellations are handled so that it is impossible for writeToDatabase and for foo’s caller to notice that it was being called from another coroutine.</p><h4>Corollary 1: Immutable context</h4><p>The first corollary to the rule of coroutine transparency is that the coroutine context is immutable. The context has to be inherited by children coroutines for the purpose of transparency and if it were mutable, just like a typical thread-local variable, then writeToDabata mutating the context would race with sendMessage that is trying to read it. It means that programmers who are used to mutating thread-local variables for things like tracking security context, logging diagnostics, etc, will have to learn a way to do so without a mutation.</p><p>Let’s take a look at logging, for example. You might have had code like this to set <a href="http://www.slf4j.org/api/org/slf4j/MDC.html">logging context</a> for doSomeWorkcall:</p><pre>MDC.put(&quot;login&quot;, &quot;user&quot;) // updates thread-local behind the scenes<br><strong>val</strong> data = doSomeWork()<br>// somehow restore the old MDC context after the call</pre><p>You cannot translate this code to coroutines by mutating the coroutine context instead of the thread-local context. What to do? You cannot change the coroutine context, but you can create a different coroutine with a different context; coroutines are phantom:</p><pre><strong>val</strong> newContextMap = MDC.getCopyOfContextMap()<br>newContextMap.put(&quot;login&quot;, &quot;user&quot;)<br><strong>val</strong> data = withContext(MDCContext(newContextMap)) <strong>{<br>    </strong>doSomeWork()<br><strong>}</strong></pre><p>This is more verbose, but it also avoids the need to restore the context after the call to doSomeWork. If you happen to need it more than a couple of times in your code, you can always introduce your own withMDCContext helper function and write the resulting code like this:</p><pre><strong>val</strong> data = withMDCContext(&quot;login&quot;, &quot;user&quot;) <strong>{<br>    </strong>doSomeWork()<br><strong>}</strong></pre><h4>Corollary 2: Non-reentrant mutex</h4><p>JVM monitors behind <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin/synchronized.html">synchronized</a> are <em>reentrant</em> and similarly <a href="https://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html">ReentrantLock</a> implementation in JDK is, which is even enshrined in its name. What does it mean for a lock to be reentrant? It means that the function foo can call doSomeWork holding the lock:</p><pre><strong>fun</strong> foo() {<br>    <em>lock</em>.lock()<br>    <strong>try</strong> { doSomeWork() }    <br>    <strong>finally</strong> { <em>lock</em>.unlock() }<br>}</pre><p>At the same time,doSomeWork can take this lock without problems, too:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2FGcvOw-lTc%3Ffrom%3D11%26to%3D15&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2FGcvOw-lTc%3Ffrom%3D11%26to%3D15&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/8954c4e65577de2c2b2d7412cf772c49/href">https://medium.com/media/8954c4e65577de2c2b2d7412cf772c49/href</a></iframe><p>So, it might come as a surprise that a similar <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.sync/-mutex/">Mutex</a> primitive for coroutines is non-reentrant. If you try to rewrite the same code line-by-line to coroutines, it does not work. It hangs due to a deadlock:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2Fd8CKTLLI3%3Ffrom%3D11%26to%3D15&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2Fd8CKTLLI3%3Ffrom%3D11%26to%3D15&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/c99c004185e91f538558fe8bacb93d87/href">https://medium.com/media/c99c004185e91f538558fe8bacb93d87/href</a></iframe><p>How come? To understand that, think about an implementation of a reentrant lock or take a look at <a href="http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/concurrent/locks/ReentrantLock.java">its source</a>. It is reentrant because it internally stores a reference to the owner <em>thread</em> and does not attempt to acquire the lock from the same thread twice. But this cannot be done with coroutines. There’s no way to store a reference to the current coroutine, because of the rule of coroutine transparency.</p><p>So, is there any way to work around it and still have reentrant mutex with coroutines? Absolutely! Use similar thinking to the previous section. Instead of trying to effectively mutate the current context with two separate lock and unlock functions, define a higher-order withReentrantLock function that creates a new coroutine with a custom context element that marks acquiring this mutex in the context, so that it can skip acquiring the same mutex on the second attempt from the same context. Now it works!</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2F7WCxXdh5L%3Ffrom%3D13%26to%3D17&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2F7WCxXdh5L%3Ffrom%3D13%26to%3D17&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/89e036db0dfafa82633f38e0e4877bdc/href">https://medium.com/media/89e036db0dfafa82633f38e0e4877bdc/href</a></iframe><h4>Conclusion</h4><p>Coroutines are not just light-weight threads, coroutines are phantom. You can transparently move a piece of work from one coroutine to another, but with this power comes a responsibility to think about coroutines differently from how you used to think about threads.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=afc63b03a131" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Deep recursion with coroutines]]></title>
            <link>https://elizarov.medium.com/deep-recursion-with-coroutines-7c53e15993e3?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/7c53e15993e3</guid>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[recursion]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Sat, 25 Apr 2020 20:33:55 GMT</pubDate>
            <atom:updated>2020-07-06T06:25:18.938Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*6oEmMaFgYMDUixkkqTxSrg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@craig000?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Riccardo Pelati</a> on <a href="https://unsplash.com/s/photos/deep?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>Kotlin Coroutines are typically used for asynchronous programming. However, the underlying design of coroutines and their implementation in Kotlin compiler are quite universal, solving problems beyond asynchronous programming. Let’s take a look at one such problem that can be elegantly solved with coroutines— writing deeply recursive functions.</p><h3>Setup</h3><p>Consider a tree data structure. For this example, let’s use this simple binary tree where each Tree node has a reference to its left and right children:</p><pre><strong>class</strong> Tree(<strong>val</strong> left: Tree?, <strong>val</strong> right: Tree?)</pre><p>The depth of the tree is defined as the length of the longest path from its root to its child nodes. It can be computed by the following recursive function:</p><pre><strong>fun</strong> depth(t: Tree?): Int =<br>    <strong>if</strong> (t == <strong>null</strong>) 0 <strong>else</strong> maxOf(<br>        depth(t.left), <em>// recursive call one</em><br>        depth(t.right) <em>// recursive call two</em><br>    ) + 1</pre><p>The logic here is straightforward. The depth is simply the maximum of the depth of the left and right children plus one, with the special case of zero when the tree is empty.</p><p>Recursion is a great tool for working with tree-like data structures, but there is a catch. Let’s generate a deep tree containing 100K nodes. Start with a leaf node Tree(null, null) as a seed and repeatedly generate parent nodes that link to the previous node as their left children:</p><pre><strong>val</strong> n = 100_000<br><strong>val</strong> deepTree = <em>generateSequence</em>(Tree(<strong>null</strong>, <strong>null</strong>)) <strong>{</strong> prev -&gt;<strong><br>    </strong>Tree(prev<strong>, null</strong>) <br><strong>}</strong>.<em>take</em>(n).<em>last</em>()</pre><p>This is not a particularly big data structure. It occupies less than 2MiB of memory, which is not much at all for a modern machine with gigabytes of available memory. Now, let’s try to use our depth function on it:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2FehZCsczZg%3Ffrom%3D16%26to%3D16&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2FehZCsczZg%3Ffrom%3D16%26to%3D16&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/f919e36404c4bf79aef919395ab51918/href">https://medium.com/media/f919e36404c4bf79aef919395ab51918/href</a></iframe><p>If you run it in Kotlin Playground you’ll get “<em>Your program produces too much output!</em>” message. If you run the same code on your local machine you’ll see what kind of output that is:</p><pre><strong>Exception in thread &quot;main&quot; java.lang.StackOverflowError</strong><br>    at FileKt.depth(File.kt:5)<br>    ... <em>// many more lines like that</em></pre><h3>Problem</h3><p>Oops! Despite the relatively small size of this tree, the depth call is running out of resources. It is written as a recursive function and it depletes a separate memory region that keeps track of thread’s call stack. A typical default limit for stack size is just a few megabytes, which is not enough here.</p><p>If the only thing we were doing in our software was to find the depth of this deepTree, then we could have reconfigured our runtime to use a larger stack size. But this kind of solution does not scale to real-life applications.</p><p>The other problem is that modern runtimes, like JVM HotSpot (that we are running this example under), are not really designed to work with large call stacks and run into all sorts of performance problems with deep recursion. Fortunately, there is another solution.</p><h3>Solution</h3><p>There is a lot of heap memory available in our system. Obviously, we should use the heap memory instead of a severely limited call stack memory. We can rewrite our depth algorithm by hand to avoid recursion and to use the heap. It is a mechanical and tenuous transformation whereby we write the code to create a call activation frame with the state of each ongoing call to depth in the heap and transform the sequential logic into a state-machine to avoid having to make recursive calls. The key component of this implementation is a while loop that processes the topmost call frame until there is no more work left to do.</p><p>Applying this approach, I’ve got this monster of a function. It works, but it is ugly and completely obscures the original simple algorithm that computes tree depth:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2FP3odE970u%3Ffrom%3D9%26to%3D27&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2FP3odE970u%3Ffrom%3D9%26to%3D27&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/07305661e262689d7566d6748d09dace/href">https://medium.com/media/07305661e262689d7566d6748d09dace/href</a></iframe><blockquote>There is also another way to implement depth computation that avoids the need to explicitly maintain state using a graph depth-first traversal, but it yields a slightly different algorithm as opposed to the above code that is a direct translation of our original depth function into a non-recursive form.</blockquote><h3>A better solution</h3><p>This kind of transformation of sequential logic into a state machine is exactly what happens behind the scenes in Kotlin to implement suspending functions. It can be exploited to solve the problem with deep recursion. To start, we define a wrapper class that takes a suspending block of code and turns it into a DeepRecursiveFunction&lt;T, R&gt; with a parameter of type T and a result of type R.</p><pre><strong>class</strong> DeepRecursiveFunction&lt;T, R&gt;(<br>    <strong>val</strong> block: <strong>suspend</strong> DeepRecursiveScope&lt;T, R&gt;.(T) -&gt; R<br>)</pre><p>The block is an extension on DeepRecursiveScope class that defines a <em>suspending function</em> callRecursive to make recursive calls:</p><pre><strong>class</strong> DeepRecursiveScope&lt;T, R&gt; {<br>    <strong>suspend fun </strong>callRecursive(value: T): R<br>}</pre><p>In the end, we want to rewrite the depth function like this, replacing recursive calls to depth with callRecursive, but otherwise keeping all the function’s code intact:</p><pre><strong>val</strong> depth = DeepRecursiveFunction&lt;Tree?, Int&gt; <strong>{ </strong>t <strong>-&gt;<br>    if</strong> (t == <strong>null</strong>) 0 <strong>else</strong> <em>maxOf</em>(<br>        callRecursive(t.left),<br>        callRecursive(t.right)<br>    ) + 1<br><strong>}</strong></pre><p>The key idea here is that callRecursive should not actually perform a regular recursive call to the block in DeepRecursiveFunction. Instead, its implementation <em>suspends the current coroutine</em> and saves both the current continuation and the parameter value for the recursive call to make:</p><pre><strong>suspend fun</strong> callRecursive(value: T): R = <br>    suspendCoroutineUninterceptedOrReturn <strong>{ </strong>cont <strong>-&gt;<br>    </strong><em>    </em><strong>this</strong>.cont = cont<br>        <strong>this</strong>.value = value<br>        <em>COROUTINE_SUSPENDED<br>    </em><strong>}</strong></pre><p>When the coroutine is suspended, the call stack unwinds and execution goes up to the outer loop. The purpose of this outer loop is very similar to the loop in the previous, manually crafted implementation. It initiates recursive calls when value was set by callRecursive. Essentially, it needs to call <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.coroutines.experimental/start-coroutine.html\">startCoroutine</a> on a block, but the Kotlin standard library only defines startCoroutine for suspending functions with zero and one argument, while the suspending block that we have has two arguments: scope and value. To start it as a coroutine we directly employ Kotlin’s Continuation-Passing Style (CPS) conversion for suspending functions that is explained in the <a href="https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md">coroutines design document</a> — a suspending function with two arguments is a regular function with three arguments, the last one being a continuation:</p><pre><strong>val</strong> function = block <strong>as</strong> Function3&lt;Any?, Any?, Continuation&lt;R&gt;, Any?&gt;</pre><p>Now, the recursive call can be started like this:</p><pre>function(<strong>this</strong>, value, cont)</pre><p>The last parameter is the continuation of the current frame that becomes the <em>completion</em> callback of the new call. There is no need to explicitly maintain a stack. This completion reference maintains the stack implicitly. When the newly initiated call completes, it automatically continues the caller’s execution at the state where it was suspended in callRecursive.</p><p>The only piece left to handle is the case when the whole computation completes. For that we implement the Continuation&lt;R&gt; interface in the DeepRecursiveScope class and store the result of the whole computation:</p><pre><strong>override val</strong> context: CoroutineContext<br>    <strong>get</strong>() = EmptyCoroutineContext<br><br><strong>override fun</strong> resumeWith(result: Result&lt;R&gt;) {<br>    this.cont = null<br>    this.result = result<br>}</pre><p>The DeepRecursiveScope instance itself is going to serve as the initial value for completion continuation and the whole implementation of the main loop becomes:</p><pre><strong>var</strong> cont: Continuation&lt;R&gt;? = <strong>this</strong></pre><pre><strong>fun</strong> runCallLoop(): R {<br>    <strong>while</strong> (<strong>true</strong>) {<br>        <strong>val</strong> result = <strong>this</strong>.result<br>        <strong>val</strong> cont = this<strong>.</strong>cont <em>// null means done</em><br>            ?: <strong>return</strong> result.<em>getOrThrow</em>()<br>        <em>// ~startCoroutineUninterceptedOrReturn<br>        </em><strong>val</strong> r = <strong>try</strong> {<br>            function(this, value, cont)<br>        } <strong>catch</strong> (e: Throwable) {<br>            cont.<em>resumeWithException</em>(e)<br>            <strong>continue</strong><br>        }<br>        <strong>if</strong> (r !== <em>COROUTINE_SUSPENDED</em>)<br>            cont.<em>resume</em>(r <strong>as</strong> R)<br>    }<br>}</pre><p>As a final touch, we can define operator fun invoke for DeepRecursiveFunction, so that we can start the recursive computation with a regular function invocation syntax:</p><pre><strong>operator fun</strong> &lt;T, R&gt; DeepRecursiveFunction&lt;T, R&gt;.invoke(t: T): R =<br>    DeepRecursiveScope&lt;T, R&gt;(block, t).runCallLoop()</pre><h3>What we’ve got</h3><p>Having implemented deep recursive function framework using Kotlin Coroutines we can write the depth function in a direct style and run it without StackOverflowError:</p><iframe src="https://cdn.embedly.com/widgets/media.html?src=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2Fx85sDbMYl%3Ffrom%3D62%26to%3D71&amp;display_name=Kotlin+Playground&amp;url=https%3A%2F%2Fround-lake.dustinice.workers.dev%3A443%2Fhttps%2Fpl.kotl.in%2Fx85sDbMYl%3Ffrom%3D62%26to%3D71&amp;key=a19fcc184b9711e1b4764040d3dc5c07&amp;type=text%2Fhtml&amp;schema=kotl" width="800" height="300" frameborder="0" scrolling="no"><a href="https://medium.com/media/85aab1894a59503287a4f44110d49561/href">https://medium.com/media/85aab1894a59503287a4f44110d49561/href</a></iframe><h3>Closing thoughts</h3><p>While there’s not that much code, there’s a lot to understand to grasp the way it works. Reading <a href="https://github.com/Kotlin/KEEP/blob/master/proposals/coroutines.md">Coroutines Design document</a> should help with better understanding. On the other hand, you don’t have to understand how it is implemented in order to use it and benefit from it.</p><p>I have a gist (see <a href="https://gist.github.com/elizarov/861dda8c3e8c5ee36eaa6db4ad996568">here</a>) with a fully productized implementation of DeepRecursiveFunction that has documentation and also supports mutually recursive functions. It might become an addition to the Kotlin standard library. Do you think it is useful or not? Leave your feedback in this Kotlin YouTrack issue, please: <a href="https://youtrack.jetbrains.com/issue/KT-31741">KT-31741</a>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7c53e15993e3" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The End of the Semicolon Era]]></title>
            <link>https://elizarov.medium.com/the-end-of-the-semicolon-era-60ab95e669ab?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/60ab95e669ab</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[language]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Sun, 09 Feb 2020 11:33:53 GMT</pubDate>
            <atom:updated>2020-02-09T11:47:34.373Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VPjTDZT9kZjL71-jOZMKaw.png" /><figcaption><a href="https://flickr.com/photos/austinmulhern/19317274371/in/photolist-vr15EF-6Aa3DH-5fGTuS-a8FLoU-Li8P-rJF7vB-qMYA5c-rGttKG-rJLfzM-54b1er-rqrzfa-rJLfLi-rscFuy-rsj6Ck-rsj8j6-rscGpQ-rJLhaa-do2Xed-9xiQEv-7zaSAq-53Xmqc-q7zHCB-5k2vzy-W94b3U-dGEVxZ-cc8DF-f6jftq-iL9tEj-qNueVT-qw64Q3-qwcru2-qwcrda-HeD8X6-qw5cJQ-qw5bWN-dw8z3G-cQLjMd-dA6BQT-pRSqCH-qNz7w5-cQdJHq-pRDjfQ-qw646C-qNz7Nh-aazHfa-qNuf1c-qNz6Uo-qLmu9A-qw5cNs-dGLkML">DeadEnd by Austin Mulhern</a></figcaption></figure><p>Throughout most of my early professional career, I programmed in languages where you terminate or separate statements with a semicolon; it was like an ideograph distinguishing coders from laypeople. I had a motor reflex to type a semicolon (;) without having to think about it. So when I got involved in the early discussions about the new language that JetBrains was working on back in 2010, the language that would be later called Kotlin, I was not thrilled about the proposal to drop the mandatory semicolons. Who minds the semicolons I thought. I had quite a strong opinion about the need to solve the billion-dollar problem with nulls<a href="#e20b">¹</a><strong> </strong>to make our software safer, but the visuals I cared less about. How narrowminded I was!</p><p>As it turns out, the disappearance of the semicolon has become the distinguishing characteristic of the rising modern languages of the late 2000s and 2010s to such an extent that it almost becomes synonymous with the very feeling of programming in the “modern language”. Scala (2004), Go (2009), Kotlin (2011), and Swift (2014) are all statically typed, all follow C/C++/Java syntactic tradition with curly braces, all were introduced and grew big in the last 20 years and all had totally ditched the mandatory semicolon despite the tradition. Is it a coincidence? I don’t think so. I see a trend here.</p><h3>The ground zero</h3><p>Let’s take a look at the elephant in the room — Python programming language (1990). While it is not exactly “modern”, it has enjoyed a meteoric rise in popularity only recently. It became the most popular programming language by some forward-looking metrics like PYPL<a href="#1136">²</a> and is consistently placed into the top three.</p><p>Syntactically Python is a thing in itself. It is a rare breed of fully indentation-sensitive language (aka off-side rule<a href="#f925">³</a>) that is not some kind of an academic or a toy language. The clean and concise looks of Python code are quite appealing. Switching from something as lean as Python (unless you try object-oriented Python, that is) to something as verbose as Java (1995) or C# (2000) is tough, so no surprise that modern languages are racing to find some middle ground here.</p><p>With type-inference now a defacto-standard language feature<a href="#e6d5">⁴</a>, it is becoming feasible to avoid visual noise and repetitiveness while maintaining type-safety and scalability. The motto becomes:</p><blockquote>If both compiler and human can easily guess it then you should not explicitly spell it out in the code.</blockquote><p>Don’t Repeat Yourself (DRY) desire is getting stronger, cleanness is treasured. Sans-serif fonts had occupied our code editors for quite some time, with new coding fonts raising clarity bar even higher<a href="#dbb1">⁵</a>; programming languages are becoming sans-type and sans-semicolons.</p><h3>A false-start</h3><p>The road to the future without semicolons was a bumpy one, though. One major false-start has happened in JavaScript (1995). The rules for Automatic Semicolon Insertion (ASI) in JavaScript lead to numerous gotchas, where a properly formatted JS code with two statements, like this one:</p><pre>const name = &quot;World&quot;<br>[&quot;Hello&quot;, name].forEach(word =&gt; console.log(word))</pre><p>actually gets interpreted as one statement by the rules of JS, forcing the programmer to use the semicolon just to make sure the compiler understands what the author meant by their code, even as it is totally unambiguous to a human reader in the first place. It is so bad in JS that modern JS coding styles recommend always using semicolons to save mental energy thinking about whether you need one or not. TypeScript (2012), being backward-compatible with JavaScript, suffers from it as well.</p><h3>The solution</h3><p>The JS semicolons mishap had almost killed the very idea of dropping semicolons in a curly-braced language. Yet, it turns out it is not an insurmountable problem, once the design goal is set straight. The goal is not to omit semicolons sometimes by automatically inserting them here and there using heuristics, but to make sure that every properly-formatted code that is following a language style guide never has to include a semicolon at all.</p><p>The rigor of the modern programming style makes it possible to design such rules even for the C-family of indentation-insensitive languages. We, the developers, are taught and are used to split statements into multiple lines after opening braces and operators. Take a look at a multi-line function call statement:</p><pre>receiver.foo(<br>   parameter<br>).doSomethingWithIt() // one multi-line statement</pre><p>When formatted in a good style, it is clearly distinct from two separate statements, both for a human and for a well-engineered compiler, even if you remove all the indentation:</p><pre>receiver.foo // the first statement<br>(parameter).doSomethingWithIt() // the second statement</pre><p>Once it crosses the threshold from “I have to think whether semicolon is needed here” to “I never need a semicolon”, the bliss of not having to write and never having to <em>see</em> semicolons in the code becomes truly liberating.</p><p>As with all quality-of-life improvements they only become apparent when you have to do without them for a while. Have you ever tried to read code that is typeset in Times New Roman? You may not even realize it at first, but you get this distinct feeling that something is not right — there’s too much visual noise from all that serif. Nowadays I get the same feeling when I see semicolons in the modern code, especially if it is written in some seemingly modern language. Every time I cannot stop myself but open Wikipedia to double-check in what year the language I’m reading was introduced in.</p><p>We are living in the end of the semicolon era. The recipe is out there. It is only a matter of time for it to become truly ubiquitous.</p><h3>Further reading and footnotes</h3><ol><li><a href="#3776">^</a> <a href="https://medium.com/@elizarov/null-is-your-friend-not-a-mistake-b63ff1751dd5">Null is your friend, not a mistake</a></li><li><a href="#1b6b">^</a> <a href="http://pypl.github.io/PYPL.html">PYPL PopularitY of Programming Language</a></li><li><a href="#f694">^</a> <a href="https://en.wikipedia.org/wiki/Off-side_rule">Off-side rule</a></li><li><a href="#53cb">^</a> <a href="https://medium.com/@elizarov/types-are-moving-to-the-right-22c0ef31dd4a">Types are moving to the right</a></li><li><a href="#e31e">^</a> <a href="https://www.jetbrains.com/lp/mono/">JetBrains Mono Font</a></li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=60ab95e669ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Intentional qualities]]></title>
            <link>https://elizarov.medium.com/intentional-qualities-7e6a57bb87fc?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e6a57bb87fc</guid>
            <category><![CDATA[quality]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Sat, 19 Oct 2019 14:36:35 GMT</pubDate>
            <atom:updated>2019-10-20T10:24:13.567Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*tzfgxnd_5FiqbvQQWhOghw.png" /><figcaption><a href="https://www.flickr.com/photos/emaybe/8281842629/in/photolist-dBQBha-5W8TTe-7Bb4D8-23azuys-28hosuN-5R54Eh-XdBDQa-8mxH5b-b4yZrR-31aqFe-ekxkXx-31eVXh-bXiqSP-awLMYc-eoGUS9-5vScRb-ivhcLZ-awPyrL-a8FxF1-9s1H4k-5WdcBN-kKci4n-q9XyXb-31aqN8-bEPKS8-fW1JhW-68hhdh-aEYDwv-58V3KL-7UDXe1-9myXMN-ivheRR-qQcmu5-68d66x-eNMjkw-cJJQd5-isybyW-ikyZrn-7QZy59-68qMAn-9TMfkA-ihRyr7-8Gfwwj-68qMwx-8ekRg8-4KPaMe-skvPrL-xPSxg-9my8Yb-fW1EME">Vernier Caliper by Michael Brace</a></figcaption></figure><p>When we work on a piece of software, being it an application or a library, we often focus on its <em>functional requirements</em>. They are usually quite easy to directly observe and test; functional requirements are featured in software marketing materials in the form of “feature matrix” that compares different products. Yet, many non-functional aspects of software, also known as <em>quality attributes</em> or <em>qualities</em> for short, such as reliability, efficiency, security, maintainability, usability, etc¹ can be important, too. They are not as easy to measure, though.</p><p>Consider, for a example, a task of picking a library to parse some data interchange format. When you face a problem like this, you might have in mind some specific requirements like “it should be able to parse this and that file we have”. You can quickly sketch test code to vet the candidates you’ve found. It is even simpler if the format is formally defined in some standard or standard-like written document. You’ll just read documentation to confirm that the library claims conformance with the corresponding standard and then all you need to do is to perform a straightforward acceptance test. Trust, but verify.</p><p>But what happens if you want to find a <em>fast</em> parser, for example, or if you have some other quality attribute in mind that shall be satisfied to suit your needs? Measuring qualities is way more complicated and quite a non-trivial endeavor. Even simple things, like speed, being it either response time or bandwidth, need a proper test setup and a lot of skill. It is time consuming.</p><p>In the past I used to spend a lot of time when faced with a problem like that. You are lucky if you can find a ready-to-use quality comparison in your domain, but more often than not, all kinds of comparisons for non-functional requirements that you can find on the internet are of a very low quality (pun intended). However, over time I’ve discovered a shortcut.</p><p>See, the fact is that you cannot accidentally write a software that has a specific quality. You cannot accidentally write fast code. Your code cannot be secure by an accident. Accidentally easy to learn? Never heard of it.</p><p>Qualities are often lost by an accident, but are never accidentally acquired. It is similar to the second law of thermodynamics. In order to maintain quality you have to be very conscious, intentional about it. You have to constantly fight against losing it. What’s more, it requires spelling out the corresponding quality as your explicit goal when you start working on a piece of software and careful prioritization of qualities, since different qualities tend to conflict. Try writing something that’s very fast, yet maintainable to see how it happens.</p><p>So, the shortcut I’ve found is this. If a project documentation does not explicitly list or tout a particular quality, then it is unlikely to have one by accident. For example, if I’m really looking for a fast parser, then I’ll be looking for project that had an explicit goal to write one. I can save time testing others that did not have such goal. That’s it. Qualities are intentional.</p><p>¹ Wikipedia has a separate page that lists various <a href="https://en.wikipedia.org/wiki/List_of_system_quality_attributes">quality attributes</a>, but different sources place emphasis on different aspects of this space.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e6a57bb87fc" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Structured Concurrency Anniversary]]></title>
            <link>https://elizarov.medium.com/structured-concurrency-anniversary-f2cc748b2401?source=rss-4762e889f8fc------2</link>
            <guid isPermaLink="false">https://medium.com/p/f2cc748b2401</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coroutine]]></category>
            <category><![CDATA[concurrency]]></category>
            <dc:creator><![CDATA[Roman Elizarov]]></dc:creator>
            <pubDate>Sat, 28 Sep 2019 15:02:02 GMT</pubDate>
            <atom:updated>2019-09-30T13:12:51.232Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UELadMU0GOrYJVoTXVvViw.png" /><figcaption>Photo by <a href="https://unsplash.com/@anniespratt?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Annie Spratt</a> on <a href="https://unsplash.com/s/photos/birthday?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p>A little over a year ago I announced big conceptual shift in the design of Kotlin Coroutines called <a href="https://medium.com/@elizarov/structured-concurrency-722d765aa952">Structured Concurrency</a>. From that moment on, it took our team about a month to make the first stable 1.0.0 release of <a href="https://github.com/Kotlin/kotlinx.coroutines">kotlinx.coroutines</a> library. After a year of further work, kotlinx.coroutines had added stable support for <a href="https://medium.com/@elizarov/cold-flows-hot-channels-d74769805f9">cold flows</a> that integrate nicely with <a href="https://medium.com/@elizarov/reactive-streams-and-kotlin-flows-bfd12772cda4">reactive streams</a>. The library had reached version 1.3.2 by now. It is good time to look back and see how it all worked out — what was great, what could be improved.</p><p>Structured Concurrency accomplished more than we hoped for. Originally, the design of structured concurrency was based on the woes experienced by backend developers trying to implement all sorts of asynchronous and concurrent logic. It was focused on making sure that you cannot ever lose a running coroutine or an exception. The key building block we added back then is <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/">coroutineScope { ... }</a> function, which encapsulates concurrent operations and limits their scope to the scope of the current call.</p><p>There was not much else to it, so our recommendation to UI developers was to implement <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/index.html">CoroutineScope</a> interface in various “closeable” entities of their applications. We envisioned a simple picture with a simple scope hierarchy. It turned out to be more elaborate in practice.</p><p>Structured Concurrency was rapidly adopted by Android, which has quite complicated life-cycles. Android libraries added <a href="https://medium.com/@elizarov/extension-oriented-design-13f4f27deaee">extensions</a> like <a href="https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary.html#lifecyclescope">lifecycleScope</a> and <a href="https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary.html#viewmodelscope">viewModelScope</a>, enabling concise and safe integration of coroutines with those concepts. It became apparent that code looks clearer when an object encapsulating the scope is separate from the rest of code. Introductory <a href="https://codelabs.developers.google.com/codelabs/kotlin-coroutines/#4">Android Codelab on Coroutines</a> recommends defining coroutine scope like this:</p><pre><strong>private val </strong>scope = CoroutineScope(...)</pre><p>Nowadays, this style increasingly looks more appealing, so it’s<a href="https://github.com/Kotlin/kotlinx.coroutines/issues/1581"> time to adjust our documentation</a> to reflect it.</p><p>At the same time, structured concurrency laid a solid foundation for using <a href="https://kotlinlang.org/docs/reference/coroutines/flow.html#launching-flow">Kotlin Flows in UI applications</a>. With a dedicated <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/launch-in.html">launchIn</a> operator we can observe events from a flow in a given scope using the following code:</p><pre>flow.onEach { ... }<br>    .launchIn(scope)</pre><p>It is a perfect use-case for structured concurrency. You’d design this kind of API if you were working on a dedicated reactive framework for UI where you want to ensure that no subscription is left dangling when widgets close.</p><h4>Walk down memory lane</h4><p>I recently gave a <a href="https://hydraconf.com/2019/talks/68l5ztovlf0xm9aindouzr/">talk</a> on <a href="https://hydraconf.com/">Hydra Conference</a>, which focuses on distributed systems and computing, where I told the original story behind the design of Kotlin Coroutines and how we had discovered the concept of Structured Concurrency and found a name for it (you can <a href="https://www.youtube.com/watch?v=Mj5P47F6nJg">watch the recording</a> or <a href="https://speakerdeck.com/elizarov/structured-concurrency">scroll though the slides</a>).</p><p>To prepare for that talk I’ve actually looked through the early prototypes of what had later become kotlinx.coroutines library. It is curious enough that the original prototype name for the <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-job/index.html">Job</a> interface was Lifetime. In pre-release versions before structured concurrency you’d have to interact with Job methods quite often to manage concurrency in your application. Structured concurrency made most of it just a low-level implementation detail. You don’t deal with Job directly so often now when you have a ready-to-use implementation of CoroutineScope that encapsulates an instance of a job in its context.</p><h4>Legacy</h4><p>We are still haunted by some of the pre-release legacy. Look at the signature of the most basic coroutine builder (or any other coroutine builder):</p><pre><strong>fun </strong>CoroutineScope.<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/launch.html">launch</a>(context: CoroutineContext, ...): Job</pre><p>As explained in the <a href="https://medium.com/@elizarov/coroutine-context-and-scope-c8b255d59055">story on coroutine context and scope</a>, it combines the <a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines/-coroutine-scope/coroutine-context.html">context from the scope</a> and the explicitly passed context parameter to produce a child context. This is very convenient feature, since you can do things like:</p><pre>scope.launch(Dispatchers.IO) { .... }</pre><p>Instead of a longer code with essentially the same effect:</p><pre><br>scope.launch { <br>    withContext(Dispatchers.IO) { ... }<br>} </pre><p>However, if you pass a context parameter with another Job element inside of it, the resulting behavior of the code, while well specified, may be <a href="https://github.com/Kotlin/kotlinx.coroutines/issues/1001">unintuitive</a>. Have we designed it from scratch, we could have forbidden the job in the context argument, thus making sure that it can only come from the scope. That would have completely avoided the whole question of how it should behave by default when there are two jobs.</p><p>Yet, we had to support gradual migration of all the code our early adopters wrote before the structured concurrency, so we did not have this option. We don’t recommend passing jobs in the context parameter to coroutine builders in modern code, though. Moreover, the corresponding context-management feature of Kotlin Flow (<a href="https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/flow-on.html">flowOn</a> operator) was designed with this lesson in mind, so it does not support swapping job in its execution context and does not have any surprising behaviors.</p><h4>Road ahead</h4><p>The goal of library design is to give application developers ready-to-use tools that cover their most frequent use-cases and solve the most popular problems. If something is needed all the time by any kind of application, it should be simple and straightforward to code. The correct code shall be the easiest one to write, while advanced, rarely needed corner-cases can and should take longer. We have the essentials covered with kotlinx.coroutines library.</p><p>However, there are lots of directions where structured concurrency can be further taken to. As adoption grows, there are advanced patterns of code that nonetheless start to occur quite often. When they are hard or error-prone to write properly, it is a good indicator that we might need to introduce additional features to help. Like that problem with two jobs in the context I’ve mentioned before. In fact, there are valid use-cases where you have an entity with its own life-time, like a HTTP connection, and a client code in another module of your application making a request in its own scope. You should be able to easily combine them so that request is restricted to both life-times. Currently, this is cumbersome to implement.</p><p>There are more things like that to keep us busy working on improvements.</p><h4>Conclusion</h4><p>Structured Concurrency in Kotlin Coroutines, being just over a year old, turned out to be a nice and cute kid. It has lots of potential and an amazing and promising future, helping developers in various domains to manage lifetime of their concurrent primitives</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f2cc748b2401" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>