<?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: Glenn Faison</title>
    <description>The latest articles on DEV Community by Glenn Faison (@glennfaison).</description>
    <link>https://dev.to/glennfaison</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F395819%2Fa6049265-395f-4732-829b-69e63157e64b.png</url>
      <title>DEV Community: Glenn Faison</title>
      <link>https://dev.to/glennfaison</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/glennfaison"/>
    <language>en</language>
    <item>
      <title>JavaScript/TypeScript Development and Phantom Bugs: When Code Magically Works (or Fails)</title>
      <dc:creator>Glenn Faison</dc:creator>
      <pubDate>Wed, 11 Jun 2025 19:31:34 +0000</pubDate>
      <link>https://dev.to/glennfaison/javascripttypescript-development-and-phantom-bugs-when-code-magically-works-or-fails-4abi</link>
      <guid>https://dev.to/glennfaison/javascripttypescript-development-and-phantom-bugs-when-code-magically-works-or-fails-4abi</guid>
      <description>&lt;p&gt;Every developer has a story. You’re deep into debugging, tearing your hair out over a bug that defies logic. You step through the code, variables show impossible values, and execution paths twist unexpectedly. Then, you restart your development server, or maybe even your entire machine, and &lt;em&gt;poof&lt;/em&gt;! The bug is gone. Even more perplexing, sometimes simply adding a &lt;code&gt;console.log&lt;/code&gt; statement makes the issue vanish.&lt;/p&gt;

&lt;p&gt;This isn’t just frustrating; it chips away at your confidence and leaves you wondering: was it ever really a bug, or just a ghost in the machine?&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;A Recent Encounter with the Unexplainable&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I recently hit one such wall while working on a Next.js application, developing a higher-order component for text editing. The component &lt;code&gt;withTextEditing&lt;/code&gt; wraps other React components, adding content editing functionality and integrating with a state management system via &lt;code&gt;useReducer&lt;/code&gt; and TanStack Query.&lt;/p&gt;

&lt;p&gt;Here’s a simplified version of the TypeScript code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// Example: withTextEditing HOC&lt;/span&gt;
&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;withTextEditing&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Tag&lt;/span&gt; &lt;span class="kd"&gt;extends&lt;/span&gt; &lt;span class="nb"&gt;Extract&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ComponentTag&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;header1&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;header2&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;header3&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;paragraph&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;inline-text&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;button&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;ComponentType&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;ComponentProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Tag&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;WrappedComponentProps&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&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="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;ContentEditableComponent&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;props&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;ComponentProps&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;Tag&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// Line 1: State initialization&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;contentEditable&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setContentEditable&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;React&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;useState&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;boolean&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

    &lt;span class="c1"&gt;// Line 2: Destructuring props&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;&lt;span class="nx"&gt;otherProps&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;props&lt;/span&gt;

    &lt;span class="c1"&gt;// Line 3: Accessing a nested property&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;isConnected&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;!!&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;attributes&lt;/span&gt;&lt;span class="p"&gt;?.&lt;/span&gt;&lt;span class="nx"&gt;__data_source__&lt;/span&gt;
    &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nx"&gt;WrappedComponent&lt;/span&gt;
        &lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;component&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;contentEditable&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;contentEditable&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="nx"&gt;suppressContentEditableWarning&lt;/span&gt;
        &lt;span class="p"&gt;{...&lt;/span&gt;&lt;span class="nx"&gt;otherProps&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
      &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="sr"&gt;/WrappedComponent&lt;/span&gt;&lt;span class="err"&gt;&amp;gt;
&lt;/span&gt;    &lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;While stepping through &lt;code&gt;ContentEditableComponent&lt;/code&gt; in the browser's debugger, I observed truly baffling behavior:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;After Line 2 (the destructuring):&lt;/strong&gt; The &lt;code&gt;component&lt;/code&gt; variable was &lt;code&gt;undefined&lt;/code&gt;, even though &lt;code&gt;props.component&lt;/code&gt; clearly held a valid object. This directly contradicts how JavaScript destructuring works, where a variable should be assigned the value present at the time of destructuring.&lt;br&gt;
&lt;strong&gt;After Line 3 (accessing isConnected):&lt;/strong&gt; Even more astonishingly, the component variable (which was &lt;code&gt;const&lt;/code&gt;!) seemed to change its value. It was no longer &lt;code&gt;undefined&lt;/code&gt;; it now held the value of &lt;code&gt;props.component.attributes&lt;/code&gt;. A &lt;code&gt;const&lt;/code&gt; variable being re-assigned is fundamentally impossible in JavaScript under normal execution.&lt;br&gt;
This is the kind of situation that sends you into a debugging spiral. How can a &lt;code&gt;const&lt;/code&gt; change? How can destructuring fail so spectacularly?&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;The “Magic Fixes” We Rely On&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Often, faced with such spectral bugs, developers resort to a few common “fixes” that don’t involve changing the code itself:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;The “Sleep On It” Fix:&lt;/strong&gt; You go to bed, utterly frustrated. You wake up, run the exact same code, and it magically works. Did the computer clear some cache? Did the moon align differently?&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The “Log it and Shame it”:&lt;/strong&gt; You add &lt;code&gt;console.log&lt;/code&gt; statements throughout your code to trace variable values and execution flow. Suddenly, the bug disappears.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The Restart Ritual:&lt;/strong&gt; Restarting your development server, your browser, your IDE, or even your entire operating system.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Why Does This Happen? The Suspects (According to Me)&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;While these “magic fixes” feel supernatural, I want to believe they hint at underlying technical complexities and inconsistencies in my code OR development environments. A quick web search (probably not deep enough) doesn’t really point me towards anything useful concerning Javascript or TypeScript, but I have a few suspects (other than that I routinely write code with magical bugs):&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Debugger and Sourcemap Misalignment:&lt;/strong&gt; Modern JavaScript/TypeScript projects are heavily transpiled and bundled. Debuggers rely on sourcemaps to map the running, minified code back to their original source. If these sourcemaps are incorrect, out of date, or if the debugger itself has quirks, the execution pointer or variable values displayed in the debugger can be misleading or simply wrong.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Caching Issues:&lt;/strong&gt; Build tools (Webpack, Next.js internal caching), browser caches, IDEs handling file versions… Maybe they sometimes hold onto stale versions of code or assets, leading to unexpected behavior until a hard refresh or cache clear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Subtle Race Conditions and Timing?:&lt;/strong&gt; In highly asynchronous applications, tiny variations in execution timing can determine whether a race condition manifests or not. A restart, a &lt;code&gt;console.log&lt;/code&gt;, or even system reload can subtly alter these timings, making a bug appear or disappear.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Environmental Factors:&lt;/strong&gt; I often wonder if my environment increases my chances of facing such flakey issues; high temperatures, moisture, etc&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Long System Uptime/Memory Leaks:&lt;/strong&gt; A system running for days or weeks might accumulate memory leaks or other OS-level instabilities that affect application performance or debugging tools.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Overheating:&lt;/strong&gt; An overheated CPU or GPU can lead to unstable system behavior, potentially affecting compiler or runtime stability.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;A Call to the JavaScript/TypeScript Community&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;I’m curious to hear from other developers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;How often do you encounter these “phantom bugs” or “magic fixes” in your JavaScript/TypeScript development?&lt;/strong&gt; Is it a rare occurrence, or a frustratingly regular part of your workflow?&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Do you consider them a major hindrance to productivity and confidence, or just a minor annoyance that you quickly circumvent?&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;What’s your go-to immediate solution when you suspect such an issue?&lt;/strong&gt; (e.g., restart dev server, clear browser cache, restart browser, restart OS, add console.log?)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Have you noticed any specific conditions under which these issues are more likely to occur?&lt;/strong&gt; (e.g., specific library versions, long running processes, low disk space, specific hardware configurations, heavy network activity affecting local tools?)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let’s discuss these elusive bugs, why they happen, and share our strategies for navigating the often-magical world of JavaScript/TypeScript development.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>typescript</category>
      <category>react</category>
    </item>
    <item>
      <title>Your First/Last Name Fields Are Broken for Half the World</title>
      <dc:creator>Glenn Faison</dc:creator>
      <pubDate>Thu, 18 Jul 2024 15:10:08 +0000</pubDate>
      <link>https://dev.to/glennfaison/first-name-and-last-name-how-identity-verification-became-the-bane-of-my-existence-55bh</link>
      <guid>https://dev.to/glennfaison/first-name-and-last-name-how-identity-verification-became-the-bane-of-my-existence-55bh</guid>
      <description>&lt;p&gt;Most identity-verification and onboarding forms still assume Western "first name + last name" conventions, systematically rejecting or mangling the names of hundreds of millions of people worldwide.&lt;/p&gt;

&lt;p&gt;I want to talk about my frustrations with identification in tech, the kinds of issues that I've encountered, and some suggestions to fix it.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Self-imposed Limitations of Current Systems
&lt;/h2&gt;

&lt;p&gt;Our current systems tend to ask people for a "first name" and "last name", and our systems (computerized or not) tend to do some funny form of composition in order to generate a full name. In my opinion, they could just ask the foremost authority: the person who answers to those names.&lt;/p&gt;

&lt;p&gt;While this arbitrary composition works well for many cases, there is a significant enough population for whom it doesn't. The system ends up referring to its users in ways that are less than satisfactory.&lt;/p&gt;

&lt;p&gt;Something that should be obvious is that everybody has a full name. This is the basic common ground our systems should start from. If the system actually requires it, just ask the user for their names as they are on the birth certificate, and they will give you that. However, a full name in no way tells you what the user would like you to call them, and how they would like to be called also depends on who is doing the calling.&lt;/p&gt;

&lt;h2&gt;
  
  
  Cultural Differences in Naming Conventions
&lt;/h2&gt;

&lt;p&gt;Naming conventions and formats vary conspicuously across different cultures. For example, some cultures have the notion of a "family name", a name that every member of the family shares. That family name may come in the first or last position or in the middle, whatever it is. Some people may have 4 names, 3, or just 2.&lt;/p&gt;

&lt;p&gt;An interesting case study would be Cameroon, where there is a French-speaking population and an English-speaking one, divided in roughly a 70:30 ratio, respectively. For most of the anglophone citizens, the family name, which Europeans or Americans would refer to as "last name", tends to come last when they write out their full names. Conversely, for the Francophones, this "last name" will generally come first.&lt;/p&gt;

&lt;p&gt;And I'm actually using these terms loosely because they will mean different things in different cultures, they may or may not even exist in certain cultures. Each name has a different significance and label to it, depending on the culture where it is from. There are cultures where the last name of a person is their father's name or mother's name with a certain suffix. For some others, the last name is a tribute to the &lt;em&gt;namee&lt;/em&gt;'s godparent (or just a significant person to the &lt;em&gt;namee&lt;/em&gt;'s parents). The possible quirks in naming are very many.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Discretion of the User
&lt;/h2&gt;

&lt;p&gt;Another thing I've noticed is that for most of the systems that request first and last names from users, these names actually don't matter to the system. They are not unique in the system. They are not usually tied to identification. Usually, what the system needs, is a way to refer to the user in communications between the system and user. This issue can be solved by requesting a username.&lt;/p&gt;

&lt;p&gt;There exist a few cases where the system will need to publicly refer to a user. The system should also ask the user to give that information to them, rather than arbitrarily spinning something up. If I get a certification from some system and they make a public post about it or print out a certificate, I would like to either choose the name on the certificate or have my actual full name on it, in the right order.&lt;/p&gt;

&lt;h2&gt;
  
  
  A Case Study: Cameroon
&lt;/h2&gt;

&lt;p&gt;In my country, the majority is French-speaking, and in general, their last names or family names come first in the order of names. Across the country, the tendency is that in public services, the system will ask for your names in a disjointed &lt;em&gt;first-name-and-last-name&lt;/em&gt; fashion, and the majority of the processes are run according to the French-speaking system. The result is many cases of anglophone citizens having name mismatches between their birth certificates and public documents.&lt;/p&gt;

&lt;p&gt;Right now, what happens, roughly speaking, is that for some anglophone people, public systems will have their last names in the first position, and their first names in the last position. It could range from mildly displeasing to inconvenient if the order of your names is botched on a public document. It's a failure, but it is what it is.&lt;/p&gt;

&lt;p&gt;I observed that some anglophone citizens in these situations will choose to enter all their names in either the slot for "first name" or the slot for "last name" on any form. They just enter their full names in one of these slots and leave the other empty. However, another name composition issue comes in where these systems now have your full name, with "not applicable" or "N/A" as a prefix or suffix. This is a little better but still not ideal, the only benefit being that your names are in the right order.&lt;/p&gt;

&lt;h2&gt;
  
  
  Personal Experience with LinkedIn Verification
&lt;/h2&gt;

&lt;p&gt;With the previous context, let's move on to my passport and how putting my full name in the "last name" slot and leaving the "first name" slot empty became a problem.&lt;/p&gt;

&lt;p&gt;So on LinkedIn, I've been trying to get my profile verified, to increase my success in the job market. LinkedIn uses a third party called &lt;a href="https://withpersona.com/" rel="noopener noreferrer"&gt;Persona&lt;/a&gt; for verification, and Persona requires that I scan my international passport in order to verify my information. That makes sense.&lt;/p&gt;

&lt;p&gt;However, they absolutely require a first name and they absolutely require a last name. So what happens is that when I scan my passport, the "first name" is empty. The "last name" has my full name. Persona tells me the passport is invalid because they cannot see a first name and unfortunately, there's no way I can edit it to add that first name in. So what happens? I cannot complete my verification. The only thing I'm going to have to do is reach out to their customer support.&lt;/p&gt;

&lt;h2&gt;
  
  
  The Quirks of Social Media Names
&lt;/h2&gt;

&lt;p&gt;I created a social media account and I wanted my &lt;strong&gt;display name&lt;/strong&gt; to be "Evil Genius", I decided to enter "Evil" as "first name" and "Genius" as "last name", hoping that they're going to compose it as "Evil Genius" as opposed to "Genius Evil", "Evil", or "Genius". I wasn't warned that it was up to their discretion to pick what name to call me or show other users in notifications or posts.&lt;/p&gt;

&lt;p&gt;Imagine my disappointment when I opened the page, just to be greeted by "Hello, Evil". Doesn't sound as funny as "Evil Genius", does it?&lt;/p&gt;

&lt;h2&gt;
  
  
  Suggestions for Improvement
&lt;/h2&gt;

&lt;p&gt;All these issues with naming conventions being decided by the systems rather than by the user hurt people's sensitivities, cause issues with integrating identification between different systems, and just make the systems inconvenient for users. I would suggest:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;  just sticking with asking for usernames&lt;/li&gt;
&lt;li&gt;  asking the user how they would like to be referred to in public communications and in bilateral dealings with the system&lt;/li&gt;
&lt;li&gt;  If the user's real full name is required, ask for that, rather than composing something up.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This would save the operators of the system the trouble of having to compose these names, along with the inconveniences discussed above. The feelings and the happiness of the users are protected, and everybody is happy.&lt;/p&gt;

</description>
      <category>tech</category>
      <category>webdev</category>
      <category>userverification</category>
      <category>painpoint</category>
    </item>
    <item>
      <title>A Good Reason to NOT Copy and Paste Code</title>
      <dc:creator>Glenn Faison</dc:creator>
      <pubDate>Thu, 11 Nov 2021 20:04:54 +0000</pubDate>
      <link>https://dev.to/glennfaison/a-good-reason-why-you-should-not-copy-and-paste-code-1eoi</link>
      <guid>https://dev.to/glennfaison/a-good-reason-why-you-should-not-copy-and-paste-code-1eoi</guid>
      <description>&lt;p&gt;Many beginners are advised to “type code yourself instead of copy-pasting” because it enables one to internalize the process and the code more quickly. But as programmers gain more experience and attain the level where they need to get things done rather than ensure they learn, they get comfortable with &lt;code&gt;CTRL+C&lt;/code&gt;, &lt;code&gt;CTRL+V&lt;/code&gt; from Stack Overflow and other sources. As an experienced developer, I recently saw first-hand why I should never copy and paste any code I found online (or anywhere, for that matter)&lt;/p&gt;

&lt;p&gt;Consider the code below (see &lt;a href="https://certitude.consulting/blog/en/invisible-backdoor/" rel="noopener noreferrer"&gt;https://certitude.consulting/blog/en/invisible-backdoor/&lt;/a&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;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="nx"&gt;ENV_PROD&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ENV_DEV&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PRODUCTION&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;DEVELOPMENT&lt;/span&gt;&lt;span class="dl"&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;environment&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;PRODUCTION&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;isUserAdmin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;environmentǃ&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nx"&gt;ENV_PROD&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;true&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="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="nf"&gt;isUserAdmin&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Looks benign, right? And we would expect this code to always return false, yes? Unfortunately, no. Now open a browser console, copy this code, and paste in the console. You may be shocked that it returns true. &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxicm573a12s2iy8rnjl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftxicm573a12s2iy8rnjl.png" alt="Copying the above code into a browser console gives us an unexpected result!" width="800" height="301"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;To cut the long story short, what looks like a &lt;em&gt;loose inequality&lt;/em&gt; check on line #4, is deceptively an assignment operation, which reads like (&lt;code&gt;environmentǃ = ENV_PROD&lt;/code&gt;)! In JavaScript, assignment operations return the assigned value, which in this case is truthy (will be treated as true wherever a boolean value is expected) &lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxvfmv7rh6k60sejxdjl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqxvfmv7rh6k60sejxdjl.png" alt="Here we have an assignment, rather than an inequality check" width="372" height="118"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But isn’t &lt;code&gt;environmentǃ&lt;/code&gt; an invalid variable name in JavaScript, you ask? It’s complicated. You’d be right to say an exclamation sign cannot be part of a variable name. However, the &lt;code&gt;ǃ&lt;/code&gt; you see there is in fact not the everyday exclamation sign you know. It’s an obscure character that happens to be accepted as regular text by the JavaScript interpreter, and thus can be a valid part of a variable name&lt;/p&gt;

&lt;p&gt;So while a programmer looks at line #4 and sees &lt;code&gt;environment != ENV_PROD&lt;/code&gt;, the JavaScript interpreter sees &lt;code&gt;environmentǃ = ENV_PROD&lt;/code&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faanptmza1j9ngjx1hkjs.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Faanptmza1j9ngjx1hkjs.png" alt="Some insight into how this bug hides in plain sight" width="800" height="227"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;There! I hope this is a compelling enough reason to not take copy-pasted code lightly. And I hope you see how typing code snippets yourself can save you from bugs that hide in plain sight!&lt;/p&gt;

</description>
      <category>programming</category>
    </item>
  </channel>
</rss>
