<?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: Todd Eidson</title>
    <description>The latest articles on DEV Community by Todd Eidson (@eidsonator).</description>
    <link>https://dev.to/eidsonator</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%2F48157%2F1ea6fa03-5479-4e5f-9e70-0eca60fb6df4.jpeg</url>
      <title>DEV Community: Todd Eidson</title>
      <link>https://dev.to/eidsonator</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/eidsonator"/>
    <language>en</language>
    <item>
      <title>Alphabetize your Way to More Readable Code</title>
      <dc:creator>Todd Eidson</dc:creator>
      <pubDate>Sun, 03 Mar 2019 20:31:55 +0000</pubDate>
      <link>https://dev.to/eidsonator/alphabetize-your-way-to-more-readable-code-15i1</link>
      <guid>https://dev.to/eidsonator/alphabetize-your-way-to-more-readable-code-15i1</guid>
      <description>&lt;p&gt;Originally posted on my personal &lt;a href="https://eidson.info/post/alphabetize-for-more-readable-code"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Alphabetize your Way to More Readable Code
&lt;/h1&gt;

&lt;p&gt;Super short and simple post this time, because it's explaining a simple concept. The concept is so simple, that I'm slightly embarrassed, almost shocked, that it took me almost 7 years of professional programming, and 3 and a half years of college to see and use this pattern.&lt;/p&gt;

&lt;p&gt;The tip? Any time you are writing code and the order of the lines is irrelevant, such as declaring class variables, creating a JSON object, populating an associative array, adding dependencies, configuring your configuration, ad infinitum, use alphabetical order.&lt;/p&gt;

&lt;p&gt;Instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Marshall Mathers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;palms:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sweaty"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;knees:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"weak"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;arms:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"heavy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;nervous:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;sweater:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mom's spaghetti"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Use this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;arms:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"heavy"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;knees:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"weak"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;name:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Marshall Mathers"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;nervous:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;palms:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"sweaty"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="err"&gt;sweater:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"mom's spaghetti"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Sure, it does not rhyme as well, but if you need to change &lt;code&gt;nervous&lt;/code&gt; to false, it is much easier to find in the list. It does take a little extra brain power to add an item to the list, instead of just appending to the list, you may have to sing a phrase of the alphabet (unless you have a tool that sorts for you, see below). However, since code is read at an average of 10 times after it is written, you'll see the pay off when looking through a long JSON object or associative array to alter or fix something.&lt;/p&gt;

&lt;p&gt;I used to try and use some sort of hierarchical order with grouping in most places that I use this now, but there is no real hierarchy in most places that this can be implemented. Take for an example a 'person' object or class. Sure, 'name' is pretty important and would always go first (if not a uid), but how do you order the rest of the fields? Is a phone number more or less important than an address? As far as packages and dependencies go, one package might be used a lot more than another package, but I'd argue that one is no more important than the other. If it wasn't important, it wouldn't be a dependency. In fact, it was when I started a new Symfony project a few months ago and noticed that they had added &lt;code&gt;"config": {"sort-packages": true}&lt;/code&gt; to composer.json and the profound effect that it had had on the readability of the "require" section as to what packages I was using in a project that I it had dawned on me that I should be using alphabetical order in as many places as I could to improve readability.&lt;/p&gt;

&lt;p&gt;It wouldn't really feel like one of my blog posts if I didn't somehow mention my favorite IDE, PhpStorm, so here it is: I've recently discovered a plugin, called &lt;a href="https://plugins.jetbrains.com/plugin/5919-lines-sorter"&gt;Lines Sorter&lt;/a&gt; by &lt;a href="https://github.com/syllant"&gt;Sylvain Francois&lt;/a&gt;, that will do your sorting for you.  After you have the plugin installed, highlight the lines you wish to have sorted and then in the menu go to &lt;code&gt;Edit&amp;gt;Sort Lines&lt;/code&gt; or use the keyboard shortcut &lt;code&gt;alt + shift + L&lt;/code&gt; (I had to unmap this shortcut from the default 'Load Context' option, first). This plugin will work in any of the JetBrains IDEs.&lt;/p&gt;

&lt;p&gt;Is this something you've been doing for years?  Do you have any tips that are so simple you can't believe how long it took you to incorporate them? Do you know of a VS Code plugin to accomplish this?  Any arguments against using this tip? Please let me know in the comments.&lt;/p&gt;

</description>
      <category>beginners</category>
    </item>
    <item>
      <title>Global Hooks with Git</title>
      <dc:creator>Todd Eidson</dc:creator>
      <pubDate>Wed, 20 Feb 2019 18:35:46 +0000</pubDate>
      <link>https://dev.to/eidsonator/global-hooks-with-git-2mmj</link>
      <guid>https://dev.to/eidsonator/global-hooks-with-git-2mmj</guid>
      <description>&lt;p&gt;Originally posted on my personal &lt;a href="https://eidson.info/post/global-hooks-with-git"&gt;blog&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  Global Hooks with Git
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;Recently I began to wonder if there was a way to have a collection of &lt;a href="https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks"&gt;git hooks&lt;/a&gt; that could run on all of my git projects to save me the trouble of creating/editing hooks in each of my projects.  Yes, it is possible to do this for new projects, via the &lt;code&gt;git config --global init.templatedir&lt;/code&gt; command, but I have a lot of pre-existing git projects that I wanted to add a pre-push hook to, and I wanted to be able to make alterations to this hook after the fact that would affect all my projects.  Apparently, I'm not the only person who needed this feature, because it was added in git release 2.9.&lt;/p&gt;

&lt;h2&gt;
  
  
  Details
&lt;/h2&gt;

&lt;p&gt;Adding the global hooks was easy enough, thanks to this &lt;a href="https://stackoverflow.com/a/37293198"&gt;stackoverflow&lt;/a&gt; answer, but I did run into a couple of caveats after I had the global hooks path set.&lt;/p&gt;

&lt;h3&gt;
  
  
  git config
&lt;/h3&gt;

&lt;p&gt;To add the path to your global git configuration run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;git config &lt;span class="nt"&gt;--global&lt;/span&gt; core.hooksPath /path/to/my/centralized/hooks
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h3&gt;
  
  
  Hook file
&lt;/h3&gt;

&lt;p&gt;The next step is to add your hook file and make it executable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;touch&lt;/span&gt; /path/to/my/centralized/hooks/pre-push
&lt;span class="nb"&gt;chmod &lt;/span&gt;a+x /path/to/my/centralized/hooks/pre-push
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And then add what ever logic you'd like the hook to execute.  I use &lt;a href="https://www.sonarqube.org/"&gt;sonarqube&lt;/a&gt; to provide Continuos Inspection to my projects, so I wanted the &lt;code&gt;sonar-scanner&lt;/code&gt; to run on every push.  (I highly recommend &lt;a href="https://www.sonarqube.org/"&gt;sonarqube&lt;/a&gt; for keeping bugs and code smells out, you should check it out if you're not familiar with it!).  I use the &lt;a href="https://fishshell.com/"&gt;fish shell&lt;/a&gt;, so you might need a different shebang.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# /path/to/my/centralized/hooks/pre-push&lt;/span&gt;
&lt;span class="c"&gt;#! /usr/bin/fish&lt;/span&gt;

&lt;span class="nb"&gt;command &lt;/span&gt;sonar-scanner
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;So I created my hook and did a test push, to see that it worked as expected.  However, I do have some documentation only and small script projects that don't use the &lt;code&gt;sonar-scanner&lt;/code&gt;.  I was getting some ugly error messages when pushing these projects.  It didn't fail the push, because I wasn't checking the return code on &lt;code&gt;sonar-scanner&lt;/code&gt; command, but I still wanted to silence these messages, or not run &lt;code&gt;sonar-scanner&lt;/code&gt; at all on projects that didn't need it.  That was easy enough to do by checking for the presence of the &lt;code&gt;sonar-project.properties&lt;/code&gt; file before running the scanner.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# /path/to/my/centralized/hooks/pre-push&lt;/span&gt;
&lt;span class="c"&gt;#! /usr/bin/fish&lt;/span&gt;
&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; ./sonar-project.properties
    &lt;span class="nb"&gt;command &lt;/span&gt;sonar-scanner
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Ok, so now that is running only when it should, but I bumped into another issue.  I had some projects that had project specific pre-push hooks that I expected to still be running before a push was executed.  I had assumed that adding these global hooks would be in addition to the project hooks, not overriding them.  However, that is not the behavior I was wanting; I still wanted any project specific hooks to be executed.  The fix for this was to check for the existence of a project level git hook and fire it if it exists.  I decided it would make more sense for my use case to fire it before the global hook logic was executed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# /path/to/my/centralized/hooks/pre-push&lt;/span&gt;
&lt;span class="c"&gt;#! /usr/bin/fish&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; ./.git/hooks/pre-push
    &lt;span class="nb"&gt;command &lt;/span&gt;sh ./.git/hooks/pre-push
end

&lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="nb"&gt;test&lt;/span&gt; &lt;span class="nt"&gt;-e&lt;/span&gt; ./sonar-project.properties
    &lt;span class="nb"&gt;command &lt;/span&gt;sonar-scanner
end
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;So there I had my global hook, that only ran the &lt;code&gt;sonar-scanner&lt;/code&gt; if it was necessary and made sure to also execute any local hooks. I'm sure I've only scratched the surface of the power of this, and I'm looking forward to adding more functionality in the very near future.  I'm going to investigate failing the push if the &lt;a href="https://www.sonarqube.org/"&gt;sonarqube&lt;/a&gt; quality gate fails, next.  To improve my CI/CD work flow.  Have any suggestions or ideas for better hooks?  Please leave them in the comments below.&lt;/p&gt;

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