<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title></title>
    <link rel="self" type="application/atom+xml" href="https://lmilz.dev/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://lmilz.dev"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2026-07-11T00:00:00+00:00</updated>
    <id>https://lmilz.dev/atom.xml</id>
    <entry xml:lang="en">
        <title>Why the Sci-Fi Quantum Computer Fails at the Weather</title>
        <published>2026-07-11T00:00:00+00:00</published>
        <updated>2026-07-11T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/why-the-sci-fi-quantum-computer-fails-at-the-weather/"/>
        <id>https://lmilz.dev/blog/why-the-sci-fi-quantum-computer-fails-at-the-weather/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/why-the-sci-fi-quantum-computer-fails-at-the-weather/">&lt;p&gt;I recently read &lt;em&gt;Der Zorn des Oktopus&lt;/em&gt; (&quot;The Wrath of the Octopus&quot;) by Dirk Rossmann and Ralf Hoppe, a novel in which a quantum computer is built to predict chaotic systems. The basic idea: determine weather events once it has &quot;all the data&quot;. If x is supposed to happen, a causal chain y has to occur first, and a sufficiently large quantum computer simply plays that chain forward. In the novel the machine is promptly abused to influence events: what has to happen so that event X takes place?&lt;/p&gt;
&lt;p&gt;Reading this left me confused, because quantum computers, as they stand today, are not suited for this kind of computation. Yet the motif is popular in science fiction. Adam Fawer&#39;s thriller &lt;em&gt;Improbable&lt;/em&gt; has no quantum computer, but there someone turns into a so-called Laplace&#39;s demon: an intelligence, conceived by Pierre-Simon Laplace in 1814, that knows the exact state of every particle in the universe and computes the entire future and past from it. The punchline: long before computing power becomes the issue, the demon fails at his input, because &quot;all the data&quot; is not something that physically exists (more on that below). So the half-sentence &quot;once it has all the data&quot; does not skip some minor detail, it skips exactly the spot where physics says no. Which made me ask, also because I had not followed the state of research for over ten years: are there quantum algorithms for chaotic systems at all, and where exactly does the idea break in practice?&lt;/p&gt;
&lt;h2 id=&quot;the-basic-problem-linear-versus-nonlinear&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-basic-problem-linear-versus-nonlinear&quot; aria-label=&quot;Anchor link for: the-basic-problem-linear-versus-nonlinear&quot;&gt;The basic problem: linear versus nonlinear&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A quantum computer computes in a fundamentally different way from a classical machine. Instead of bits (0 or 1) it works with qubits, which span huge state spaces through superposition and entanglement. The decisive catch for the weather scenario is the mathematics underneath: quantum mechanical time evolution under the Schrödinger equation is necessarily linear and unitary, probabilities are preserved.&lt;/p&gt;
&lt;p&gt;The weather, however, is a highly chaotic system. Chaotic systems are deterministic and still unpredictable in the long run. They are described by nonlinear equations, such as the Navier-Stokes equations for turbulent flows or the simplified Lorenz model. Their defining feature is the butterfly effect: extreme sensitivity to initial conditions. If two initial states differ by as little as a millionth of a degree, their distance grows exponentially over time. This divergence is measured by the &lt;strong&gt;positive Lyapunov exponent&lt;/strong&gt;. Even if the quantum computer were infinitely fast, the smallest measurement error in the input data blows up any exact trajectory prediction after a short time (the Lyapunov time).&lt;/p&gt;
&lt;p&gt;So we are forcing a machine that only masters linear operations to solve a nonlinear problem. The currently most studied way to bridge this gap is called Carleman linearization.&lt;/p&gt;
&lt;h2 id=&quot;carleman-linearization-in-one-equation&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#carleman-linearization-in-one-equation&quot; aria-label=&quot;Anchor link for: carleman-linearization-in-one-equation&quot;&gt;Carleman linearization in one equation&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The idea is old and elegant. You embed a nonlinear equation into a linear system by treating the powers of the variables as new, independent variables. The clearest way to see it is the simplest nonlinear example, the logistic equation:&lt;/p&gt;
&lt;p&gt;$$
\frac{dx}{dt} = x(1 - x) = x - x^2
$$&lt;/p&gt;
&lt;p&gt;Now define $y_k := x^k$ and differentiate:&lt;/p&gt;
&lt;p&gt;$$
\frac{dy_k}{dt} = k\,x^{k-1}\frac{dx}{dt} = k\,x^{k-1}(x - x^2) = k\,(x^k - x^{k+1}) = k\,(y_k - y_{k+1})
$$&lt;/p&gt;
&lt;p&gt;That is the whole mechanism. A nonlinear equation becomes a &lt;strong&gt;linear&lt;/strong&gt; system, but one with infinitely many equations: $y_1$ depends on $y_2$, $y_2$ on $y_3$, and so on. In vector form this is $\dot{\mathbf{y}} = A\,\mathbf{y}$ with an infinitely large, sparse matrix $A$. And that is exactly the form a quantum computer likes: linear systems of differential equations can be handled by algorithms like HHL or newer linear solvers in time that grows only logarithmically with the dimension.&lt;/p&gt;
&lt;p&gt;The catch: nobody can solve infinitely many equations. You truncate at some order $N$ and set $y_{N+1} = 0$. The last row then becomes $\dot{y}_N = N\,y_N$, a pure growth term. The eigenvalues of the truncated matrix are simply $1, 2, \dots, N$, all positive. Keep that in mind for a moment.&lt;/p&gt;
&lt;h2 id=&quot;the-experiment&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-experiment&quot; aria-label=&quot;Anchor link for: the-experiment&quot;&gt;The experiment&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;How quickly this truncation breaks down is something an experiment can show. Here is the complete code: it solves the logistic equation exactly (analytically) and compares it with Carleman truncations of different orders, solved via the matrix exponential.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;import&lt;/span&gt;&lt;span&gt; numpy&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; as&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;from&lt;/span&gt;&lt;span&gt; scipy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;linalg&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; import&lt;/span&gt;&lt;span&gt; expm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; carleman_matrix&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;Carleman matrix for dx/dt = x - x^2 with y_k = x^k.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    dy_k/dt = k*(y_k - y_{k+1}). Truncation: y_{N+1} = 0.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    A&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;zeros&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;        A&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; k&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        if&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;            A&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;k&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; k&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; = -&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; exact&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; x0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; x0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;exp&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; carleman_solve&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    A&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; carleman_matrix&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    y0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;**&lt;/span&gt;&lt;span&gt;k&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; for&lt;/span&gt;&lt;span&gt; k&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;expm&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; tt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; @&lt;/span&gt;&lt;span&gt; y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; for&lt;/span&gt;&lt;span&gt; tt&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;linspace&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1.6&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0.6&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ex&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; exact&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 4&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 16&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cl&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; carleman_solve&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;x0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    err&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;abs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cl&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; ex&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    idx&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;argmax&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1e-2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;    print&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;N=&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;{&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;:2d&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;: breaks down at t=&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result as a picture:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/carleman_logistic.png&quot; alt=&quot;Carleman linearization of the logistic equation&quot; /&gt;&lt;/p&gt;
&lt;p&gt;Two things stand out.&lt;/p&gt;
&lt;p&gt;First: every truncation follows the exact solution perfectly for a while and then tips over abruptly. That is not a rounding error, it is structural. As soon as the truncated order becomes relevant, the growth term $\dot{y}_N = N\,y_N$ from above takes over and the solution explodes. In the code you see maximum errors of $10^{30}$ and beyond.&lt;/p&gt;
&lt;p&gt;Second: higher order helps, but with clearly diminishing returns. Going from order 2 to 16, eight times the number of variables, only shifts the valid window from $t \approx 0.2$ to $t \approx 0.82$.&lt;/p&gt;
&lt;h2 id=&quot;why-the-window-does-not-grow-arbitrarily&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#why-the-window-does-not-grow-arbitrarily&quot; aria-label=&quot;Anchor link for: why-the-window-does-not-grow-arbitrarily&quot;&gt;Why the window does not grow arbitrarily&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The second point is the decisive one. There is a parameter that determines whether Carleman converges at all. Liu and colleagues showed in 2021 that the ratio $R$ of nonlinearity to dissipative linearity decides the situation: for $R &amp;lt; 1$ there is a genuine quantum advantage, for $R \ge \sqrt{2}$ the problem is not efficiently solvable in the worst case&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#liu2021&quot;&gt;1&lt;/a&gt;&lt;/sup&gt;. The logistic equation has a growing linear part instead of a damping one, so it sits deliberately in the hard regime, hence the early breakdown.&lt;/p&gt;
&lt;p&gt;In fluid dynamics this $R$ carries a familiar name. Several works link it to the Reynolds number, which measures precisely the ratio of inertial to viscous forces and thus how turbulent a flow is&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#tennie2025&quot;&gt;2&lt;/a&gt;&lt;/sup&gt;. Gonzalez-Conde and colleagues tie the convergence to the Kolmogorov scale, the grid resolution needed to resolve the energy cascade of the nonlinear term&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#gonzalez2025&quot;&gt;3&lt;/a&gt;&lt;/sup&gt;. Sanavio and Succi get their lattice-Boltzmann-Carleman variant to work at moderate Reynolds numbers between 10 and 100 already at second order&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#sanavio2024&quot;&gt;4&lt;/a&gt;&lt;/sup&gt;. That is encouraging, but it does not describe real turbulence yet.&lt;/p&gt;
&lt;h2 id=&quot;from-the-principle-to-real-chaos-the-lorenz-system&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#from-the-principle-to-real-chaos-the-lorenz-system&quot; aria-label=&quot;Anchor link for: from-the-principle-to-real-chaos-the-lorenz-system&quot;&gt;From the principle to real chaos: the Lorenz system&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The logistic equation demonstrates the mechanism, but it is only one-dimensional and not chaotic. For a well-known chaotic system with several variables, consider the Lorenz system:&lt;/p&gt;
&lt;p&gt;$$
\frac{dx}{dt} = \sigma(y - x)
$$&lt;/p&gt;
&lt;p&gt;$$
\frac{dy}{dt} = x(\rho - z) - y
$$&lt;/p&gt;
&lt;p&gt;$$
\frac{dz}{dt} = xy - \beta z
$$&lt;/p&gt;
&lt;p&gt;with the classic parameters $\sigma = 10$, $\rho = 28$, $\beta = 8/3$. The system has three variables and shows real deterministic chaos with the famous butterfly attractor.&lt;/p&gt;
&lt;p&gt;Carleman linearization works analogously: you define monomials $x^a y^b z^c$ as new variables and derive the linear system in them. At truncation order $N$ (all monomials with $a+b+c \leq N$) the dimension grows quickly: $N=2$ needs 9 variables, $N=3$ already 19.&lt;/p&gt;
&lt;p&gt;Here is the code:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;import&lt;/span&gt;&lt;span&gt; numpy&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; as&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;from&lt;/span&gt;&lt;span&gt; scipy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;linalg&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; import&lt;/span&gt;&lt;span&gt; expm&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; generate_monomials&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;&amp;quot;&amp;quot;All monomials x^a y^b z^c with a+b+c &amp;lt;= N.&amp;quot;&amp;quot;&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    monoms&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; total&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        for&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;total&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;            for&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;total&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                c&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; total&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                monoms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; monoms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; deriv_monom&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;abc&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;10.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;28.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;8.0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; abc&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    terms&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        terms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;, (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; terms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; carleman_matrix_lorenz&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;10.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;28.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;8.0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    monoms&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; generate_monomials&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    idx&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt;abc&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; abc&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; enumerate&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;monoms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;monoms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    A&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;zeros&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; abc&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; enumerate&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;monoms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        for&lt;/span&gt;&lt;span&gt; coeff&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; abc_prime&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; deriv_monom&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;abc&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;            if&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; sum&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;abc_prime&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; and&lt;/span&gt;&lt;span&gt; abc_prime&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span&gt; idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;                A&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;abc_prime&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +=&lt;/span&gt;&lt;span&gt; coeff&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; A&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; monoms&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; exact_lorenz_rk4&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;10.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;28.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;8.0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; rhs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; z&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span&gt;sigma&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;rho&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;z&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;y&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span&gt;beta&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;z&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    states&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    dt&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; range&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; states&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        k1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; rhs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        k2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; rhs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0.5&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;dt&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;k1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        k3&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; rhs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0.5&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;dt&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;k2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        k4&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; rhs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; dt&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;k3&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        states&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;dt&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;k1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;k2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;k3&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; k4&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;states&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;def&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; carleman_solve_lorenz&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;10.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;28.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;8.0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;):&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    A&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; monoms&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; carleman_matrix_lorenz&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; sigma&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; rho&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; beta&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    idx&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt;abc&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; abc&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt; enumerate&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;monoms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    y0_carleman&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;**&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;**&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;**&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; for&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; c&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span&gt; monoms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    states&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; tt&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        y_t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; expm&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;A&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; tt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; @&lt;/span&gt;&lt;span&gt; y0_carleman&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        states&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;y_t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)]],&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; y_t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)]],&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; y_t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)]]])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;states&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;# main run&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;linspace&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 500&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ex&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; exact_lorenz_rk4&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 3&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    cl&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt; carleman_solve_lorenz&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;y0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    err&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;linalg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;norm&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cl&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; ex&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; axis&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    idx&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; np&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;argmax&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 10.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span&gt; idx&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; and&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; err&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 10.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;N=&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;{&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;: stable until t=&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;:.2f&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    else&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;font-style: italic;&quot;&gt;        print&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;font-style: italic;&quot;&gt;f&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;N=&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;{&lt;/span&gt;&lt;span&gt;N&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;: breaks down (error &amp;gt; 10) at t=&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;{&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;t&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;idx&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;:.3f&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;}&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result for the initial value $(1, 1, 1)$:&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/carleman_lorenz.png&quot; alt=&quot;Carleman linearization of the Lorenz system&quot; /&gt;&lt;/p&gt;
&lt;p&gt;x(t), y(t), z(t) and the error on a log scale. The exact RK4 solution (black) stays bounded, the Carleman approximations (orange N=2, blue N=3) explode from t ≈ 0.3 on. The x marker shows the respective divergence point, the red line the error threshold of 10.&lt;/p&gt;
&lt;p&gt;The difference to the logistic equation is drastic. Higher order barely helps here: from $N=2$ to $N=3$ the valid window only shifts from $t \approx 0.27$ to $t \approx 0.29$. For real chaos, the Carleman truncation is next to useless.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/carleman_lorenz_3d.png&quot; alt=&quot;Lorenz attractor in 3D, exact RK4 solution over t = 0 to 40&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The exact trajectory forms the butterfly attractor, the color gradient encodes time. The Carleman approximations (orange N=2, blue N=3) follow only briefly and then diverge (x marker).&lt;/p&gt;
&lt;h2 id=&quot;fixable-or-fundamental&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#fixable-or-fundamental&quot; aria-label=&quot;Anchor link for: fixable-or-fundamental&quot;&gt;Fixable or fundamental&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Still, the breakdown of the truncated Carleman matrix is not the final verdict on the quantum computer. By now there are workarounds such as pivot switching by Endo and Takahashi&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#endo2024&quot;&gt;5&lt;/a&gt;&lt;/sup&gt; or pivot-shifted Carleman linearization&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#wang2025&quot;&gt;6&lt;/a&gt;&lt;/sup&gt;: you shift and rescale the system around a pivot state so that the truncated dynamics no longer inevitably diverges. The exploding matrix is primarily a numerical problem. For regular dynamics it can be defused.&lt;/p&gt;
&lt;p&gt;The hard limit sits elsewhere. Lewis and colleagues proved in 2024 that any quantum algorithm that exactly propagates a system with a positive Lyapunov exponent has a complexity that grows at least exponentially with the simulated time&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#lewis2024&quot;&gt;7&lt;/a&gt;&lt;/sup&gt;. That limit comes from the chaos itself; the Carleman truncation just happens to run into it first. Better code, pivot switching, or more qubits change nothing about it.&lt;/p&gt;
&lt;p&gt;For the sci-fi plot this means keeping two things apart. The numerical breakdown of the Carleman method is a bug that clever people can fix. The exponential cost caused by chaos is a structural limit of the mathematics, not of the hardware.&lt;/p&gt;
&lt;h2 id=&quot;a-different-line-quantum-reservoir-computing&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#a-different-line-quantum-reservoir-computing&quot; aria-label=&quot;Anchor link for: a-different-line-quantum-reservoir-computing&quot;&gt;A different line: quantum reservoir computing&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Carleman line tries to solve the differential equation itself. Since around 2023 a second route has established itself that reframes the problem. Quantum reservoir computing (QRC) uses the quantum system as a nonlinear substrate that approximates the attractor from a short observed time series. Only a classical readout layer is trained. The results are remarkable: Ahmed, Tennie and Magri predict extreme events in a turbulent shear flow model with correct timing using a recurrence-free variant&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#ahmed2024&quot;&gt;8&lt;/a&gt;&lt;/sup&gt;, Steinegger and Räth reproduce the short-term behavior and long-term statistics of eight prototypical chaotic systems with four qubits&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#steinegger2025&quot;&gt;9&lt;/a&gt;&lt;/sup&gt;, and Connerty and colleagues ran a quantum echo state network on real IBM hardware, far longer than the coherence times of the chip&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#connerty2025&quot;&gt;10&lt;/a&gt;&lt;/sup&gt;.&lt;/p&gt;
&lt;p&gt;What matters is what QRC cannot do. It approximates the attractor and delivers short-term forecasts, not a single trajectory over long horizons. The Lyapunov exponent strikes here just the same, without Carleman and without truncation. QRC shows that quantum computers can indeed take over a certain prediction task. It is just not the one the novel promises: the exact future from complete initial data.&lt;/p&gt;
&lt;h2 id=&quot;getting-the-data-in-and-out&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#getting-the-data-in-and-out&quot; aria-label=&quot;Anchor link for: getting-the-data-in-and-out&quot;&gt;Getting the data in and out&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The half-sentence &quot;once it has all the data&quot; hides an assumption that is easy to miss: that the data can get into the machine at all. Algorithms of the HHL family deliver their exponential speedup only if the input is already available as a quantum state. Loading trillions of classical sensor readings into the amplitudes of a quantum register is the state preparation problem, and the standard answer to it, a quantum random access memory (QRAM)&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#giovannetti2008&quot;&gt;11&lt;/a&gt;&lt;/sup&gt;, exists essentially on paper: the circuits it requires are deep and error-prone, and nobody has built one at scale. Aaronson pointed at exactly this fine print years ago: if loading the data costs as much as solving the problem classically, the quantum advantage evaporates&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#aaronson2015&quot;&gt;12&lt;/a&gt;&lt;/sup&gt;. So the sci-fi computer does not fail at the computation first, it fails at the loading dock, and by the time the state is prepared, the weather has moved on.&lt;/p&gt;
&lt;p&gt;The other end is not free either. If the algorithm produces a quantum state that encodes the solution field, a measurement collapses it and yields only a limited amount of classical information per run. For an expectation value that is fine, for a complete weather field it is not. To be fair, the QRC line has learned to work around the collapse where it hurts most: weak measurement protocols read the reservoir gently enough that the computation can keep running&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#mujal2023&quot;&gt;13&lt;/a&gt;&lt;/sup&gt;, and feedback-driven QRC performs projective measurements but feeds the results straight back into the circuit as new rotation angles, keeping the memory of the system alive&lt;sup class=&quot;footnote-reference&quot;&gt;&lt;a href=&quot;#kobayashi2024&quot;&gt;14&lt;/a&gt;&lt;/sup&gt;. Both tricks keep a running forecast going. Neither hands you the full solution vector of a differential equation.&lt;/p&gt;
&lt;p&gt;And then there is the initial data itself. Chaos means sensitive dependence on initial conditions: $\delta(t) \approx \delta(0)\,e^{\lambda t}$ with a positive Lyapunov exponent. To predict $T$ time steps ahead you need roughly $\lambda T$ additional bits of precision in the input. This holds for every computer, classical or quantum. &quot;Having all the data&quot; does not physically exist.&lt;/p&gt;
&lt;p&gt;This is exactly the premise Laplace&#39;s demon already hangs on. The demon could own perfect instruments and still fail: the required precision grows without bound with the forecast horizon, so &quot;all the data&quot; would have to mean arbitrarily many decimal places. The premise breaks classically, no quantum mechanics required. With that, the foundation of &quot;once it has all the data&quot; is gone, with or without a quantum computer.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Back to the original question: are there quantum algorithms for chaotic systems, and where does the novel&#39;s idea break? They exist, and they are an active field of research, with Carleman linearization as the common thread. But the gap between &quot;can efficiently simulate other quantum systems&quot; and &quot;predicts the weather once it has all the data&quot; does not hang on the hardware. It hangs on two mathematical facts: linear time evolution meets nonlinear chaos, and the initial conditions would require a precision that does not physically exist. Both are structural, and structure does not yield to better hardware. On top of them sit the practical barriers at both ends, loading the data without a QRAM and reading the solution out through measurements: those are engineering, but engineering nobody has done.&lt;/p&gt;
&lt;p&gt;That is also what makes the novel&#39;s idea interesting: it points at real research, but the half-sentence &quot;once it has all the data&quot; quietly defines both problems away. The experiment above makes the first one visible, the second sits in a single exponential function. An afternoon of code is enough to make a whole novel&#39;s premise wobble.&lt;/p&gt;
&lt;h2 id=&quot;sources&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#sources&quot; aria-label=&quot;Anchor link for: sources&quot;&gt;Sources&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;liu2021&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;/sup&gt;
&lt;p&gt;Liu, Kolden, Krovi, Loureiro, Trivisa, Childs: &lt;em&gt;Efficient quantum algorithm for dissipative nonlinear differential equations&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://www.pnas.org/doi/10.1073/pnas.2026805118&quot;&gt;PNAS 118, e2026805118 (2021)&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;tennie2025&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;/sup&gt;
&lt;p&gt;Tennie, Laizet, Lloyd, Magri: &lt;em&gt;Quantum computing for nonlinear differential equations and turbulence&lt;/em&gt;, Nature Reviews Physics 7, 220 (2025).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;gonzalez2025&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;/sup&gt;
&lt;p&gt;Gonzalez-Conde, Lewis, Bharadwaj, Sanz: &lt;em&gt;Quantum Carleman linearization efficiency in nonlinear fluid dynamics&lt;/em&gt;, Phys. Rev. Research 7, 023254 (2025). &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2410.23057&quot;&gt;arXiv:2410.23057&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;sanavio2024&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;/sup&gt;
&lt;p&gt;Sanavio, Succi: &lt;em&gt;Lattice Boltzmann-Carleman quantum algorithm and simulation of the 1D Burgers equation at moderate Reynolds number&lt;/em&gt;, AVS Quantum Science 6 (2024).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;endo2024&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;/sup&gt;
&lt;p&gt;Endo, Takahashi: &lt;em&gt;Divergence-free algorithms for solving nonlinear differential equations on quantum computers&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2411.16233&quot;&gt;arXiv:2411.16233&lt;/a&gt; (2024).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;wang2025&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;6&lt;/sup&gt;
&lt;p&gt;Wang et al.: &lt;em&gt;Quantum Algorithms for Nonlinear Differential Equations via Pivot-Shifted Carleman Linearization&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2605.20071&quot;&gt;arXiv:2605.20071&lt;/a&gt; (2025).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;lewis2024&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;7&lt;/sup&gt;
&lt;p&gt;Lewis, Eidenbenz, Nadiga, Subaşı: &lt;em&gt;Limitations for Quantum Algorithms to Solve Turbulent and Chaotic Systems&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://quantum-journal.org/papers/q-2024-10-24-1509/&quot;&gt;Quantum 8, 1509 (2024)&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;ahmed2024&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;8&lt;/sup&gt;
&lt;p&gt;Ahmed, Tennie, Magri: &lt;em&gt;Prediction of chaotic dynamics and extreme events: A recurrence-free quantum reservoir computing approach&lt;/em&gt;, Phys. Rev. Research 6, 043082 (2024). &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2405.03390&quot;&gt;arXiv:2405.03390&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;steinegger2025&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;9&lt;/sup&gt;
&lt;p&gt;Steinegger, Räth: &lt;em&gt;Predicting three-dimensional chaotic systems with four qubit quantum systems&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2501.15191&quot;&gt;arXiv:2501.15191&lt;/a&gt; (2025).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;connerty2025&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;10&lt;/sup&gt;
&lt;p&gt;Connerty, Evans, Angelatos, Narayanan: &lt;em&gt;Quantum Observers: A NISQ Hardware Demonstration of Chaotic State Prediction Using Quantum Echo-state Networks&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2505.06799&quot;&gt;arXiv:2505.06799&lt;/a&gt; (2025).&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;giovannetti2008&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;11&lt;/sup&gt;
&lt;p&gt;Giovannetti, Lloyd, Maccone: &lt;em&gt;Quantum Random Access Memory&lt;/em&gt;, Phys. Rev. Lett. 100, 160501 (2008). &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/0708.1879&quot;&gt;arXiv:0708.1879&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;aaronson2015&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;12&lt;/sup&gt;
&lt;p&gt;Aaronson: &lt;em&gt;Read the fine print&lt;/em&gt;, Nature Physics 11, 291 (2015). &lt;a rel=&quot;external&quot; href=&quot;https://www.scottaaronson.com/papers/qml.pdf&quot;&gt;PDF&lt;/a&gt;&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;mujal2023&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;13&lt;/sup&gt;
&lt;p&gt;Mujal, Martínez-Peña, Giorgi, Soriano, Zambrini: &lt;em&gt;Time-series quantum reservoir computing with weak and projective measurements&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://www.nature.com/articles/s41534-023-00682-z&quot;&gt;npj Quantum Information 9, 16 (2023)&lt;/a&gt;.&lt;/p&gt;
&lt;/div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;kobayashi2024&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;14&lt;/sup&gt;
&lt;p&gt;Kobayashi, Fujii, Yamamoto: &lt;em&gt;Feedback-driven quantum reservoir computing for time-series analysis&lt;/em&gt;, &lt;a rel=&quot;external&quot; href=&quot;https://arxiv.org/abs/2406.15783&quot;&gt;arXiv:2406.15783&lt;/a&gt; (2024).&lt;/p&gt;
&lt;/div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Algorithms Belong in the Slice, Not the Container</title>
        <published>2026-06-14T00:00:00+00:00</published>
        <updated>2026-06-14T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/algorithms-belong-in-the-slice-not-the-container/"/>
        <id>https://lmilz.dev/blog/algorithms-belong-in-the-slice-not-the-container/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/algorithms-belong-in-the-slice-not-the-container/">&lt;p&gt;I have spent the last few weeks building a small mini-STL in C++. This is a learning project: the point is not a library that competes with &lt;code&gt;std&lt;/code&gt;, but understanding the design decisions behind containers, views and algorithms by building them myself. I am a big fan of the method-chaining style: &lt;code&gt;my_array.sort().find(5)&lt;/code&gt;. That raised an interesting question: &lt;em&gt;where do the algorithms live?&lt;/em&gt; In the C++ STL they are offered as free functions over pairs of iterators. That lets you implement an algorithm once and apply it to any range. Rust takes a different route with slices. My question was: how does the slice concept carry over to C++?&lt;/p&gt;
&lt;p&gt;The answer sits in a 76-line class:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; noexcept&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;value_type&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; size_type&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; noexcept&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;        :&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; data_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; len_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    [[&lt;/span&gt;&lt;span&gt;nodiscard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; size_type&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; len_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; value_type&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; begin&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; data_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; value_type&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; end&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; data_&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; len_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; value_type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; operator&lt;/span&gt;&lt;span&gt;[]&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;size_type&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; index&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; data_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;index&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    [[&lt;/span&gt;&lt;span&gt;nodiscard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SortedSlice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;        // Insertion sort, in place ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SortedSlice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;data_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; len_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Option&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;size_type&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; find&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; value_type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt; ... &lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; contains&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; value_type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt; ... &lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;Slice&amp;lt;T&amp;gt;&lt;/code&gt; owns nothing. It is nothing more than a pair of pointer and length. But this is where all the algorithms live. Not in the containers that hold the data. Exactly one place.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;static_array&lt;/code&gt; simply hands out its internal storage as a slice:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; as_slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;data_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is Rust&#39;s &lt;code&gt;&amp;amp;[T]&lt;/code&gt; deref coercion in C++ syntax. The consequence: &lt;code&gt;contains&lt;/code&gt;, &lt;code&gt;find&lt;/code&gt; and &lt;code&gt;sort&lt;/code&gt; are written once and usable by any container that can hand out a slice. Add another contiguous container later and it gets search and sort for free, no algorithm code duplicated. &lt;code&gt;static_array::sort()&lt;/code&gt; itself is therefore only three lines:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SortedArray&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; static_array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    auto&lt;/span&gt;&lt;span&gt; copy&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; = *&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    static_cast&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;copy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;as_mut_slice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;());&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // the slice does the work&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SortedArray&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;(&lt;/span&gt;&lt;span&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;move&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;copy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The container makes a copy, hands out a mutable slice over it, and delegates. No sorting algorithm in the container itself. The STL solves this differently: algorithms in &lt;code&gt;&amp;lt;algorithm&amp;gt;&lt;/code&gt; as free functions over iterator pairs. A different abstraction with a different trade-off, the length is not bundled in, it follows from &lt;code&gt;end - begin&lt;/code&gt;. When you build it yourself you have to commit to &lt;em&gt;one&lt;/em&gt; variant and feel in your own code what it costs.&lt;/p&gt;
&lt;p&gt;The obvious follow-up: which containers does this even work for? It hangs on a single property, is the memory contiguous or not. From that a clear split follows.&lt;/p&gt;
&lt;p&gt;Slice heirs are all containers that allocate contiguously: a fixed-size or growable array, the dense matrices and vectors of a linear-algebra type, the slots of an open-addressing hash table. Whatever the structure, as long as its elements sit back to back in memory it can hand out a slice and inherits every algorithm for free.&lt;/p&gt;
&lt;p&gt;Adapters like &lt;code&gt;stack&lt;/code&gt; and &lt;code&gt;queue&lt;/code&gt; build on a container and deliberately restrict the interface. They do not hand out a slice, because &lt;code&gt;push&lt;/code&gt;, &lt;code&gt;pop&lt;/code&gt; and &lt;code&gt;top&lt;/code&gt; make sense precisely when the container is &lt;em&gt;not&lt;/em&gt; sortable and not freely indexable. A stack that hands out a slice is no longer a stack. This is the point where the rule &quot;write once, use everywhere&quot; deliberately stops.&lt;/p&gt;
&lt;p&gt;Iterator containers are the structures whose memory is not contiguous: linked lists, trees without an array backing, graphs with adjacency lists, hash maps with separate chaining. Here a fat pointer is pointless, because it would have to point at several scattered regions. Instead of a slice an iterator steps in, closing off the layer downward cleanly. Rust shows the same convention: contiguous containers implement &lt;code&gt;Deref&amp;lt;Target=[T]&amp;gt;&lt;/code&gt; and thereby hand out &lt;code&gt;&amp;amp;[T]&lt;/code&gt;, pointer-backed structures only offer an &lt;code&gt;Iterator&lt;/code&gt;. The two are not mutually exclusive: &lt;code&gt;Vec&amp;lt;T&amp;gt;&lt;/code&gt; has both, because its iterator is internally the same pointer arithmetic over the same memory.&lt;/p&gt;
&lt;p&gt;From that follows the question I ask myself for every new container: array-backed or pointer-backed? Array-backed means slice for free, cache-friendly, with size limits. Pointer-backed means iterator API, more flexible. Both are legitimate, mixing them is the trap: a non-contiguous container that hands out a slice lies about its memory layout. This question has become the first one I ask, before I even start writing a new container.&lt;/p&gt;
&lt;p&gt;What surprised me most along the way is how far the principle stretches. &lt;code&gt;static_array::sort()&lt;/code&gt; returns no sorted &lt;code&gt;static_array&lt;/code&gt;, but a new type:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[[&lt;/span&gt;&lt;span&gt;nodiscard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SortedArray&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; sort&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;And &lt;code&gt;binary_search&lt;/code&gt; is available only on that type:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;size_t&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SortedArray&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    constexpr&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; BinarySearchResult&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; binary_search&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; value&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // sort() does not exist here anymore&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;binary_search&lt;/code&gt; on an unsorted array silently violates the precondition of the algorithm: no compiler error, no crash, just a wrong result. The STL solves this by convention: you just sort beforehand, and the compiler does not help. In my implementation &lt;code&gt;binary_search&lt;/code&gt; is simply not a member of &lt;code&gt;static_array&lt;/code&gt;. It is only available on &lt;code&gt;SortedArray&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;binary_search&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;          // compile error: no member&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;arr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;sort&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;().&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;binary_search&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;42&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;   // OK&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That is exactly the point where the type system makes an assumption of the program logic &lt;em&gt;visible&lt;/em&gt;. Rust&#39;s &lt;code&gt;[T]::binary_search()&lt;/code&gt; shows the alternative: the method exists on every slice, the precondition lies with the caller, the compiler does not check it. The newtype approach, which enforces the same contract at the type level, you find in Rust as a deliberate design pattern, not as a stdlib feature. Building it yourself, you grasp &lt;em&gt;why&lt;/em&gt; that is so. Using the STL, it stays a good tip from a book.&lt;/p&gt;
&lt;p&gt;Both observations are ultimately one: build the right abstraction layer, and the algorithms come for free. Not because my 76-line Slice class is better than &lt;code&gt;std::span&lt;/code&gt;. But because at every line you make a design decision that stays invisible in the STL. Building the slice myself was the fastest way not just to know Rust&#39;s &lt;code&gt;&amp;amp;[T]&lt;/code&gt; deref coercion, but to understand why it exists.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Synapse: A Knowledge Graph for the Whole Monorepo</title>
        <published>2026-06-01T00:00:00+00:00</published>
        <updated>2026-06-01T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/synapse-a-knowledge-graph-for-the-whole-monorepo/"/>
        <id>https://lmilz.dev/blog/synapse-a-knowledge-graph-for-the-whole-monorepo/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/synapse-a-knowledge-graph-for-the-whole-monorepo/">&lt;p&gt;Everything in my second brain lives in one repository: Zettelkasten notes, a compiled wiki, journal entries, C++ projects, Rust libraries, Bazel build files, blog drafts. A thought and the code that proves it belong in the same place, &lt;a href=&quot;/blog/code-is-a-thinking-tool-not-an-artifact/&quot;&gt;and I have written about that before&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Obsidian, which I use to navigate the notes, only sees the Markdown half. A note about cache line alignment links to a wiki page on CPU architecture, which touches the same topic as a C++ file that implements a memory pool, which is built by a &lt;code&gt;BUILD&lt;/code&gt; file that depends on a benchmark library. Obsidian sees two of those connections. The rest are invisible.&lt;/p&gt;
&lt;p&gt;I built Synapse to make the whole graph visible. It crawls a repository, extracts references from every file type it understands, resolves them to concrete paths, and serves a force-directed graph over a small HTTP API. No database, no external Go dependencies. D3.js is embedded in the binary; the graph view works offline without any CDN.&lt;/p&gt;
&lt;h2 id=&quot;what-gets-indexed&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-gets-indexed&quot; aria-label=&quot;Anchor link for: what-gets-indexed&quot;&gt;What Gets Indexed&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The core question for a tool like this is: what counts as a &quot;reference&quot;?&lt;/p&gt;
&lt;p&gt;For Markdown files, there are two kinds: standard links &lt;code&gt;[text](path)&lt;/code&gt; and Obsidian-style wiki-links &lt;code&gt;note name&lt;/code&gt;. For C and C++ files, local &lt;code&gt;#include &quot;header.h&quot;&lt;/code&gt; directives. For Rust, &lt;code&gt;use crate::module&lt;/code&gt; and &lt;code&gt;mod submodule&lt;/code&gt; declarations. For Bazel &lt;code&gt;BUILD&lt;/code&gt; files, the &lt;code&gt;deps&lt;/code&gt;, &lt;code&gt;srcs&lt;/code&gt;, &lt;code&gt;data&lt;/code&gt;, and similar attributes.&lt;/p&gt;
&lt;p&gt;The extractor design reflects this. Each file type gets one or more &lt;code&gt;Extractor&lt;/code&gt; implementations:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Extractor&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; interface&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    Extract&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;content&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) []&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;RawRef&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;A &lt;code&gt;RawRef&lt;/code&gt; is the literal text from the source file, before any path resolution. &lt;code&gt;some-note&lt;/code&gt; in Markdown becomes &lt;code&gt;RawRef{Target: &quot;some-note&quot;, Type: EdgeWikiLink}&lt;/code&gt;. &lt;code&gt;#include &quot;util/math.h&quot;&lt;/code&gt; becomes &lt;code&gt;RawRef{Target: &quot;util/math.h&quot;, Type: EdgeCInclude}&lt;/code&gt;. The extractor does not know where the file lives; it only reads bytes and returns what it finds.&lt;/p&gt;
&lt;p&gt;The dispatch is by extension for most types, and by exact filename for cases where the extension alone is not enough:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;var&lt;/span&gt;&lt;span&gt; ByExtension&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;][]&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Extractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;md&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:  {&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;markdownLinkExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{},&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; wikilinkExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;cpp&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;cIncludeExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;rs&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:  {&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;rustUseExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{},&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; rustModExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;bzl&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;bazelDepExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Extension-less files need separate handling.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;var&lt;/span&gt;&lt;span&gt; ByName&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;][]&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Extractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;BUILD&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:     {&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;bazelDepExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;    &amp;quot;WORKSPACE&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;: {&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;bazelDepExtractor&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{}},&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The worker pool reads each file and dispatches to both maps:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; ex&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; range&lt;/span&gt;&lt;span&gt; extract&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ByExtension&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;ext&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    refs&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;refs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; ex&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;Extract&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;content&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; ex&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; range&lt;/span&gt;&lt;span&gt; extract&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;ByName&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;filepath&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;Base&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)] {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    refs&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; append&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;refs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; ex&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;Extract&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;content&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;...&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This turned out to be necessary because &lt;code&gt;BUILD&lt;/code&gt; files have no extension, and a lookup in &lt;code&gt;ByExtension[&quot;&quot;]&lt;/code&gt; would match any extension-less file in the repository.&lt;/p&gt;
&lt;h2 id=&quot;no-graph-database&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#no-graph-database&quot; aria-label=&quot;Anchor link for: no-graph-database&quot;&gt;No Graph Database&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The graph itself is a plain in-memory structure. Two maps: paths to node pointers, edge keys to edge pointers. A sync.RWMutex for concurrent ingestion.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Node&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Path&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;     string&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;   `json:&amp;quot;path&amp;quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    FileType&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; string&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;   `json:&amp;quot;file_type&amp;quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Tags&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;     []&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; `json:&amp;quot;tags,omitempty&amp;quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Outgoing&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Edge&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;   `json:&amp;quot;outgoing&amp;quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Incoming&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Edge&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;   `json:&amp;quot;incoming&amp;quot;`&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; BrainGraph&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; struct&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    nodes&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Node&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    edges&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; map&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;string&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Edge&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    mu&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    sync&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;RWMutex&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Each node holds its own adjacency lists. After the crawl, the graph is read-only and the API serves from it directly. For a repository of 1,500 files, this fits comfortably in a few megabytes.&lt;/p&gt;
&lt;p&gt;The crawl itself uses a worker pool. Walking the file tree and reading files is I/O bound; multiple goroutines keep the disk busy while others wait. The workers return &lt;code&gt;fileResult&lt;/code&gt; structs over a channel; the main goroutine collects results and adds edges to the graph after all workers finish:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;jobs&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    :=&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;chan&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; fileJob&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;files&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;results&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;chan&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; fileResult&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;files&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;Workers&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    wg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;Add&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    go&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; crawlWorker&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; jobs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; results&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;wg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Collect after workers close results channel&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; res&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; range&lt;/span&gt;&lt;span&gt; results&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; ref&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; range&lt;/span&gt;&lt;span&gt; res&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;refs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        target&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; resolved&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span&gt; resolver&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;ResolveRawRef&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ref&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; res&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        g&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;AddEdge&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;graph&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Edge&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;span&gt; Source&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;span&gt; res&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;path&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; Target&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;span&gt; target&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ...&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; })&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Resolution happens centrally because it needs the full file tree. A wiki-link &lt;code&gt;Rust Ownership&lt;/code&gt; needs to be matched against all indexed paths to find &lt;code&gt;zettelkasten/Rust Ownership.md&lt;/code&gt;. The extractor only sees one file at a time and cannot do this.&lt;/p&gt;
&lt;h2 id=&quot;three-bugs-that-only-real-data-reveals&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#three-bugs-that-only-real-data-reveals&quot; aria-label=&quot;Anchor link for: three-bugs-that-only-real-data-reveals&quot;&gt;Three Bugs That Only Real Data Reveals&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I wrote tests for each extractor. They all passed. Then I ran the crawler against my actual repository and found three classes of errors that no unit test had caught.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;C++ attributes look like wiki-links.&lt;/strong&gt; My Zettelkasten notes contain C++ code examples in fenced blocks. A note about &lt;code&gt;nodiscard&lt;/code&gt; as a design technique wrote it in a code block. The extractor ran the wiki-link regex over the raw bytes and added 113 edges to nonexistent nodes: &lt;code&gt;nodiscard&lt;/code&gt;, &lt;code&gt;likely&lt;/code&gt;, &lt;code&gt;unlikely&lt;/code&gt;, and one memorable &lt;code&gt;gnu::target_clones(&quot;sse4.2,avx2,avx512f,default&quot;)&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The fix was to strip code regions before running the regex, while preserving all newline characters so that byte offsets for line numbers stayed valid:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;go&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;func&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; stripCode&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;src&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; []&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) []&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    dst&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :=&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;byte&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;src&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    copy&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; src&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Replace fenced blocks and inline spans with spaces.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Newlines are never touched, so line counts stay correct.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; dst&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;filepath.Ext&lt;/code&gt; has an opinion about book titles.&lt;/strong&gt; The wiki-link resolver builds a lookup table of &lt;code&gt;lowercase-basename-without-extension -&amp;gt; file path&lt;/code&gt;. A note titled &lt;code&gt;Robert C. Martin - Clean Code.md&lt;/code&gt; should be resolvable by &lt;code&gt;Robert C. Martin - Clean Code&lt;/code&gt;. It was not.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;filepath.Ext(&quot;Robert C. Martin - Clean Code&quot;)&lt;/code&gt; returns &lt;code&gt;. Martin - Clean Code&lt;/code&gt;, because &quot;the extension is the suffix beginning at the last dot in the last element of the path.&quot; The last dot in that title is the one after &lt;code&gt;C&lt;/code&gt;. The lookup key computed from the link target was &lt;code&gt;&quot;robert c&quot;&lt;/code&gt;, not &lt;code&gt;&quot;robert c. martin - clean code&quot;&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The fix: try the full lowercased basename as a key first, fall back to extension-stripping only if that fails. This handles both note titles with dots and explicit &lt;code&gt;note.md&lt;/code&gt;-style references.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Bazel labels have colons.&lt;/strong&gt; The Bazel extractor used a regex that excluded colons from label strings: &lt;code&gt;[^&quot;:]+&lt;/code&gt;. Bazel&#39;s label syntax is &lt;code&gt;//package:target&lt;/code&gt;. Every cross-package dependency was silently dropped. The fix was a regex that matches the actual label grammar rather than &quot;everything that is not a quote or colon.&quot;&lt;/p&gt;
&lt;h2 id=&quot;tags-need-to-be-nodes&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#tags-need-to-be-nodes&quot; aria-label=&quot;Anchor link for: tags-need-to-be-nodes&quot;&gt;Tags Need to Be Nodes&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/Synapse_tags.png&quot; alt=&quot;Synapse graph view with tag nodes enabled&quot; /&gt;&lt;/p&gt;
&lt;p&gt;The first version of the graph rendered 1,489 nodes with a standard D3 &lt;code&gt;forceSimulation&lt;/code&gt;. The result was indistinguishable from a scatterplot. Every node drifted away from every other node with equal force. No clusters, no structure.&lt;/p&gt;
&lt;p&gt;The Obsidian graph, visualizing the same notes, shows clear topic regions: a dense area for &lt;code&gt;#rust&lt;/code&gt; notes, another for &lt;code&gt;#embedded&lt;/code&gt;, a looser cloud for &lt;code&gt;#journal&lt;/code&gt; entries. Obsidian treats tags as nodes.&lt;/p&gt;
&lt;p&gt;Every note with &lt;code&gt;tags: [rust, embedded]&lt;/code&gt; has two edges in Obsidian&#39;s graph: one to a &lt;code&gt;rust&lt;/code&gt; node, one to an &lt;code&gt;embedded&lt;/code&gt; node. Notes sharing tags end up near each other because they are all connected to the same anchor. The clustering is an emergent property of the graph structure, not a layout algorithm.&lt;/p&gt;
&lt;p&gt;Synapse extracts tags from YAML frontmatter for each Markdown file and includes them in the API response. The frontend creates virtual tag nodes from the frequency map and adds note-to-tag edges to the simulation. No tag data is stored server-side beyond the &lt;code&gt;tags&lt;/code&gt; field on each node.&lt;/p&gt;
&lt;p&gt;The other piece is &lt;code&gt;distanceMax&lt;/code&gt; on &lt;code&gt;forceManyBody&lt;/code&gt;. Without it, D3&#39;s Barnes-Hut approximation treats distant clusters as combined bodies and generates a global outward pressure that blows every cluster apart. With &lt;code&gt;distanceMax(200)&lt;/code&gt;, nodes only repel their local neighborhood and distant clusters do not interact at all. [A separate post covers the D3 physics in more detail.][d3-post]&lt;/p&gt;
&lt;p&gt;Running against my repository: 1,082 of 1,134 Markdown files have tags, 240 unique tags, top tags are &lt;code&gt;#learning&lt;/code&gt; (361 notes), &lt;code&gt;#journal&lt;/code&gt; (176), &lt;code&gt;#software-engineering&lt;/code&gt; (149), &lt;code&gt;#rust&lt;/code&gt; (138), &lt;code&gt;#embedded&lt;/code&gt; (123). The graph now shows these as distinct regions with overlap where topics intersect.&lt;/p&gt;
&lt;h2 id=&quot;what-it-shows-that-obsidian-does-not&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-it-shows-that-obsidian-does-not&quot; aria-label=&quot;Anchor link for: what-it-shows-that-obsidian-does-not&quot;&gt;What It Shows That Obsidian Does Not&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The most useful output is the stats.&lt;/p&gt;
&lt;p&gt;My repository has 2,204 edges across 1,489 nodes. 699 of those edges point to files that do not exist: wiki-links to notes I referenced but have not written yet, C++ headers that are system includes, Rust crate references that resolve to external packages. These are knowingly unresolved and that is fine. But examining them surfaces planned notes, gaps in the knowledge base, and occasionally a broken link that used to point somewhere.&lt;/p&gt;
&lt;p&gt;There are also 673 orphan nodes: files with no incoming or outgoing edges at all. Most are blog drafts and README files. Some are Zettelkasten notes that have never been linked from anywhere. Seeing them listed is useful: either they should be linked or they should not exist.&lt;/p&gt;
&lt;p&gt;The top hub in my repository is &lt;code&gt;zettelkasten/Zettelkasten Methodik.md&lt;/code&gt; with degree 87: 71 notes link to it, and it links to 16 others. The second is a wiki page on software architecture. These are the conceptual load-bearing nodes of the second brain, and they are not obvious when browsing the vault file by file.&lt;/p&gt;
&lt;p&gt;The connected components count is less reassuring: 877 separate components, meaning the graph is nowhere close to a single connected whole. Most notes live in small clusters of 2 to 10 nodes with no path to the rest.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/Synapse_folder.png&quot; alt=&quot;Synapse graph view with directory clustering&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;running-it&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#running-it&quot; aria-label=&quot;Anchor link for: running-it&quot;&gt;Running It&lt;/a&gt;&lt;/h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;go&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; install codeberg.org/lmilz/synapse/cmd/synapse@latest&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;synapse&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; serve /path/to/repo&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The server starts on port 8080. The graph view loads the full index, builds tag nodes in the browser, and lets you filter by file type, minimum degree, or search. All data lives in the Go binary at runtime; there is no configuration file, no schema, no migration.&lt;/p&gt;
&lt;p&gt;The source is at &lt;a rel=&quot;external&quot; href=&quot;https://github.com/lmilz/synapse&quot;&gt;githut.com/lmilz/synapse&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://codeberg.org/lmilz/synapse&quot;&gt;codeberg.org/lmilz/synapse&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Code is a Thinking Tool, Not an Artifact</title>
        <published>2026-05-18T00:00:00+00:00</published>
        <updated>2026-05-18T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/code-is-a-thinking-tool-not-an-artifact/"/>
        <id>https://lmilz.dev/blog/code-is-a-thinking-tool-not-an-artifact/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/code-is-a-thinking-tool-not-an-artifact/">&lt;p&gt;Since school I have had a problem with notes. Taking legible notes during a lecture was never possible for me, so I would rewrite everything neatly in the evening. Digital notes, a tablet with a stylus, OneNote: I tried a lot. But none of it ever truly convinced me.&lt;/p&gt;
&lt;p&gt;When my second son was born I had a month off and read Tiago Forte&#39;s book: &lt;em&gt;How to Build a Second Brain&lt;/em&gt;. PARA and CODE clicked immediately. Finally a framework that made sense. But the longer I worked with it, the clearer the problem became:&lt;/p&gt;
&lt;p&gt;All of these approaches treat knowledge as text.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;As a developer, my knowledge also lives in code.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;PARA structures by purpose, not topic: not &quot;where does this belong?&quot; but &quot;which project needs it?&quot; CODE keeps information moving from capture to expression. Information serves a purpose, or it stays out. Right foundation. But only for text.&lt;/p&gt;
&lt;h2 id=&quot;the-difference-between-describing-and-understanding&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-difference-between-describing-and-understanding&quot; aria-label=&quot;Anchor link for: the-difference-between-describing-and-understanding&quot;&gt;The Difference Between Describing and Understanding&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;PARA answers: &quot;What do I use this for?&quot;
When developing, I am often more interested in: &quot;What happens when I change this?&quot;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Code is not an artifact, it is a thinking tool.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;I have a note that says: use a tag type to distinguish iterators between containers. But six months later I could not reconstruct why.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Why not just: using iterator = T*?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// std::iterator_traits&amp;lt;T*&amp;gt; is specialized, all STL algorithms work.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// But then vector&amp;lt;int&amp;gt;::iterator and static_array&amp;lt;int, 5&amp;gt;::iterator&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// are the same type — the compiler cannot catch misuse across containers.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;//&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Fix: a Tag parameter. Empty struct, private to the container.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Zero runtime cost, the compiler erases it entirely.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Each container gets a distinct iterator type. Misuse becomes a compile error.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;template&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;typename&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; typename&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Tag&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; bool&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; IsConst&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; pointer_iterator&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Declared inside static_array — no outside type can share it:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; static_array&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; iterator_tag&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    using&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; iterator&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; pointer_iterator&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;T&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; iterator_tag&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; false&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The comments are the note. The code is the proof. But that only works if the note and the code can actually live together.&lt;/p&gt;
&lt;h2 id=&quot;code-as-a-first-class-citizen&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#code-as-a-first-class-citizen&quot; aria-label=&quot;Anchor link for: code-as-a-first-class-citizen&quot;&gt;Code as a First-Class Citizen&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;For a long time I had everything across separate repos: blog, journal, Zettelkasten, small projects. A note about a data structure and the corresponding code lived in different places. Today everything is in a monorepo. A thought and the code that proves it live in the same repository, often in the same commit.&lt;/p&gt;
&lt;p&gt;Kepano, the CEO of Obsidian, called it &quot;File over App.&quot; Knowledge does not belong in a tool, it belongs in files you own and can read without anyone running a server. That applies to Markdown just as much as to code, with the difference that code can be executed.&lt;/p&gt;
&lt;h2 id=&quot;writing-and-seeing&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#writing-and-seeing&quot; aria-label=&quot;Anchor link for: writing-and-seeing&quot;&gt;Writing and Seeing&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My current setup uses two tools, not one. Neovim for writing: fast, close to the code, with the movements I have known for years. Obsidian for the graph view: I see which notes are connected, which are isolated, where clusters form. That overview I cannot get in Neovim.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/Obsidian_Graph.png&quot; alt=&quot;Obsidian Vault Graph View&quot; /&gt;&lt;/p&gt;
&lt;p&gt;For a long time I thought I had to choose: Obsidian or Neovim. Every time the question was: which tool is the right one?&lt;/p&gt;
&lt;p&gt;The question was wrong. As long as my notes are plain Markdown in a Git repo, I can use both tools in parallel. Neovim writes, Obsidian shows, both work on the same files. The repo is the source, the tools are views on it.&lt;/p&gt;
&lt;p&gt;At the start of this post I wrote that for a long time I could not name why none of the earlier systems worked. Today I can: because they made the tool the source, not the file.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Wrapping a C library in RAII: unique_ptr with custom deleters and why Subsystem can&#39;t be moveable</title>
        <published>2026-05-15T00:00:00+00:00</published>
        <updated>2026-05-15T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/wrapping-a-c-library-in-raii-unique-ptr-with-custom-deleters-and-why-subsystem-canot-be-moveable/"/>
        <id>https://lmilz.dev/blog/wrapping-a-c-library-in-raii-unique-ptr-with-custom-deleters-and-why-subsystem-canot-be-moveable/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/wrapping-a-c-library-in-raii-unique-ptr-with-custom-deleters-and-why-subsystem-canot-be-moveable/">&lt;p&gt;I got into the habit of wrapping C libraries before I start working with them. Not out of principle, but because I have seen the same mistakes often enough.&lt;/p&gt;
&lt;p&gt;C has no RAII. Every resource you create, you have to release yourself. One function returns a pointer, another takes it back to free it. Between those two calls sits all the code that can go wrong. That leads to three recurring bugs: leaks on early return, when a later call fails and the cleanup code is never reached; double-free, when a pointer gets copied by accident and destroyed twice; and unclear ownership, because the return type alone does not say who is responsible for cleanup.&lt;/p&gt;
&lt;p&gt;SDL2 is a typical C library in this situation, and it serves as the concrete example here. &lt;code&gt;std::unique_ptr&lt;/code&gt; with a custom deleter solves all three. The trick is connecting the SDL type to its cleanup function:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Deleter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Window&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;   p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDL_DestroyWindow&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);   }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Renderer&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDL_DestroyRenderer&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;); }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Texture&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;  p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDL_DestroyTexture&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Surface&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;  p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const noexcept&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDL_FreeSurface&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;p&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);     }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; WindowPtr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;   =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Window&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;   Deleter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RendererPtr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Renderer&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Deleter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; TexturePtr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Texture&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;  Deleter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SurfacePtr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;  =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;SDL_Surface&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;  Deleter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;One type, four resources. &lt;code&gt;unique_ptr&lt;/code&gt; picks the right overload based on the template parameter. The overhead is zero: empty structs with &lt;code&gt;operator()&lt;/code&gt; are eliminated via Empty Base Optimization. The &lt;code&gt;noexcept&lt;/code&gt; makes explicit what SDL already guarantees through its C ABI, and gives the compiler room for better optimizations.&lt;/p&gt;
&lt;p&gt;Direct construction is not quite enough, though. When &lt;code&gt;SDL_CreateWindow&lt;/code&gt; fails, it returns &lt;code&gt;nullptr&lt;/code&gt; and sets an internal error string. The caller has to check afterward, and &lt;code&gt;SDL_GetError()&lt;/code&gt; must be read immediately after the failing call, because later SDL calls overwrite it. A factory function handles this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[[&lt;/span&gt;&lt;span&gt;nodiscard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]]&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; inline&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; WindowPtr&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; make_window&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string title&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                                           uint32_t&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; uint32_t&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                                           uint32_t&lt;/span&gt;&lt;span&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; uint32_t&lt;/span&gt;&lt;span&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                                           uint32_t&lt;/span&gt;&lt;span&gt; flags&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    auto*&lt;/span&gt;&lt;span&gt; raw&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDL_CreateWindow&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;title&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;c_str&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(),&lt;/span&gt;&lt;span&gt; x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; flags&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!&lt;/span&gt;&lt;span&gt;raw&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; throw&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDLError&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;SDL_CreateWindow&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; WindowPtr&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;raw&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Either &lt;code&gt;make_window&lt;/code&gt; returns a valid object, or it throws. No null check at the call site, no chance of an invalid &lt;code&gt;unique_ptr&lt;/code&gt; making it into the rest of the code.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;SDL_Init&lt;/code&gt; and &lt;code&gt;SDL_Quit&lt;/code&gt; are a special case: they have process-wide effect and do not fit into &lt;code&gt;unique_ptr&lt;/code&gt;. A dedicated class is more direct:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Subsystem&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    explicit&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Subsystem&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;uint32_t&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; flags&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;SDL_Init&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;flags&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; !=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; throw&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDLError&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;SDL_Init&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    ~Subsystem&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SDL_Quit&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(); }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    Subsystem&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Subsystem&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;            =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; delete&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Subsystem&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; operator&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Subsystem&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; delete&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    Subsystem&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Subsystem&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;                 =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; delete&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Subsystem&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; operator&lt;/span&gt;&lt;span&gt;=&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Subsystem&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;      =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; delete&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;code&gt;Subsystem&lt;/code&gt; is not moveable because &lt;code&gt;SDL_Quit&lt;/code&gt; affects the whole process. A moved-from object could call &lt;code&gt;SDL_Quit&lt;/code&gt; again on destruction while other parts of the program are still using SDL. Deleting the move operations means there is exactly one instance in exactly one place.&lt;/p&gt;
&lt;p&gt;The result looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;    sdl2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Subsystem&lt;/span&gt;&lt;span&gt; sdl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;span&gt;SDL_INIT_VIDEO&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    auto&lt;/span&gt;&lt;span&gt; window&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;   =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; sdl2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_window&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;Demo&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 100&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 640&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 480&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    auto&lt;/span&gt;&lt;span&gt; renderer&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; sdl2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;make_renderer&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;window&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;get&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; SDL_RENDERER_ACCELERATED&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Cleanup is automatic, in the right order: renderer, then window, then SDL_Quit.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The order follows from the declaration order: C++ destroys local variables in reverse. That is exactly the order SDL requires, and the language guarantees it rather than relying on discipline.&lt;/p&gt;
&lt;p&gt;The pattern is generic. Any C library that manages resources with create and destroy functions benefits from the same approach: a &lt;code&gt;Deleter&lt;/code&gt; struct with one overload per resource type, &lt;code&gt;using&lt;/code&gt; aliases for &lt;code&gt;unique_ptr&lt;/code&gt;, factory functions that catch &lt;code&gt;nullptr&lt;/code&gt; and throw immediately, and for process-wide init and cleanup, a class with deleted copy and move operations. Write it once and you can forget that SDL2 is a C library.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>When &#39;Close to the Hardware&#39; Isn&#39;t Close Enough</title>
        <published>2026-04-19T00:00:00+00:00</published>
        <updated>2026-04-19T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/embedded-hello-world/"/>
        <id>https://lmilz.dev/blog/embedded-hello-world/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/embedded-hello-world/">&lt;p&gt;I recently bought myself an STM32 Nucleo microcontroller board to play around with. What fascinated me was how much more flexible things are at this level, how much more you can do yourself. With an ESP32 that&#39;s not really the case, you&#39;re always tied to ESP-IDF or some other framework.&lt;/p&gt;
&lt;p&gt;I started with a first simple example, the kind everyone knows and has done before: the famous &lt;em&gt;Hello World&lt;/em&gt;. It&#39;s simple, and that&#39;s exactly why it&#39;s useful. You don&#39;t learn a language&#39;s syntax with it, it&#39;s too small for that. You learn how to actually &lt;em&gt;use&lt;/em&gt; the language. What file format, how to compile, how to link, how to run the result.&lt;/p&gt;
&lt;p&gt;That&#39;s why I always reach for &lt;em&gt;Hello World&lt;/em&gt; first whenever I pick up a new language or a new environment. It forces me to run the whole build system once before I do anything else. Two things I learned this way that weren&#39;t obvious to me at the start. First: you can learn a surprising amount from a simple example if you take it seriously. Second: simple is almost never really simple. Most things that look easy are easy because someone else hid the complexity for you.&lt;/p&gt;
&lt;p&gt;The embedded world has a &lt;em&gt;Hello World&lt;/em&gt; too: the blinking LED. Most microcontroller boards have an onboard LED that you turn on and off at some frequency. Sounds trivial. And it is, if you use a Hardware Abstraction Layer (HAL) and some ready-made project template.&lt;/p&gt;
&lt;p&gt;I&#39;ve been working in automotive software for years, and before that on physics simulations at university. In my head I&#39;d always been &quot;close to the hardware&quot;, I write embedded software after all, not frontend. &lt;a rel=&quot;external&quot; href=&quot;https://lmilz.dev/blog/from-c-to-rust-evolving-programming-languages-in-automotive-development/&quot;&gt;A while back I wrote about the roles of C, C++, and Rust in automotive&lt;/a&gt; and quietly took for granted that &quot;embedded equals close to the hardware&quot;. At some point it hit me that this was a delusion. MCAL, AUTOSAR OS, RTE: there are more layers between me and the silicon than between a web app and the kernel. I wanted to actually get down to the bottom for once. No HAL, no framework, no vendor black box. Just the reference manual and the compiler.&lt;/p&gt;
&lt;h2 id=&quot;a-blinky-in-rust&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#a-blinky-in-rust&quot; aria-label=&quot;Anchor link for: a-blinky-in-rust&quot;&gt;A Blinky in Rust&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In the Rust ecosystem the example quickly ends up looking like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;no_std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;no_main&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;use&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; cortex_m_rt&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span&gt;entry&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;use&lt;/span&gt;&lt;span&gt; panic_halt &lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;as&lt;/span&gt;&lt;span&gt; _&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;use&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; stm32f4xx_hal&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;span&gt;pac&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; prelude&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;entry&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&amp;gt; !&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; pac&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Peripherals&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;take&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;unwrap&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let&lt;/span&gt;&lt;span&gt; rcc&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;RCC&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;constrain&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let&lt;/span&gt;&lt;span&gt; clocks&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; rcc&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span&gt;cfgr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;sysclk&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;48&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;MHz&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;())&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;freeze&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let&lt;/span&gt;&lt;span&gt; gpiob&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;GPIOB&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;split&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; led1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; gpiob&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span&gt;pb0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;into_push_pull_output&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; led2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; gpiob&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span&gt;pb7&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;into_push_pull_output&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    let mut&lt;/span&gt;&lt;span&gt; delay&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; dp&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;TIM1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;delay_ms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;clocks&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    loop&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        led1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;toggle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        delay&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;delay_ms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;400&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u32&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        led2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;toggle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        delay&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;delay_ms&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;100&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u32&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;It&#39;s short, type-safe, and it works. The compiler keeps me from toggling an input pin. The clock config goes through a builder pattern. Pin types carry their configuration in the type system, so &lt;code&gt;toggle()&lt;/code&gt; on an input pin is a compile error. The delay is timed off SYSCLK. And everything you &lt;em&gt;don&#39;t&lt;/em&gt; see, the vector table, the reset handler, the copy loop for &lt;code&gt;.data&lt;/code&gt;, the zeroing of &lt;code&gt;.bss&lt;/code&gt;, all of that comes from the &lt;code&gt;cortex-m-rt&lt;/code&gt; crate. The linker just gets a small &lt;code&gt;memory.x&lt;/code&gt; that tells it where flash and RAM are.&lt;/p&gt;
&lt;p&gt;That&#39;s exactly the problem I wanted to dig into. Not in the &quot;the HAL is bad&quot; sense (I actually came away appreciating it more), but: &lt;strong&gt;I wanted to see what the HAL does for me.&lt;/strong&gt; So the same thing again, but in C, no HAL, no CMSIS, just register addresses straight out of the reference manual.&lt;/p&gt;
&lt;h2 id=&quot;hello-world-embedded&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#hello-world-embedded&quot; aria-label=&quot;Anchor link for: hello-world-embedded&quot;&gt;Hello World, embedded&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The board I picked was the Nucleo-F446ZE, and I started reading the docs (Reference Manual RM0390, chapter 6 for RCC and chapter 8 for GPIO).&lt;/p&gt;
&lt;p&gt;The blinky itself is quickly explained. Enable the GPIOB clock, configure PB0 as an output, in a loop toggle the output register. In C, with no abstraction, it looks like this. First the registers as macros, then &lt;code&gt;main()&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;stdbool.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#include&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;lt;stdint.h&amp;gt;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; RCC_BASE&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;   0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;40023800&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; GPIOB_BASE&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;40020400&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; RCC_AHB1ENR&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;volatile uint32_t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)(&lt;/span&gt;&lt;span&gt;RCC_BASE  &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;30&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; GPIOB_MODER&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;volatile uint32_t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)(&lt;/span&gt;&lt;span&gt;GPIOB_BASE &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;00&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; GPIOB_ODR&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;volatile uint32_t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)(&lt;/span&gt;&lt;span&gt;GPIOB_BASE &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;14&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; RCC_AHB1ENR_GPIOBEN&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;#define&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; LED_PIN&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;             0&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;U&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;static void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; delay&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;volatile uint32_t&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; n&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;--&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        __asm__&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;nop&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; main&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    RCC_AHB1ENR &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;|=&lt;/span&gt;&lt;span&gt; RCC_AHB1ENR_GPIOBEN&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    GPIOB_MODER &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;= ~&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;LED_PIN &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    GPIOB_MODER &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;|=&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  (&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;LED_PIN &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        GPIOB_ODR &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;^=&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;UL&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; LED_PIN&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        delay&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;500000&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Three things about this code need explaining.&lt;/p&gt;
&lt;p&gt;First: &lt;code&gt;*(volatile uint32_t*)(...)&lt;/code&gt;. That&#39;s memory-mapped I/O in its purest form. The hardware exposes certain addresses that don&#39;t point to ordinary RAM cells, but to registers of the peripherals. Writing to &lt;code&gt;RCC_AHB1ENR&lt;/code&gt; doesn&#39;t mean &quot;write into a memory cell&quot;, it means &quot;tell the RCC block which clocks to enable&quot;. The &lt;code&gt;volatile&lt;/code&gt; cast isn&#39;t a style choice, it&#39;s mandatory. Without &lt;code&gt;volatile&lt;/code&gt;, the compiler wouldn&#39;t care how often you write, it would optimize the accesses away as dead stores, and the blinky would silently do nothing. &lt;code&gt;volatile&lt;/code&gt; is the contract with the compiler: &quot;hands off, every access has a side effect you can&#39;t see.&quot;&lt;/p&gt;
&lt;p&gt;Second: initializing &lt;code&gt;GPIOB_MODER&lt;/code&gt;. I clear the two mode bits for PB0 first, then set them to &lt;code&gt;01&lt;/code&gt; (General Purpose Output). Read-modify-write with &lt;code&gt;&amp;amp;=&lt;/code&gt; and &lt;code&gt;|=&lt;/code&gt;, so that other pins in the same register stay untouched. On Cortex-M, by the way, this is &lt;em&gt;not&lt;/em&gt; atomic, that&#39;s three instructions (&lt;code&gt;LDR&lt;/code&gt;, &lt;code&gt;ORR&lt;/code&gt;/&lt;code&gt;BIC&lt;/code&gt;, &lt;code&gt;STR&lt;/code&gt;), and an ISR could fire in between. It works here because no interrupts are active during init. If you actually need atomicity, you use the bit-band region (where available, it&#39;s gone on the Cortex-M7) or &lt;code&gt;LDREX&lt;/code&gt;/&lt;code&gt;STREX&lt;/code&gt;. For pure set-or-clear on GPIO output pins there&#39;s also the &lt;code&gt;BSRR&lt;/code&gt; register, which is specifically designed to let you set or reset individual bits atomically in one write, no read-modify-write required.&lt;/p&gt;
&lt;p&gt;Third: &lt;code&gt;delay()&lt;/code&gt;. The combination of &lt;code&gt;volatile&lt;/code&gt; on the parameter and the explicit &lt;code&gt;nop&lt;/code&gt; isn&#39;t decoration. Without &lt;code&gt;volatile&lt;/code&gt;, and depending on the optimization level, the compiler may simply skip decrementing the counter, because nobody reads the value. Without the &lt;code&gt;nop&lt;/code&gt;, it&#39;s free to collapse the loop body. Together they force the loop to actually run. The comment &quot;500 ms at 16 MHz&quot; is wishful thinking, since the real duration depends on the optimizer, flash wait states, and the pipeline. For a blinky that&#39;s fine, in production you&#39;d use SysTick.&lt;/p&gt;
&lt;p&gt;So much for the functionality. The really interesting question isn&#39;t what&#39;s &lt;em&gt;in&lt;/em&gt; &lt;code&gt;main()&lt;/code&gt;, it&#39;s: how does &lt;code&gt;main()&lt;/code&gt; ever get called in the first place? On a PC the operating system does that. On a microcontroller there is no operating system, no loader, no process, nothing that reads in code, allocates memory, or prepares a runtime. Someone has to do all of this by hand. That&#39;s where it got interesting for me.&lt;/p&gt;
&lt;h2 id=&quot;the-hardware-doesn-t-know-about-main&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-hardware-doesn-t-know-about-main&quot; aria-label=&quot;Anchor link for: the-hardware-doesn-t-know-about-main&quot;&gt;The hardware doesn&#39;t know about &lt;code&gt;main()&lt;/code&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When the ARM Cortex-M4 in the STM32 powers on, it does something very concrete. It reads 4 bytes from address &lt;code&gt;0x08000000&lt;/code&gt; and loads them as the initial stack pointer. Then it reads the next 4 bytes from &lt;code&gt;0x08000004&lt;/code&gt;, interprets them as an address, and jumps there. That&#39;s not a software instruction, that&#39;s circuit logic, set in silicon. Everything that happens after that is software.&lt;/p&gt;
&lt;p&gt;One detail that can cost you hours if you don&#39;t know it: bit 0 of the reset vector address has to be set. The Cortex-M4 only knows the Thumb instruction set, and the CPU uses bit 0 of the jump address as a mode bit. If it&#39;s zero, you get a HardFault right after reset. The linker usually takes care of this for you, but anyone who builds the vector table by hand and has to cast a function pointer symbol themselves will learn this one the hard way.&lt;/p&gt;
&lt;p&gt;Which gives us a clear requirement: at address &lt;code&gt;0x08000000&lt;/code&gt; exactly the right thing has to be sitting there. This structure is called the vector table, and it&#39;s really just an array of function pointers. First entry is the stack pointer (cast as a function pointer, the hardware doesn&#39;t care about the type, it just reads 4 bytes). Second entry is the address of the reset handler. After that come NMI, HardFault, and the other handlers. On an interrupt, the hardware looks into this table, reads the address, jumps there. It&#39;s a hardware jump table, not a software dispatch.&lt;/p&gt;
&lt;p&gt;In code, heavily shortened, it looks like this. The full table also has MemManage, BusFault, UsageFault, SVCall, PendSV, SysTick, and then the roughly 80 STM32-specific IRQs:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;__attribute__&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;section&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;.isr_vector&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span&gt; vector_table&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;[]&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    (&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;_estack&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Reset_Handler&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Default_Handler&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* NMI */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    Default_Handler&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* HardFault */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The &lt;code&gt;section(&quot;.isr_vector&quot;)&lt;/code&gt; attribute matters. It tells the compiler: this data belongs in a specially named section. Where that section ends up in memory, though, isn&#39;t decided here. That was the first moment I realized the compiler and the hardware don&#39;t talk to each other directly. Something&#39;s missing in between.&lt;/p&gt;
&lt;p&gt;Since the Cortex-M4 is a licensed ARM core, none of this is STM-specific. It works the same way on boards from NXP, Microchip, or TI. Once you&#39;ve understood it once, you can dive right in on a different board.&lt;/p&gt;
&lt;h2 id=&quot;sections-floating-in-nothing&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#sections-floating-in-nothing&quot; aria-label=&quot;Anchor link for: sections-floating-in-nothing&quot;&gt;Sections floating in nothing&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The STM32 has two memory regions. Flash, non-volatile, starting at &lt;code&gt;0x08000000&lt;/code&gt;. RAM, volatile, starting at &lt;code&gt;0x20000000&lt;/code&gt;. Both on the same 32-bit address bus. From the CPU&#39;s point of view both regions are equally addressable; which addresses point to flash and which to RAM is decided by how the chip is wired.&lt;/p&gt;
&lt;p&gt;The C compiler knows none of this. It takes &lt;code&gt;main.c&lt;/code&gt;, produces machine code, puts it into a section called &lt;code&gt;.text&lt;/code&gt;. Constants go into &lt;code&gt;.rodata&lt;/code&gt;, initialized variables into &lt;code&gt;.data&lt;/code&gt;, uninitialized variables into &lt;code&gt;.bss&lt;/code&gt;. These are all just names. The compiler has no idea that &lt;code&gt;.text&lt;/code&gt; is supposed to end up in flash later and &lt;code&gt;.bss&lt;/code&gt; in RAM. It doesn&#39;t even know that flash and RAM exist. The sections have no absolute addresses. They&#39;re just floating in nothing.&lt;/p&gt;
&lt;p&gt;So someone has to decide which section ends up at which physical address. That&#39;s the job of the linker script.&lt;/p&gt;
&lt;h2 id=&quot;the-linker-script-is-the-floor-plan&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-linker-script-is-the-floor-plan&quot; aria-label=&quot;Anchor link for: the-linker-script-is-the-floor-plan&quot;&gt;The linker script is the floor plan&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;A linker script is a text file with a &lt;code&gt;.ld&lt;/code&gt; extension. It describes two things: which memory regions exist, and which section goes where.&lt;/p&gt;
&lt;p&gt;The line&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;ENTRY(Reset_Handler)&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;tells the linker where the entry point is.&lt;/p&gt;
&lt;p&gt;The &lt;code&gt;MEMORY&lt;/code&gt; block lists the physical regions. The numbers come straight from the chip&#39;s datasheet:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;plain&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;MEMORY&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    FLASH (rx)  : ORIGIN = 0x08000000, LENGTH = 512K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    RAM   (xrw) : ORIGIN = 0x20000000, LENGTH = 128K&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;In the &lt;code&gt;SECTIONS&lt;/code&gt; block every section gets assigned to a region. &lt;code&gt;.isr_vector&lt;/code&gt; goes at the beginning of flash, because that&#39;s where the hardware reads its first 4 bytes. The &lt;code&gt;KEEP(*(.isr_vector))&lt;/code&gt; keeps the linker from throwing the vector table away, since nothing in the C code explicitly references the &lt;code&gt;vector_table&lt;/code&gt; symbol.&lt;/p&gt;
&lt;p&gt;&lt;code&gt;.text&lt;/code&gt; and &lt;code&gt;.rodata&lt;/code&gt; go into flash, because they&#39;re supposed to be non-volatile. &lt;code&gt;.bss&lt;/code&gt; goes into RAM, because it gets filled with zeros at runtime.&lt;/p&gt;
&lt;p&gt;My favorite part is &lt;code&gt;.data&lt;/code&gt;. These are variables with an initial value: &lt;code&gt;int baud_rate = 9600;&lt;/code&gt;. At runtime they need to live in RAM, otherwise they aren&#39;t writable. But the initial value has to be stored somewhere before the board ever gets power. So the initial value has to live in flash and get copied into RAM at startup.&lt;/p&gt;
&lt;p&gt;The linker script solves this by giving &lt;code&gt;.data&lt;/code&gt; two addresses. A virtual address (VMA) in RAM, that&#39;s the address the code expects the variable at. And a load address (LMA) in flash, that&#39;s where the initial values physically sit. Who actually does the copying, the linker script doesn&#39;t say. It only emits boundary markers as symbols: &lt;code&gt;_sidata&lt;/code&gt; (start of the initial values in flash), &lt;code&gt;_sdata&lt;/code&gt; and &lt;code&gt;_edata&lt;/code&gt; (start and end in RAM), &lt;code&gt;_sbss&lt;/code&gt; and &lt;code&gt;_ebss&lt;/code&gt; for the &lt;code&gt;.bss&lt;/code&gt; section.&lt;/p&gt;
&lt;p&gt;These symbols aren&#39;t variables in the usual sense. They don&#39;t occupy any memory. They&#39;re just numbers that the linker stamps in at the end. In C you access them by declaring them &lt;code&gt;extern&lt;/code&gt; and then taking their address:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;extern uint32_t&lt;/span&gt;&lt;span&gt; _sidata&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;extern uint32_t&lt;/span&gt;&lt;span&gt; _sdata&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;extern uint32_t&lt;/span&gt;&lt;span&gt; _edata&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That confused me for a minute, because they look like variables but aren&#39;t. You never read the &lt;em&gt;value&lt;/em&gt; of &lt;code&gt;_sdata&lt;/code&gt;, you always use &lt;code&gt;&amp;amp;_sdata&lt;/code&gt;. The &quot;variable&quot; &lt;em&gt;is&lt;/em&gt; its own address.&lt;/p&gt;
&lt;h2 id=&quot;the-startup-code-is-a-mini-os&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-startup-code-is-a-mini-os&quot; aria-label=&quot;Anchor link for: the-startup-code-is-a-mini-os&quot;&gt;The startup code is a mini OS&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The linker script sets the boundaries. Filling everything in is the job of the startup code. Traditionally a file called &lt;code&gt;startup.s&lt;/code&gt; in assembly, nowadays often &lt;code&gt;startup.c&lt;/code&gt; or inline in the same file. I kept mine inline in the blinky, for maximum transparency.&lt;/p&gt;
&lt;p&gt;The startup code is the reset handler. It does three things. It copies &lt;code&gt;.data&lt;/code&gt; from flash to RAM so that initialized variables have their initial values. It fills &lt;code&gt;.bss&lt;/code&gt; with zeros so the C standard for uninitialized variables is upheld. Then it calls &lt;code&gt;main()&lt;/code&gt;. With C++ there&#39;s a fourth step: calling global constructors, which runs through a section called &lt;code&gt;__init_array&lt;/code&gt;. In an AUTOSAR project this exact work lives inside the supplier&#39;s startup code and runs before &lt;code&gt;EcuM_Init&lt;/code&gt; ever sees a register.&lt;/p&gt;
&lt;p&gt;In the blinky it looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Reset_Handler&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    uint32_t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt; src &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;= &amp;amp;&lt;/span&gt;&lt;span&gt;_sidata&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    uint32_t&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt; dst &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;= &amp;amp;&lt;/span&gt;&lt;span&gt;_sdata&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;dst &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt; &amp;amp;&lt;/span&gt;&lt;span&gt;_edata&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;        *&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;++ = *&lt;/span&gt;&lt;span&gt;src&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    dst &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;= &amp;amp;&lt;/span&gt;&lt;span&gt;_sbss&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;dst &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt; &amp;amp;&lt;/span&gt;&lt;span&gt;_ebss&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;        *&lt;/span&gt;&lt;span&gt;dst&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;++ =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    main&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    while&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;true&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) { }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Two things stood out to me when I wrote this for the first time.&lt;/p&gt;
&lt;p&gt;First: the startup code uses the linker symbols directly as loop bounds. That&#39;s the contract between the two files. The linker script promises that the symbols are there and point to the right addresses. The startup code just trusts it blindly. If you rename the symbols in the linker script, the startup code breaks silently. Nothing warns you. As an aside: the copy loop runs over &lt;code&gt;uint32_t*&lt;/code&gt;, not &lt;code&gt;uint8_t*&lt;/code&gt;. That&#39;s faster, one word per bus transaction instead of four bytes, and it works because the linker aligns the sections to 4 bytes. Unaligned 32-bit accesses can trigger a fault on Cortex-M depending on the configuration.&lt;/p&gt;
&lt;p&gt;Second: the &lt;code&gt;while (true) {}&lt;/code&gt; after &lt;code&gt;main()&lt;/code&gt;. On a PC, &lt;code&gt;main()&lt;/code&gt; returns to the operating system. Here there is no operating system. If &lt;code&gt;main()&lt;/code&gt; accidentally returns, the processor has to go somewhere. The infinite loop is insurance against it running wild through memory.&lt;/p&gt;
&lt;p&gt;The only reason the startup code can run at all, before the C environment is set up, is that it uses only stack-local variables. And the stack already works because the hardware loaded the stack pointer from the vector table at reset. The whole sequence is a domino chain. Each step has exactly the one precondition the previous step just created.&lt;/p&gt;
&lt;h2 id=&quot;from-c-to-bytes-in-flash&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#from-c-to-bytes-in-flash&quot; aria-label=&quot;Anchor link for: from-c-to-bytes-in-flash&quot;&gt;From &lt;code&gt;.c&lt;/code&gt; to bytes in flash&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;What happens between source and a blinking LED is also not one step, but several. The preprocessor resolves includes and macros. The compiler translates each &lt;code&gt;.c&lt;/code&gt; file individually into assembly, architecture-specific via &lt;code&gt;-mcpu=cortex-m4 -mthumb&lt;/code&gt;. The assembler produces object files in ELF format, with relative addresses and unresolved symbols. The linker collects all sections of the same name from all object files, assigns them absolute addresses via the linker script, and resolves the symbols. What used to say &quot;jump to &lt;code&gt;delay&lt;/code&gt;&quot; now carries the concrete flash address of &lt;code&gt;delay&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;The final product is an ELF file. It contains the machine code, but also program headers (which bytes go where in memory), the symbol table (function and variable names with their addresses, for the debugger), and optionally debug information (the mapping of machine code to source lines). A &lt;code&gt;.bin&lt;/code&gt; file, which you produce via &lt;code&gt;objcopy -O binary&lt;/code&gt;, is just the raw bytes without any metadata.&lt;/p&gt;
&lt;p&gt;To flash it, you use a tool like probe-rs or OpenOCD. The tool talks over a debug adapter (ST-Link, J-Link, or CMSIS-DAP) to the Cortex-M4&#39;s SWD port (Serial Wire Debug). The debug port has direct access to the entire address space of the chip, regardless of whether the CPU is running. The tool reads the ELF file, extracts the program headers, writes the bytes into flash, and triggers a reset. After which the whole cycle starts over. Hardware reads the vector table, jumps to the reset handler, startup initializes, &lt;code&gt;main()&lt;/code&gt; runs, LED blinks.&lt;/p&gt;
&lt;p&gt;I want to stress again that this is a little learning project of mine, not production code. In production you&#39;d put an abstraction on top, either a vendor HAL (for example ST&#39;s) or the Cortex Microcontroller Software Interface Standard (CMSIS), which is vendor-agnostic.&lt;/p&gt;
&lt;h2 id=&quot;what-i-took-away&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-i-took-away&quot; aria-label=&quot;Anchor link for: what-i-took-away&quot;&gt;What I took away&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Two things stuck with me.&lt;/p&gt;
&lt;p&gt;The first: a &lt;em&gt;Hello World&lt;/em&gt; is very much not a waste of time if you take it seriously. I could have written the blinky with a HAL in five lines, done. Instead I wrote the vector table myself, the reset handler, read the linker script, understood the boundary markers. And I learned more about the system than I would have in weeks of framework tutorials.&lt;/p&gt;
&lt;p&gt;The second: simple is almost never actually simple. The blinky with a HAL isn&#39;t any easier than the blinky with registers, it&#39;s just further away from what&#39;s actually happening. Between &lt;code&gt;make flash&lt;/code&gt; and a blinking LED sit the linker script, the startup code, the ELF, the flashing, the hardware reset. All of this is there, even when you don&#39;t see it.&lt;/p&gt;
&lt;p&gt;For those of us in automotive this is particularly interesting. In a classical AUTOSAR project we never see the vector table, never see the reset handler, never see the linker script. The MCAL, the supplier&#39;s startup code, the OS init, BswM scheduling: all of that arrives as a black box, and we write runnables that get called from the RTE. Between power-on and &lt;code&gt;Rte_MainFunction_*&lt;/code&gt; there&#39;s the exact same chain as here. Hardware reads 4 bytes, jumps to the reset handler, someone initializes &lt;code&gt;.data&lt;/code&gt; and &lt;code&gt;.bss&lt;/code&gt;, someone calls the OS init, someone starts the tasks. It&#39;s just that each of those steps is buried inside an AUTOSAR configuration we normally don&#39;t open. Once you&#39;ve built this foundation yourself, you read an MCAL doc differently. It also sharpens the language question. &lt;a rel=&quot;external&quot; href=&quot;https://lmilz.dev/blog/from-c-to-rust-evolving-programming-languages-in-automotive-development/&quot;&gt;In the earlier post I argued that C, C++, and Rust each have their layer&lt;/a&gt;, but the layer we usually work on in AUTOSAR is several steps above the one where that choice really matters. The reset handler, the &lt;code&gt;.data&lt;/code&gt; copy, the linker script, those are all C, regardless of what we write on top. The supplier picks the language down there, we don&#39;t.&lt;/p&gt;
&lt;p&gt;Not seeing it is comfortable as long as everything works. The moment it doesn&#39;t, you have to go a level deeper. And then it&#39;s good to have been there before.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>From Read-It-Later to Zettelkasten: Automating My Reading Workflow with n8n</title>
        <published>2026-03-16T00:00:00+00:00</published>
        <updated>2026-03-16T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/from-read-it-later-to-zettelkasten-automating-my-reading-workflow-with-n8n/"/>
        <id>https://lmilz.dev/blog/from-read-it-later-to-zettelkasten-automating-my-reading-workflow-with-n8n/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/from-read-it-later-to-zettelkasten-automating-my-reading-workflow-with-n8n/">&lt;h2 id=&quot;background&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#background&quot; aria-label=&quot;Anchor link for: background&quot;&gt;Background&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Over the past few weeks I ran into a familiar problem: my personal projects started to feel like work.&lt;/p&gt;
&lt;p&gt;Family, work, and then spending evenings on side projects slowly drained my motivation.&lt;/p&gt;
&lt;p&gt;So I asked myself: what is actually bothering me, and where does it make sense to invest personal energy? The answer came quickly: my homelab. Not because I didn&#39;t have one, but because I had been neglecting it. A few Docker containers were running on the Raspberry Pi that I had tried out and then forgotten about. The NAS, which was supposed to handle backups, never really worked as planned. And my self-hosted Git instance was only reachable within my home network, which made it almost useless for day-to-day work.&lt;/p&gt;
&lt;p&gt;The idea was clear: build a simple setup that automates a lot, is as open source as possible, and takes privacy seriously.&lt;/p&gt;
&lt;h2 id=&quot;the-stack&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-stack&quot; aria-label=&quot;Anchor link for: the-stack&quot;&gt;The Stack&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The goal was a small, practical stack that solves real problems rather than hosting everything under the sun.&lt;/p&gt;
&lt;p&gt;As a foundation I rented a VPS from a German provider for a few euros a month. Everything runs behind Caddy as a reverse proxy, which also handles HTTPS automatically without any configuration on my part.&lt;/p&gt;
&lt;p&gt;The first thing I set up was Forgejo as a Git server, reachable via its own subdomain. That sounds small, but for me it made a real difference: I can now work on private projects from anywhere, whether on a train using a hotspot or somewhere else. At the same time I moved my website from GitHub Pages to the new repo and set up a deploy workflow so that the site gets automatically built and deployed on every push. That actually turned out to be smoother than using GitHub Pages.&lt;/p&gt;
&lt;p&gt;After that came CommaFeed as an RSS reader and Readeck as a read-it-later app. Both were set up quickly, and the value was immediately noticeable: I can now read anywhere, save articles, and add notes directly.&lt;/p&gt;
&lt;p&gt;Up to this point: solid, but unremarkable self-hosting. The interesting part came after.&lt;/p&gt;
&lt;h2 id=&quot;the-problem-with-notes&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-problem-with-notes&quot; aria-label=&quot;Anchor link for: the-problem-with-notes&quot;&gt;The Problem with Notes&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I read a lot. And like most developers, I have a familiar problem: I save articles, read them, take notes, and then they disappear into the void. The highlights collect dust, the insights fade. I do use the Zettelkasten method for this. But sometimes I simply lacked the focus and time to turn my notes into actual Zettelkasten entries.&lt;/p&gt;
&lt;p&gt;While thinking about which other containers I wanted to host, I remembered n8n. I had watched several YouTube videos about what n8n can do.&lt;/p&gt;
&lt;p&gt;So I hosted that too and started experimenting. My goal was clear: everything I read and find valuable should flow automatically into my Zettelkasten. Not as a bland summary, but as a thoughtful note that reflects my own thinking and highlights.&lt;/p&gt;
&lt;h2 id=&quot;the-idea&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-idea&quot; aria-label=&quot;Anchor link for: the-idea&quot;&gt;The Idea&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My setup consists of three components connected through n8n:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Readeck: where I save, read, and highlight articles&lt;/li&gt;
&lt;li&gt;Claude (via the Anthropic API): which generates a Zettelkasten note from my highlights&lt;/li&gt;
&lt;li&gt;Forgejo: where my Zettelkasten lives as Markdown files&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The key idea is that the workflow never sends the entire article to the language model.
Instead it only sends my highlights, notes, and a short summary. This keeps it privacy-friendly, saves tokens, and most importantly the generated note actually reflects my understanding rather than a generic summary.&lt;/p&gt;
&lt;h2 id=&quot;the-reading-process&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-reading-process&quot; aria-label=&quot;Anchor link for: the-reading-process&quot;&gt;The Reading Process&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before the workflow can do its thing, I need to do my part. My reading process in Readeck looks like this: I read an article and mark the most important passages as highlights. For each highlight I write a short note with my thoughts. At the end I write an overall summary on one of the highlights, prefixed with &quot;Zusammenfassung:&quot;. Finally I assign the label &lt;code&gt;Done&lt;/code&gt;, which signals to the workflow: this article is ready.&lt;/p&gt;
&lt;p&gt;This convention is essential:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;the &lt;code&gt;Done&lt;/code&gt; label triggers the workflow&lt;/li&gt;
&lt;li&gt;the &lt;code&gt;Zusammenfassung:&lt;/code&gt; prefix tells the LLM what the central idea is&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-readeck-api&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-readeck-api&quot; aria-label=&quot;Anchor link for: the-readeck-api&quot;&gt;The Readeck API&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Readeck offers a REST API and since version 0.22 there are also annotations (highlights with notes) and a Markdown export endpoint that is worth its weight in gold for this workflow.&lt;/p&gt;
&lt;p&gt;The endpoint &lt;code&gt;GET /api/bookmarks?labels=Done&lt;/code&gt; returns all articles I have marked for processing. For each bookmark I then fetch the content as Markdown via &lt;code&gt;GET /api/bookmarks/{id}/article.md&lt;/code&gt;. The nice thing about this: the endpoint delivers not just the article text, but also the highlights as &lt;code&gt;==highlighted text==&lt;/code&gt; and my notes as footnotes &lt;code&gt;[^1]&lt;/code&gt;, all in a clean Markdown document with YAML frontmatter.&lt;/p&gt;
&lt;h2 id=&quot;the-n8n-workflow&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-n8n-workflow&quot; aria-label=&quot;Anchor link for: the-n8n-workflow&quot;&gt;The n8n Workflow&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The workflow consists of a loop that processes each article individually. The full chain looks like this:&lt;/p&gt;
&lt;p&gt;A &lt;strong&gt;Schedule Trigger&lt;/strong&gt; starts the workflow every night at 2am. An &lt;strong&gt;HTTP Request&lt;/strong&gt; then fetches all bookmarks with the label &lt;code&gt;Done&lt;/code&gt; from Readeck. A &lt;strong&gt;Loop Over Items&lt;/strong&gt; node iterates over each bookmark. Inside the loop another &lt;strong&gt;HTTP Request&lt;/strong&gt; fetches the article as Markdown. A &lt;strong&gt;Code Node&lt;/strong&gt; parses the Markdown: it extracts the frontmatter, separates the highlights from the regular text, looks for my &lt;code&gt;Zusammenfassung:&lt;/code&gt;, and assembles the input for the language model. A &lt;strong&gt;Basic LLM Chain&lt;/strong&gt; sends everything to the Anthropic API and gets a Zettelkasten note back. A second &lt;strong&gt;Code Node&lt;/strong&gt; encodes the LLM response as Base64 for the Forgejo API and generates a slugified filename. Three &lt;strong&gt;HTTP Requests&lt;/strong&gt; against the Forgejo API create a feature branch, commit the file, and open a pull request. A final &lt;strong&gt;HTTP Request&lt;/strong&gt; updates the label in Readeck from &lt;code&gt;Done&lt;/code&gt; to &lt;code&gt;processed&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;&lt;img src=&quot;/assets/images/n8n_workflow.png&quot; alt=&quot;n8n_workflow&quot; /&gt;&lt;/p&gt;
&lt;h2 id=&quot;parsing-extracting-highlights&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#parsing-extracting-highlights&quot; aria-label=&quot;Anchor link for: parsing-extracting-highlights&quot;&gt;Parsing: Extracting Highlights&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The core is the Code Node that takes the Markdown article apart. It extracts the highlights (everything between &lt;code&gt;==&lt;/code&gt; characters), maps them to the footnote notes, and specifically looks for my summary.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;javascript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Extract highlights with footnote number&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span&gt; highlightRegex&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;==(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;)==(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;?:&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\[&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\^&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;?&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;g&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Extract footnotes&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span&gt; footnoteRegex&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt; /\[\^&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\d&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;+&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\]&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*?&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;$&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;gm&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Find summary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;for&lt;/span&gt;&lt;span&gt; (&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span&gt; h&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; of&lt;/span&gt;&lt;span&gt; highlightPairs)&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  const&lt;/span&gt;&lt;span&gt; note&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; h&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span&gt;footnoteNum&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ?&lt;/span&gt;&lt;span&gt; (footnoteMap[h&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span&gt;footnoteNum]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ||&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;#39;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;#39;&amp;#39;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;  if&lt;/span&gt;&lt;span&gt; (note&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;toLowerCase&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;startsWith&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;#39;zusammenfassung:&amp;#39;&lt;/span&gt;&lt;span&gt;))&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    zusammenfassung&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; note&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;replace&lt;/span&gt;&lt;span&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;^&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;Zz&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;usammenfassung:&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;\s&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;/&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;#39;&amp;#39;&lt;/span&gt;&lt;span&gt;)&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;  }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The output then contains only the frontmatter, my summary, and the individual highlights with notes, no full article text.&lt;/p&gt;
&lt;h2 id=&quot;the-prompt&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-prompt&quot; aria-label=&quot;Anchor link for: the-prompt&quot;&gt;The Prompt&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The prompt is designed to generate an atomic Zettelkasten note that reflects my understanding. It took a few iterations before the results felt right.&lt;/p&gt;
&lt;p&gt;Key aspects: the LLM should use my summary as a guide and weight it more heavily than individual highlights. The note should distill one central idea, not retell the article. Tags should be thematic and connections to other areas of knowledge should be written as running text. The output format is a fixed Markdown template with YAML frontmatter that fits directly into my Zettelkasten.&lt;/p&gt;
&lt;p&gt;A typical note generated by the workflow looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;markdown&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;title&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;Deliberate Practice Is the Bottleneck of Learning Software Development&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;date&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;2025-11-12&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;tags&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;learning&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; deliberate-practice&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; software-craftsmanship&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;source&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;https://example.com/deliberate-practice-programming&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;author&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;Jane Doe&amp;quot;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;&quot;&gt;type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; literature&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;---&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;## Core Idea&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Improvement in software engineering does not primarily come from accumulated experience but from deliberate practice with feedback. Writing code every day is not the same as intentionally improving one’s skills.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;## Note&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;The central idea of the article is that many developers spend most of their time performing rather than practicing. Day-to-day work usually focuses on delivering features or fixing bugs, not on deliberately improving specific programming skills.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Deliberate practice requires isolating a skill and working on it intentionally. In software engineering this could mean refactoring a small piece of code multiple times, implementing a known algorithm from memory, or experimenting with different design approaches for the same problem.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Another important aspect is feedback. Without feedback it is difficult to know whether a mental model is actually improving. Code reviews, pair programming, and discussions about architecture decisions can provide exactly this kind of feedback loop.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;This aligns with my own experience. Many side projects feel productive but often do not improve specific skills. Short, focused exercises such as refactoring a function or re-implementing a concept from scratch tend to lead to deeper learning.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;## Highlights&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;&amp;gt; Expertise is not the result of experience alone, but of structured and deliberate practice.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;Experience alone does not automatically improve programming skills. Improvement requires consciously focusing on specific aspects of the craft.&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;&amp;gt; Most professionals spend their time performing, not practicing.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;In daily work the focus is on delivering results rather than improving individual skills.&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F5C2E7;&quot;&gt;&amp;gt; Feedback is the mechanism that turns repetition into learning.&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;Practices like code reviews or pair programming create the feedback loops that deliberate practice depends on.&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;*&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;## Connections&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;The idea of deliberate practice originates from learning psychology and is widely applied in areas such as music or sports. In software engineering it aligns closely with ideas from the software craftsmanship movement, which emphasizes continuous improvement of programming skills. It also connects to practices such as coding katas and focused refactoring sessions, where the primary goal is learning rather than delivering features.&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;h2 id=&quot;the-forgejo-pr-flow&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-forgejo-pr-flow&quot; aria-label=&quot;Anchor link for: the-forgejo-pr-flow&quot;&gt;The Forgejo PR Flow&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Instead of committing directly to the main branch, the workflow creates a feature branch and a pull request. This gives me the chance to review and adjust the generated note before merging, in the morning over coffee, without rushing.&lt;/p&gt;
&lt;p&gt;The three Forgejo API calls are straightforward: &lt;code&gt;POST /api/v1/repos/{owner}/{repo}/branches&lt;/code&gt; creates a branch like &lt;code&gt;zettelkasten/2026-03-15-atomic-habits&lt;/code&gt;. Then &lt;code&gt;POST /api/v1/repos/{owner}/{repo}/contents/{path}&lt;/code&gt; commits the Markdown file to that branch. Finally &lt;code&gt;POST /api/v1/repos/{owner}/{repo}/pulls&lt;/code&gt; opens the pull request.&lt;/p&gt;
&lt;h2 id=&quot;cost-and-performance&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#cost-and-performance&quot; aria-label=&quot;Anchor link for: cost-and-performance&quot;&gt;Cost and Performance&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The Anthropic API is billed through prepaid credits. Running one article through Claude Sonnet costs a few cents, since I only send the highlights and metadata rather than the full article text. A budget of 5 euros per month is more than enough.&lt;/p&gt;
&lt;h2 id=&quot;stumbling-block&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#stumbling-block&quot; aria-label=&quot;Anchor link for: stumbling-block&quot;&gt;Stumbling Block&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;n8n is marketed as a no-code automation platform. But here comes the catch. It works great with mainstream tools like GitHub, which can be connected quickly and easily.&lt;/p&gt;
&lt;p&gt;For tools like Forgejo and Readeck you basically have to model the REST calls by hand, exactly as in my workflow. You can import &lt;code&gt;curl&lt;/code&gt; commands, but still need to adjust a few things. That raised a question for me: would a script not have been a better solution here? Because apart from the Anthropic API call, I essentially modelled the REST calls directly in n8n.&lt;/p&gt;
&lt;p&gt;That said, I will keep using n8n because I am still just getting started with it. There might be better approaches I have not discovered yet. And it gives me a lot of flexibility without having to wrestle with bash syntax.&lt;/p&gt;
&lt;h2 id=&quot;conclusion&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion&quot; aria-label=&quot;Anchor link for: conclusion&quot;&gt;Conclusion&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The entire workflow took one afternoon, from the first Readeck API call to the finished pull request in Forgejo. Most of the time actually went into refining the prompt and parsing the highlights correctly.&lt;/p&gt;
&lt;p&gt;The result is a system that connects my reading process seamlessly to my Zettelkasten, without manually transferring notes.&lt;/p&gt;
&lt;p&gt;What I like most: the workflow forces me to read deliberately. I have to set highlights, write notes, and compose a summary before an article gets processed. The language model amplifies my thinking, it does not replace it.&lt;/p&gt;
&lt;p&gt;The complete stack is self-hosted: Readeck, n8n, and Forgejo run on my own infrastructure. Only the LLM call goes to the Anthropic API. In the future I could imagine replacing that with a local model too, but the quality of Claude Sonnet for this task is hard to beat right now.&lt;/p&gt;
&lt;p&gt;And the original motivation problem? Gone. When a project actually improves something you deal with every day, it becomes fun again.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Beyond Busyness: Why Output Is Not Value</title>
        <published>2026-02-01T00:00:00+00:00</published>
        <updated>2026-02-01T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/beyond-busyness-why-output-is-not-value/"/>
        <id>https://lmilz.dev/blog/beyond-busyness-why-output-is-not-value/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/beyond-busyness-why-output-is-not-value/">&lt;p&gt;Cal Newport writes about knowledge work. Gene Kim writes about DevOps transformations. Dave Farley writes about continuous delivery. Robert C. Martin writes about software craftsmanship. They come from different worlds, use different vocabularies, and address different audiences.&lt;/p&gt;
&lt;p&gt;And yet they are all saying the same thing: producing more is not the same as creating value.&lt;/p&gt;
&lt;h2 id=&quot;the-common-thread&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-common-thread&quot; aria-label=&quot;Anchor link for: the-common-thread&quot;&gt;The Common Thread&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Newport&#39;s &lt;em&gt;Slow Productivity&lt;/em&gt; rests on three principles: do fewer things, work at a natural pace, obsess over quality.&lt;/p&gt;
&lt;p&gt;Kim&#39;s &lt;em&gt;The Phoenix Project&lt;/em&gt; introduces the Three Ways: flow, feedback, and continuous learning. His follow-up &lt;em&gt;The Unicorn Project&lt;/em&gt; describes the Five Ideals: locality and simplicity, focus and flow, improvement of daily work, psychological safety, customer focus.&lt;/p&gt;
&lt;p&gt;Farley&#39;s &lt;em&gt;Modern Software Engineering&lt;/em&gt; argues that software development is an exercise in learning and discovery. He emphasizes iterative work, fast feedback, and empirical approaches over big upfront design.&lt;/p&gt;
&lt;p&gt;Martin&#39;s Clean series makes the case for professionalism and quality as non-negotiable foundations. Code should be readable, systems should be maintainable, and developers should take pride in their craft.&lt;/p&gt;
&lt;p&gt;Strip away the terminology and the pattern is clear:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Less is more.&lt;/strong&gt; Fewer concurrent projects, smaller batches, shorter feedback loops.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flow over busyness.&lt;/strong&gt; Uninterrupted focus beats fragmented multitasking.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Quality is not a phase.&lt;/strong&gt; It is built in continuously, not inspected in at the end.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Trust enables speed.&lt;/strong&gt; Autonomy and psychological safety are not luxuries, they are prerequisites for good work.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Learning is the work.&lt;/strong&gt; Software development is discovery, not manufacturing.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is not news to anyone who has been paying attention. What is harder to find are concrete examples of what this looks like in practice, especially in regulated industries where process overhead is often seen as inevitable.&lt;/p&gt;
&lt;h2 id=&quot;when-everything-clicks-a-greenfield-project&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#when-everything-clicks-a-greenfield-project&quot; aria-label=&quot;Anchor link for: when-everything-clicks-a-greenfield-project&quot;&gt;When Everything Clicks: A Greenfield Project&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I once worked on a greenfield project where I rebuilt a component from scratch in C. It was embedded software in a regulated industry, which usually means heavy process overhead, but this project was different.&lt;/p&gt;
&lt;p&gt;We had a strong product manager who spoke directly with customers and kept us developers in close, ongoing exchange. That meant we understood what customers actually needed without waiting for a formal technical specification. This was unusual. In most projects I had been on, requirements arrived as finished documents, weeks after the customer conversation had happened.&lt;/p&gt;
&lt;p&gt;The workflow stayed tight throughout. I would refine a piece of the design, write the tests, implement the change, and open a pull request. Communication happened asynchronously over chat. Code reviews came back fast. We could deploy directly onto test hardware and get immediate feedback, not just on our own component but on how it behaved in the full system. Small changes, integrated frequently, validated against reality rather than assumptions.&lt;/p&gt;
&lt;p&gt;What made it work was the presence of trust. No one told me which design pattern to use or how to structure my modules. I could start building before having every detail figured out, try things, fail fast, learn, and iterate.&lt;/p&gt;
&lt;p&gt;The principles from the books were not something I applied consciously. They just described what was already happening when the conditions were right.&lt;/p&gt;
&lt;h2 id=&quot;when-it-is-hard-legacy-systems&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#when-it-is-hard-legacy-systems&quot; aria-label=&quot;Anchor link for: when-it-is-hard-legacy-systems&quot;&gt;When It Is Hard: Legacy Systems&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;But here is the question I kept asking myself: does this only work for greenfield projects? What about the legacy systems, the inherited codebases, the regulated environments where everything feels immovable?&lt;/p&gt;
&lt;p&gt;I have worked on several of those too. And the answer is: the same principles apply. It just requires a different approach.&lt;/p&gt;
&lt;p&gt;The improvements came through deliberate, incremental steps:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Start with requirements.&lt;/strong&gt; Reduce them to the essentials. Focus on &lt;em&gt;what&lt;/em&gt; and &lt;em&gt;why&lt;/em&gt;, not &lt;em&gt;how&lt;/em&gt;. Bloated specifications create bloated software and tests. When you force clarity at the requirements level, you often discover that half the complexity was never necessary.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Improve the documentation.&lt;/strong&gt; This sounds mundane, but it is powerful. The act of documenting what the system actually does, not what it was supposed to do, gives you a first sense of where the architecture has drifted and where refactoring is possible.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Write tests against the legacy code.&lt;/strong&gt; Unit tests, component tests, whatever you can get in. They are your safety net for everything that follows. Without tests, every change is a gamble. With tests, you can move with confidence.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modularize through refactoring.&lt;/strong&gt; And critically: remove unnecessary code entirely. Dead code is not harmless. It is confusion waiting to happen. Every line you delete is a line no one has to understand, maintain, or debug.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reduce tool fragmentation.&lt;/strong&gt; Instead of a separate tool for every step, limit yourself to two or three. Every tool switch is a context switch, and context switches kill flow.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Keep the team small and the communication direct.&lt;/strong&gt; Every handoff, from requirements engineer to architect, to developer, to tester, loses information. In my experience, a small group of developers working closely with a strong product manager creates far more value than a large team with many handoff points.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;This is Kim&#39;s Third Ideal: improvement of daily work. The Scout Rule, popularized by Martin: leave the code cleaner than you found it. It is Farley&#39;s emphasis on managing complexity through continuous, small improvements. It is not a separate initiative. It is how the work gets done.&lt;/p&gt;
&lt;h2 id=&quot;the-myth-of-regulatory-constraints&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-myth-of-regulatory-constraints&quot; aria-label=&quot;Anchor link for: the-myth-of-regulatory-constraints&quot;&gt;The Myth of Regulatory Constraints&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There is a common excuse in regulated industries: &quot;We would love to work this way, but compliance makes it impossible.&quot;&lt;/p&gt;
&lt;p&gt;I do not buy it.&lt;/p&gt;
&lt;p&gt;Standards like ASPICE describe best practices. You need requirements, architecture, design, and tests. You need traceability and verification. But ASPICE does not prescribe how heavyweight those need to be. You can do all of this lean. The standard asks &lt;em&gt;that&lt;/em&gt; you demonstrate rigor, not &lt;em&gt;that&lt;/em&gt; you drown in documents.&lt;/p&gt;
&lt;p&gt;On one project, we replaced huge specification documents with a structured set of few requirements, each linked directly to its test. The auditor accepted it. The team could actually maintain it.&lt;/p&gt;
&lt;p&gt;The real barrier is rarely the regulation. It is the assumption that regulation demands bureaucracy. Once teams realize they can improve incrementally, within the rules, the path opens up.&lt;/p&gt;
&lt;p&gt;I have seen this happen. Teams that thought they were stuck discovered they had more freedom than they assumed. The constraint was not the standard, it was their interpretation of it.&lt;/p&gt;
&lt;p&gt;Kim describes exactly this in &lt;em&gt;The Phoenix Project&lt;/em&gt;: the bottleneck is rarely where you think it is. Often the biggest constraint is not technical or regulatory, it is organizational belief.&lt;/p&gt;
&lt;h2 id=&quot;the-work-that-does-not-show-up-on-the-board&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-work-that-does-not-show-up-on-the-board&quot; aria-label=&quot;Anchor link for: the-work-that-does-not-show-up-on-the-board&quot;&gt;The Work That Does Not Show Up on the Board&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;There is a less obvious effect of this kind of work, one that affects the people writing the code as much as the code itself.&lt;/p&gt;
&lt;p&gt;There is a concept in psychology called self-efficacy: the belief that your actions matter and that you have agency over how you do your work. Research consistently shows it is one of the strongest predictors of motivation, performance, and resilience in knowledge work.&lt;/p&gt;
&lt;p&gt;This is what depth over busyness enables. Not just better software, but better working conditions. The sense of ownership over technical decisions. The tight feedback loop. The freedom to explore and recover on your own terms.&lt;/p&gt;
&lt;p&gt;The best developers I have worked with all share one quality: they see work that others overlook.&lt;/p&gt;
&lt;p&gt;They notice that a module has grown too complex, that an abstraction no longer fits, that a dependency could be removed. They write the test that will save three hours of debugging next month. They improve the documentation so the next person does not have to reverse-engineer the code. They delete the dead code that everyone else steps around.&lt;/p&gt;
&lt;p&gt;This work rarely shows up on a board. There is no ticket for &quot;make tomorrow&#39;s feature possible.&quot; No story points for removing confusion. But it is often the reason the next feature can be built cleanly instead of hacked on top of existing debt.&lt;/p&gt;
&lt;p&gt;I think of it as gardening. A garden is never &quot;done.&quot; It grows, it changes, and it needs continuous care. Good developers treat codebases the same way. They do not wait for permission to pull weeds. They do not need a feature request to improve the soil. They understand that small, consistent effort prevents the kind of decay that eventually requires a rewrite.&lt;/p&gt;
&lt;p&gt;The job of a software engineer is not to produce code. It is to reduce complexity and create value. Doing fewer things is not laziness, it is judgment. Working at a natural pace is not slowness, it is sustainability. Obsessing over quality is not perfectionism, it is professional responsibility.&lt;/p&gt;
&lt;h2 id=&quot;starting-today&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#starting-today&quot; aria-label=&quot;Anchor link for: starting-today&quot;&gt;Starting Today&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The conditions for this kind of work can be cultivated. Not everywhere, not always, but more often than we assume. It starts with one project, one team willing to experiment with fewer meetings, smaller batches, more trust.&lt;/p&gt;
&lt;p&gt;Newport, Kim, Farley, and Martin come from different worlds. But they all arrived at the same place: less, but better. The only question left is whether we are willing to do less, so we can build something that matters.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>When &#39;It Works&#39; Is No Longer Enough</title>
        <published>2026-01-15T00:00:00+00:00</published>
        <updated>2026-01-15T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/when-it-works-is-no-longer-enough/"/>
        <id>https://lmilz.dev/blog/when-it-works-is-no-longer-enough/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/when-it-works-is-no-longer-enough/">&lt;h2 id=&quot;background-story&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#background-story&quot; aria-label=&quot;Anchor link for: background-story&quot;&gt;Background Story&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;It was a relaxed evening on vacation. No todo list, no deadline, no code review. Just me, my laptop, and the idea to code something for fun.&lt;/p&gt;
&lt;p&gt;Spoiler: It didn&#39;t stay simple. But that&#39;s exactly why this story is worth telling.&lt;/p&gt;
&lt;p&gt;A few days earlier, I had found my old bachelor thesis while cleaning up. The topic back then was a particle simulation. Geometric shapes get randomly distributed in a 2D space. When two particles overlap, they push each other away. Over time, order emerges from chaos. Physicists call this Random Organization.&lt;/p&gt;
&lt;p&gt;I really enjoyed that project back then. There were conference talks, even a published paper. I was proud of the physics results.&lt;/p&gt;
&lt;p&gt;Less proud of the code.&lt;/p&gt;
&lt;p&gt;How did I develop back then? Chaotic. No version control, no tests, no documentation. The result: The code is gone. No backups, nothing.&lt;/p&gt;
&lt;p&gt;But the idea was still there. And now I also had the knowledge of how to do it better. So that evening, I started rewriting the simulation. This time properly.&lt;/p&gt;
&lt;h2 id=&quot;rapid-prototyping&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#rapid-prototyping&quot; aria-label=&quot;Anchor link for: rapid-prototyping&quot;&gt;Rapid Prototyping&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Was my old code bad? Yes. But that&#39;s okay. Back then I was a physicist, not a developer. Code was a means to an end.&lt;/p&gt;
&lt;p&gt;Today, after several years as a professional developer, I see it differently. Code is communication with the computer, with other developers, with my future self.&lt;/p&gt;
&lt;p&gt;So I started fresh. Quick, without thinking too much. Get something running first. The prototype worked. The simulation did what it was supposed to. But I wanted to do better. So I started refactoring.&lt;/p&gt;
&lt;h2 id=&quot;design-patterns-bringing-structure-to-chaos&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#design-patterns-bringing-structure-to-chaos&quot; aria-label=&quot;Anchor link for: design-patterns-bringing-structure-to-chaos&quot;&gt;Design Patterns: Bringing Structure to Chaos&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;The prototype worked. But reading through the code, I realized this can be better. Over the past years, I had learned a lot about design patterns and used many. Patterns aren&#39;t magic. They&#39;re documented solutions to problems other developers had before me. Why reinvent the wheel?&lt;/p&gt;
&lt;p&gt;So I looked at the code again. Where does something repeat? Where is the code inflexible? Where are potential bugs lurking?&lt;/p&gt;
&lt;h3 id=&quot;1-factory-pattern-three-methods-are-two-too-many&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#1-factory-pattern-three-methods-are-two-too-many&quot; aria-label=&quot;Anchor link for: 1-factory-pattern-three-methods-are-two-too-many&quot;&gt;1. Factory Pattern: Three Methods Are Two Too Many&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;In the prototype, I had three almost identical methods:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; Simulation&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;initCircleSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;uint16_t&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; num&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; Simulation&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;initRectangleSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;uint16_t&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; num&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; Simulation&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;initSquareSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;uint16_t&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; num&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Classic Copy Paste Programming.&lt;/p&gt;
&lt;p&gt;Plus there was a string comparison for selecting the type:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;circle&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    initCircleSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;num_particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;else if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;rectangle&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    initRectangleSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;num_particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;else if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ==&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;square&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    initSquareSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;num_particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;else&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    initCircleSetup&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;cfg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;num_particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // Silent fallback&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What happens with a typo? &quot;cicle&quot; instead of &quot;circle&quot;? The program silently falls back to the default. No warning, no exception. The bug only gets discovered hours later during debugging.&lt;/p&gt;
&lt;p&gt;After refactoring, the code looks like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ShapeType.hpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;enum class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; ShapeTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    Rectangle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    Square&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; ShapeType&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    static&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; create&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;ShapeTypes&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; type&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                       double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; size1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; size2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        switch&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;type&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;            case&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; ShapeTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; size1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;            case&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; ShapeTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;Rectangle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Rectangle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; size1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; size2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;            case&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; ShapeTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;Square&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;                return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; Square&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; size1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        throw&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;invalid_argument&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;Unknown shape type&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Simulation.cpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; Simulation&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;initParticles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;uint16_t&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; num&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;uniform_real_distribution&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; xDist&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;uniform_real_distribution&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;double&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; yDist&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;reserve&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;num&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;uint16_t&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span&gt; num&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;        Vec&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;xDist&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;rng&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; yDist&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;rng&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;push_back&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;ShapeType&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;create&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;shape_type&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The result: Three methods become one. The DRY principle (Don&#39;t Repeat Yourself) in action. Plus type safety through the enum class. A typo in &lt;code&gt;ShapeTypes::Cicle&lt;/code&gt; leads to a compiler error, not a silent bug at runtime.&lt;/p&gt;
&lt;p&gt;Shape creation is now in a central place. If the creation changes, for example because Circle now needs two parameters instead of one, I change it in one place. Not three. And a new shape? Extend the enum, add a case in the factory, done.&lt;/p&gt;
&lt;h3 id=&quot;2-strategy-pattern-more-than-just-true-or-false&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#2-strategy-pattern-more-than-just-true-or-false&quot; aria-label=&quot;Anchor link for: 2-strategy-pattern-more-than-just-true-or-false&quot;&gt;2. Strategy Pattern: More Than Just True or False&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The next problem was the boundary condition. In the prototype, I only had a boolean:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Simulation&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool&lt;/span&gt;&lt;span&gt; periodicBoundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // true or false, nothing more&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That works for the simple case. But what if I want reflective boundaries or hard walls? With a boolean, that&#39;s not possible. I&#39;d need an enum and then a switch statement that grows with every new boundary. The Simulation class would keep getting bloated.&lt;/p&gt;
&lt;p&gt;The solution: The Strategy Pattern. Instead of having the logic in the Simulation class, it gets extracted into separate classes. The simulation only knows the interface and doesn&#39;t know which concrete implementation is behind it.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Boundary.hpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;enum class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; BoundaryTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    Hardwall&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    Periodic&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    Reflective&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Boundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    virtual&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; ~Boundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    virtual void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; particle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; PeriodicBoundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Boundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; particle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const override&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; ReflectiveBoundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Boundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; particle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const override&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; HardwallBoundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Boundary&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; apply&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; particle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const override&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// Simulation.cpp&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; Simulation&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;randomPush&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; particle1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; particle2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // displacement logic&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    move&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;particle1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; displacement&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    move&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;particle2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; displacement&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Apply boundary&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    boundary_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;apply&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;particle1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    boundary_&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;apply&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;particle2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The beauty of this: The Simulation class never needs to be touched again when a new boundary is added. That&#39;s the Open/Closed Principle in practice. The code is open for extension but closed for modification.&lt;/p&gt;
&lt;p&gt;Plus: Each strategy can be tested in isolation. A unit test for PeriodicBoundary, one for ReflectiveBoundary, without having to spin up the entire simulation.&lt;/p&gt;
&lt;h3 id=&quot;3-builder-pattern-configuration-without-silent-failures&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#3-builder-pattern-configuration-without-silent-failures&quot; aria-label=&quot;Anchor link for: 3-builder-pattern-configuration-without-silent-failures&quot;&gt;3. Builder Pattern: Configuration Without Silent Failures&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;For the simulation configuration, I had a simple struct:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;SimulationConfig config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;num_particles&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_width&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;area_height&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 50&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;max_displacement&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0.5&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;max_iterations&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 5000&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;boundary_type&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; BoundaryTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;Periodic&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Simulation&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; sim&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;That works. But what if someone forgets to set num_particles? Division by zero, somewhere deep in the code.&lt;/p&gt;
&lt;p&gt;The solution is the Builder Pattern. Instead of manipulating the struct directly, there&#39;s a builder that gets configured step by step and validates the input:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;auto&lt;/span&gt;&lt;span&gt; config&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; SimulationConfiguration&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;withShapeType&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;ShapeTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;withParticles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;50&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;withArea&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;50.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 50.0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;withMaxDisplacement&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0.5&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;withMaxIterations&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;5000&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;withBoundaryType&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;BoundaryTypes&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;Hardwall&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    .&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;build&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Simulation&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; sim&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;config&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The trick: The SimulationConfig struct has sensible default values. In build(), it checks if all values are valid. Is a value invalid? Exception. No silent failure. Plus, the code reads almost like a sentence. withParticles(50) is clearer than config.num_particles = 50. The methods document themselves.&lt;/p&gt;
&lt;h2 id=&quot;the-moment-everything-collapsed&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-moment-everything-collapsed&quot; aria-label=&quot;Anchor link for: the-moment-everything-collapsed&quot;&gt;The Moment Everything Collapsed&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I was proud. Factory, Strategy, Builder. Everything there. The code looked professional. I even wrote tests. Time for a demo.&lt;/p&gt;
&lt;p&gt;Circles? Perfect.
Rectangles? Works.
Circles AND Rectangles mixed? Segmentation Fault.&lt;/p&gt;
&lt;p&gt;No warning. No exception. Just crash.&lt;/p&gt;
&lt;p&gt;I stared at the screen. &quot;This can&#39;t be. I did everything right!&quot;&lt;/p&gt;
&lt;p&gt;After a debugging session, I found the culprit. The classes for geometric shapes are built according to classic inheritance hierarchy:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    virtual&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; ~Shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; default&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    virtual bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; overlaps&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; other&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;protected&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Vec&lt;/span&gt;&lt;span&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;public&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; overlaps&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; other&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const override&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;private&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    double&lt;/span&gt;&lt;span&gt; radius&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem lurked in the overlaps implementation:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;overlaps&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; other&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Circle&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;*&lt;/span&gt;&lt;span&gt; otherCircle&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; = dynamic_cast&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span&gt; Circle&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;other&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Vec&lt;/span&gt;&lt;span&gt; diff&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; position&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; otherCircle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // CRASH!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    double&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; diff&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;magnitude&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; distance&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;radius&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; otherCircle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;radius&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The problem: dynamic_cast returns nullptr if other is not a Circle. And what do I do? I dereference the pointer directly without checking. Classic mistake.&lt;/p&gt;
&lt;p&gt;The quick solution would be a nullptr check:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!&lt;/span&gt;&lt;span&gt;otherCircle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    throw&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;invalid_argument&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt;&amp;quot;Circle::overlaps called with non-Circle&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But that felt wrong. I had put a lot of effort into design patterns and now I&#39;m supposed to add nullptr checks everywhere? Throw runtime exceptions? That&#39;s exactly the kind of defensive code I wanted to avoid.&lt;/p&gt;
&lt;p&gt;The problem wasn&#39;t the implementation. The problem was the design.&lt;/p&gt;
&lt;h2 id=&quot;the-search-for-a-better-solution&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-search-for-a-better-solution&quot; aria-label=&quot;Anchor link for: the-search-for-a-better-solution&quot;&gt;The Search for a Better Solution&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;I researched. How do other developers solve this problem?&lt;/p&gt;
&lt;p&gt;Double Dispatch? Possible, but lots of boilerplate.
Visitor Pattern? Works, but even more code.&lt;/p&gt;
&lt;p&gt;And then I came across std::variant.&lt;/p&gt;
&lt;p&gt;A feature from C++17 that I had heard of but never really understood. It comes from functional programming and solves exactly my problem. Not with runtime checks, but at compile time.&lt;/p&gt;
&lt;h3 id=&quot;what-is-a-sum-type&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-is-a-sum-type&quot; aria-label=&quot;Anchor link for: what-is-a-sum-type&quot;&gt;What Is a Sum Type?&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;std::variant is a sum type: A value can be exactly ONE of several types. Not &quot;maybe one&quot;, not &quot;multiple simultaneously&quot;. Exactly one. Always. No nullptr.&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;variant&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;string&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 42&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;         // now int&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 3.14&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;       // now double&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;value&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;hello&amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // now string&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The crucial difference to classic inheritance: With inheritance, the type hierarchy is open. Anyone can derive new classes from Shape. That makes it easy to add new types. But adding a new operation? Then every class needs to be touched. Every class needs a new method.&lt;/p&gt;
&lt;p&gt;With sum types, it&#39;s the opposite. The type set is closed. All types are known at compile time. That makes it harder to add new types because all handlers need to be adjusted. But a new operation? Just write a new handler. The existing code stays untouched.&lt;/p&gt;
&lt;p&gt;For my simulation, that means: My shapes are known. Circle, Rectangle, Square. New shapes? Rare. But new operations? Overlap check, rendering, collision response? Those come all the time.&lt;/p&gt;
&lt;p&gt;Sum types fit better here than inheritance.&lt;/p&gt;
&lt;p&gt;The killer feature: The compiler enforces completeness. Forget a case, you get a compiler error. Not a crash at runtime. Not a nullptr. An error at compile time.&lt;/p&gt;
&lt;h3 id=&quot;from-classes-to-data-structs&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#from-classes-to-data-structs&quot; aria-label=&quot;Anchor link for: from-classes-to-data-structs&quot;&gt;From Classes to Data Structs&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Before, I had a class hierarchy:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; virtual bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; overlaps&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;...&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; };&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Circle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt; ... &lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;class&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Rectangle&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; :&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; public&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt; ... &lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;vector&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;unique_ptr&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span&gt; particles&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;After, I have pure data structs:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CircleData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Vec&lt;/span&gt;&lt;span&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    double&lt;/span&gt;&lt;span&gt; radius&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    CircleData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) :&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; radius&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;r&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RectangleData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Vec&lt;/span&gt;&lt;span&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    double&lt;/span&gt;&lt;span&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    double&lt;/span&gt;&lt;span&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    RectangleData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; w&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; h&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;        :&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; width&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;w&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; height&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;h&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SquareData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;    Vec&lt;/span&gt;&lt;span&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    double&lt;/span&gt;&lt;span&gt; size&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    SquareData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; double&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) :&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; size&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;using&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;variant&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;CircleData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RectangleData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SquareData&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;&amp;gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Since shapes are now pure data structs, not classes with methods, operations are implemented as free functions:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Vec&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; getPosition&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;visit&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([](&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const auto&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;position&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; },&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; setPosition&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;visit&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;](&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;span&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;position&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; pos&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; },&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;void&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; move&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; delta&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;    std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;visit&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;([&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;delta&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;](&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;auto&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;span&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;position&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; shape&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span&gt;position&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; delta&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;; },&lt;/span&gt;&lt;span&gt; s&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;For collision detection, we create a handler with overloaded operator():&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; OverlapHandler&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CircleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CircleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;	// Implementation of circle-circle collision&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RectangleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RectangleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;	// Implementation of rectangle-rectangle collision&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SquareData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; SquareData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;	// Implementation of square-square collision&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CircleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RectangleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;        // Implementation of circle-rectangle collision&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    bool operator&lt;/span&gt;&lt;span&gt;()&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; RectangleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; r&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CircleData&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;this&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)(&lt;/span&gt;&lt;span&gt;c&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; r&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // Symmetric&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // more combinations&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;bool&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; overlaps&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Shape&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;{&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;visit&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;OverlapHandler{}&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;What does all this bring? The most obvious advantage is type safety. With inheritance, type checking happens at runtime with dynamic_cast. With std::variant, it happens at compile time. A forgotten case leads to a compiler error, not a crash at 3 AM in production.&lt;/p&gt;
&lt;p&gt;Plus: There&#39;s no nullptr anymore. The value is always one of the defined types. Never null, never undefined. That alone eliminates an entire category of bugs.&lt;/p&gt;
&lt;p&gt;Performance is also a factor. Inheritance needs virtual dispatch. The compiler doesn&#39;t know which method gets called, that&#39;s decided at runtime. With std::variant, the compiler knows exactly which function gets called. Direct call instead of indirection. Plus, the data sits on the stack instead of scattered on the heap. More cache-friendly, faster.&lt;/p&gt;
&lt;p&gt;The only downside: Adding new types is more expensive. Every handler needs to be adjusted. But for my simulation with three known shapes, that wasn&#39;t a problem.&lt;/p&gt;
&lt;h2 id=&quot;what-i-really-learned&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-i-really-learned&quot; aria-label=&quot;Anchor link for: what-i-really-learned&quot;&gt;What I Really Learned&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This project taught me more than just C++ features. It changed how I think about software design.&lt;/p&gt;
&lt;h3 id=&quot;1-it-works-is-not-the-same-as-it-s-good&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#1-it-works-is-not-the-same-as-it-s-good&quot; aria-label=&quot;Anchor link for: 1-it-works-is-not-the-same-as-it-s-good&quot;&gt;1. &quot;It Works&quot; Is Not the Same as &quot;It&#39;s Good&quot;&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;My prototype worked. But the difference between working code and good code is the time I invest in refactoring. Not because someone demands it, but because I want to do better.&lt;/p&gt;
&lt;h3 id=&quot;2-design-patterns-are-not-an-academic-exercise&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#2-design-patterns-are-not-an-academic-exercise&quot; aria-label=&quot;Anchor link for: 2-design-patterns-are-not-an-academic-exercise&quot;&gt;2. Design Patterns Are Not an Academic Exercise&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;Patterns are documented solutions to problems others had before me. Why reinvent the wheel?&lt;/p&gt;
&lt;h3 id=&quot;3-paradigms-are-tools-not-religions&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#3-paradigms-are-tools-not-religions&quot; aria-label=&quot;Anchor link for: 3-paradigms-are-tools-not-religions&quot;&gt;3. Paradigms Are Tools, Not Religions&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;OOP or FP? The answer is: Both. My final code uses OOP patterns for structure and FP concepts for type safety. The best solution is often a hybrid.&lt;/p&gt;
&lt;h3 id=&quot;4-the-compiler-is-my-friend&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#4-the-compiler-is-my-friend&quot; aria-label=&quot;Anchor link for: 4-the-compiler-is-my-friend&quot;&gt;4. The Compiler Is My Friend&lt;/a&gt;&lt;/h3&gt;
&lt;p&gt;The more I can check at compile time, the less can go wrong at runtime. Type-safe enums instead of strings. std::variant instead of inheritance plus dynamic_cast. Every compiler error is a bug I don&#39;t have to debug.&lt;/p&gt;
&lt;h2 id=&quot;conclusion-why-it-s-worth-it&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#conclusion-why-it-s-worth-it&quot; aria-label=&quot;Anchor link for: conclusion-why-it-s-worth-it&quot;&gt;Conclusion: Why It&#39;s Worth It&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;What started as a relaxed vacation evening became several weeks of work. Was that too much for a hobby project? Maybe. But it was about getting better. Not perfect. Better.&lt;/p&gt;
&lt;p&gt;I learned how std::variant works. I understood when OOP and when FP fits. I experienced that refactoring isn&#39;t a waste of time.&lt;/p&gt;
&lt;p&gt;And most importantly: It was fun again.&lt;/p&gt;
&lt;p&gt;No deadline. No code review. No stakeholders. Just me, the code, and the question: &quot;How can I make this better?&quot;&lt;/p&gt;
&lt;p&gt;This freedom is often missing in daily work. That makes it all the more important to find it in hobby projects.&lt;/p&gt;
&lt;p&gt;The complete code is available on &lt;a rel=&quot;external&quot; href=&quot;https://github.com/lmilz/random-organization&quot;&gt;GitHub&lt;/a&gt; and &lt;a rel=&quot;external&quot; href=&quot;https://codeberg.org/lmilz/random-organization&quot;&gt;Codeberg&lt;/a&gt;.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>From Defensive Programming to Type-Driven Design</title>
        <published>2025-12-11T00:00:00+00:00</published>
        <updated>2025-12-11T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/from-defensive-programming-to-type-driven-design/"/>
        <id>https://lmilz.dev/blog/from-defensive-programming-to-type-driven-design/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/from-defensive-programming-to-type-driven-design/">&lt;h2 id=&quot;the-test-that-never-ran&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-test-that-never-ran&quot; aria-label=&quot;Anchor link for: the-test-that-never-ran&quot;&gt;The Test That Never Ran&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let me be clear upfront: this isn&#39;t about &quot;Rust is better than C.&quot; It&#39;s about recognizing patterns in how we ensure safety. Some languages require discipline and process. Others provide guarantees through their type system. Understanding the difference helps us make better choices about where to invest our effort.&lt;/p&gt;
&lt;p&gt;I was working on a linear algebra library in Rust (yes, the 1000th one) when I wrote a test for matrix addition with mismatched dimensions. &lt;code&gt;cargo build&lt;/code&gt; compiled fine, but &lt;code&gt;cargo test&lt;/code&gt; surprised me: the test never ran. Instead:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;error[E0308]:&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; mismatched types&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;   --&lt;/span&gt;&lt;span&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; src/matrix.rs:122:29&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    |&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;122&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; |&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;         let&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; result = mat1 + mat2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;    |&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;                             ^^^^&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; expected&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; `&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;`&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; found&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; `&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;`&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;My first thought was: &quot;Okay, I need to add a check in the library...&quot; Then I stopped. Rust had already caught the error. At compile time. I didn&#39;t need to check anything.&lt;/p&gt;
&lt;p&gt;This is Rust&#39;s famous safety, I thought. In C or C++, I would need error handling to catch this case. But then I realized: is that really true? In C, definitely. But in C++, maybe not? This made me think about how different languages handle errors, and more importantly, when those errors are caught. The answer reveals fundamental design philosophies.&lt;/p&gt;
&lt;h2 id=&quot;the-three-philosophies-of-error-handling&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-three-philosophies-of-error-handling&quot; aria-label=&quot;Anchor link for: the-three-philosophies-of-error-handling&quot;&gt;The Three Philosophies of Error Handling&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Before diving into specific languages, let&#39;s establish what we&#39;re actually talking about. Error handling isn&#39;t just about preventing crashes. It&#39;s about where and how we detect problems.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Compile-Time Detection&lt;/strong&gt; means errors are caught by the compiler before the program runs. Type mismatches, invalid operations, memory safety violations: these never make it into production.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Runtime Detection&lt;/strong&gt; means errors are detected during execution. Invalid inputs, resource failures, unexpected states: these require explicit handling in code.&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;No Detection&lt;/strong&gt; is the most dangerous category. Undefined behavior, silent corruption, race conditions: errors that slip through and cause unpredictable failures.&lt;/p&gt;
&lt;p&gt;Each language chooses a different balance. That choice defines not just how we write code, but how we think about safety, performance, and maintainability. Let me show you what this means in practice.&lt;/p&gt;
&lt;h2 id=&quot;the-c-reality-check&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-c-reality-check&quot; aria-label=&quot;Anchor link for: the-c-reality-check&quot;&gt;The C Reality Check&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Professionally, I write a lot of C for automotive ECUs. The approach in C to error handling is straightforward: it gives you nothing. No exceptions, no type system enforcement, no safety nets. Just return codes and discipline. So how would I solve the same matrix problem there?&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;typedef struct&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    int&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; rows&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    size_t&lt;/span&gt;&lt;span&gt; cols&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;span&gt; Matrix&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; matrix_add&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;Matrix&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span&gt; Matrix&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; const&lt;/span&gt;&lt;span&gt; Matrix&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 1: Null pointer?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!&lt;/span&gt;&lt;span&gt;result &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;|| !&lt;/span&gt;&lt;span&gt;a &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;|| !&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ERR_NULL_POINTER&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 2: Null data?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; || !&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ERR_INVALID_DATA&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 3: Dimension mismatch?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;rows&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; !=&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;rows&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; ||&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;cols&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; !=&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;cols&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ERR_DIMENSION_MISMATCH&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 4: Result buffer allocated?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ERR_NO_MEMORY&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Finally: The actual addition&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;size_t&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span&gt; i &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;rows&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;cols&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;++&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span&gt; SUCCESS&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Four defensive checks for a simple operation. And this is just the function itself. Every caller must also check:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Matrix mat1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; mat2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ... initialization ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;int&lt;/span&gt;&lt;span&gt; err &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;=&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; matrix_add&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;mat1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt;mat2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;err &lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!=&lt;/span&gt;&lt;span&gt; SUCCESS&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Error handling&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    switch&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;err&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        case&lt;/span&gt;&lt;span&gt; ERR_NULL_POINTER&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        case&lt;/span&gt;&lt;span&gt; ERR_DIMENSION_MISMATCH&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt; /* ... */&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;        // ...&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This is defensive programming in its purest form. I must anticipate every conceivable error and catch it. Every function is half error handling, half logic.&lt;/p&gt;
&lt;p&gt;What this means in practice is straightforward but demanding. Every function that can fail returns an error code. Every caller must check that code. Forgetting a check? Undefined behavior. Passing wrong types? Undefined behavior. Race condition? Undefined behavior.&lt;/p&gt;
&lt;p&gt;In the field of automotive software, this becomes critical. MISRA C exists precisely because C requires perfect discipline. We compensate with strict coding guidelines, static analysis tools, and extensive code reviews. But these are processes trying to replace what the language doesn&#39;t provide.&lt;/p&gt;
&lt;h2 id=&quot;the-automotive-reality-autosar-misra-and-iso-26262&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-automotive-reality-autosar-misra-and-iso-26262&quot; aria-label=&quot;Anchor link for: the-automotive-reality-autosar-misra-and-iso-26262&quot;&gt;The Automotive Reality: AUTOSAR, MISRA, and ISO 26262&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let me be clear about something: I&#39;m simplifying. The automotive software world is more structured than &quot;just use C everywhere.&quot;&lt;/p&gt;
&lt;p&gt;We have AUTOSAR Classic for traditional ECUs, built on C with strict architectural patterns. We have AUTOSAR Adaptive for high-performance computing platforms, which allows modern C++. These aren&#39;t just coding standards. They&#39;re complete software architectures that define how components communicate, how resources are managed, and how safety is achieved.&lt;/p&gt;
&lt;p&gt;The defensive checks I described aren&#39;t random. They&#39;re systematically required by these architectures. AUTOSAR&#39;s Runtime Environment expects certain error codes. Safety standards like ISO 26262 require certain failure detection mechanisms. MISRA C and MISRA C++ define exactly which language features we can use.&lt;/p&gt;
&lt;p&gt;So when I talk about defensive programming versus type-driven design, I&#39;m not suggesting we abandon these standards tomorrow. I&#39;m asking: within these frameworks, where could better language features reduce the burden?&lt;/p&gt;
&lt;p&gt;Could AUTOSAR runnables benefit from safer type systems? Could the communication between software components use types instead of runtime validation? Could we use modern C++ features where AUTOSAR Adaptive allows them? Features like &lt;code&gt;std::optional&lt;/code&gt;, &lt;code&gt;std::variant&lt;/code&gt;, or constexpr validation could reduce defensive checks while staying within the standard, instead of staying in the C-style comfort zone.&lt;/p&gt;
&lt;h2 id=&quot;the-daily-cost-of-defensive-code&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-daily-cost-of-defensive-code&quot; aria-label=&quot;Anchor link for: the-daily-cost-of-defensive-code&quot;&gt;The Daily Cost of Defensive Code&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This pattern appears everywhere in typical C codebases. Whether I look at open source projects, tutorial code, or professional embedded software, the ratio is similar.&lt;/p&gt;
&lt;p&gt;Let me show you a more automotive-specific example. Consider CAN message handling, something every ECU developer knows:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;c&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;typedef struct&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    uint32_t&lt;/span&gt;&lt;span&gt; id&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    uint8_t&lt;/span&gt;&lt;span&gt; data&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    uint8_t&lt;/span&gt;&lt;span&gt; length&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;span&gt; CANMessage&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;int&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; validate_and_send_can_message&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span&gt; CANMessage&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;font-style: italic;&quot;&gt; msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 1: Message exists?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;!&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; ERR_NULL_POINTER&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 2: Valid CAN ID?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1FFFFFFF&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; ERR_INVALID_ID&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 3: Valid length?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;length&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; return&lt;/span&gt;&lt;span&gt; ERR_INVALID_LENGTH&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Check 4: Standard vs Extended ID consistency?&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    if&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;7FF&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&amp;amp; !&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;-&amp;gt;&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; 0x&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;80000000&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)) {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        return&lt;/span&gt;&lt;span&gt; ERR_ID_FORMAT_MISMATCH&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;    // Finally: Send the message&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    return&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; can_driver_send&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;msg&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Every CAN stack has similar functions. Most of the code is validation, not communication. And every project implements these checks slightly differently, leading to subtle bugs across teams.&lt;/p&gt;
&lt;p&gt;In my experience with typical embedded C codebases, defensive checks and cleanup logic often make up more than half of the code. The actual business logic gets buried under layers of safety validation. And here&#39;s the dangerous part: forget a single check or a cleanup path, and you have a bug. Every code review must verify these paths. Every test must cover them. This is the cost of safety in C.&lt;/p&gt;
&lt;p&gt;Now imagine if the type system could enforce this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; StandardCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u16&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // 11-bit, 0x000-0x7FF&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; ExtendedCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u32&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // 29-bit&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;enum&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    Standard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;StandardCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;    Extended&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;ExtendedCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;struct&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANMessage&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; usize&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;],&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // Length is part of the type&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;impl&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;const&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; N&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; usize&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANMessage&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;N&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; send&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Result&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(),&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANError&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;        // No validation needed - invalid messages can&amp;#39;t be constructed&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        can_driver_send&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;self&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// This compiles:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;let&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANMessage&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANId&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;Standard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;StandardCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0x123&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// This doesn&amp;#39;t - length is wrong:&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;let&lt;/span&gt;&lt;span&gt; msg&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANMessage&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANId&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;Standard&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;StandardCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0x123&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)),&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    data&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; [&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;u8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 9&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;  // Compiler error!&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;};&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The validation isn&#39;t gone. It&#39;s moved to the constructor of &lt;code&gt;StandardCANId&lt;/code&gt; and &lt;code&gt;ExtendedCANId&lt;/code&gt;:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;impl&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; StandardCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; new&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;&quot;&gt;id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;:&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; u16&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Result&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F38BA8;&quot;&gt;Self&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; CANError&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        if&lt;/span&gt;&lt;span&gt; id&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0x7FF&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;            return&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Err&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;CANError&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;InvalidStandardId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;        }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;        Ok&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;StandardCANId&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;id&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;))&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Once you have a valid &lt;code&gt;StandardCANId&lt;/code&gt;, the compiler guarantees it stays valid. The type system enforces CAN protocol constraints.&lt;/p&gt;
&lt;p&gt;This is conceptual. In practice, you&#39;d still need unsafe code to interface with the CAN hardware driver. But the type safety lives in the application layer, where most bugs actually happen.&lt;/p&gt;
&lt;p&gt;Now, I need to be realistic here. Rust in automotive production is still early days. There are no ISO 26262 qualified Rust compilers yet. Tool qualification is expensive and time-consuming. The automotive industry moves slowly, and for good reasons.&lt;/p&gt;
&lt;p&gt;Rust also has &lt;code&gt;unsafe&lt;/code&gt; blocks where you explicitly opt out of safety guarantees. You still need discipline there. And Rust doesn&#39;t prevent logic errors or deadlocks. It&#39;s not magic.&lt;/p&gt;
&lt;p&gt;But here&#39;s what matters: Rust demonstrates that compile-time safety and zero-cost abstractions are possible. Even if we can&#39;t use Rust in production tomorrow, it shows us what modern C++ could be if we used it more fully within AUTOSAR Adaptive contexts.&lt;/p&gt;
&lt;h2 id=&quot;the-shift-in-thinking&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-shift-in-thinking&quot; aria-label=&quot;Anchor link for: the-shift-in-thinking&quot;&gt;The Shift in Thinking&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This is the moment that hit me. In C, I think: &quot;What can go wrong? What checks do I need?&quot; In Rust, I think: &quot;What do I want to express? What type describes that?&quot;&lt;/p&gt;
&lt;p&gt;This is not just less code. It&#39;s a fundamental difference in mental model. With defensive programming in C, I catch errors at runtime. Every developer must think about every edge case. I write a lot of code for safety, and testing proves correctness.&lt;/p&gt;
&lt;p&gt;The difference is profound. In defensive programming, safety is a property I must actively maintain through discipline and process. In type-driven design, safety is a property the language guarantees through its type system. With type-driven design in Rust and modern C++, I make certain classes of errors impossible at compile time. The compiler prevents type mismatches, null pointer dereferences, and memory safety violations. Safety comes through design, and types prove these specific properties. They don&#39;t catch logic errors or algorithmic mistakes, but they eliminate entire categories of bugs that plague C codebases.&lt;/p&gt;
&lt;h2 id=&quot;the-automotive-perspective&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-automotive-perspective&quot; aria-label=&quot;Anchor link for: the-automotive-perspective&quot;&gt;The Automotive Perspective&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In typical automotive ECU code, we have thousands of defensive checks scattered throughout the codebase. Everywhere you look, you see the same patterns. Null pointer checks, bounds checks, range checks, state validation checks. All necessary, all manually written, all manually verified in code reviews.&lt;/p&gt;
&lt;p&gt;With MISRA C and MISRA C++, code reviews, and static analysis, we try to ensure that no check is forgotten. This is process as a replacement for language features. We build elaborate systems to catch what the language cannot.&lt;/p&gt;
&lt;p&gt;In Rust or modern C++, many defensive checks would take different forms:&lt;/p&gt;
&lt;p&gt;Null pointer checks could become &lt;code&gt;Option&amp;lt;T&amp;gt;&lt;/code&gt; (explicitly marking that a value might be absent) or references (guaranteeing a value exists). Bounds checks could become compile-time &lt;code&gt;const&lt;/code&gt; sizes or explicit &lt;code&gt;.get()&lt;/code&gt; calls that return &lt;code&gt;Option&lt;/code&gt;. State machines could use the type-state pattern, where each state is a different type and transitions are enforced by the compiler. Value ranges could use newtype patterns, where a type like &lt;code&gt;Speed0to100(u8)&lt;/code&gt; can only be constructed with valid values.&lt;/p&gt;
&lt;p&gt;These aren&#39;t just different syntax. They shift validation from runtime checks scattered throughout the code to compile-time constraints or explicit construction points.&lt;/p&gt;
&lt;h2 id=&quot;the-practical-takeaway&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-practical-takeaway&quot; aria-label=&quot;Anchor link for: the-practical-takeaway&quot;&gt;The Practical Takeaway&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;My realization is not &quot;Rust is better than C.&quot; It is this: I am spending time writing defensive code that would be unnecessary in modern languages.&lt;/p&gt;
&lt;p&gt;This doesn&#39;t mean I should rewrite all our C code tomorrow. But it changes how I think about new components. For the hardware layer, C remains essential for direct register access. There&#39;s no choice there. For the safety layer, I must invest in defensive checks because the language requires it. But for business logic? There, Rust or modern C++ could replace defensive checks with types.&lt;/p&gt;
&lt;p&gt;This distinction matters. It means I can choose where to invest effort. Where I must use C, I accept the cost of defensive programming. Where I have flexibility, I can leverage type systems to reduce that cost.&lt;/p&gt;
&lt;h2 id=&quot;the-elephant-in-the-room-why-not-switch-tomorrow&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-elephant-in-the-room-why-not-switch-tomorrow&quot; aria-label=&quot;Anchor link for: the-elephant-in-the-room-why-not-switch-tomorrow&quot;&gt;The Elephant in the Room: Why Not Switch Tomorrow?&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Let me address what I&#39;m not saying. I&#39;m not suggesting we rewrite our ECU software in Rust tomorrow. That would be naive for several reasons:&lt;/p&gt;
&lt;p&gt;First, there&#39;s certification. Our tools are qualified according to ISO 26262. Our processes are qualified according to ASPICE. Introducing a new language means re-qualifying everything. That&#39;s years of work and significant investment.&lt;/p&gt;
&lt;p&gt;Second, there&#39;s legacy. We have millions of lines of proven C code. It works. It&#39;s tested. It&#39;s certified. Rewriting it would be expensive and risky.&lt;/p&gt;
&lt;p&gt;Third, there&#39;s the team. We have engineers trained in C and AUTOSAR. Learning Rust or modern C++ takes time. Not everyone will embrace it.&lt;/p&gt;
&lt;p&gt;Fourth, there&#39;s the ecosystem. Our suppliers deliver AUTOSAR components in C. Our customers expect C interfaces. The entire automotive software supply chain is built around C.&lt;/p&gt;
&lt;p&gt;So why am I writing this article? Because I believe we should ask different questions. Not &quot;should we switch to Rust&quot; but &quot;where are we paying unnecessary costs with current tools?&quot;&lt;/p&gt;
&lt;p&gt;Are there new components where we could use AUTOSAR Adaptive with modern C++ features? Are there application layers where type safety would prevent entire bug categories? Are there areas where the process overhead of defensive C programming outweighs the migration cost?&lt;/p&gt;
&lt;p&gt;These are the questions this compiler error made me ask.&lt;/p&gt;
&lt;h2 id=&quot;the-question-i-m-left-with&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-question-i-m-left-with&quot; aria-label=&quot;Anchor link for: the-question-i-m-left-with&quot;&gt;The Question I&#39;m Left With&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;How much of my daily work is actually just manual type checking? How many bugs have I fixed that wouldn&#39;t have compiled in Rust? How much code review time do we spend verifying that all defensive checks are present?&lt;/p&gt;
&lt;p&gt;These are the real questions this compiler error raised. Not &quot;which language is better,&quot; but &quot;how much of my work could be automated?&quot;&lt;/p&gt;
&lt;p&gt;For new projects, I will think more about this: Can I model the problem so that erroneous states are excluded at compile time? For legacy code: Can I at least move the defensive checks into clearly bounded layers instead of scattering them everywhere?&lt;/p&gt;
&lt;p&gt;Looking back, this small moment with a matrix library taught me something fundamental. The goal is not to eliminate error handling. The goal is to move it to where it&#39;s most effective. Sometimes that&#39;s runtime checks with discipline and process. Sometimes that&#39;s compile-time guarantees with types and compilers.&lt;/p&gt;
&lt;p&gt;Knowing the difference and choosing deliberately makes all the difference.&lt;/p&gt;
&lt;h2 id=&quot;what-i-m-taking-away&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#what-i-m-taking-away&quot; aria-label=&quot;Anchor link for: what-i-m-taking-away&quot;&gt;What I&#39;m Taking Away&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;This compiler error taught me to recognize a pattern in my daily work. When I write defensive checks, I should ask: is this a runtime constraint that could fail (like &quot;file not found&quot;), or is it a type constraint that should never occur (like &quot;wrong matrix dimension&quot;)?&lt;/p&gt;
&lt;p&gt;Runtime constraints need runtime checks. That&#39;s fine. But type constraints masquerading as runtime checks? That&#39;s where we&#39;re wasting effort.&lt;/p&gt;
&lt;p&gt;In new code, I&#39;m trying to push more constraints into types. Not because it&#39;s fashionable, but because every type constraint I encode is one less thing I need to check, test, review, and debug.&lt;/p&gt;
&lt;p&gt;In existing code, I&#39;m thinking more about boundaries. Can I at least isolate defensive checks at system boundaries, rather than scattering them throughout? Can I create a safe inner core where certain classes of errors are impossible by construction?&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Why I Do Not Like Linux Distro Hopping but Keep Doing It Anyway</title>
        <published>2025-12-01T00:00:00+00:00</published>
        <updated>2025-12-01T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/why-i-do-not-like-linux-distro-hopping-but-keep-doing-it-anyway/"/>
        <id>https://lmilz.dev/blog/why-i-do-not-like-linux-distro-hopping-but-keep-doing-it-anyway/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/why-i-do-not-like-linux-distro-hopping-but-keep-doing-it-anyway/">&lt;p&gt;I will come out and say it: I do not enjoy distro hopping. Yet I keep ending up in the middle of it. I am not someone who asks a lot from my system. I have been using Linux privately for many years, and while I am not a complete beginner, I am also not the kind of person who compiles their own kernel just for the challenge. For a long time my setup was simple and stable. Xubuntu was my comfort zone. It was lightweight, reliable, and mostly invisible. Over time, though, the Ubuntu ecosystem started to feel increasingly heavy. More layers, more defaults, more decisions made on my behalf, and many of them solving problems I did not have. At some point I realized that I was not learning much anymore. I wanted more transparency, more control, and a deeper understanding of what was happening on my machine.&lt;/p&gt;
&lt;p&gt;That desire led me to EndeavourOS. It offered a good compromise. I gained access to the Arch ecosystem without the full multi-day process of building everything manually. I appreciated the idea of having complete control, but I also needed a practical balance. EndeavourOS delivered exactly that. Enough freedom to stay flexible and enough structure to stay productive.&lt;/p&gt;
&lt;p&gt;Life changed, though. I have kids now, and long evenings spent adjusting configuration files are no longer realistic. I still enjoy tuning my system, but I want to do it intentionally, not endlessly. I want to spend time on decisions that matter, not on choosing between terminal emulators.&lt;/p&gt;
&lt;p&gt;As a software engineer, I learned early that complexity grows when you refuse to let go. In one of my first projects, I created the initial codebase and carried it all the way into production. Over time, holding on too tightly made the architecture rigid. Features accumulated, shortcuts accumulated, and eventually the entire system became heavy. The moment I understood that simplicity is not a downgrade but a professional skill was the moment I finally started to grow. Removing unnecessary code, cleaning up design decisions, and simplifying architecture made the software not only better, but also more enjoyable to maintain.&lt;/p&gt;
&lt;p&gt;Perhaps that mindset pushed me into my next experiment. I switched from EndeavourOS to Linux Mint because I wanted a distribution with lower ongoing maintenance, even if it meant more preinstalled packages. I wrote a Bash script to remove what I did not need. It worked, but the system still felt misaligned. No matter how much I cleaned up, something else surfaced. Mint targets a broad audience, and at some point I realized that I simply am not part of that audience.&lt;/p&gt;
&lt;p&gt;This brought me back to a basic truth about myself: I prefer systems that stay simple. I do not need much. My workflow consists mainly of Obsidian for thinking, Visual Studio Code for coding, and Chrome for browsing. That is essentially everything. The older I get, the clearer it becomes that I want to focus on tools that genuinely add value, not on tools that merely exist.&lt;/p&gt;
&lt;p&gt;One insight became obvious. It is easier to build a system aligned with your priorities from the start than to continuously reshape one that was never designed for them. Switching systems, even when inconvenient, can be useful. It forces you out of your comfort zone and makes you re-evaluate what matters. Every switch makes me rediscover tools and approaches that motivate me again.&lt;/p&gt;
&lt;p&gt;Returning to a simpler setup had an unexpected effect. It made me curious again. With fewer distractions and less default noise, I started experimenting because I wanted to, not because I had to. Exploring, adjusting, and learning suddenly felt effortless again. The system stopped competing for my attention, and that created space for genuine curiosity.&lt;/p&gt;
&lt;p&gt;This shift goes beyond the choice of distribution. It influences how I choose tools in general. I am gradually moving from VS Code to Neovim because it is faster, leaner, and more focused. Another good example is Rust, which I recently started learning and whose built-in tooling aligns perfectly with this mindset. Cargo provides a complete toolchain without the need for external frameworks. Tests live next to the implementation. Clippy improves code quality. Fmt ensures consistency. Even with my browser, I often look for something that does not carry around an entire ecosystem I never interact with.&lt;/p&gt;
&lt;p&gt;The software world is constantly expanding. New abstractions, new frameworks, new tools appear every day, often solving problems that did not need solving. Devcontainers everywhere. Layers on top of layers. CI pipelines that resemble small companies even for personal projects. At some point I realized that I was recreating the same complexity in my own environment that I was trying to avoid. It felt heavy. It felt unnecessary.&lt;/p&gt;
&lt;p&gt;What I want in software and in my personal setup is straightforward. A build script that is enough. An editor that opens instantly. A browser that loads websites without additional overhead. A system that stays invisible until I need it. A setup that respects my attention.&lt;/p&gt;
&lt;p&gt;And that is how I eventually returned to Arch. Not because I want to configure everything in detail. I returned because Arch is built around a simple principle. You choose what matters. It does not impose defaults, it does not push an ecosystem on you, and it remains unobtrusive. When I want to configure something, I can do so without resistance. This type of control feels natural. It is not about endless customization. It is about intentional customization.&lt;/p&gt;
&lt;p&gt;In the end, I realized that distro hopping is not the real issue. It is a signal. I am not looking for the perfect distribution. I am looking for a system that supports the way I think and work. A system that enables focus instead of distracting from it. For me, simplicity is not a limitation. It is a deliberate choice.&lt;/p&gt;
&lt;p&gt;Maybe that is why I keep switching even though I say I do not like it. Not out of frustration, but because each iteration sharpens my understanding of what I actually need. Less noise. More clarity. A system that quietly does its job so I can do mine.&lt;/p&gt;
&lt;p&gt;Simplicity above everything.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Dr. Strangelove or How I Learned to Stop Worrying and Love Testing</title>
        <published>2025-11-15T00:00:00+00:00</published>
        <updated>2025-11-15T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/dr-strangelove-or-how-i-learned-to-stop-worrying-and-love-testing/"/>
        <id>https://lmilz.dev/blog/dr-strangelove-or-how-i-learned-to-stop-worrying-and-love-testing/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/dr-strangelove-or-how-i-learned-to-stop-worrying-and-love-testing/">&lt;p&gt;When I was still programming at university, my “testing” looked roughly like this:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt;std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;cout&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; &amp;quot;Value: &amp;quot;&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span&gt; value&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;lt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;&quot;&gt; std&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;::&lt;/span&gt;&lt;span&gt;endl&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;along with some classic debugging: setting breakpoints, inspecting variables, stepping through the code. Looking back, this was clearly not an effective approach. I don’t even want to know how many bugs went unnoticed back then. Over several professional roles, I gradually improved my testing skills. In my first job, I initially continued exactly as I had done at university. Later, I had the chance to dive deeper into testing. At that time, my workflow looked like this: implement changes, document them, integrate, deploy, and only then start testing. This naturally led to discovering bugs late, being unable to fix them right away, and shipping bugfixes only with the next release.&lt;/p&gt;
&lt;p&gt;This became increasingly frustrating for me. At the same time, I noticed that many developers “automate” their manual debugging process by reproducing the implementation inside their tests. This is a classic anti-pattern: you end up testing the implementation instead of the behavior. If you simply reproduce the production code in your tests, you&#39;re effectively writing the same code twice and that adds no value.&lt;/p&gt;
&lt;p&gt;I started with small steps: whenever I worked on a new feature, I wrote tests immediately. In parallel, I began adding tests to our legacy codebase to gain experience building a meaningful and maintainable test suite.&lt;br /&gt;
Early on, I fell into the usual traps myself: &lt;em&gt;I recreated the implementation in the test&lt;/em&gt; or &lt;em&gt;wrote tests that were so granular that the suite became hard to maintain.&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Along the way, I realized that testing is not just testing. In particular, we distinguish between:&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;Verification: Are we building the product right?&lt;/li&gt;
&lt;li&gt;Validation: Are we building the right product?&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;Consider a simple example: a football field has exact specifications. If those are implemented correctly, verification is satisfied. But whether you can play well on that field is a matter of validation. A field can meet all technical requirements and still be unplayable if the ground is uneven or sloped.&lt;/p&gt;
&lt;p&gt;Eventually, I had a kind of Eureka moment: proper testing is a learning process. In a way, it felt like my own little Dr. Strangelove moment. I stopped worrying about breaking things and started to love testing.  Because once your tests define the expected behavior, fear turns into confidence.
Tests help me understand how the code is meant to behave and provide immediate feedback. The only problem was that my feedback always came too late. So I looked for ways to move it earlier in the process.&lt;/p&gt;
&lt;p&gt;After many iterations, I finally found the answer to my problem: I must not test the implementation, I must test the behavior. The method that enforces exactly this is called Test-Driven Development (TDD).&lt;/p&gt;
&lt;p&gt;TDD is often misunderstood. It’s not a testing technique but a design method. Tests are the tool we use to shape the design. TDD can be used for both verification and validation.&lt;/p&gt;
&lt;p&gt;To demonstrate my approach, I’ll use the calculation of the inverse square root. This function is used to normalize vectors. From this, we can derive verifiable mathematical properties as well as validation tests based on intended behavior. These properties must hold regardless of the internal implementation of &lt;code&gt;inverse_sqrt&lt;/code&gt;. Below is an example of the tests (with the input data &lt;code&gt;inputs&lt;/code&gt; and &lt;code&gt;vectors&lt;/code&gt; omitted for readability):&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// 1. Positivity Test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;//    f(x) &amp;gt; 0 for all x &amp;gt; 0&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; always_positive&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        assert!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// 2. Monotonicity Test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;//    f(x) decreases as x increases&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; monotony&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;..&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        assert!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt; &amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]));&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// 3. Product Rule Test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;//    f(a·b) ≈ f(a)·f(b)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; product_rule&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;..&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; x1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; x2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        assert!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;x1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; x2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;abs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;25&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// 4. Division Rule Test&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;//    f(a/b) ≈ f(a)/f(b)&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; division_rule&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span&gt; i&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;..&lt;/span&gt;&lt;span&gt;inputs&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;len&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span&gt; inputs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span&gt;i&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; x1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; /&lt;/span&gt;&lt;span&gt; b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; x2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;a&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; /&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; inverse_sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;b&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        assert!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;x1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span&gt; x2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;abs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// 5. Validation: Normalization Length&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;//    A vector normalized with this function should have length ≈ 1&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;font-style: italic;&quot;&gt;// ------------------------------------------------------------&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; normalization_length_is_one&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;    for&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; z&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;amp;&lt;/span&gt;&lt;span&gt;vectors&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; normalize&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;x&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; y&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; z&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;        let&lt;/span&gt;&lt;span&gt; len&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; (&lt;/span&gt;&lt;span&gt;n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; +&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt; n&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;sqrt&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;        assert!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;((&lt;/span&gt;&lt;span&gt;len&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;abs&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt; &amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 1&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;e-&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;6&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;    }&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Now it gets practical: you write the implementation and the tests immediately verify whether it behaves as expected. This instant feedback is one of the greatest strengths of TDD. The test run produces results like these:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;shellscript&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;running&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; tests&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; tests::monotony ... ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; tests::division_rule ... ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; tests::always_positive ... ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; tests::normalization_length_is_one ... ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; tests::product_rule ... ok&lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F38BA8;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; result: ok.&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 5&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; passed&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; failed&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; ignored&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; measured&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; filtered out&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;;&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; finished&lt;/span&gt;&lt;span style=&quot;color: #A6E3A1;&quot;&gt; in 0.00s&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This illustrates the essence of TDD: define the behavior first, then write the implementation that fulfills it.&lt;/p&gt;
&lt;p&gt;Another essential benefit: once tests clearly define the expected behavior, you can refactor the implementation without fear. The tests act as a safety net. They ensure that the visible behavior remains unchanged, even if the internals change dramatically.&lt;/p&gt;
&lt;p&gt;To make this even more concrete, here’s a small example that shows the real power of testing for behavior: you can completely change the implementation, yet the tests and therefore the behavior remain unchanged.&lt;/p&gt;
&lt;p&gt;Consider a simple function that filters out negative numbers. The tests describe what the function must do, not how it must do it:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; filters_out_negative_numbers&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;	let&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; vec!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;1&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 9&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;	let&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; filter_positive&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;input&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;	assert_eq!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; vec!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;3&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 9&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]);&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;span&gt;  &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;#&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt;test&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;]&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; keeps_order&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;() {&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;	let&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; vec!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt;5&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 2&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 8&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;];&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;	let&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; filter_positive&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;&amp;amp;&lt;/span&gt;&lt;span&gt;input&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;	assert_eq!&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span&gt;result&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;,&lt;/span&gt;&lt;span&gt; input&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;These tests specify two behaviors:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Negative numbers must be removed.&lt;/li&gt;
&lt;li&gt;The order of the remaining elements must be preserved.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Now here is the first, simple implementation, fully correct but not particularly elegant:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; filter_positive&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;&quot;&gt;values&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;: &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;i32&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;i32&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;	let mut&lt;/span&gt;&lt;span&gt; result&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; =&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;::&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;new&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;();&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;	for&lt;/span&gt;&lt;span&gt; v&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt; in&lt;/span&gt;&lt;span&gt; values&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;		if&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; *&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt;             &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;			result&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;push&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;*&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;);&lt;/span&gt;&lt;span&gt;         &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;		}&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;	}&lt;/span&gt;&lt;span&gt;     &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	result &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Later, you might want to refactor it into a more idiomatic and functional style:&lt;/p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #CDD6F4; background-color: #1E1E2E;&quot; &gt;&lt;code data-lang=&quot;rust&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;fn&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt; filter_positive&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #EBA0AC;&quot;&gt;values&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;: &amp;amp;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;[&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;i32&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;])&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; -&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #F9E2AF;font-style: italic;&quot;&gt; Vec&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;lt;&lt;/span&gt;&lt;span style=&quot;color: #CBA6F7;&quot;&gt;i32&lt;/span&gt;&lt;span style=&quot;color: #89DCEB;&quot;&gt;&amp;gt;&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt; {&lt;/span&gt;&lt;span&gt;    &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;	values&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;iter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;copied&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;filter&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;(&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;|&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;| *&lt;/span&gt;&lt;span&gt;v&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt; &amp;gt;=&lt;/span&gt;&lt;span style=&quot;color: #FAB387;&quot;&gt; 0&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;)&lt;/span&gt;&lt;span style=&quot;color: #94E2D5;&quot;&gt;.&lt;/span&gt;&lt;span style=&quot;color: #89B4FA;font-style: italic;&quot;&gt;collect&lt;/span&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;()&lt;/span&gt;&lt;span&gt; &lt;/span&gt;&lt;/span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9399B2;&quot;&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The internal implementation is now completely different, yet the behavior remains exactly the same.&lt;/p&gt;
&lt;p&gt;And that&#39;s the crucial point: the tests still pass without changing a single line of them. They don&#39;t care how the behavior is achieved. They only care that it is achieved. This is exactly the kind of freedom and safety TDD provides: the ability to evolve and improve internal code confidently, without risking regressions.&lt;/p&gt;
&lt;p&gt;This is how software is created that not only works but also remains maintainable and evolvable over time, a core principle of good software design.&lt;/p&gt;
&lt;p&gt;Looking back, I don’t write tests today because I’m “supposed to”. I write them because they make me faster, calmer, and better at my craft.&lt;/p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>From C to Rust - Evolving Programming Languages in Automotive Development</title>
        <published>2025-11-08T00:00:00+00:00</published>
        <updated>2025-11-08T00:00:00+00:00</updated>
        
        <author>
          <name>Unknown</name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://lmilz.dev/blog/from-c-to-rust-evolving-programming-languages-in-automotive-development/"/>
        <id>https://lmilz.dev/blog/from-c-to-rust-evolving-programming-languages-in-automotive-development/</id>
        
        <content type="html" xml:base="https://lmilz.dev/blog/from-c-to-rust-evolving-programming-languages-in-automotive-development/">&lt;h2 id=&quot;introduction&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#introduction&quot; aria-label=&quot;Anchor link for: introduction&quot;&gt;Introduction&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;In recent years, the automotive software landscape has changed dramatically. What used to be small, specialized electronic control units (ECUs) with narrowly defined functions has evolved into highly networked systems with powerful System-on-Chips (SoCs) and a growing fusion between traditional embedded and high-performance software development.&lt;/p&gt;
&lt;p&gt;This evolution brings new challenges, especially when it comes to choosing the right programming language. The language doesn’t just impact performance and resource usage; it also defines maintainability, safety, and ultimately the certifiability of the software.&lt;/p&gt;
&lt;h2 id=&quot;the-automotive-software-landscape&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#the-automotive-software-landscape&quot; aria-label=&quot;Anchor link for: the-automotive-software-landscape&quot;&gt;The Automotive Software Landscape&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;Low-level microcontrollers still handle classic tasks such as engine control, sensor interfaces, or basic actuators. At the same time, high-performance SoCs power increasingly complex domains like driver assistance, infotainment, and connectivity.&lt;/p&gt;
&lt;p&gt;Each layer has its own constraints and challenges. Many ECUs must meet real-time requirements while operating under tight memory and processing limits. Some controllers still run with only a few kilobytes of RAM or Flash, demanding highly efficient and deterministic code. Meanwhile, system complexity continues to grow, with vehicle platforms ranging from dozens of distributed ECUs to centralized zone architectures, each with unique communication and software requirements.&lt;/p&gt;
&lt;p&gt;In such environments, many language features are off-limits: dynamic heap allocation, uncontrolled pointers, and large standard libraries are often forbidden. That leaves us with three real contenders for automotive software development: C, C++, and Rust.&lt;/p&gt;
&lt;h2 id=&quot;c-the-embedded-classic&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#c-the-embedded-classic&quot; aria-label=&quot;Anchor link for: c-the-embedded-classic&quot;&gt;C - The Embedded Classic&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;If you work in automotive software, C is unavoidable. Since the early days of electronic control units, it has been the de facto standard and for good reason.&lt;/p&gt;
&lt;p&gt;C was designed for system programming, and that’s exactly why it fits so well in the automotive domain: it offers direct hardware access, minimal abstraction, and precise control over timing and resources. When you need to process a signal within microseconds or trigger an actuator at exactly the right time, C is unbeatable.&lt;/p&gt;
&lt;p&gt;Why C still rules:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Full hardware control: Direct access to registers, memory, and interrupts, essential for real-time control.&lt;/li&gt;
&lt;li&gt;Minimal overhead: No hidden runtime mechanisms, no unnecessary memory use.&lt;/li&gt;
&lt;li&gt;Excellent toolchain support: C is universally supported across microcontroller families, including certified compilers, debuggers, and analysis tools.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;The downsides:
C lacks type safety and relies entirely on the developer to manage memory and concurrency correctly. Pointer errors or race conditions can lead to unpredictable behavior. Large C codebases, often grown over decades, are notoriously hard to maintain.&lt;/p&gt;
&lt;p&gt;Still, C remains the bedrock of embedded software: predictable, efficient, and deeply integrated into automotive safety and toolchain ecosystems.
Modern languages build on its shoulders, but they don’t replace its role in low-level, deterministic control.&lt;/p&gt;
&lt;h2 id=&quot;c-structured-power-for-complex-systems&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#c-structured-power-for-complex-systems&quot; aria-label=&quot;Anchor link for: c-structured-power-for-complex-systems&quot;&gt;C++ - Structured Power for Complex Systems&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;As ECUs grow more powerful and interconnected, C++ has gained significant traction in the automotive world. It brings structure, modularity, and abstraction, qualities that are becoming essential in modern vehicle software.&lt;/p&gt;
&lt;p&gt;Why C++ matters:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Object-oriented design: Enables clear separation of concerns and better code organization in large projects.&lt;/li&gt;
&lt;li&gt;Templates and generic programming: Combine type safety and code reuse without runtime cost.&lt;/li&gt;
&lt;li&gt;RAII and stronger type checking: Improve memory safety and resource management.&lt;/li&gt;
&lt;li&gt;Modern language features: &lt;code&gt;constexpr&lt;/code&gt;, &lt;code&gt;auto&lt;/code&gt;, and &lt;code&gt;std::array&lt;/code&gt; improve readability and reliability at zero runtime cost.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;In practice, this means we can design flexible, modular interfaces for instance, a unified abstraction layer for radar, camera, and lidar sensors to enable sensor fusion. In C, that would require far more boilerplate and unsafe pointer tricks.&lt;/p&gt;
&lt;p&gt;However, embedded C++ comes with caveats. Many automotive compilers only support subsets of the language, and standard libraries (&lt;code&gt;&amp;lt;iostream&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;string&amp;gt;&lt;/code&gt;, &lt;code&gt;&amp;lt;vector&amp;gt;&lt;/code&gt;) are often restricted due to dynamic memory usage or exceptions. Templates and inlining can easily bloat the binary size, and long compile times are common.&lt;/p&gt;
&lt;p&gt;In short: C++ has become the bridge between low-level efficiency and modern software design. When used with discipline and clear safety guidelines like MISRA, it enables robust and maintainable systems without sacrificing performance.&lt;/p&gt;
&lt;h2 id=&quot;rust-the-rising-challenger&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#rust-the-rising-challenger&quot; aria-label=&quot;Anchor link for: rust-the-rising-challenger&quot;&gt;Rust – The Rising Challenger&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;When talking about the future of automotive software, one language always comes up: Rust.&lt;/p&gt;
&lt;p&gt;Rust brings something that C and C++ have struggled to achieve memory and thread safety at compile time, without sacrificing performance.
In a safety-critical industry, that’s a big deal.&lt;/p&gt;
&lt;p&gt;What makes Rust so exciting:&lt;/p&gt;
&lt;ol&gt;
&lt;li&gt;Compile-time memory safety: No null pointers, no use-after-free, no buffer-overflows.&lt;/li&gt;
&lt;li&gt;Ownership and borrowing: Clear rules for who owns memory and how it can be accessed.&lt;/li&gt;
&lt;li&gt;Zero-cost abstractions: High-level expressiveness without garbage collection or runtime overhead.&lt;/li&gt;
&lt;li&gt;Safe concurrency: The type system prevents data races by design.&lt;/li&gt;
&lt;li&gt;Growing embedded ecosystem: Frameworks like &lt;code&gt;embedded-hal&lt;/code&gt; and &lt;code&gt;no_std&lt;/code&gt; make Rust viable on microcontrollers.&lt;/li&gt;
&lt;/ol&gt;
&lt;p&gt;Of course, Rust isn’t production-ready everywhere yet. Certified toolchains and ISO 26262 support are still emerging, and the learning curve can be steep. But after that initial hurdle, developers gain an unprecedented level of reliability and maintainability.&lt;/p&gt;
&lt;p&gt;To me, Rust feels like the natural evolution of C and C++: the same low-level control and efficiency, but with built-in safety guarantees. Especially in application layers or safety-critical software, Rust has the potential to fundamentally reshape how we build automotive systems.&lt;/p&gt;
&lt;h2 id=&quot;outlook-balancing-the-proven-and-the-new&quot;&gt;&lt;a class=&quot;zola-anchor&quot; href=&quot;#outlook-balancing-the-proven-and-the-new&quot; aria-label=&quot;Anchor link for: outlook-balancing-the-proven-and-the-new&quot;&gt;Outlook: Balancing the Proven and the New&lt;/a&gt;&lt;/h2&gt;
&lt;p&gt;C, C++, and Rust are not competitors, they complement each other.&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;C remains indispensable for hardware-near programming, where every cycle and byte matters.&lt;/li&gt;
&lt;li&gt;C++ provides structure and abstraction for complex, modular systems and middleware.&lt;/li&gt;
&lt;li&gt;Rust introduces memory safety, concurrency safety, and modern reliability to the mix.&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;The future of automotive software will be hybrid. C will stay the foundation for low-level, deterministic code; C++ will handle structured, object-oriented logic; and Rust will take over where safety, reliability, and modern software practices meet.&lt;/p&gt;
&lt;p&gt;Over the next few years, we’ll see how quickly Rust gains traction in production automotive environments. But one thing is certain: the future won’t belong to a single language, it will belong to those who choose the right tool for the right layer. Combining efficiency, maintainability, and safety is what will define the next generation of automotive software.&lt;/p&gt;
</content>
        
    </entry>
</feed>
