<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Johnson Lee on Medium]]></title>
        <description><![CDATA[Stories by Johnson Lee on Medium]]></description>
        <link>https://medium.com/@johnson.lee?source=rss-cd9e19338af2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*5tl1Crglg2MnNI27ASllIQ.jpeg</url>
            <title>Stories by Johnson Lee on Medium</title>
            <link>https://medium.com/@johnson.lee?source=rss-cd9e19338af2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 16 Nov 2026 04:23:36 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@johnson.lee/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com" rel="hub"/>
        <item>
            <title><![CDATA[Give your coding agent a program graph]]></title>
            <link>https://medium.com/@johnson.lee/give-your-coding-agent-a-program-graph-f98222751a46?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f98222751a46</guid>
            <category><![CDATA[mcps]]></category>
            <category><![CDATA[agentic]]></category>
            <category><![CDATA[ai-coding]]></category>
            <category><![CDATA[agents]]></category>
            <category><![CDATA[ai]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Tue, 29 Sep 2026 23:43:20 GMT</pubDate>
            <atom:updated>2026-09-29T23:43:20.863Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DybiF17z45I1v2hgEcLmIg.png" /></figure><p>Graphite turns JVM bytecode into a graph you can query, so an AI agent can ask “who calls this?” or “which constant reaches this API?” and get back the call sites and values that answer it.</p><p>Ask a coding agent a simple question about a large Java or Kotlin codebase:</p><blockquote>Which feature-flag keys reach OpenFeature’s `Client.getBooleanValue()`, and which methods pass them in?</blockquote><p>The agent greps, opens a file, follows an import and opens five more. Halfway through the call chain it runs out of context. Then it answers with confidence, and sometimes the answer is wrong.</p><p>The model can reason about code fine. What it lacks is the relationships between pieces of code, because we hand it text files and the codebase is really a graph. Finding a method name is easy. Answering “which values can flow into this argument?” means linking callers, fields, locals, types and dependencies spread across hundreds of files, and the agent has to rebuild those links one tool call at a time.</p><p><a href="https://github.com/johnsonlee/graphite">Graphite</a> builds those links once, ahead of time, and lets you query them.</p><h3>What Graphite is</h3><p>Graphite is an open-source (Apache 2.0) program-graph engine for JVM and Android bytecode. Point it at a JAR, WAR or APK and it builds a graph. The nodes are program elements: constants, locals, parameters, return values, fields and call sites. The edges are dataflow, calls, type hierarchy, annotations, lambda and method-reference targets, and reads from the config files packaged inside your JAR.</p><p>You query the graph with Cypher from the CLI, a web Explorer or a Kotlin API. The most useful entry point for agents is MCP: the `graphite` binary runs as an MCP server your agent talks to directly.</p><h3>A 30-second example</h3><p>Here’s the whole application:</p><pre>package demo;<br><br>static void startCheckout() {<br>    enableFeature(42);<br>}<br><br>static void enableFeature(int flagId) {<br>    System.out.println(flagId);<br>}</pre><p>Compile it, then build a graph from the JAR. You don’t need the source:</p><pre>graphite build checkout.jar -o checkout-graph --include demo</pre><p>Ask which constant reaches `enableFeature`:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*TqHEdCVgocr87XyguDvqSw.png" /><figcaption>Ask which constant reaches `enableFeature`</figcaption></figure><pre>MATCH (c:IntConstant)-[:DATAFLOW*]-&gt;(call:CallSiteNode)<br>WHERE call.callee_class = &#39;demo.Checkout&#39; AND call.callee_name = &#39;enableFeature&#39;<br>RETURN DISTINCT c.value AS flag, call.callee_name AS method</pre><pre>{ &quot;rows&quot;: [{ &quot;flag&quot;: 42, &quot;method&quot;: &quot;enableFeature&quot; }], &quot;rowCount&quot;: 1 }</pre><p>Ask who calls it:</p><pre>MATCH (call:CallSiteNode)<br>WHERE call.callee_class = &#39;demo.Checkout&#39; AND call.callee_name = &#39;enableFeature&#39;<br>RETURN call.caller_signature AS caller</pre><pre>{ &quot;rows&quot;: [{ &quot;caller&quot;: &quot;demo.Checkout.startCheckout()&quot; }], &quot;rowCount&quot;: 1 }</pre><p>That’s a demo. In a real codebase the `42` is an enum constant stored in a field, boxed by `Integer.valueOf()`, wrapped in `List.of(…)` and passed through three layers of helpers, and the same query follows the dataflow through all of it.</p><p>The repo ships this demo as a script, `bash examples/quickstart/run.sh`. It runs without a server, a database or an AI subscription.</p><h3>Integrate it with your agent</h3><p>For Claude Code:</p><pre>claude mcp add --transport stdio --scope project graphite -- \<br>  graphite mcp --graph app:/data/app-graph</pre><p>The agent gets tools such as `schema`, `cypher`, `incoming` and `outgoing` for traversal, `endpoints`, `resources` and `c4`. I’d start with `schema`. It returns every node label, property key and relationship type in the graph, plus the most common patterns, in milliseconds. An agent that reads it first usually writes a working query on its first attempt and skips the rounds of exploratory probes.</p><p>The repo also has an <a href="https://github.com/johnsonlee/graphite/tree/main/skills/graphite">Agent Skill</a> that walks the agent through an investigation: find entry points, trace control and data flow, locate feature-flag touch points and estimate the blast radius of a change, citing graph evidence along the way.</p><pre>npx skills add johnsonlee/graphite --skill graphite -a claude-code</pre><h3>Why start from bytecode</h3><p>A common way to build code tools is to parse source with something like Tree-sitter, which gives you syntax trees. “Which constants reach this argument?” isn’t a syntax question, though. To answer it on top of a parser you still need symbol and type resolution, control-flow and dataflow models, argument-to-parameter mapping and interprocedural analysis, and each of those is a serious project.</p><p>Graphite starts from compiled bytecode, where the compiler has already settled much of that. Method descriptors are resolved, overloads are picked, Kotlin property syntax has become method calls, and lambdas have become `invokedynamic` with concrete targets. In the Kotlin compiler’s own code, for example, `fqName.isRoot` is property syntax for a call to the Java method `FqName.isRoot()`. Graphite records those calls; a source-based code-graph tool we measured had indexed the same methods but had no edges for them.</p><p>Bytecode also means you can analyze code you have no source for: third-party dependencies, vendor SDKs, the APK you shipped last year.</p><h3>Scale</h3><p>A lot of code-intelligence tools demo well on a small repo and struggle on a monorepo. Graphite’s serving backend is written in Rust. Graphs are stored in the compressed <a href="https://webgraph.di.unimi.it/">WebGraph</a> format, at 2 to 4 bits per edge, and memory-mapped.</p><p>One production deployment serves <strong><em>40+ graphs </em></strong>from a single process: <strong><em>100M+ nodes, 100M+ edges and 20M+ call sites</em></strong>. Lookups for a specific class, method or constant come back from string indexes in milliseconds, and the P50 over the mixed, mostly cross-graph production stream is about 500 ms.</p><p>There’s also a public demo you can reproduce. It loads 64 graphs built from Android, Apache Tika, Apache Hive and the Kotlin compiler, 19.4M nodes and 20.4M edges in total, and you can browse all of it in the web Explorer.</p><p>A graph packs into one `.graphite` file with a SHA-256 fingerprint. To swap a graph on a live server you `rename` the new file over the old one; queries already running finish on the old snapshot. Cypher queries can span every loaded graph, and a ` — topology` rule turns RPC call sites into a service-to-service map.</p><h3>How it compares with a source-based tool</h3><p>We ran Graphite and a popular source-based code-graph tool against real published projects and <a href="https://github.com/johnsonlee/graphite/blob/main/docs/codegraph-comparison.md">published the raw responses, scripts and hardware details</a>.</p><p>Graphite found the Kotlin-to-Java property calls described above, and the other graph had no edges for them. For “how many distinct callers does Guava’s `Preconditions.checkNotNull` have?”, Graphite answers with one `count(DISTINCT …)` query: 1,016. The other tool’s MCP callers tool stops at 100 results and has no pagination. On the same nine-caller lookup over Commons Lang, Graphite’s median latency was <strong><em>2.38 to 2.65 times lower</em></strong> across five independent runs. That is one selective workload, and the write-up does not present it as a general speedup.</p><h3>What else it does</h3><p>It discovers HTTP endpoints (following controller inheritance), answers generic annotation queries and infers nested generic types such as `ApiResponse&lt;PageData&lt;User&gt;&gt;`. It indexes `.properties`, YAML, JSON and XML inside fat JARs, so you can ask which call sites read `feature.mode`. It derives C4 architecture views from the graph and exports them as Structurizr, Mermaid or PlantUML. The Kotlin API has backward slicing, a query DSL and a `GraphiteExtension` SPI for your own analyses. For operations there are Prometheus metrics, query timeouts, concurrency limits and an OpenAPI description of the server.</p><h3>Limits</h3><p>Graphite analyzes the artifacts you give it statically, so reflection and dynamically loaded code can leave relationships unresolved. The released frontend covers JVM and Android. A Swift/iOS frontend is in progress and not merged yet.</p><h3>Try it</h3><pre>brew tap johnsonlee/tap<br>brew install johnsonlee/tap/graphite<br><br>graphite build app.jar -o app-graph --include com.yourcompany<br>graphite serve --id app app-graph --port 8080   # open http://localhost:8080</pre><p>GitHub: <a href="https://github.com/johnsonlee/graphite">https://github.com/johnsonlee/graphite</a></p><p>If you point it at a large JVM codebase, I’d like to hear how it goes. Stars, issues and hard questions are all welcome.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f98222751a46" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Essence of LLM: Function]]></title>
            <link>https://medium.com/@johnson.lee/the-essence-of-llm-function-df4c9984ad8f?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/df4c9984ad8f</guid>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[chatgpt]]></category>
            <category><![CDATA[llm]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Sat, 16 May 2026 05:26:58 GMT</pubDate>
            <atom:updated>2026-05-16T05:27:10.753Z</atom:updated>
            <content:encoded><![CDATA[<p>A while back, my son asked me: “Dad, how does ChatGPT know what to say?”</p><p>I decided to give a real answer. Not a hand-wavy “it’s very smart,” but actually break down how LLMs work for him. So I made a slide deck — <a href="https://llm.johnsonlee.io/">LLM for Kids</a> — walking through Token, Embedding, Attention, and Transformer, using “the cat sat on the mat” as an example, “report cards” and “pie charts” as analogies.</p><p>Making that deck taught me more than I expected. When you’re forced to explain a concept so that an elementary schooler can understand it, you’re forced to strip away all the jargon and confront the essence.</p><p>And that essence is surprisingly simple:</p><blockquote><strong>An LLM is a function.</strong></blockquote><p>Not a metaphor. Not an analogy. A function in the mathematical sense. It takes a sequence of tokens as input and outputs a probability distribution. Every behavior that makes people think “AI seems to be thinking” is just this function calling itself repeatedly.</p><h3>Starting from a d-Dimensional Space</h3><p>Training an LLM begins with positing a d-dimensional space. d could be 4096, 8192 — the exact number depends on the model design.</p><p>Each token — a word, a subword, a punctuation mark — is mapped to a vector in this space. This operation is called Embedding, and it’s essentially a lookup table: token ID in, d-dimensional vector out.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/199/1*5sCr99Q1nz2tFiqRPISHqw.png" /></figure><p>Before training, these vectors are randomly initialized. “Cat” and “dog” might be far apart. “Cat” and “interest rate” might be right next to each other. But after training, semantically similar words get pulled closer together — not by human design, but by gradient descent tuning it on its own.</p><p><strong>A word’s “meaning” is its position in high-dimensional space.</strong></p><h3>Attention: Dynamic Routing</h3><p>But there’s a problem: Embedding gives each token a <strong>static, context-independent</strong> position. Whether “apple” appears in “I ate an apple” or “Apple released a new iPhone,” the lookup table returns the same vector — encoding only the average semantics of “apple,” with no idea whether it’s a fruit or a company in the current sentence.</p><p>What Attention does is: <strong>dynamically adjust each token’s representation based on context.</strong> Embedding assigns each token a “default identity.” Attention lets them communicate with each other and then adjust according to context. Without Attention, every word lives in its own world, unaware of its neighbors.</p><p>For each position in the sequence, Attention answers one question: <strong>Who should I pay attention to, and how much?</strong></p><p>Mathematically, it transforms each vector into three roles:</p><ul><li><strong>Q (Query)</strong>: What am I looking for</li><li><strong>K (Key)</strong>: What can I offer</li><li><strong>V (Value)</strong>: My actual content</li></ul><p>Then a single formula handles matching and aggregation:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/297/1*-DfTfCK8r2R3072y-uZ6oQ.png" /></figure><p>QK^T computes the relevance score between every pair of positions. √(d_k) is a scaling factor that prevents dot products from growing too large, which would push softmax outputs toward one-hot (vanishing gradients). Softmax normalizes the scores into weights, which are then used to compute a weighted sum over V.</p><p>In one sentence: <strong>Attention is a learnable, dynamic weighted sum.</strong></p><p>Multi-Head Attention runs multiple such operations in parallel. Each head learns a different attention pattern — some focus on syntactic dependencies, some on semantic similarity, some on positional distance. The results from all heads are concatenated and passed through a linear transformation.</p><h3>FFN: The Knowledge Store</h3><p>Inside each Transformer block, right after Attention, there’s a Feed-Forward Network (FFN):</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/277/1*VATkgshRejsw3pey2TQFzw.png" /></figure><p>Two fully connected layers with an activation function in between. Looks unremarkable, but recent Mechanistic Interpretability research has revealed an interesting division of labor:</p><p><strong>Attention handles information routing — deciding where to pull information from. FFN handles knowledge storage — the “facts” the model has memorized are largely encoded in FFN parameters.</strong></p><p>This means when you ask an LLM “What is the capital of France?”, Attention connects “France” and “capital,” while FFN “recalls” “Paris” from its parameters.</p><h3>Training Objective: Almost Too Simple</h3><p>The entire training process has a single objective: <strong>Next Token Prediction.</strong></p><p>Given the first n tokens, predict the n+1th. Compute the cross-entropy loss between the predicted probability distribution and the ground truth, backpropagate, update parameters.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/248/1*vyIDhn3njI03mmKKtAoSuQ.png" /></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/960/1*v9_HMid189YleN3fuAuFxQ.png" /></figure><p>That’s the only objective. Nobody teaches it grammar, logic, or how to write code. Yet when the model is large enough and the data abundant enough, these capabilities “emerge.”</p><p>Why? Because to accurately predict the next token, you must understand context. To understand context, you implicitly learn grammar, semantics, logic, common sense, and even world knowledge. <strong>Predicting the next word is the ultimate compression of language understanding.</strong></p><h3>So What Is “Intelligence”?</h3><p>Back to the opening thesis: an LLM is a function.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/135/1*59rV2q5ysX6eefwlFHzb9Q.png" /></figure><p>Billions to hundreds of billions of parameters , trained on massive data, mapping token sequences to probability distributions over the vocabulary. A single forward pass, pure matrix operations, no side effects, deterministic output.</p><p>What about conversation? It’s just autoregressive invocation of this function — append the previous output to the input and call it again. Temperature and Top-p sampling introduce randomness, but that’s an inference-stage engineering choice, not a property of the model itself.</p><p>This isn’t diminishing LLMs. Quite the opposite. <strong>The fact that a system “merely” doing function approximation can exhibit behavior that looks like reasoning, like creativity, like understanding — that is what’s truly awe-inspiring.</strong></p><p>Conway’s Game of Life is also a function — a few simple rules that evolve into infinitely complex patterns. LLMs are similar: a simple training objective, through a sufficiently large parameter space and enough data, gives rise to capabilities that exceed intuition.</p><h3>The Value of Demystification</h3><p>Understanding “LLMs are functions” has practical value.</p><p>It lets you stop treating LLM errors as “AI is unreliable” and instead understand them as the function’s poor fit in certain input regions. It helps you see what Prompt Engineering actually does — adjusting the input vector’s position in high-dimensional space so it lands in a region where the function fits well. It helps you understand why the Context Window has a limit — it’s not just a technical constraint, but a consequence of Attention’s O(n²) computational complexity.</p><p><strong>No need for reverence. No need for fear. What’s needed is understanding.</strong> When you know what’s under the hood, you can push it to its limits.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=df4c9984ad8f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Fast Is the Most Expensive Slow]]></title>
            <link>https://medium.com/@johnson.lee/fast-is-the-most-expensive-slow-69be94c0275a?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/69be94c0275a</guid>
            <category><![CDATA[harness-engineering]]></category>
            <category><![CDATA[agentic-ai]]></category>
            <category><![CDATA[harness]]></category>
            <category><![CDATA[ai-agent]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Mon, 09 Mar 2026 21:00:19 GMT</pubDate>
            <atom:updated>2026-05-16T05:45:22.919Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*dWeVUcXZyhiVQgd7.png" /></figure><p>Over the weekend I was working on a side project — a macOS voice assistant written in Rust. At the start, I had Claude generate a ROADMAP. The result was beautiful: 7 milestones, each listing specific features, file structures, and dependencies, with module decomposition all figured out for me. Far more systematic than anything I would have planned myself.</p><p>So I said: execute this.</p><h3>Everything Looked Great</h3><p>AI executed fast. M1 through M7, one after another, lines of code shooting up. I asked: did you write tests? No. Add them; get coverage to 80%. Done quickly too. CI all green, compilation passed, test coverage met the bar.</p><p>Everything appeared ready, so I had it build an installer, put it on my machine, and got ready to try it out.</p><p>Opened the app — voice didn’t work. Switched to chat mode — the AI’s responses were like a soulless customer service bot, bearing no resemblance to the carefully crafted persona definition I’d written. Checked the system tray — it wasn’t packaged at all; the installer simply didn’t include the UI.</p><p><strong>This is what AI told me was “done.”</strong></p><p>I had no choice but to start debugging feature by feature myself. The audio capture format didn’t match the STT service. The WAV parser assumed a fixed file structure and crashed on macOS’s non-standard output. Playback wasn’t blocking, so echo cancellation was useless — every single one of these was invisible on the ROADMAP, and every single one only surfaced when actually running the thing.</p><p>As I debugged, the architecture went through major restructuring. By the time I’d fixed each core feature to a working state, I looked back and the code no longer matched the ROADMAP. Some ROADMAP modules had been deleted; some features not on the ROADMAP had been added.</p><p>That’s when I realized: <strong>the ROADMAP could no longer tell me the state of this project.</strong> I needed something different.</p><h3>The Moment the PRD Called My Bluff</h3><p>After writing the PRD, I ran an audit against it — checking every functional requirement’s completion status line by line.</p><p>The results were quite surprising.</p><p><strong>Text REPL was not in the PRD at all.</strong> The PRD was explicit: this is a voice-first application where users interact via voice; “no button press, hotkey, or wake word is needed.” Text mode was merely a fallback option in settings, not a core interaction path. But the ROADMAP placed it as the very first item in M1, so it became the first feature I built.</p><p>That wasn’t even the most absurd part. Continuing the audit, I found more issues:</p><ul><li>The persona definition file had substantial effort poured into it; it was encrypted and compiled into the binary during build — but the chat path never used it, opting for a hardcoded generic prompt instead</li><li>The UI module code was complete, but the release workflow didn’t package it into the app bundle, meaning it wouldn’t be installed even on release</li><li>Several functions marked “allow dead code” all traced back to text REPL remnants</li></ul><p><strong>None of these were compilation errors. None would fail CI. But every single one meant the product goals were not met.</strong></p><h3>AI Excels at Planning Execution, Not at Defining Goals</h3><p>Looking back, the problem wasn’t the quality of the ROADMAP itself — it was genuinely well-crafted, clearly structured, with sensible dependencies and phased delivery. The problem was that <strong>the ROADMAP answers “how to do it” and “in what order,” but it doesn’t answer “is this the right thing to do.”</strong></p><p>When AI generated the ROADMAP, its input was my description of the project. It derived a reasonable execution plan from that information, but it had no ability to judge for me whether “Text REPL actually matters to users.” That judgment requires product intuition and understanding of user scenarios, not logical deduction.</p><p>More subtly, the AI-generated ROADMAP looked too professional — so professional it let me drop my guard. <strong>When a plan’s form is polished enough, you unconsciously trust its substance.</strong> Every milestone delivered real code output, tests passed, features worked — but the gap between “it runs” and “it’s right” is far wider than most people assume.</p><p>This was also a lesson for myself: I’d previously written <a href="https://johnsonlee.io/2026/02/10/agent-oriented-engineering/">Agent-Oriented Engineering</a>, discussing how human engineers need to shift from execution to judgment. Then I turned around and made exactly this mistake — treating an AI-generated execution plan as a substitute for judgment.</p><h3>The PRD Is Your Own Judgment</h3><p>The value of a PRD lies not in its format or length, but in the fact that it’s something you’ve thought through yourself.</p><p>Writing the PRD forced me to answer: “What problem does this product actually solve? In what scenario do users use it? What features are core, and what’s nice-to-have?” AI can’t help you with these questions, because the answers come from your understanding of user scenarios and your own trade-offs.</p><p>With a PRD in hand, the lens for reviewing code changes entirely. The ROADMAP lens asks “was this module built?”; the PRD lens asks “does this feature meet the end-to-end bar?” Take the persona definition file: the ROADMAP lens sees “encrypted compilation done”; the PRD lens sees “AI persona in the chat path doesn’t match the definition.”</p><p><strong>A ROADMAP is internally consistent — each milestone can be independently verified. But internal consistency does not equal correctness.</strong> A ROADMAP divorced from product goals can let you efficiently do a pile of wrong things.</p><h3>Post-Mortem</h3><p>When I deleted the text REPL, I didn’t feel much regret. What truly bothered me was something else: if I’d written the PRD first and then had AI generate the ROADMAP, that code would never have existed. The weekend hours behind it — design, coding, testing, debugging — could have been spent polishing the voice pipeline instead.</p><p>Similarly, the persona definition not being used by the chat path — if I’d validated against the PRD after completing each feature, I would have caught it on the spot. But the corresponding milestone in the ROADMAP only said “implement SSE streaming + conversation history”; check it off and move on, with nobody verifying whether the output content matched the product definition.</p><h3>The Right Order</h3><p>To be clear, I’m not dismissing the value of AI-generated ROADMAPs. They genuinely help you quickly turn fuzzy ideas into executable plans. But the order matters:</p><ul><li><strong>Write the PRD first</strong> — think through what to build, what not to build, and what “done” means</li><li><strong>Then have AI generate the ROADMAP</strong> — plan the execution path within the PRD’s constraints</li><li><strong>Audit against the PRD regularly</strong> — stop and recalibrate every few milestones</li></ul><p>The PRD is the anchor, the ROADMAP is the course, and the audit is the compass. You need all three, but the anchor must be one you drop yourself — you can’t let AI drop it for you.</p><h3>The Cost of Stopping</h3><p>One last takeaway: <strong>the value of auditing is severely underestimated.</strong></p><p>A single audit took about two hours. The result? It uncovered 6 gaps, 3 of which were on the critical path. If I’d kept charging ahead following the ROADMAP, these issues might not have surfaced until I actually needed the product — and the cost of fixing them then would be ten times what it is now.</p><p>Developers inherently dislike “stopping to look back.” Writing new code gives you dopamine; reviewing old code gives you only anxiety. But this experience convinced me: <strong>periodically stopping to recalibrate against the PRD is one of the highest-ROI engineering activities there is.</strong> The cost is two hours of review; the payoff is avoiding further investment in the wrong direction.</p><p>What this experience taught me isn’t “don’t write bad code,” but rather “don’t go full speed on an ocean with no anchor” — even if the course was charted by AI and looks flawless.</p><p><em>Originally published at </em><a href="https://johnsonlee.io/en/2026/03/09/fast-is-the-most-expensive-slow/index.html"><em>https://johnsonlee.io</em></a><em> on March 9, 2026.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=69be94c0275a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From LLMs to Effective Communication]]></title>
            <link>https://medium.com/@johnson.lee/from-llms-to-effective-communication-e2509d50767d?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/e2509d50767d</guid>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[communication]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Thu, 26 Feb 2026 00:03:18 GMT</pubDate>
            <atom:updated>2026-05-16T05:37:34.803Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*IW_AEvvj6TvUGCVe.png" /></figure><p>Recently I was explaining how LLMs work to my son and put together a <a href="https://llm.johnsonlee.io/">slide deck</a>. When I got to the context window, I reached for an off-the-cuff example:</p><blockquote><em>If you tell an AI “recommend me a movie,” it can only give you a generic answer. But if you say “I like thrillers, prefer nonlinear narratives, just watched </em>Mulholland Drive<em> and want something similar but a bit faster-paced” — the recommendation will be far more precise.</em></blockquote><p>My son asked: why?</p><p>I said: because you gave it more feature dimensions. Its understanding of you went from one-dimensional to multi-dimensional, so it can naturally find a better match within a smaller search space.</p><p>The moment I finished that sentence, I paused.</p><p>Isn’t this exactly the underlying logic of how people communicate with each other?</p><h3>LLMs Make Communication Patterns Observable</h3><p>Communication efficiency between people has always been something vaguely “mystical.” Some are naturally articulate; others talk for ages and still leave everyone confused. But few can articulate where exactly the gap lies.</p><p>LLMs make this quantifiable.</p><p>The more precise and dimensionally rich your prompt, the higher the output quality. This isn’t mysticism; it’s math — <strong>more feature dimensions mean a smaller search space, less ambiguity, and higher matching precision.</strong></p><p>Conversely, if you give a vague instruction, the model can only “guess” within an enormous possibility space. If it guesses right, that’s luck; if it guesses wrong, you conclude “AI isn’t good enough.”</p><p>But is AI really the problem?</p><h3>“Low-Dimensional Communication” Between People</h3><p>Map this logic onto interpersonal communication and you’ll find the root cause of most inefficient communication is exactly the same — <strong>insufficient information dimensions.</strong></p><p>A common workplace example:</p><blockquote><em>“This page loads too slowly. Optimize it.”</em></blockquote><p>How many dimensions does this sentence have? One — “slow.” The engineer receiving this request immediately has at least ten questions swirling: which page? Under what conditions? How slow? First load or every load? Is there profiling data? What’s the target? What’s the priority?</p><p>Now consider a different phrasing:</p><blockquote><em>“The product detail page takes over 5 seconds for first contentful paint on a weak network (3G), and the bounce rate is 40% higher than on Wi-Fi. Target: get TTI under 3 seconds on weak networks. P1 priority, due this quarter.”</em></blockquote><p>Same underlying request — “optimize page load” — but the second version adds at least six dimensions: page, scenario, metric, comparison baseline, target, and priority. The recipient can almost start working without a single follow-up question.</p><p><strong>The difference in communication efficiency is fundamentally a difference in feature dimensions.</strong></p><h3>More Dimensions, Less Ambiguity</h3><p>The way LLMs process language gives this principle an extremely intuitive explanation.</p><p>After tokens enter the model, they’re mapped into a high-dimensional vector. A single token in isolation could have countless meanings, but when combined with other tokens in context, each added dimension compresses the possible semantic space once more. Eventually the model can “lock onto” your intent within a sufficiently small range.</p><p>The human brain processes information similarly. If you say “book me a meeting room,” the other person’s mind conjures any room at any time. But say “tomorrow, 2 to 3 pm, 6 people, need screen sharing, preferably near a window” — each additional constraint shrinks the decision space, and execution accuracy goes up a notch.</p><p>This isn’t a “communication technique.” It’s information theory.</p><h3>Why Don’t Most People Do This?</h3><p>If multi-dimensional communication is so effective, why do most people still default to one-dimensional expression?</p><p>Because providing multi-dimensional information has a cognitive cost.</p><p>You first have to think things through in your own head — which dimensions are critical, which are noise, and what level of granularity is appropriate. This requires completing an “internal modeling” step before you speak, transforming a fuzzy feeling into structured information.</p><p>Most people skip this step. Not out of laziness, but because they haven’t figured it out themselves.</p><p>This is also why “prompt engineering” sounds simple yet so many people still can’t write a good prompt — <strong>it’s not that they can’t talk to AI; it’s that they can’t talk to themselves.</strong> You cannot output a structure that doesn’t exist in your own mind.</p><h3>An Unexpected Takeaway from Teaching LLM Fundamentals</h3><p>Back to the scene of explaining things to my son.</p><p>I originally just wanted him to understand how LLMs work, but as I went on I realized the most valuable part of the lesson wasn’t the technical principles themselves — it was the communication patterns they reveal:</p><ul><li>If you want the other party (human or AI) to understand you accurately, you must provide enough effective dimensions</li><li>Effective dimensions are not a pile of information, but <strong>constraints relevant to the goal that shrink the search space</strong></li><li>The ceiling of your expressive ability is determined by how deeply you understand your own needs</li></ul><p>These patterns are instantly verifiable with an LLM — tweak the prompt, watch the output change, cause and effect crystal clear. In human-to-human communication, feedback is delayed and fuzzy, making precise attribution nearly impossible.</p><p><strong>An LLM is like a communication laboratory.</strong> It turns “the more precise the expression, the better the understanding” from mysticism into a reproducible experiment.</p><h3>What Real Communication Ability Is</h3><p>Many equate communication ability with eloquence, expressiveness, or even emotional intelligence. Those are all means, not the essence.</p><p><strong>Real communication ability is the capacity to transform fuzzy intent in your mind into multi-dimensional structured information.</strong></p><p>The more deeply you understand your own needs, the more effective dimensions you can extract, the less the other party needs to guess, and the more efficient the communication.</p><p>This has nothing to do with whether you’re talking to a person or an AI. The physics of information transfer don’t change just because the receiver is carbon-based or silicon-based.</p><p>So next time communication efficiency is low, don’t rush to blame the other party for “poor comprehension.”</p><p>First ask yourself: how many dimensions did I give?</p><p><em>Originally published at </em><a href="https://johnsonlee.io/en/2026/02/26/from-llm-to-effective-communication/index.html"><em>https://johnsonlee.io</em></a><em> on February 26, 2026.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e2509d50767d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[From Compiler to LLM: The Recurring Cycle of Software Layering]]></title>
            <link>https://medium.com/@johnson.lee/from-compiler-to-llm-the-recurring-cycle-of-software-layering-164df744e0b6?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/164df744e0b6</guid>
            <category><![CDATA[gpt]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[llm]]></category>
            <category><![CDATA[chatgpt]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Wed, 25 Feb 2026 12:28:59 GMT</pubDate>
            <atom:updated>2026-05-16T05:32:24.235Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/600/0*kF1UPZwBvKcW30ej.png" /></figure><p>Both call the Claude API to write code, yet Cursor became a product valued at tens of billions while countless “wrapper” apps died in silence. What’s the difference?</p><p>This question reminded me of a much older analogy.</p><h3>An Old Story, New Version</h3><p>Traditional software engineering has a clear dividing line: <strong>on one side, people who build tools; on the other, people who use tools.</strong></p><p>Tool builders write compilers, runtimes, operating systems — GCC, JVM, LLVM, Linux. Tool users take these to build business software — Word, Photoshop, Taobao. The two groups need entirely different skills, their career paths barely overlap, and each forms its own ecosystem.</p><p>This dividing line held steady for decades.</p><p>Now, the AI era is redrawing it. On one side are people training foundation models — OpenAI, Anthropic, Google, Meta — doing pretraining, RLHF, inference optimization, requiring large-scale distributed training, data engineering, and alignment research. On the other side are people building products with models — Cursor, Perplexity, Harvey — who need Prompt Engineering, RAG, tool orchestration, and product design.</p><p>Two groups, two skill sets, two paths. History repeating itself.</p><h3>But This Time Is Different</h3><p>The analogy holds, but the differences are more worth noting.</p><h3>The Interface Is No Longer Deterministic</h3><p>A compiler’s behavior is deterministic. Same source code, same compiler options, same output every time. You can build a complete mental model of compiler behavior, write tests, make assertions, calculate complexity. The entire methodology of software engineering — unit tests, CI/CD, type systems — rests on this determinism.</p><p>An LLM’s “interface” is probabilistic. Same prompt, different temperature, or even the same parameters — the output can differ. You can’t make traditional assertions against a probabilistic system. <strong>This isn’t an engineering detail you can work around; it changes the very nature of “development.”</strong></p><p>When your infrastructure is probabilistic, application-layer engineering is no longer just writing logic and calling APIs — it’s handling uncertainty: validation, fallbacks, retries, constraints. This makes AI application development feel more like collaborating with a smart but not entirely reliable colleague than calling an API with a well-defined contract.</p><h3>The Abstraction Layer Iterates Too Fast</h3><p>C++ standards come out every few years; the JVM has been backward-compatible for decades. Java knowledge you learned in 2005 mostly still works in 2025. The stability of compilers and language standards gave the application layer ample time to accumulate — accumulate code, best practices, and engineering expertise.</p><p>Foundation models turn over every few months. What the last generation couldn’t do, the next suddenly can. A complex Prompt Chain you spent three months building might become completely unnecessary after a model upgrade. The RAG Pipeline you carefully designed might be rendered obsolete by a longer Context Window.</p><p><strong>The application layer’s moat is much shallower than in traditional software.</strong> Not because application-layer people aren’t smart enough, but because the foundation is moving at an unprecedented pace.</p><h3>Boundaries Are Dissolving in Both Directions</h3><p>In the traditional era, you wouldn’t expect GCC to build a web application for you. A compiler is a compiler; it stays in its lane.</p><p>But LLMs inherently possess “application capability.” Raw Claude can analyze financial reports, write code, and do translations. It doesn’t need a shell to work. It’s as if the JVM itself could understand user needs, generate results, and deliver them directly — which massively compresses the reason for an “application layer” to exist.</p><p>So we see an interesting phenomenon: foundation model companies are building applications upward (ChatGPT, Claude.ai), and application companies are doing model fine-tuning downward. <strong>The boundary isn’t solidifying; it’s dissolving.</strong></p><h3>Where Is the Moat for AI Applications?</h3><p>Given an unstable foundation and blurred boundaries, “wrappers” obviously can’t survive. But Cursor survived. Perplexity survived. What did they get right?</p><p>The answer isn’t on a single dimension — it’s a combination at four levels of depth.</p><h3>Context Engineering</h3><p>LLMs are general-purpose, but users’ problems are specific. <strong>Whoever provides the model with more precise context builds the better product.</strong></p><p>The first thing Cursor did wasn’t writing a better prompt — it built Codebase Indexing, enabling the model to understand your entire project. This is a pure engineering problem: how to efficiently index code, how to select relevant context, how to pack the most useful information within token limits.</p><p>Model vendors won’t do this for you, because they don’t know what project your user is working on.</p><h3>Workflow Orchestration</h3><p>Good AI applications don’t make users change their habits to accommodate AI; they embed AI into users’ existing workflows.</p><p>Take Cursor again — it didn’t invent a new way of programming. It added AI on top of VS Code. You’re still writing code, reviewing diffs, running tests, except now AI handles some of the steps. <strong>The best AI applications are invisible.</strong></p><h3>Output Constraints and Validation</h3><p>LLMs make mistakes. In a chat window, users can judge for themselves. But mistakes embedded in a workflow can have serious consequences — buggy generated code, wrong legal advice, miscalculated financial data.</p><p>The application layer must constrain and validate LLM output — type checking, format validation, business rule fallbacks, human confirmation checkpoints. <strong>This is the most direct arena for traditional software engineering expertise.</strong></p><h3>Domain Knowledge Injection</h3><p>General-purpose models know a little about everything but aren’t deep enough in specialized domains. Harvey was able to establish itself in legal not by being a generic LLM wrapper, but by injecting what law firms actually need: specific legal practice workflows, compliance constraints, document format standards. This knowledge either isn’t in the model’s pretraining data, or it’s there but not precise enough.</p><p><strong>Domain knowledge is the most traditional and most durable moat.</strong> Models will upgrade, but your understanding of an industry won’t depreciate because of it.</p><h3>The Overlooked Middle Layer</h3><p>Back to the layering analogy at the start. The traditional era wasn’t just two layers of “compiler” and “application.” There was a critically important middle layer: runtimes, frameworks, build tools, package managers. JVM, Spring, Gradle, Maven. This layer didn’t face users directly, but without it, the application layer couldn’t operate efficiently.</p><p>The AI era is growing its own middle layer: Agent frameworks, MCP (Model Context Protocol), tool-calling protocols, Prompt management systems, evaluation frameworks.</p><p>What this layer does is fundamentally the same as traditional runtimes and build tools — <strong>bridging the gap between non-deterministic infrastructure and deterministic engineering requirements.</strong></p><p>LLM output needs to be validated, constrained, routed to the right tools, and integrated into existing systems. None of this is what the model itself does, nor is it a detail the final application cares about. It needs a middle layer to handle it.</p><p>Interestingly, people who’ve worked on compilers, static analysis, and bytecode manipulation in traditional software engineering have a natural advantage in this layer. Because the core problem is the same: <strong>understanding a system’s inputs and outputs, applying constraints and transformations in the middle, and ensuring the final result meets expectations.</strong> The only difference is that in the past you were constraining bytecode; now you’re constraining tokens.</p><h3>Cycles and Renewal</h3><p>Software engineering undergoes a layering restructuring every so often. From assembly to high-level languages, from desktop to web, from monolith to microservices — each restructuring redraws the boundary of “who builds tools and who uses tools.”</p><p>AI is the latest round. The logic of layering hasn’t changed, but the specifics have: the interface shifted from deterministic to probabilistic, iteration speed from years to months, and boundaries from clear to blurred.</p><p>In this landscape, the most valuable position may not be at either end — not training bigger models, not building flashier applications — but in the middle: <strong>building bridges between probabilistic intelligence and deterministic engineering.</strong></p><p><em>Originally published at </em><a href="https://johnsonlee.io/2026/02/25/from-compiler-to-llm.en"><em>https://johnsonlee.io</em></a><em> on February 25, 2026.</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=164df744e0b6" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin FunctionReference Deep Dive]]></title>
            <link>https://medium.com/@johnson.lee/kotlin-functionreference-deep-dive-27eb2236679?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/27eb2236679</guid>
            <category><![CDATA[method-handle]]></category>
            <category><![CDATA[lambda]]></category>
            <category><![CDATA[method-references]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Fri, 09 Dec 2022 11:21:42 GMT</pubDate>
            <atom:updated>2022-12-09T11:25:27.267Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DbXhC-4m5RHX5axwxE_SSg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/es/@louis993546?utm_source=medium&amp;utm_medium=referral">Louis Tsai</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Prior to <a href="https://github.com/didi/booster">Booster</a> 4.15.0, it always uses Kotlin 1.3, The main reason for using a lower Kotlin version is the incompatibility issues. However, to support AGP 7.3, The dependent Kotlin version of <a href="https://github.com/didi/booster">Booster</a> has to be upgraded, because AGP 7.3 relies on Kotlin 1.5, so it took a long time to solve the Kotlin compatibility issues in Booster.</p><h3>Kotlin First-Class Citizen — Function</h3><p>First-class functions are an indispensable feature in functional programming languages, and Kotlin is no exception. Since Function is widely used in Kotlin, it is also a high-incidence area for compatibility issues. Are you curious about Kotlin’s Function at the bytecode level? How is it achieved? Take the following code as an example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ae4d9c4ab44f803a6a807f940f1c17b1/href">https://medium.com/media/ae4d9c4ab44f803a6a807f940f1c17b1/href</a></iframe><p>If you want to implement the example above in Java, you need to define a Functional Interface:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/f4082775a1864ba30fdcdad60249d6e4/href">https://medium.com/media/f4082775a1864ba30fdcdad60249d6e4/href</a></iframe><p>or use the built-in <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html">Function&lt;T, R&gt;</a> API of JDK:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/01e5dc24c27fe3ace9899f74234f308c/href">https://medium.com/media/01e5dc24c27fe3ace9899f74234f308c/href</a></iframe><p>The Java 8 standard API only provides <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html">Function&lt;T, R&gt;</a> and <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/BiFunction.html">BiFunction&lt;T, U, R&gt;</a>. If you want to add a Function that supports more parameters, you need either customize the <a href="https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html">FunctionalInterface</a> or use a Lambda expression.</p><p>Kotlin naturally supports Lambda expressions very well, and defines <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/functions/Functions.kt#L11">Function0</a>, <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/functions/Functions.kt#L16">Function1&lt;P1, R&gt;</a>, …, <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/functions/Functions.kt#L121">Function22&lt;P1, …, P22, R&gt;</a> in the Kotlin standard library, a total of 23 Function interfaces. Seeing this, do you suddenly feel that the design of the Kotlin standard library is so stupid? What if there are functions with more than 22 parameters?</p><h3>Lambda vs Function Reference</h3><p>FunctionReference is a concept in Kotlin. Its equivalent concept in Java is MethodReference. They all talk about the same thing, which is a reference to a method, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e2e9b4c7c436d84756c265934b941983/href">https://medium.com/media/e2e9b4c7c436d84756c265934b941983/href</a></iframe><p>Among them, System.out::println is a reference to the println method of the System.out instance, so what is the difference between it and Lambda?</p><p>Generally, there are a number of ways we might represent a lambda expression in bytecode:</p><ol><li>Inner classes</li><li>Method handles(JDK built-in API)</li><li>Dynamic proxies</li><li>Other methods</li></ol><p>Each of these approaches has pros and cons. In selecting a strategy, there are two competing goals:</p><ol><li>Maximizing flexibility for future optimization by not committing to a specific strategy</li><li>Providing stability in the class file representation</li></ol><p>Since the implementation of Lambda will generate anonymous methods, in order to avoid unnecessary anonymous methods, both Java or Kotlin support conversion between Lambda and method references, that is, replacing Lambda with method references, for example:</p><h4>Representation in Lambda</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1d082d3cf3c4a78f32c3e1d1385d327c/href">https://medium.com/media/1d082d3cf3c4a78f32c3e1d1385d327c/href</a></iframe><h4>Representation in Function Reference</h4><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/03a60360873d32a91be3efbb508ca26f/href">https://medium.com/media/03a60360873d32a91be3efbb508ca26f/href</a></iframe><h3>Function Reference in Kotlin</h3><p>In Kotlin, <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReference.java">FunctionReference</a> is mainly implemented through <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReferenceImpl.java">FunctionReferenceImpl</a> at the bytecode level. After Kotlin 1.7+, <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunInterfaceConstructorReference.java">FunInterfaceConstructorReference</a> is added, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/0745371feb7d9bebc198d41601dbb40c/href">https://medium.com/media/0745371feb7d9bebc198d41601dbb40c/href</a></iframe><p>Therefore, as long as the method reference is used in the Koltin code, FunctionReferenceImpl will appear in the compiled class file.</p><h3>Disadvantages of Kotlin 1.3 Function Reference</h3><p>In Kotlin, we often write code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/27f859c9a65143cc34b11973eafc27db/href">https://medium.com/media/27f859c9a65143cc34b11973eafc27db/href</a></iframe><p>Is there anything wrong with writing this way?</p><p>It looks perfectly fine superficially, However, from the perspective of bytecode, there are many problems here. The code above decompiled into Java looks roughly like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ad3c8cbcad3a14aa76ea555d6957c6b4/href">https://medium.com/media/ad3c8cbcad3a14aa76ea555d6957c6b4/href</a></iframe><p>Have you seen the problem?</p><h3>Callable Reference Optimization in Kotlin 1.4</h3><p>Through the decompiled code above, it is obvious to find that the Kotlin compiler has generated many additional methods, and most of these methods are rarely used. For some methods that are hardly used, why should they be generated? Is it possible not to generate it?</p><p>The answer is yes, that is the callable reference optimization in Kotlin 1.4, the <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/AdaptedFunctionReference.java">AdaptedFunctionReference</a> has been added, and the <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReferenceImpl.java">FunctionReferenceImpl</a> has been modified to add 2 new construction methods:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/5fac247f720e0f447259dcd1a48f4be5/href">https://medium.com/media/5fac247f720e0f447259dcd1a48f4be5/href</a></iframe><p>Then, the parameters are passed to the parent class <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/CallableReference.java">CallableReference</a> through the newly added construction method to the parent class <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReference.java">FunctionReference</a> of <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReferenceImpl.java">FunctionReferenceImpl</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/444e9269b5d6358df44247e224ef4008/href">https://medium.com/media/444e9269b5d6358df44247e224ef4008/href</a></iframe><p>Then, adding related fields, constructors and getter methods to the parent class <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/CallableReference.java">CallableReference</a>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/8d3f33bc4bbc2755aee9298e7c0c1e4e/href">https://medium.com/media/8d3f33bc4bbc2755aee9298e7c0c1e4e/href</a></iframe><figure><img alt="Hierarchy of FunctionReference" src="https://cdn-images-1.medium.com/max/1024/1*QZ5YMF_e2r3kSBLCT268Hg.png" /></figure><p>Therefore, most of the methods with constant return values generated in the anonymous inner class are implemented by constructing and passing to the base class, thus reducing the bytecode size of the entire application.</p><p>However, this optimization is enabled in Kotlin 1.4 by default, which leads to incompatible bytecode compiled for the same Kotlin code. The newly added <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/jvm/internal/FunctionReferenceImpl.java">FunctionReferenceImpl</a> constructor has been introduced into the bytecode which is only available in Kotlin 1.4 and above, and thhis is also a very common error when upgrading Kotlin:</p><blockquote>NoSuchMethodError: ‘void kotlin.jvm.internal.FunctionReferenceImpl.&lt;init&gt;(int, java.lang.Class, java.lang.String, java.lang.String, int)’</blockquote><h3>The Solution for Incompatible Callable Reference</h3><p>I believe that many engineers have encountered the same problem above. By dive deep into the Kotlin source code, I have found that the above optimizations can be disabled by Kotlin compiler options:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/d00d99c5c38be810fcfaa9124adb4ea7/href">https://medium.com/media/d00d99c5c38be810fcfaa9124adb4ea7/href</a></iframe><p>This is an efficient solution to address the Kotlin compatibility issues, but, are there any systematic solutions?</p><p>Let’s dive deeper in the next story …</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=27eb2236679" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Generating Build Info as Source Code for Gradle Project]]></title>
            <link>https://medium.com/@johnson.lee/generating-build-info-as-source-code-for-gradle-project-f0bfd58ba079?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/f0bfd58ba079</guid>
            <category><![CDATA[android]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[gradle-plugin]]></category>
            <category><![CDATA[booster]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Sat, 09 Jul 2022 06:17:34 GMT</pubDate>
            <atom:updated>2022-07-09T06:17:34.392Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*vQDwEWrEfTw79uHJ" /><figcaption>Photo by <a href="https://unsplash.com/@jstrippa?utm_source=medium&amp;utm_medium=referral">James Harrison</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><h3>Background</h3><p>Three years ago, I started the <a href="https://github.com/didi/booster">Booster</a> project, which provides a collection of Gradle plugins for Android app quality optimization, including some runtime libraries injected by the Booster Gradle plugins, the code for injecting runtime time libraries looks like as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/035441b463cb2f4c21eda862c66c8d22/href">https://medium.com/media/035441b463cb2f4c21eda862c66c8d22/href</a></iframe><p>it’s equivalent to the following code, but injected by the Booster Gradle plugin:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/31aa1156771d016509109b1aa66e9817/href">https://medium.com/media/31aa1156771d016509109b1aa66e9817/href</a></iframe><p>As you can see, the library com.didiglobal.booster:booster-android-instrument-thread:1.0.0 is injected by Booster, and this library is also a sub-project of Booster, due to the hard-coded library version, it’s not very convenient for Booster project publishing, every single time I want to publish a new version for the Booster project, including the subprojects, I have to modify the version of injected libraries to keep consistent dependency versions.</p><p>My initial thought was to get the configured project version property from the build.gradle:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/86ba6e83b3276cec75b9111d645e02fb/href">https://medium.com/media/86ba6e83b3276cec75b9111d645e02fb/href</a></iframe><p>After investigation, it seems impossible to get current Gradle project properties in the source code directly:</p><h3>Workaround</h3><p>As we know, Gradle provides a lot of built-in tasks, such as the Copy task, it provides a method — expand(Map&lt;String, ?&gt; properties) , according to the Gradle API reference, it can expand property references in each file as it is copied, and each file is transformed using Groovy’s <a href="https://docs.groovy-lang.org/3.0.9/html/gapi/groovy/text/SimpleTemplateEngine.html">SimpleTemplateEngine</a> , so, we can generate build info by leveraging the Copy task:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1f2f78132502c163c151eb325d945ee1/href">https://medium.com/media/1f2f78132502c163c151eb325d945ee1/href</a></iframe><p>The Build.kt file looks like as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/1ccf5792da95017ff9ae836598a26be8/href">https://medium.com/media/1ccf5792da95017ff9ae836598a26be8/href</a></iframe><p>Using the Copy task, we can generate the build info as source code but need to write a lot of boilerplate code, such as configuring source sets, registering tasks, configuring the task dependencies, etc.</p><p>Is it possible to generate the build info as source code in the simplest way?</p><p>After investigation, I decided to write a Gradle plugin to generate the build info as source code.</p><h3>Buildprops Gradle Plugin</h3><p>The <a href="https://github.com/johnsonlee/buildprops-gradle-plugin">buildprops-gradle-plugin</a> is used to generate Build.java source file with the following constant fields:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7a2b947907795d4d39bff2e1f706b80c/href">https://medium.com/media/7a2b947907795d4d39bff2e1f706b80c/href</a></iframe><p>The generated Build.java source file also be added to the current project’s source sets, so that it’s able to reference the constant fields of Build.java in the source code of the current project directly:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a5b75599eb21c3407b630b9ddee35ce3/href">https://medium.com/media/a5b75599eb21c3407b630b9ddee35ce3/href</a></iframe><p>It’s equivalent to the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9eeb3f63ef47eb17e2389e3ffe81c656/href">https://medium.com/media/9eeb3f63ef47eb17e2389e3ffe81c656/href</a></iframe><p>or</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/457c8dceeb0dc9dc6f9fa4657e33fdef/href">https://medium.com/media/457c8dceeb0dc9dc6f9fa4657e33fdef/href</a></iframe><p>Now, it only needs to write a single line to generate build info as source code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/70b10ac182ffedb84be372b310bf6419/href">https://medium.com/media/70b10ac182ffedb84be372b310bf6419/href</a></iframe><p>or using the plugins DSL:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/dd5496511e152fbf42c8ecc783d9486f/href">https://medium.com/media/dd5496511e152fbf42c8ecc783d9486f/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f0bfd58ba079" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Kotlin Metadata Deep Dive]]></title>
            <link>https://medium.com/@johnson.lee/kotlin-metadata-deep-dive-2a966b45521a?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2a966b45521a</guid>
            <category><![CDATA[kotlin-compiler]]></category>
            <category><![CDATA[kotlin-metadata]]></category>
            <category><![CDATA[kapt]]></category>
            <category><![CDATA[annotation-processor]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Wed, 08 Jun 2022 16:52:12 GMT</pubDate>
            <atom:updated>2022-06-09T15:11:37.291Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DbXhC-4m5RHX5axwxE_SSg.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/es/@louis993546?utm_source=medium&amp;utm_medium=referral">Louis Tsai</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>Recently, I encountered a headache when using KAPT to generate Kotlin source code at compile time, the generated Kotlin code needs to call the properties from an existing Kotlin class. In theory, it would be possible to call the properties directly by using the ‘.’ operator, however, things are not as simple as imagined.</p><h3>Kotlin Property</h3><p>A Kotlin property can be either a field or a method at the bytecode level, depending on whether it has been annotated with other JVM-related annotations, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c1b8e3d90bef832893c89967fda036d/href">https://medium.com/media/3c1b8e3d90bef832893c89967fda036d/href</a></iframe><p>As a property, the <strong><em>value</em></strong> is actually a method named <strong><em>getValue() </em></strong>at the bytecode level, however, it’s totally different in the following example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/7501c7ba0df00cacdb7712cdd9c3d42b/href">https://medium.com/media/7501c7ba0df00cacdb7712cdd9c3d42b/href</a></iframe><p>the <strong><em>value</em></strong> is actually a static field, in order to call the <strong><em>value</em></strong> property in generated Kotlin code, should we call the <strong><em>value</em></strong> field or the <strong><em>getValue()</em></strong> method?</p><h3>Kotlin Metadata</h3><p>Everyone who has used KAPT might know more or less that KAPT is actually implemented based on the APT (Annotation Processor Toolkit) provided by JDK, it generates Java stubs for Kotlin code at compile time, a Java stub is just a definition of a class without implementation. For the Kotlin compiler, how did it solves the problem of property invocation?</p><p>For each Java stub, you can find that it is always annotated by the <strong><em>@kotlin.Metadata</em></strong> annotation, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/bf001142c3f9586ec975e79dd07ccea4/href">https://medium.com/media/bf001142c3f9586ec975e79dd07ccea4/href</a></iframe><p>I believe that a dozen of questions will come out when anyone sees this pile of characters. What does this pile of characters mean, and what is <strong><em>d1</em></strong>? What is <strong><em>d2</em></strong>? What…?</p><p>When I saw it for the first time, I was dumbfounded. My first reaction was —— Searching Kotlin&#39;s official document, I searched for a long time and found nothing, so, I decided to start from the Kotlin source code, fortunately, I found an interesting class —— <a href="https://github.com/JetBrains/kotlin/blob/master/core/metadata.jvm/src/org/jetbrains/kotlin/metadata/jvm/deserialization/JvmProtoBufUtil.kt">JvmProtobufUtil.kt</a>, and there is such a method:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/a0c7ba7b3b2680cd1335d8ab18f54910/href">https://medium.com/media/a0c7ba7b3b2680cd1335d8ab18f54910/href</a></iframe><p>According to the method name, I guess it might have something to do with the metadata, and according to the two parameters, both of them are arrays of String, and the <strong><em>d1</em></strong> and <strong><em>d2</em></strong> properties from <strong><em>@kotlin.Metadata</em></strong> are also String arrays, what if <strong><em>d1</em></strong> and <strong><em>d2</em></strong> are passed in as parameters? Why not try it?</p><p>The magic happened when I ran the following code:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/933b33ed4e5a663e098b080c8ec7cbc3/href">https://medium.com/media/933b33ed4e5a663e098b080c8ec7cbc3/href</a></iframe><p>It parsed successfully, based on the comments in <a href="https://github.com/JetBrains/kotlin/blob/master/libraries/stdlib/jvm/runtime/kotlin/Metadata.kt">Metadata.kt</a>, we can derive the whole picture of <strong><em>@kotlin.Metadata</em></strong>:</p><ul><li><strong><em>k</em></strong>: the encoded data type<br>- 1: Class<br>- 2: File<br>- 3: Synthetic class<br>- 4: Multi-file class facade<br>- 5: Multi-file class part</li><li><strong><em>mv</em></strong>: The metadata version</li><li><strong><em>xi</em></strong>: A flag</li><li><strong><em>d1</em></strong>: serialized <a href="https://github.com/JetBrains/kotlin/blob/master/core/metadata/src/metadata.proto">metadata.proto</a></li><li><strong><em>d2</em></strong>: indexed string constant pool</li></ul><p>Through the <strong><em>JvmNameResolver</em></strong> and <strong><em>ProtoBuf.Class</em></strong> returned by <a href="https://github.com/JetBrains/kotlin/blob/master/core/metadata.jvm/src/org/jetbrains/kotlin/metadata/jvm/deserialization/JvmProtoBufUtil.kt">JvmProtobufUtil</a>.<a href="https://github.com/JetBrains/kotlin/blob/master/core/metadata.jvm/src/org/jetbrains/kotlin/metadata/jvm/deserialization/JvmProtoBufUtil.kt#L26">readClassDataFrom</a>, you can parse out all the encoded content in <strong><em>Metadata</em></strong>. For Kotlin Property, you can get all the properties through <strong><em>getPropertyList()</em></strong> of <strong><em>ProtoBuf.Class</em></strong>:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6725a0234dcd0e3060777dc0e2087476/href">https://medium.com/media/6725a0234dcd0e3060777dc0e2087476/href</a></iframe><p>So that, we can distinguish if a property should be a field or a method in bytecode.</p><h3>Interoperability</h3><p>When I was complacent about having solved the Kotlin metadata problem, I have been punched again, If you want to generate the corresponding Kotlin code according to the type of the element annotated by annotations, you will find that the <strong><em>kotlin.String</em></strong> cannot be replaced by <strong><em>java.lang.String</em></strong>, because in terms of type, they are really two different types, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3c1b8e3d90bef832893c89967fda036d/href">https://medium.com/media/3c1b8e3d90bef832893c89967fda036d/href</a></iframe><p>If you want to generate a wrapper class for the value property, it will look like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04e6458f2ead71a1b99f32ae4d75e3c1/href">https://medium.com/media/04e6458f2ead71a1b99f32ae4d75e3c1/href</a></iframe><p>However, returning <strong><em>Data.value</em></strong> in <strong><em>ValueWrapper.get()</em></strong> throws an error:</p><pre>Type mismatch.<br> Required: java.lang.String<br> Found:    kotlin.String</pre><p>How could this be???</p><p>The types provided by the Kotlin standard library are defined in Kotlin, such as <strong><em>String</em></strong>, is defined as <strong><em>kotlin.String</em></strong>, so why is it <strong><em>java.lang.String</em></strong> in the stub file and bytecode? To take a peek, we have to go to the Kotlin source code — <a href="https://github.com/JetBrains/kotlin/blob/master/core/metadata.jvm/src/org/jetbrains/kotlin/metadata/jvm/deserialization/ClassMapperLite.kt">ClassMapperLite.kt</a>. It turns out that the Kotlin compiler will convert Kotlin’s standard types to Java’s standard types automatically. Therefore, in the stub file, we will find the original definition of <strong><em>kotlin.String</em></strong> has been converted to <strong><em>java.lang.String</em></strong>.</p><p>Therefore, if you want to change the Java standard types in the generated code into Kotlin standard types, you need to reverse the mapping, that is, reverse the mapping relationship in <a href="https://github.com/JetBrains/kotlin/blob/master/core/metadata.jvm/src/org/jetbrains/kotlin/metadata/jvm/deserialization/ClassMapperLite.kt">ClassMapperLite.kt</a>, so that you can generate beautiful Kotlin code as follows:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6f56fd58ca6b5f081ab574754ccb51d5/href">https://medium.com/media/6f56fd58ca6b5f081ab574754ccb51d5/href</a></iframe><h3>Incompatible Kotlin Metadata</h3><p>Anyone who is still using Kotlin versions below 1.5.0 may encounter the following problems when introducing third-party Kotlin libraries:</p><p>“Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.x, expected version is 1.x.y”</p><pre>Module was compiled with an incompatible version of Kotlin. The binary version of its metadata is 1.5.x, expected version is 1.y.z</pre><p>Based on what we know about Kotlin metadata, it can be inferred that Kotlin 1.5.0 made a backward-incompatible change, if this error happens, there are only two options:</p><ul><li>Upgrade the Kotlin version used in your project</li><li>Use a lower version of the third-party library</li></ul><p>Seeing this, do you think you have mastered Kotlin?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2a966b45521a" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Best Practices for Data Immutability]]></title>
            <link>https://medium.com/@johnson.lee/best-practices-for-data-immutability-921f7950f937?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/921f7950f937</guid>
            <category><![CDATA[immutability]]></category>
            <category><![CDATA[immutable]]></category>
            <category><![CDATA[mutable]]></category>
            <category><![CDATA[immutable-objects]]></category>
            <category><![CDATA[mutability]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Sun, 22 May 2022 00:02:20 GMT</pubDate>
            <atom:updated>2022-05-22T00:02:20.516Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*wqX7hzUybZywfZxw" /><figcaption>Photo by <a href="https://unsplash.com/@theshubhamdhage?utm_source=medium&amp;utm_medium=referral">Shubham Dhage</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As we know, a String object in Java is immutable, as a wide-used data type, immutable String objects have more benefits:</p><ul><li>Thread-safe</li><li>Performant</li></ul><p>In fact, we always use mutable objects instead of immutable objects naturally even if they are thread-safe and performant because in programmers’ minds data in the digital world has always been modifiable.</p><h3>The final keyword</h3><p>Just a handful of fingers, I’ve been programming for more than ten years, and Java code accounts for the vast majority of the code I have written. When I write Java code, I will unconsciously add the final keyword in front of variables, why do I have such a habit?</p><p>Usually, I not only write Java but also C/C++, a very simple operation in Java, but it requires several lines or even dozens of lines of code to implement in C, and early C compilers have certain requirements for the location of variable declarations. Unlike Java, which declares as it is used, in the early C world, it’s better to declare variables at the beginning of the function as much as possible.</p><p>The problem caused by declaring variables at the beginning of the function is that when the code is very long, it is not easy to know where the variables will be modified, and They can easily be changed by mistake and not easily recognized.</p><p>So, I am cautious when writing C code, and usually make the modification of variables as concentrated as possible. In order to avoid unexpected modification of variables, I naturally cultivated the habit of adding the final keyword before variables. At that time, I just thought that there are two advantages to writing this way:</p><ul><li>Prevent unexpected mistakes</li><li>Use closures naturally</li></ul><h3>Unmodifiable collections</h3><p>When I review code, I often see some code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/80d6c9834c0be8b1e6f6ad8d118d979d/href">https://medium.com/media/80d6c9834c0be8b1e6f6ad8d118d979d/href</a></iframe><p>There’s nothing wrong with it at first glance, but what if it’s written like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/655d0590fb93ddc93b2ff97c44f39c2d/href">https://medium.com/media/655d0590fb93ddc93b2ff97c44f39c2d/href</a></iframe><p>In order to avoid internal data being modified externally, I usually write this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/fad6b4aea8a5655d1fc20b691a7d7a52/href">https://medium.com/media/fad6b4aea8a5655d1fc20b691a7d7a52/href</a></iframe><h3>Copy-on-write</h3><p>There is also another habit cultivated after reading the source code of Java Swing — notifying the Listener with the Observer pattern, most engineers write it like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/51ab4cfa7c2860a5a311af065577777a/href">https://medium.com/media/51ab4cfa7c2860a5a311af065577777a/href</a></iframe><p>In order to avoid external modification during iterating the listeners, I usually write it like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/4f6ec795ff643fad94084fb53bc87e9b/href">https://medium.com/media/4f6ec795ff643fad94084fb53bc87e9b/href</a></iframe><h3>Closures and lambdas</h3><p>For functional programming languages, Haskell is the originator. In the Haskell world, data is immutable. If you want to modify it, you can only copy it. Many engineers may not understand why Haskell is designed in this way. Does copying every time you modify it will not affect performance? If you have used Java’s closures or Lambdas, I think everyone has a deep understanding that the local variables referenced by closures or Lambdas must be final, why?</p><p>I think engineers who have written JavaScript must have had experiences similar to the following:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a3f81641735a1d2176b2c29faa58e0b/href">https://medium.com/media/3a3f81641735a1d2176b2c29faa58e0b/href</a></iframe><p>The intention of the above code is to create 10 &lt;A&gt; tags as buttons. When any button is clicked, then pop up a dialog to display the corresponding index of the button. At first glance, there is nothing wrong, but when we run it, we can find that it is not what we expected. The actual situation is — all the button popups display 11.</p><blockquote>What the f**k!!??</blockquote><p>The root cause of this problem is that the function corresponding to btn.onclick has a closure, and variables referenced by closures in JavaScript are not required to be final like in Java. And the variable i referenced in the onclick method becomes 11 after the loop is executed. Therefore, the actual execution result of the above example is 11, not its corresponding index. In order to solve this problem, it is generally written like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6d32d40cdcf79cc703a1c409a7fd78d/href">https://medium.com/media/e6d32d40cdcf79cc703a1c409a7fd78d/href</a></iframe><h3>Single source of truth</h3><p>In the MVVM architecture, it is generally recommended to use immutable objects for the data model. Since it is not mandatory, it leads to an inconsistent understanding of the architecture by engineers and abuse of the data model. For example, “immutable objects” are used in some places, and “mutable objects” are used in other places. Some engineers may think that “mutable objects” are not bad, right?</p><p>For simple business logic, the immutability of objects may not be that important, however, once the business complexity increases, and there are multiple consumers of the same data, and if the data model is modified due to “accidental” or “other unexpected reasons” instead of modifying the object through LiveData or StateFlow, it will cause a series of problems:</p><ul><li>Modifications to the Data Model are not observable</li><li>Data flow and control flow are chaotic, and code complexity increases dramatically, which causes weird bugs</li></ul><p>If the data model is mutable, any calls to the data model must assume that it may be modified elsewhere, which complicates the design and development of the architecture, engineers need to consider the case of multi-threaded access, which is undoubtedly a psychological burden.</p><p>Just imagine, if the data model is immutable by default, and there is only one data source for each consumer, everything will become simple, that is — UDF (<a href="https://developer.android.com/jetpack/compose/architecture#udf">Unidirectional Data Flow</a>)!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=921f7950f937" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Convenient Way to Mutate Immutable Objects]]></title>
            <link>https://medium.com/@johnson.lee/convenience-way-to-mutate-immutable-objects-839143f2c270?source=rss-cd9e19338af2------2</link>
            <guid isPermaLink="false">https://medium.com/p/839143f2c270</guid>
            <category><![CDATA[mutator]]></category>
            <category><![CDATA[immutable-objects]]></category>
            <category><![CDATA[immutable]]></category>
            <dc:creator><![CDATA[Johnson Lee]]></dc:creator>
            <pubDate>Sat, 21 May 2022 05:13:49 GMT</pubDate>
            <atom:updated>2022-05-21T05:19:27.924Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*XDjRWF1AhpKrNCUq" /><figcaption>Photo by <a href="https://unsplash.com/@sortino?utm_source=medium&amp;utm_medium=referral">Joshua Sortino</a> on <a href="https://unsplash.com?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>As the <a href="https://en.wikipedia.org/wiki/Immutable_object">Immutable Object</a> becomes the default pattern of functional programming language, such as <a href="https://en.wikipedia.org/wiki/Value_object">Value Object</a> in Java, and <a href="https://kotlinlang.org/docs/data-classes.html">Data Class</a> in Kotlin, even though the immutable pattern has a lot of advantages, modifying properties of deeply nested immutable objects is still not very convenient, for example:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9f4b3e7b56ad6ff4a6cd8872876e16d2/href">https://medium.com/media/9f4b3e7b56ad6ff4a6cd8872876e16d2/href</a></iframe><p>If we want to modify the Person.address.nation.code, we have to write code like this:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ddc2c2eb65b6dbddc1af9ae1c1b834dc/href">https://medium.com/media/ddc2c2eb65b6dbddc1af9ae1c1b834dc/href</a></iframe><p>So, How to make it convenient to modify nested properties?</p><p>Emm. Before we figure out the solution, I would like to ask you guys:</p><blockquote>What’s your expectation for the proposal?</blockquote><blockquote>How would you like to modify nested properties in a convenient way?</blockquote><h3>My Proposal</h3><p>The KISS (Keep it simple, stupid) principle has always been the primary principle of my design, to simplify the manipulation, if I were the API user, I would like to use the API as below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/749ccbcdb0b67bd61f262ff03dd58c8f/href">https://medium.com/media/749ccbcdb0b67bd61f262ff03dd58c8f/href</a></iframe><p>Now, the problem is how to implement the mutate API.</p><p>As we know, each data class in Kotlin has a copy(...) method, why don’t we use the copy(...) method to clone a new immutable object with a new property value?</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6568e8941109bc540b96aa36d6edfb8a/href">https://medium.com/media/6568e8941109bc540b96aa36d6edfb8a/href</a></iframe><p>The mutate function above demonstrated how to mutate an immutable object with specified property and value.</p><p>Wait, the mutate function can only mutate a property with 1 depth, how to mutate a deeper property, e.g. Person.address.nation.code ? The answer is — Recursion.</p><p>We can override a mutate function to mutate a property with depth 2 as shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/c699174a0abb81ae5da72f7c54da5cd9/href">https://medium.com/media/c699174a0abb81ae5da72f7c54da5cd9/href</a></iframe><p>By analogy, we can override a mutate function to mutate a property with depth 3 as shown below:</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/04149e68c8bf7ca4814eef390ae6f963/href">https://medium.com/media/04149e68c8bf7ca4814eef390ae6f963/href</a></iframe><p>Therefore, we can override a mutate function to mutate a deeper property.</p><h3>Source Code</h3><p>Now that the source code has been published to the maven central repository, you can refer to <a href="https://github.com/johnsonlee/mutator">https://github.com/johnsonlee/mutator</a> to use it as a Kotlin library.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=839143f2c270" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>