<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: yuudaikido</title>
    <description>The latest articles on DEV Community by yuudaikido (@yuudaikido).</description>
    <link>https://dev.to/yuudaikido</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4069552%2Fb58465ca-d64b-47f7-8782-673118256377.jpg</url>
      <title>DEV Community: yuudaikido</title>
      <link>https://dev.to/yuudaikido</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yuudaikido"/>
    <language>en</language>
    <item>
      <title>Two Pieces of Code, Same Answer — Which One Should You Write?</title>
      <dc:creator>yuudaikido</dc:creator>
      <pubDate>Sun, 23 Aug 2026 04:21:30 +0000</pubDate>
      <link>https://dev.to/yuudaikido/two-pieces-of-code-same-answer-which-one-should-you-write-2p43</link>
      <guid>https://dev.to/yuudaikido/two-pieces-of-code-same-answer-which-one-should-you-write-2p43</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;This is my first post. It has been one year since I became an engineer with no prior experience.&lt;/p&gt;

&lt;p&gt;Now, once you become an engineer and start learning programming, there is a phrase you will hear at least once:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"Shorter (simpler) code is better."&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Is that true? The other day, I put my own code next to a model-answer-style solution, and I ended up doubting this phrase.&lt;/p&gt;

&lt;p&gt;The subject is a classic matchstick problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Matchstick Square Problem
&lt;/h2&gt;

&lt;p&gt;You make squares in a row with matchsticks. The first square needs 4 sticks, and from the second square on, each one shares a side with its neighbor, so you only add 3 sticks each time. The question: how many matchsticks do you need to make n squares?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; _ _ _
|_|_|_|   ← 3 squares = 4 + 3 + 3 = 10 sticks
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;What came to my mind was not a formula but a &lt;strong&gt;motion&lt;/strong&gt;. Build the first square with 4 sticks, then keep adding 3 — and that hand movement became my code, just as it was.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;① Thinking turned into code&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countMatchsticks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;FIRST_SQUARE&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// the first square takes 4 sticks&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;STICKS_TO_ADD&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// add 3 for each square after that&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;FIRST_SQUARE&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;squareCount&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;total&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;STICKS_TO_ADD&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;total&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can see it turns the earlier description — "4 sticks for the first square, then 3 more for each square after that" — into code, motion by motion.&lt;/p&gt;

&lt;p&gt;On the other hand, there is a much shorter way to write this.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;② The short version&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;countMatchsticks&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;squareCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;squareCount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Both return the same answer. So, which one should you write?&lt;/p&gt;

&lt;h2&gt;
  
  
  What Happened in My Head When I Read Them
&lt;/h2&gt;

&lt;p&gt;Let me be honest.&lt;/p&gt;

&lt;p&gt;With the longer code, I saw the matchsticks the moment I read it. That motion — build a square with 4 sticks, keep adding 3 — sits right on top of the code. So &lt;strong&gt;I knew it was correct just by reading it&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The short one was different. Looking at &lt;code&gt;3 * squareCount + 1&lt;/code&gt;, no matchsticks appeared in my head. So how did I convince myself that this one was also correct? — &lt;strong&gt;I plugged in the same numbers and checked.&lt;/strong&gt; n=1 gives 4, n=2 gives 7, n=3 gives 10... OK, it works.&lt;/p&gt;

&lt;p&gt;There is an important asymmetry here.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;① Thinking turned into code: you understand it by reading it&lt;/li&gt;
&lt;li&gt;② The short version: &lt;strong&gt;you cannot trust it without verifying it&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Short code outsources the labor of verification to the reader.&lt;/p&gt;

&lt;h2&gt;
  
  
  Naming the Variables Reveals a Cruel Difference
&lt;/h2&gt;

&lt;p&gt;There is an experiment that makes this difference much clearer: &lt;strong&gt;try giving meaningful names to both pieces of code.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The long one was easy. &lt;code&gt;FIRST_SQUARE&lt;/code&gt; (the first square takes 4 sticks), &lt;code&gt;STICKS_TO_ADD&lt;/code&gt; (3 more for each square after). Even with every comment deleted, the variable names alone tell the story of the matchstick world.&lt;/p&gt;

&lt;p&gt;What about the short one?&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;3&lt;/code&gt; can still be named: "three sticks per square." But &lt;strong&gt;there is no name you can give to the &lt;code&gt;+ 1&lt;/code&gt;&lt;/strong&gt;. In the world of matchstick squares, "one stick" does not exist as a thing. "One stick" is a step in a process.&lt;/p&gt;

&lt;p&gt;That &lt;code&gt;+1&lt;/code&gt; is nothing but a leftover of the calculation that split the 4-stick square into "3 sticks + 1 stick."&lt;/p&gt;

&lt;p&gt;In other words, the short code breaks down the moment you try to express its intent through variable names. It is proof that &lt;strong&gt;the intent no longer lives in the code&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  So Is "Shorter Is Better" Completely Wrong?
&lt;/h2&gt;

&lt;p&gt;Here is the interesting part: &lt;strong&gt;in the world of mathematics, the conclusion flips.&lt;/strong&gt; In math, the fully expanded &lt;code&gt;3n + 1&lt;/code&gt; is without question the better form.&lt;/p&gt;

&lt;p&gt;Why does the right answer flip?&lt;/p&gt;

&lt;p&gt;In math, &lt;code&gt;3n + 1&lt;/code&gt; shows up as the &lt;strong&gt;result&lt;/strong&gt; of stating a proposition and proving it through logical argument. The story of "why this works" is stored outside the formula — in the proof itself. That is why the formula can be stripped down to its most minimal, beautiful form: the story is safely kept somewhere else.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Coding is not like that.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For example, say a colleague (or your future self) opens this code for the first time six months from now. All they can read is the code, the documentation that comes with it, and maybe the comments.&lt;/p&gt;

&lt;p&gt;No proof notebook is attached. So the story — the author's intent — has nowhere to live but inside the code.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;4 + (n-1)*3&lt;/code&gt; is the form that folds the proof into the expression&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;3n + 1&lt;/code&gt; is the form that throws the proof away&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The belief that "short code is justice" turns out to be a bad import: we took the aesthetics of mathematics — where the story can live &lt;em&gt;outside&lt;/em&gt; the formula — and carried it straight into the world of code, where the story has no choice but to live &lt;em&gt;inside&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion: Which One Should You Write?
&lt;/h2&gt;

&lt;p&gt;I have been sounding like a story-first advocate, so let me step back and look at it flatly.&lt;/p&gt;

&lt;p&gt;It is neither "short = justice" nor "long = careful." The question to ask is this:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;"What do I want to tell the next person who reads this code?"&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;If you want to convey the story of the motion&lt;/strong&gt;, write it so the steps are visible. &lt;strong&gt;If the answer alone is enough&lt;/strong&gt; — the logic is already proven and shared, or comments and tests keep the story somewhere else — write it short.&lt;/p&gt;

&lt;p&gt;My conclusion this time: as a beginner, you have absolutely no reason to be ashamed of the "long but motion-visible" code you wrote.&lt;/p&gt;

&lt;h2&gt;
  
  
  Bonus: Where This Story Goes Next
&lt;/h2&gt;

&lt;p&gt;At its core, this was a story about a choice: &lt;strong&gt;keep the intent in the code, or erase it.&lt;/strong&gt; Now, flip the question — what if there were situations where you &lt;em&gt;want&lt;/em&gt; to erase the intent on purpose...? There are. The world of obfuscation and security.&lt;/p&gt;

&lt;p&gt;But that is a story for another article.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>javascript</category>
      <category>cleancode</category>
      <category>codequality</category>
    </item>
  </channel>
</rss>
