<?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: Mārtiņš</title>
    <description>The latest articles on DEV Community by Mārtiņš (@graizies).</description>
    <link>https://dev.to/graizies</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%2F1004111%2F6ff014a6-601a-4f2e-8940-f1831102ee26.jpeg</url>
      <title>DEV Community: Mārtiņš</title>
      <link>https://dev.to/graizies</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/graizies"/>
    <language>en</language>
    <item>
      <title>Refactoring the Hyper terminal</title>
      <dc:creator>Mārtiņš</dc:creator>
      <pubDate>Sun, 08 Jan 2023 21:22:06 +0000</pubDate>
      <link>https://dev.to/graizies/refactoring-the-hyper-terminal-26na</link>
      <guid>https://dev.to/graizies/refactoring-the-hyper-terminal-26na</guid>
      <description>&lt;h2&gt;
  
  
  Greetings!
&lt;/h2&gt;

&lt;p&gt;Hi, Hello! My name is Mārtiņš and I am a 2nd year ICT student. Today I have decided to challenge myself and try to refactor part of an already existing codebase.&lt;/p&gt;

&lt;p&gt;In order to decide which codebase I would like to try and refactor, I decided to &lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;look within myself&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;(I think that's what Albert Einstein said once, right?) by searching through my computer for programs that I use or have used in the past. The reason I did that was to maybe find that one of these programs are open-source.&lt;/p&gt;

&lt;p&gt;And guess what? One of them was! This time I am talking about the &lt;a href="https://hyper.is/" rel="noopener noreferrer"&gt;Hyper terminal&lt;/a&gt; developed by Vercel. The repository can be found &lt;a href="https://github.com/vercel/hyper" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  But why am I even writing this, when there are much smarter developers that you can listen to?
&lt;/h2&gt;

&lt;p&gt;Well, maybe you, dear reader, will find it interesting to see how a beginner developer refactors code in a large codebase. Maybe my way of thinking will entertain you.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is Hyper?
&lt;/h2&gt;

&lt;p&gt;Hyper is an Electron-based terminal that is built on Web technologies (HTML, CSS, JS). The terminal supports plugins, meaning that you can customize it to whatever level you feel like. The reason I had it on my computer was because I had a phase in my "developer lifecycle" where I wanted my default terminal application to look as nice as possible.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://i.giphy.com/media/35OOkbcHtrFr8cQD7E/giphy.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://i.giphy.com/media/35OOkbcHtrFr8cQD7E/giphy.gif" width="480" height="270"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  What tools did I use?
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmq7x67z54wu1kpyfdupf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmq7x67z54wu1kpyfdupf.png" alt="Image description" width="800" height="201"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In order to find a code smell I used &lt;a href="https://www.sonarsource.com/products/sonarqube/" rel="noopener noreferrer"&gt;SonarQube&lt;/a&gt;. It is an open-source platform developed for continuous inspection of code quality to perform automatic reviews and static analysis of code. The tool works by scanning the source code and comparing it against a set of rules that define what is considered good code quality. It can detect issues such as coding style violations, potential bugs, security vulnerabilities, and other problems that can impact the quality and maintainability of the code.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.freecodecamp.org%2Fnews%2Fcontent%2Fimages%2F2021%2F08%2Fvscode.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fwww.freecodecamp.org%2Fnews%2Fcontent%2Fimages%2F2021%2F08%2Fvscode.png" width="800" height="418"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We all know and love Visual Studio Code...right?&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FLRVnPYqM8DLag%2Fgiphy.gif%3Fcid%3Decf05e4789ydr18opzn8p8g6vlekszcbb0kvxder1ac03sz7%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia1.giphy.com%2Fmedia%2FLRVnPYqM8DLag%2Fgiphy.gif%3Fcid%3Decf05e4789ydr18opzn8p8g6vlekszcbb0kvxder1ac03sz7%26rid%3Dgiphy.gif%26ct%3Dg" width="480" height="270"&gt;&lt;/a&gt;&lt;br&gt;
...right?&lt;/p&gt;
&lt;h2&gt;
  
  
  The actual refactoring process
&lt;/h2&gt;

&lt;p&gt;Since the Hyper codebase is quite large, I used the aforementioned SonarQube tool to find code smells. In order to avoid refactoring something insignificant like a regex expression (gross, right?), I looked through the ones marked as &lt;strong&gt;Critical&lt;/strong&gt;. Upon looking at the method at first, I was quite scared, honestly. Because (according to GitHub) this code was written 6 years ago, which means it is quite outdated. The function itself falls under the &lt;a href="https://refactoring.guru/smells/long-method" rel="noopener noreferrer"&gt;&lt;em&gt;Long Method&lt;/em&gt;&lt;/a&gt; category, as it contains lots of &lt;strong&gt;if&lt;/strong&gt; statements, is quite long and is definitely hard to read.&lt;/p&gt;

&lt;p&gt;So, what code smells were detected by SonarQube?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv3t2i038m5mv3q6ku501.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv3t2i038m5mv3q6ku501.png" alt="Image description" width="800" height="401"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FBqLnHVdrW1UQpBJBwV%2Fgiphy.gif%3Fcid%3Decf05e47hcsbsd097kf639p07hpcuzh5479key8pqasq8aax%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2FBqLnHVdrW1UQpBJBwV%2Fgiphy.gif%3Fcid%3Decf05e47hcsbsd097kf639p07hpcuzh5479key8pqasq8aax%26rid%3Dgiphy.gif%26ct%3Dg" width="1024" height="1024"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Cognitive Complexity&lt;/em&gt; is a measure of how difficult a unit of code is to intuitively understand.&lt;/p&gt;

&lt;p&gt;You can read more about cognitive complexity &lt;a href="https://docs.codeclimate.com/docs/cognitive-complexity#:~:text=Cognitive%20Complexity%20is%20a%20measure,be%20to%20read%20and%20understand." rel="noopener noreferrer"&gt;here&lt;/a&gt;&lt;/p&gt;
&lt;h2&gt;
  
  
  Before
&lt;/h2&gt;

&lt;p&gt;Here's what the code looked like prior to my refactoring:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function range(a, b, str) {
  var begs, beg, left, right, result;
  var ai = str.indexOf(a);
  var bi = str.indexOf(b, ai + 1);
  var i = ai;

  if (ai &amp;gt;= 0 &amp;amp;&amp;amp; bi &amp;gt; 0) {
    begs = [];
    left = str.length;

    while (i &amp;gt;= 0 &amp;amp;&amp;amp; !result) {
      if (i == ai) {
        begs.push(i);
        ai = str.indexOf(a, i + 1);
      } else if (begs.length == 1) {
        result = [ begs.pop(), bi ];
      } else {
        beg = begs.pop();
        if (beg &amp;lt; left) {
          left = beg;
          right = bi;
        }

        bi = str.indexOf(b, i + 1);
      }

      i = ai &amp;lt; bi &amp;amp;&amp;amp; ai &amp;gt;= 0 ? ai : bi;
    }

    if (begs.length) {
      result = [ left, right ];
    }
  }

  return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Mystical variable names, hard to read and &lt;strong&gt;var&lt;/strong&gt;??&lt;br&gt;
Unacceptable.&lt;/p&gt;

&lt;p&gt;Okay, so this is a function that helps you find special letters or symbols in a word or sentence. It has three parts, &lt;code&gt;a&lt;/code&gt;, &lt;code&gt;b&lt;/code&gt;, and &lt;code&gt;str&lt;/code&gt;. &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are the special letters or symbols that you want to find, and &lt;code&gt;str&lt;/code&gt; is the word or sentence that you want to look in.&lt;/p&gt;

&lt;p&gt;The function first looks for the first special letter &lt;code&gt;a&lt;/code&gt; in &lt;code&gt;str&lt;/code&gt;, and then looks for the first special letter &lt;code&gt;b&lt;/code&gt; after &lt;code&gt;a&lt;/code&gt;. Then, it starts a loop that keeps going until it finds the special letters that go together or until it can't find any more special letters.&lt;/p&gt;

&lt;p&gt;If it finds the special letters that go together, it returns an array with their positions. If it doesn't find any, it returns &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;
&lt;h2&gt;
  
  
  After
&lt;/h2&gt;

&lt;p&gt;And now, let's have a look at the refactored code...&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2F3o7TKNTYGvBRDvq67S%2Fgiphy.gif%3Fcid%3Decf05e47r9ikeze3tjcjzb8u8k87c34vq6kburmg4qaq0vqy%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2F3o7TKNTYGvBRDvq67S%2Fgiphy.gif%3Fcid%3Decf05e47r9ikeze3tjcjzb8u8k87c34vq6kburmg4qaq0vqy%26rid%3Dgiphy.gif%26ct%3Dg" width="480" height="223"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function getRange(startChar, endChar, string) {
  let startIndices = [];
  let startIndex = string.indexOf(startChar);
  let endIndex = string.indexOf(endChar, startIndex + 1);
  let currentIndex = startIndex;
  let result;

  if (startIndex &amp;gt;= 0 &amp;amp;&amp;amp; endIndex &amp;gt; 0) {
    let leftmostStartIndex = string.length;
    while (currentIndex &amp;gt;= 0 &amp;amp;&amp;amp; !result) {
      if (currentIndex == startIndex) {
        startIndices.push(currentIndex);
        startIndex = string.indexOf(startChar, currentIndex + 1);
      } else if (startIndices.length == 1) {
        result = [startIndices.pop(), endIndex];
      } else {
        let start = startIndices.pop();
        if (start &amp;lt; leftmostStartIndex) {
          leftmostStartIndex = start;
        }
        endIndex = string.indexOf(endChar, currentIndex + 1);
      }
      currentIndex = startIndex &amp;lt; endIndex &amp;amp;&amp;amp; startIndex &amp;gt;= 0 ? startIndex : endIndex;
    }
    if (startIndices.length) {
      result = [leftmostStartIndex, endIndex];
    }
  }
  return result;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In my humble opinion, this is much better.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;No more var's &lt;/li&gt;
&lt;li&gt;Variables like &lt;code&gt;a&lt;/code&gt; and &lt;code&gt;b&lt;/code&gt; are renamed to more "easy-to-read" names&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final thoughts / conclusion
&lt;/h2&gt;

&lt;p&gt;In conclusion, refactoring code can be a challenging but rewarding task. It allows you to improve the readability and maintainability of the code, as well as fix any potential issues that may have been overlooked in the past. In this blog post, I walked through my process of refactoring a function in the Hyper terminal codebase, using tools like SonarQube to identify areas that needed improvement and implementing changes to make the code easier to understand and work with. Whether you are a beginner or an experienced developer, there is always room to improve and learn, and refactoring existing code is a great way to do so.&lt;/p&gt;

&lt;p&gt;Thank you very much for reading my post!&lt;br&gt;
Until next time :)&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2Fm9eG1qVjvN56H0MXt8%2Fgiphy.gif%3Fcid%3Decf05e47dc66wcec1b3f9kx17c9nzpwr14dbd00mvh8bxa5z%26rid%3Dgiphy.gif%26ct%3Dg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fmedia3.giphy.com%2Fmedia%2Fm9eG1qVjvN56H0MXt8%2Fgiphy.gif%3Fcid%3Decf05e47dc66wcec1b3f9kx17c9nzpwr14dbd00mvh8bxa5z%26rid%3Dgiphy.gif%26ct%3Dg" width="306" height="498"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
      <category>website</category>
      <category>webdev</category>
    </item>
  </channel>
</rss>
