<?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: james</title>
    <description>The latest articles on DEV Community by james (@varjmes).</description>
    <link>https://dev.to/varjmes</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%2F50795%2F7977f323-faa9-478b-9717-5b42b81f3985.png</url>
      <title>DEV Community: james</title>
      <link>https://dev.to/varjmes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/varjmes"/>
    <language>en</language>
    <item>
      <title>Guides to Refactoring</title>
      <dc:creator>james</dc:creator>
      <pubDate>Sun, 03 Nov 2019 09:59:56 +0000</pubDate>
      <link>https://dev.to/varjmes/guides-to-refactoring-4ndb</link>
      <guid>https://dev.to/varjmes/guides-to-refactoring-4ndb</guid>
      <description>&lt;p&gt;One of the things I am lacking in experience with is refactoring code. So I did what I always do, and asked Twitter for their thoughts.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://twitter.com/varjmes/status/1189574679142588417"&gt;https://twitter.com/varjmes/status/1189574679142588417&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I picked out a few things from the replies I intend to go through and added some of my own. I cannot vouch for any of these yet, so do let me know what you think of them.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;📙 Paid: &lt;a href="https://www.sandimetz.com/99bottles"&gt;99 Bootles of OOP by Sandi Metz&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📙 Free: &lt;a href="http://refactoringjs.com/"&gt;Refactoring JavaScript&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📙 Paid: &lt;a href="https://www.amazon.co.uk/Refactoring-Improving-Design-Existing-Technology/dp/0201485672/"&gt;Refactoring by Martin Fowler&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📙 &lt;a href="https://insightloop.digdeeproots.com/"&gt;Insight Loop: Code by Refactoring, a workshop by Deep Roots&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📺 &lt;a href="https://www.youtube.com/watch?v=KA9i5IGS-oU"&gt;Therapeutic Refactoring by Katrina Owen&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;📺 &lt;a href="https://www.youtube.com/watch?v=DC-pQPq0acs"&gt;Refactoring from Good to Great by Ben Orenstein&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;A book is never going to teach you something fully though, everything you learn needs to be put into practice. So, find some small pieces of the codebase you're most familiar with and ask yourself if it could use some cleanup. Maybe there's a &lt;code&gt;Class&lt;/code&gt; that's 700 lines long? Or a React component that has turned into an absolute monster? 🧟‍♂️&lt;/p&gt;

&lt;p&gt;I'm curious if you have any other resources or tips on the art of refactoring, so do share in the comments!&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>tutorial</category>
      <category>books</category>
      <category>refactorit</category>
    </item>
    <item>
      <title>TIL: The pecularity of the sort function</title>
      <dc:creator>james</dc:creator>
      <pubDate>Sun, 27 Oct 2019 19:41:20 +0000</pubDate>
      <link>https://dev.to/varjmes/til-the-pecularity-of-the-sort-function-1jma</link>
      <guid>https://dev.to/varjmes/til-the-pecularity-of-the-sort-function-1jma</guid>
      <description>&lt;p&gt;Today I learned something strange about the &lt;code&gt;sort&lt;/code&gt; function in JavaScript.&lt;/p&gt;

&lt;p&gt;This evening I was going through a course on algorithms in JavaScript. The instructor gave a task to create a unique sort function that took an array, removed duplicate values and sorted it in ascending order.&lt;/p&gt;

&lt;p&gt;They gave some starting code to work with:&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="nx"&gt;uniqSort&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;arr&lt;/span&gt; &lt;span class="o"&gt;=&amp;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;breadcrumbs&lt;/span&gt; &lt;span class="o"&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;arr&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&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;Before tackling the task at hand I said to myself: aha! I don't need to have that function within &lt;code&gt;sort&lt;/code&gt; as the &lt;code&gt;sort&lt;/code&gt; function already sorts in ascending order!&lt;/p&gt;

&lt;p&gt;But then I thought to myself: wait, the instructor put that there for a reason. Let's find out why.&lt;/p&gt;

&lt;p&gt;So I went digging. Here is &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort"&gt;the documentation on the &lt;code&gt;sort&lt;/code&gt; function&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If compareFunction is not supplied, all non-undefined array elements are sorted by converting them to strings and comparing strings in UTF-16 code units order.&lt;br&gt;
...&lt;br&gt;
In a numeric sort, 9 comes before 80, but because numbers are converted to strings, "80" comes before "9" in the Unicode order.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Pardon?&lt;/p&gt;

&lt;p&gt;So, if I do the following:&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I get&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because my array values are converted to strings before sorting. And as we saw before, "'80' comes before '9' in the Unicode order"&lt;/p&gt;

&lt;p&gt;I'm told this is a common "gotcha!" and I didn't encounter this issue in the 4+ years I've been a developer. I count myself lucky as I bet that would have been annoying to debug.&lt;/p&gt;

&lt;p&gt;Make sure you always do your sorts with a function!&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="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;80&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;a&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm so curious about why this decision was made when the &lt;code&gt;sort&lt;/code&gt; function was originally written. If anyone knows please comment. What other weird things in JavaScript do you know about that I might not?&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>todayilearned</category>
    </item>
  </channel>
</rss>
