<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Nikita Katchik</title>
    <description>The latest articles on DEV Community by Nikita Katchik (@nikitakatchik).</description>
    <link>https://dev.to/nikitakatchik</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F856747%2F7d87f58c-4c82-403e-9238-013fe8d4bce4.jpeg</url>
      <title>DEV Community: Nikita Katchik</title>
      <link>https://dev.to/nikitakatchik</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nikitakatchik"/>
    <language>en</language>
    <item>
      <title>They know how to fix notifications</title>
      <dc:creator>Nikita Katchik</dc:creator>
      <pubDate>Sun, 09 Oct 2022 20:08:13 +0000</pubDate>
      <link>https://dev.to/nikitakatchik/they-know-how-to-fix-notifications-d0n</link>
      <guid>https://dev.to/nikitakatchik/they-know-how-to-fix-notifications-d0n</guid>
      <description>&lt;h1&gt;
  
  
  The problem
&lt;/h1&gt;

&lt;p&gt;The notifications on mobile devices are terrible. On all of them. Once in a while, mobile operating systems get new notifications-related features claiming to give the users more control over the notifications flooding their space visually and auditorily. While providing more flexibility, those features usually burden the users with maintaining the correct settings. Misconfiguring the notifications could result in missing important notifications. However, even if the users do keep their configuration perfect, they will still get spammed, regardless, since there is no control over frequency.&lt;/p&gt;

&lt;p&gt;Now, I would like to avoid speculating on the mobile operating systems developers' motives for not providing any control over the frequency. However, one thing is crystal clear: they know about the problem.&lt;/p&gt;

&lt;p&gt;Let us talk about one simple idea for solving this problem. Imagine the following situation.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;[11:15:02] Text notification 1 arrives&lt;br&gt;
[11:15:03] Text notification 2 arrives&lt;br&gt;
[11:15:04] Text notification 3 arrives&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In total, these events would fire three notifications. However, was there any thinkable purpose for the last 2 of them? Did they bring any actionable information? The only thing that comes to mind is the number of messages. Although, what if there were ten messages? Would we still count the beeps, or would we just conclude that a bunch of messages has been delivered? That kind of conclusion could be driven without listening to 10 eternally annoying beeps.&lt;/p&gt;

&lt;h1&gt;
  
  
  Frequency suppresion
&lt;/h1&gt;

&lt;p&gt;One simple thing to do would be to work smart with the &lt;em&gt;reminder-notifications&lt;/em&gt; which usually fire with certain interval after the first or the last unread notification, depending on the platform and the app.&lt;/p&gt;

&lt;p&gt;Let us look at the conventional way first. &lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7h140af6hfdxpi1vnyd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fl7h140af6hfdxpi1vnyd.png" alt="Conventional" width="689" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, each event fires a notification, and there are also the reminder-notifications. For simplicity, let us assume that notifications are read/released/forgotten by the system after a reminder is fired.&lt;/p&gt;

&lt;p&gt;A way to handle a series of single-type events here would be to only fire a notification instantly when there are no unread notifications. All subsequent events would only affect the &lt;strong&gt;interval&lt;/strong&gt; and the &lt;strong&gt;tone&lt;/strong&gt; of the reminder notification.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The tone is changed so that the user can hear that the reminder notification is getting more urgent.&lt;/li&gt;
&lt;li&gt;The interval is shortened so that the delay is appropriate for the suspected urgency.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is how it would look.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7h8at15kszh3xiytbk56.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7h8at15kszh3xiytbk56.png" alt="Suggestion" width="689" height="283"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The apparent effects this strategy brought are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The number of notifications fired is down to 6 from 13.&lt;/li&gt;
&lt;li&gt;The chance of two notifications being fired with almost no interval has significantly decreased (it happened only once in the second chart).&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  Types, sources and caveats
&lt;/h1&gt;

&lt;p&gt;The mentioned frequency suppression strategy can be applied globally or together with an event grouping strategy, including, but not limited to:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;By notification type&lt;/li&gt;
&lt;li&gt;By app, which dispatched the notification&lt;/li&gt;
&lt;li&gt;By notification urgency level declared by the dispatcher&lt;/li&gt;
&lt;li&gt;By contact or other app-level sources&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This part could be much bigger than implementing the core of the strategy itself, and this is also the kind of thing that cannot be investigated in a short dev.to piece in your free time. The complications of the event grouping may be precisely why this or a similar notification frequency suppression strategy is not implemented.&lt;/p&gt;

&lt;p&gt;Nevertheless, the notification frequency problem exists. Suppose you are not ready to disable some apps' notifications entirely. In that case, it is currently an unsolvable problem, regardless of how much effort you are ready to invest in configuring mobile notifications.&lt;/p&gt;

</description>
      <category>ios</category>
      <category>android</category>
      <category>mobile</category>
      <category>ux</category>
    </item>
    <item>
      <title>Building with Docker</title>
      <dc:creator>Nikita Katchik</dc:creator>
      <pubDate>Sun, 02 Oct 2022 22:51:03 +0000</pubDate>
      <link>https://dev.to/nikitakatchik/building-with-docker-2a9h</link>
      <guid>https://dev.to/nikitakatchik/building-with-docker-2a9h</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;Client engineers, embedded systems engineers, and other engineers who never worked with the backend can still benefit significantly from using containers. In this piece, I would like to share how I structure nearly all of my side projects so that they can be built on any machine that has &lt;code&gt;make&lt;/code&gt; and &lt;code&gt;docker&lt;/code&gt; installed.&lt;/p&gt;

&lt;h2&gt;
  
  
  Motivation
&lt;/h2&gt;

&lt;h4&gt;
  
  
  Environment guarantee
&lt;/h4&gt;

&lt;p&gt;Multiple contributors and the continuous integration server (CI) can be confident they are building in exactly the same environment.&lt;/p&gt;

&lt;h4&gt;
  
  
  Environment versioning
&lt;/h4&gt;

&lt;p&gt;Every team member does not have to build the builder images locally. Docker Registry can be used to store and version the images.&lt;/p&gt;

&lt;h4&gt;
  
  
  Easy workstation/CI build agent setup
&lt;/h4&gt;

&lt;p&gt;As mentioned above, the only tools required to build such a project are &lt;code&gt;make&lt;/code&gt; and &lt;code&gt;docker&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;Make&lt;/code&gt; is not used here as the project build system but rather as a convenient tool to wrap lengthy &lt;code&gt;docker&lt;/code&gt; commands. For the scope of the article, we will assume a Rust project located at &lt;code&gt;$(ROOT_DIR)/main&lt;/code&gt; using &lt;code&gt;cargo&lt;/code&gt; build system.&lt;/p&gt;

&lt;h2&gt;
  
  
  Linux target
&lt;/h2&gt;

&lt;p&gt;Let us start simple. Docker is always Linux, so the easiest platform to target is Linux since this does not imply cross-compilation. This also works perfect for unit testing if you are writing a Linux or platform-independent application.&lt;/p&gt;

&lt;p&gt;The following &lt;code&gt;Makefile&lt;/code&gt; utilizes an official &lt;code&gt;rust:1.61.0&lt;/code&gt; image.&lt;/p&gt;

&lt;h6&gt;
  
  
  Makefile
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="nv"&gt;ROOT_DIR&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;shell &lt;span class="nb"&gt;dirname&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;realpath&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;firstword &lt;span class="p"&gt;$(&lt;/span&gt;MAKEFILE_LIST&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;span class="nv"&gt;SOURCE_DIR&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;)&lt;/span&gt;/main

&lt;span class="nv"&gt;IMAGE_BUILDER_LINUX&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; rust:1.61.0

&lt;span class="nl"&gt;build_linux&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;builder_linux&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker run &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;SOURCE_DIR&lt;span class="p"&gt;)&lt;/span&gt;:/src &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_BUILDER_LINUX&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'cd /src &amp;amp;&amp;amp; cargo build'&lt;/span&gt;

&lt;span class="nl"&gt;test_linux&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;builder_linux&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker run &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;SOURCE_DIR&lt;span class="p"&gt;)&lt;/span&gt;:/src &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_TESTER_LINUX&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'cd /src &amp;amp;&amp;amp; cargo test'&lt;/span&gt;

&lt;span class="nl"&gt;builder_linux&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker pull &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_BUILDER_LINUX&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now a simple &lt;code&gt;make build_linux&lt;/code&gt; and &lt;code&gt;make test_linux&lt;/code&gt; should:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Make sure the builder Docker image is pulled&lt;/li&gt;
&lt;li&gt;Run build/test using the image&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Custom builders
&lt;/h2&gt;

&lt;p&gt;It is most likely that, eventually, you will need to customize the builder image. That would imply creating a &lt;code&gt;Dockerfile&lt;/code&gt; and building an image using it instead of pulling the image as we did above.&lt;/p&gt;

&lt;p&gt;Assuming the &lt;code&gt;Dockerfile&lt;/code&gt; is located at &lt;code&gt;$(ROOT_DIR)/image/x&lt;/code&gt;:&lt;/p&gt;

&lt;h6&gt;
  
  
  Makefile
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="nv"&gt;ROOT_DIR&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;shell &lt;span class="nb"&gt;dirname&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;realpath&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;firstword &lt;span class="p"&gt;$(&lt;/span&gt;MAKEFILE_LIST&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;span class="nv"&gt;SOURCE_DIR&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;)&lt;/span&gt;/main
&lt;span class="nv"&gt;PROJECT_NAME&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;shell &lt;span class="nb"&gt;basename&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="nv"&gt;IMAGE_BUILDER_X&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;PROJECT_NAME&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-builder-x&lt;/span&gt;

&lt;span class="nl"&gt;builder_x&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_BUILDER_X&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;)&lt;/span&gt;/image/x
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  WebAssembly
&lt;/h2&gt;

&lt;p&gt;Let me use this opportunity to share some scripts to build Rust code into WebAssembly and run the unit tests.&lt;/p&gt;

&lt;h6&gt;
  
  
  Makefile
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight make"&gt;&lt;code&gt;&lt;span class="nv"&gt;ROOT_DIR&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;shell &lt;span class="nb"&gt;dirname&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="nb"&gt;realpath&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;firstword &lt;span class="p"&gt;$(&lt;/span&gt;MAKEFILE_LIST&lt;span class="p"&gt;))))&lt;/span&gt;
&lt;span class="nv"&gt;SOURCE_DIR&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;)&lt;/span&gt;/main
&lt;span class="nv"&gt;PROJECT_NAME&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;shell &lt;span class="nb"&gt;basename&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;))&lt;/span&gt;

&lt;span class="nv"&gt;RUST_TARGET_WEB&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; wasm32-unknown-unknown

&lt;span class="nv"&gt;IMAGE_BUILDER_WEB&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;PROJECT_NAME&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="nt"&gt;-builder-web&lt;/span&gt;

&lt;span class="nl"&gt;build_web&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;builder_web&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker run &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;SOURCE_DIR&lt;span class="p"&gt;)&lt;/span&gt;:/src &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_BUILDER_WEB&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'cd /src &amp;amp;&amp;amp; cargo build --target &lt;/span&gt;&lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="s1"&gt;RUST_TARGET_WEB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;

&lt;span class="nl"&gt;test_web&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;builder_web&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker run &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;--rm&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="nt"&gt;-v&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;SOURCE_DIR&lt;span class="p"&gt;)&lt;/span&gt;:/src &lt;span class="se"&gt;\&lt;/span&gt;
    &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_BUILDER_WEB&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="se"&gt;\&lt;/span&gt;
    sh &lt;span class="nt"&gt;-c&lt;/span&gt; &lt;span class="s1"&gt;'cd /src &amp;amp;&amp;amp; CARGO_TARGET_WASM32_UNKNOWN_UNKNOWN_RUNNER=wasmtime_test_runner.sh cargo test --target &lt;/span&gt;&lt;span class="p"&gt;$(&lt;/span&gt;&lt;span class="s1"&gt;RUST_TARGET_WEB&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="s1"&gt;'&lt;/span&gt;

&lt;span class="nl"&gt;builder_web&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;
    &lt;span class="p"&gt;@&lt;/span&gt;docker build &lt;span class="nt"&gt;-t&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;IMAGE_BUILDER_WEB&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;$(&lt;/span&gt;ROOT_DIR&lt;span class="p"&gt;)&lt;/span&gt;/image/web
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  image/web/Dockerfile
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight docker"&gt;&lt;code&gt;&lt;span class="k"&gt;FROM&lt;/span&gt;&lt;span class="s"&gt; rust:1.61.0&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;&lt;span class="se"&gt;\
&lt;/span&gt;rustup toolchain &lt;span class="nb"&gt;install&lt;/span&gt; &lt;span class="s1"&gt;'1.61.0-x86_64-unknown-linux-gnu'&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;span class="nt"&gt;--target&lt;/span&gt; &lt;span class="s1"&gt;'wasm32-unknown-unknown'&lt;/span&gt; &lt;span class="se"&gt;\
&lt;/span&gt;&lt;span class="nt"&gt;--component&lt;/span&gt; &lt;span class="s1"&gt;'rust-std'&lt;/span&gt;

&lt;span class="k"&gt;RUN &lt;/span&gt;curl https://wasmtime.dev/install.sh &lt;span class="nt"&gt;-sSf&lt;/span&gt; | bash

&lt;span class="k"&gt;COPY&lt;/span&gt;&lt;span class="s"&gt; wasmtime_test_runner.sh /usr/bin&lt;/span&gt;

&lt;span class="k"&gt;VOLUME&lt;/span&gt;&lt;span class="s"&gt; ["/usr/local/cargo/registry"]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h6&gt;
  
  
  image/web/wasmtime_test_runner.sh
&lt;/h6&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;#!/bin/sh&lt;/span&gt;

/root/.wasmtime/bin/wasmtime &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$@&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="nt"&gt;--invoke&lt;/span&gt; main 0 0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is relatively easy with Rust because &lt;code&gt;rustup&lt;/code&gt; has a &lt;code&gt;wasm32&lt;/code&gt; toolchain ready to download, but with a more complicated cross-compiler setup, being able to make a &lt;code&gt;Dockerfile&lt;/code&gt; describing the setup sequence is a lifesaver.&lt;/p&gt;

&lt;h2&gt;
  
  
  Thoughts
&lt;/h2&gt;

&lt;p&gt;The more complicated the required build setup is, the more I recommend looking at the Docker-based building. Setting up a workstation to build a project requiring multiple cross compilers, especially when using certain toolchains that might require some user-fixing, can take up to a few hundred bash commands. That could theoretically be done without containers by a bash script, but that how numerous caveats, including:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;State zero would have to be identical before running the script&lt;/li&gt;
&lt;li&gt;Updating the build setup while keeping it identical between workstations is a challenging task&lt;/li&gt;
&lt;li&gt;No environment guarantee&lt;/li&gt;
&lt;li&gt;No switching between build setup versions&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>docker</category>
      <category>make</category>
      <category>build</category>
    </item>
    <item>
      <title>The simplest traits showcase</title>
      <dc:creator>Nikita Katchik</dc:creator>
      <pubDate>Mon, 22 Aug 2022 23:39:36 +0000</pubDate>
      <link>https://dev.to/nikitakatchik/traits-showcase-3ol4</link>
      <guid>https://dev.to/nikitakatchik/traits-showcase-3ol4</guid>
      <description>&lt;h2&gt;
  
  
  Intro
&lt;/h2&gt;

&lt;p&gt;It is handy to be able to separate the interface from the implementation; however, it often is not enough. In this piece, I'd like to talk about probably the simplest example of an interface-class paradigm limitation.&lt;/p&gt;

&lt;p&gt;What if we had to design a &lt;code&gt;Size&lt;/code&gt; class in Java or other classic OOP language? There are many variations, especially in construction (factories) and setters (if any), but one thing would have remained unchanged in every single implementation: the two numbers.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight swift"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Java&lt;/span&gt;

&lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="kt"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mWidth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;mHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;width&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="kt"&gt;IllegalArgumentException&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;mWidth&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;width&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;mHeight&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;height&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mWidth&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mHeight&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="n"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Every engineer had to write something like this a couple of times, but has this never bothered you from the data perspective? It strikes me odd every time a pair of numbers is suddenly a &lt;code&gt;Size&lt;/code&gt;. A pair of numbers can be anything, but since the data is not detachable from implementation, we create classes like &lt;code&gt;Size&lt;/code&gt;, &lt;code&gt;Point&lt;/code&gt;, and &lt;code&gt;ComplexNumber&lt;/code&gt; – all containing precisely two numbers: from a data perspective, they are not different types.&lt;/p&gt;

&lt;p&gt;Of course, we do this because, in Java, we have to think from the interface's perspective; implementation simply contains all the necessary member variables to fulfil the interface contract. The price we pay here is having to create a &lt;code&gt;Size&lt;/code&gt; even if we have a pair of perfectly fine numbers on the stack.&lt;br&gt;
In the case of &lt;code&gt;Size&lt;/code&gt;, it does not seem like a big sacrifice; however, once the structure of the types, the transformations performed on them, and the wiring become more complex, not being able to use perfectly fine data can become rather limiting, especially if it's be something big.&lt;/p&gt;
&lt;h2&gt;
  
  
  The solution
&lt;/h2&gt;

&lt;p&gt;What if there was an idiomatic way to treat data in different ways on different occasions? This is precisely what a trait is: a predefined way to interpret certain data type. Let us have a look at how similar thing could be done with traits in Rust.&lt;/p&gt;

&lt;p&gt;First, we don't even have to declare a data type here: we can simply use a tuple instead. Here is how we'd declare a variable suitable to be treated as a size.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Rust&lt;/span&gt;

&lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then we declare a trait. Notice that we define &lt;code&gt;area()&lt;/code&gt; function right away. That's because the area calculation does not depend on the details of &lt;code&gt;width()&lt;/code&gt; and &lt;code&gt;height()&lt;/code&gt; functions implementation details.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Rust&lt;/span&gt;

&lt;span class="k"&gt;trait&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="nf"&gt;.width&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="nf"&gt;.height&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;All that is left to do is to define to interpret a tuple of two integers &lt;code&gt;(u32, u32)&lt;/code&gt; as a &lt;code&gt;Size&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Rust&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="na"&gt;.1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It is just as simple to implement the &lt;code&gt;Size&lt;/code&gt; trait for a slice of two &lt;code&gt;u32&lt;/code&gt;'s.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Rust&lt;/span&gt;

&lt;span class="k"&gt;impl&lt;/span&gt; &lt;span class="n"&gt;Size&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;u32&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;width&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;height&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;u32&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;self&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We have just implemented &lt;code&gt;Size&lt;/code&gt; trait for two basic data types which preexisted in the language. Now let us try invoking these implementations.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight rust"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Rust&lt;/span&gt;

&lt;span class="k"&gt;fn&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;tuple&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// Automatic trait resolution&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="nf"&gt;.area&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Explicitly choose trait implementation&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;tuple&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;


    &lt;span class="k"&gt;let&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt;

    &lt;span class="c1"&gt;// Automatic trait resolution&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="nf"&gt;.area&lt;/span&gt;&lt;span class="p"&gt;());&lt;/span&gt;

    &lt;span class="c1"&gt;// Explicitly choose trait implementation&lt;/span&gt;
    &lt;span class="nd"&gt;println!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Area: {}"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nn"&gt;Size&lt;/span&gt;&lt;span class="p"&gt;::&lt;/span&gt;&lt;span class="nf"&gt;area&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;array&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Thoughts
&lt;/h2&gt;

&lt;p&gt;Data doesn't have to be glued together with the implementation even when the interface is declared separately, and creating useful code without defining redundant data types is possible. With traits, we can interpret the same piece of data differently on different occasions, which changes everything in the pursuit of a &lt;em&gt;good design&lt;/em&gt;. Traits are sometimes referred to as a concept of object-oriented programming, but in my opinion it is misleading: the complete traits can only be found in languages leaning towards data-oriented design, like Rust.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>java</category>
    </item>
    <item>
      <title>Demystifying the complexity of RAII</title>
      <dc:creator>Nikita Katchik</dc:creator>
      <pubDate>Tue, 10 May 2022 22:14:11 +0000</pubDate>
      <link>https://dev.to/nikitakatchik/demystifying-the-complexity-of-raii-12hp</link>
      <guid>https://dev.to/nikitakatchik/demystifying-the-complexity-of-raii-12hp</guid>
      <description>&lt;h2&gt;
  
  
  The idiom
&lt;/h2&gt;

&lt;p&gt;Whenever I look at an article explaining &lt;strong&gt;Resource Acquisition Is Initialization (RAII)&lt;/strong&gt; idiom, I feel uncertain if I would understand it if I did not already know it, so here is my attempt to make it easy.&lt;/p&gt;

&lt;p&gt;First, let us roughly group the mainstream languages by the memory management model.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Manual: C, Assembly&lt;/li&gt;
&lt;li&gt;Garbage-collected (GC): Java, Python, Javascript, Kotlin, C#&lt;/li&gt;
&lt;li&gt;Automatic Reference Counting (ARC): Swift, Objective-C&lt;/li&gt;
&lt;li&gt;Resource Acquisition Is Initialization (RAII): C++, Rust&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are numerous differences between ARC and tracing garbage collection, but we are interested in a particular one. Unlike tracing garbage collection, there is no background process deallocating the objects asynchronously in ARC. Therefore the objects are destroyed within the same reference decrement invocation where the reference count reaches zero.&lt;/p&gt;

&lt;p&gt;One of the ways to see RAII is as ARC, with excess freedoms removed. If we take ARC as a baseline, then RAII would be different in the following ways:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Reference count can only be equal to 0 or 1. In other words, every resource can only be owned by a single variable.&lt;/li&gt;
&lt;li&gt;A reference can be explicitly moved from one variable to another using features built into the language.&lt;/li&gt;
&lt;li&gt;A reference can be borrowed when providing it as a function argument without increasing the reference count.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let us take a look at some RAII pseudocode.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// RAII pseudocode&lt;/span&gt;

&lt;span class="c1"&gt;// Create new object and assign it to the variable.&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a0&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// 🟢 OK&lt;/span&gt;

&lt;span class="c1"&gt;// This wouldn't build.&lt;/span&gt;
&lt;span class="c1"&gt;// An object can only be held by one variable.&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ERROR&lt;/span&gt;

&lt;span class="c1"&gt;// Move the object to another variable.&lt;/span&gt;
&lt;span class="c1"&gt;// a1 is uninitialized after that (known at compile-time).&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;a0&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 🟢 OK&lt;/span&gt;

&lt;span class="c1"&gt;// Copy the object into another variable.&lt;/span&gt;
&lt;span class="c1"&gt;// a1 keeps its original value.&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;copy&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// 🟢 OK&lt;/span&gt;

&lt;span class="c1"&gt;// This function requires ownership of an instance of type A.&lt;/span&gt;
&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Provide the object as a function argument, moving it.&lt;/span&gt;
&lt;span class="c1"&gt;// Just like when we moved the object to initialize a&lt;/span&gt;
&lt;span class="c1"&gt;// variable, this leaves the source variable uninitialized.&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ERROR&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;borrow&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ERROR&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;a1&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 🟢 OK&lt;/span&gt;

&lt;span class="c1"&gt;// This function does not require a dedicated instance.&lt;/span&gt;
&lt;span class="c1"&gt;// We use &amp;amp; to signal that a borrowed object is enough.&lt;/span&gt;
&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;A&lt;/span&gt;&lt;span class="o"&gt;&amp;amp;)&lt;/span&gt; &lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="no"&gt;B&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Provide the object as a function argument, borrowing it.&lt;/span&gt;
&lt;span class="c1"&gt;// Although function bar does not require a dedicated&lt;/span&gt;
&lt;span class="c1"&gt;// instance, it can work with one.&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;с1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// ❌ ERROR&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;с2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;borrow&lt;/span&gt; &lt;span class="n"&gt;a2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 🟢 OK&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;с3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;a2&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// 🟢 OK&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Languages &amp;amp; features
&lt;/h2&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;In RAII languages, every line is basically a try-with-resource block.&lt;/strong&gt;&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;RAII originated in C++, but do not let yourself be intimidated by that. If you code in Java 7 or above, you have already glanced at certain aspects of this idiom – try-with-resource blocks.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Java&lt;/span&gt;

&lt;span class="c1"&gt;// Java standard interface&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;AutoCloseable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt; 
    &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;close&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="kd"&gt;throws&lt;/span&gt; &lt;span class="nc"&gt;Exception&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtain&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// The resource, and the variable referencing it, both&lt;/span&gt;
    &lt;span class="c1"&gt;// only exist inside this block.&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The variable only exists inside the block.&lt;/li&gt;
&lt;li&gt;Lifetime of the resource is bound to the lifetime of the variable.&lt;/li&gt;
&lt;li&gt;Any usage of the &lt;code&gt;res&lt;/code&gt; variable is effectively borrowing. No matter what happens inside the block (within reason), it will not change the scope of &lt;code&gt;res&lt;/code&gt;, and, therefore, the lifetime of the resource it holds.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If multiple resources are required, blocks can be stacked.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Java&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtain&lt;/span&gt;&lt;span class="o"&gt;())&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
            &lt;span class="o"&gt;}&lt;/span&gt;
        &lt;span class="o"&gt;}&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Furthermore, multiple resources can be declared in a single block. Once control exits the block, the resources are destroyed in reverse order of declaration, mimicking stacked blocks behaviour.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Java&lt;/span&gt;

&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;static&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;try&lt;/span&gt; &lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtain&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
         &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;))&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In RAII languages, every line of code is basically a try-with-resource block. Not just every line of the function body but also every member variable declaration in a class or a structure. Destruction in reverse order of declaration mentioned above is the key to making this possible because subsequently declared resources can be dependent on one or multiple previously declared ones.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// RAII pseudocode&lt;/span&gt;

&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;foo&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtain&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Destructed third&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Destructed second&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Destructed first&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="c1"&gt;// RAII pseudocode&lt;/span&gt;

&lt;span class="n"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;ResourceTypeA&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Destructed third&lt;/span&gt;
    &lt;span class="nc"&gt;ResourceTypeB&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Destructed second&lt;/span&gt;
    &lt;span class="nc"&gt;ResourceTypeC&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Destructed first&lt;/span&gt;
&lt;span class="o"&gt;};&lt;/span&gt;

&lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="nf"&gt;bar&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtain&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;obtainDependent&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="c1"&gt;// After this line, the a, b and c variables are empty.&lt;/span&gt;
    &lt;span class="c1"&gt;// Destroying d will trigger the destruction of all Foo&lt;/span&gt;
    &lt;span class="c1"&gt;// member variables in reverse order of declaration.&lt;/span&gt;
    &lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;Foo&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="n"&gt;move&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The variables, and the resources they hold, declared in either function body or as a member variable in a class or a structure, are destructed in reverse order of declaration, similar to the multiple resources in a single try-with-resource block mentioned above.&lt;/p&gt;

&lt;p&gt;Python, too has a similar with-as feature, allowing to specify multiple resources.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# Python
&lt;/span&gt;
&lt;span class="nf"&gt;with &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;obtain&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="nf"&gt;obtainDependent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;a&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 
      &lt;span class="nf"&gt;obtainDependent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt;
    &lt;span class="c1"&gt;# The resources are owned by a, b, and c variables.
&lt;/span&gt;    &lt;span class="c1"&gt;# The resources and the variables only exist inside
&lt;/span&gt;    &lt;span class="c1"&gt;# this block.
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  RAII origins
&lt;/h2&gt;

&lt;p&gt;C++ first appeared in 1985; however, if we look at the design patterns and common practices that have developed within the community pre C++11, it becomes clear that the community has not been sticking to the core idiom of the language, and for a good reason.&lt;/p&gt;

&lt;p&gt;Before C++11, the language lacked a critical feature. There was simply no idiomatic way to move a resource (object) from one variable to another or pass the ownership in a function call. That left C++ engineers of the time with very limited options: pass raw pointers around like there is no resource management built into the language or wrap most objects in &lt;code&gt;std::auto_ptr&lt;/code&gt;. A fallback to any of those substantially defeats the purpose of the RAII idiom.&lt;/p&gt;

&lt;h1&gt;
  
  
  Thoughts
&lt;/h1&gt;

&lt;p&gt;I believe it is time to reevaluate the contract we signed when Java, and other languages with tracing garbage collection, appeared. Is the benefit of being able sometimes to disregard the scope of a lifetime of an object worth having to design the language around this commodity? The more experience I get with Java/C++/Rust, the more I find the language design sacrifices made in Java harder and harder to justify. To name just a few:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No real constants semantics like in Rust, C++, or even C.&lt;/li&gt;
&lt;li&gt;We still care about memory management, but the problem moved into another perspective. At some point, any application, client or server starts having performance issues if the behaviour of the garbage collector is not taken into consideration. Java engineers have to go through a tremendous amount of learning to predict and fight the GC behaviour.&lt;/li&gt;
&lt;li&gt;The problems above lead to having to make some classes immutable by design, which puts engineers in a weird position of having to make this choice ahead of time.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Those sacrifices may be a result of a conscious choice. However, we know way more about their consequences today than when we agreed to them in the 90s.&lt;/p&gt;

&lt;p&gt;Although Swift does not have these problems because it uses ARC instead of a tracing garbage collector, it is still vulnerable to the reference loops. Rust, on the other hand, is not just immune to the reference loops but will not even let us make one. Except for when we use shared pointers. A strict resource management model helps to design good software by making the engineer think about the relationships.&lt;/p&gt;

&lt;p&gt;With this in mind, I would like to invite every sceptic to have a serious look at RAII, and more specifically, Rust. As we pointed out earlier, this is not only about performance. In fact, performance is entirely secondary: the critical reasoning for giving RAII a better look is the language design decisions it allows us to make.&lt;/p&gt;

</description>
      <category>rust</category>
      <category>cpp</category>
      <category>java</category>
      <category>python</category>
    </item>
  </channel>
</rss>
