<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Kyle Fadigan on Medium]]></title>
        <description><![CDATA[Stories by Kyle Fadigan on Medium]]></description>
        <link>https://medium.com/@kfadigan?source=rss-b884380f12bd------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/2*FdOYEVFma-KxG4oRA8RmJg.jpeg</url>
            <title>Stories by Kyle Fadigan on Medium</title>
            <link>https://medium.com/@kfadigan?source=rss-b884380f12bd------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 20 Nov 2026 16:09:20 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kfadigan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Trust the Process(es)]]></title>
            <link>https://medium.com/tech-at-power/trust-the-process-es-c4515293fca5?source=rss-b884380f12bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/c4515293fca5</guid>
            <category><![CDATA[technology]]></category>
            <category><![CDATA[process]]></category>
            <category><![CDATA[task-management]]></category>
            <category><![CDATA[terminal]]></category>
            <category><![CDATA[command-line]]></category>
            <dc:creator><![CDATA[Kyle Fadigan]]></dc:creator>
            <pubDate>Mon, 01 Jul 2019 18:25:07 GMT</pubDate>
            <atom:updated>2019-07-01T18:25:07.177Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This post is written by a Power Code Academy Student Developer. To learn more about this program, follow the </em><a href="https://blog.techatpower.com/benefits-of-training-your-own-entry-level-developers-3d23103df7af"><strong><em>Training Your Own Entry Level Developers</em></strong></a><strong><em> </em></strong><em>blog series on </em><strong><em>Tech at Power</em></strong><em>.</em></p><h3>Trust the Process(es)</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HbFoJ_MvGU9wyhMrR1lEAg.jpeg" /></figure><h3>Are the processes on your computer running?</h3><p><strong>Then you better go catch them!</strong></p><p>My first experience with having to look into what processes were running on my computer was a pretty stressful one as a new developer. I was writing some loops in the Ruby language when all of a sudden, my computer sounded like shuttle rockets and was about to launch itself into orbit. I feared it was going to take me with it because the keys felt like they were hot enough to fuse my fingertips to them.</p><p>Using some quick processing of my own, I realized this shouldn’t be happening, I was trapped in an infinite loop!! I had a process running over and over with no end in sight. Shutting down the application that launched the process didn’t help, it was already set in motion! I had to dig into what processes were running on my machine and put a stop to the loop once and for all. Let’s go through the basics of seeing what processes are up and running.</p><p>The ps command, which stands for <strong>process status, </strong>is used to provide information about the currently running processes. The information can be queried in different ways, yielding many results, we’ll go over some of the basics here. You can follow along with your own by opening up your shell:</p><p><strong>On a Mac</strong>: The Terminal app is in the Utilities folder in Applications. To open it, open your Applications folder, then open Utilities &amp; double-click on Terminal.</p><p><strong>On a PC</strong>: Click the “Start &gt;&gt; Program Files &gt;&gt; Accessories &gt;&gt; Command Prompt” to open a Command Prompt session.</p><p>Now that we have the terminal up, let’s walk through some commands using ps and see what it tells us. These commands will stay the same, but the output may vary depending on the tasks you have running currently. The output shown below will be what I currently have running and may be cut off to be more compact. The $ tells one that it is the start of a line where a shell command may be entered.</p><pre>$ ps</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/998/1*VaJqYVcqmWkOkQPJbc_cDg.png" /></figure><p>When the ps command is run without any arguments, it will return results for the processes that belong to the current user, that are associated with the same terminal in which the ps command is run. Let’s go over what those headers above the letters and numbers are.</p><ul><li><strong>PID</strong>: The process ID of the process itself.</li><li><strong>TTY</strong>: The controlling terminal for the process.</li><li><strong>TIME</strong>: The cumulative CPU time the process has used since the process started.</li><li><strong>CMD</strong>: The name of the command that is running.</li></ul><p>So now we can identify the process by its unique ID number for reference in later commands. We can see which terminal session it’s running on, different TTY #s may indicate a different tab or window. The command name can help identify the process even further by telling you the application name it’s associated with.</p><p>What about when we give the ps command an argument? There are many, which can be found <a href="https://www.ibm.com/support/knowledgecenter/en/SSLTBW_2.2.0/com.ibm.zos.v2r2.bpxa500/ps.htm">here</a>, but let’s cover the one I found most useful. We’re going to enter the $ ps aux command to our terminal next. That’s going to give us a little more of a detailed response, with some of the output the same from before &amp; some new.</p><p>The -a option tells ps to list the processes of all users on the system, not just those of the current user, with the exception of group leaders and processes not associated with a terminal. A group leader defines the first member of a group of related processes.</p><p>The -u option tells ps to give us detailed information about each process. The -x option adds to the list processes that have no controlling terminal, such as daemons, which are programs that are launched during startup &amp; run in the background until activated by a particular event or condition. The x option is important when you’re hunting for information regarding a graphical application.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9SHt-wGA6aSzliOGd-jSMw.png" /></figure><ul><li><strong>USER</strong>: The user that the process is running under.</li><li><strong>%CPU</strong>: The percentage of time the process has used the CPU since the process started.</li><li><strong>%MEM</strong>: The percentage of real memory used by this process.</li><li><strong>VSZ</strong>: Indicates, as a decimal integer, the size in kilobytes of the process in virtual memory.</li><li><strong>RSS</strong>: The real-memory size of the process (in 1 KB units).</li><li><strong>STAT:</strong> The status of the process. The S Marks a process that is sleeping for less than about 20 seconds, while the s means it’s a session leader.</li><li><strong>STARTED</strong>: When the process was started.</li></ul><p>With a much more detailed look at the processes running on our machine, we can keep track of things we may not need running anymore, or desperately need to shut down right away. This query about the processes can give us a glimpse into what’s going on “under the hood”, and an opportunity to then learn about those processes via their command name.</p><p>The attribute in that query that stands out the most, to me, would be the <strong>%CPU</strong>. That’s going to tell me how much percentage an individual task is taking up currently. Which could impact the performance and stability of our internal components.</p><h3>Complete The Process</h3><p>We’ve just taken a detailed look at what processes are running on our computer, which we feel great about. Now we’re left wondering what planet my computer landed on after liftoff. Fortunately for me, there’s another command that exists to shut those commands down from the terminal. We’re going to use what we learned from our process query to shut that infinite loop down.</p><p>We’ll use the killcommand to get rid of any processes that you want to kill. Entering kill followed by the PID ( that we discovered after running our ps aux command) will stop that process. Always understand the process you’re killing, as they may be essential to other programs and cause many issues. Let’s see how that works.</p><p>To bring the process full circle, let’s kill a running task. Use your ps aux command to see which tasks are running…</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*o9HEEmy6ZFhEug-7oa05tg.png" /></figure><p>The email application is just using 8.7% CPU and I’m not even using that right now. Let’s kill it! Make note of that PID and go back to the command line.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/612/1*QgT0QzmMKScpfQzny9eFbg.png" /></figure><p>BOOM! Got that task killed and that email app closed down. Is the task really killed though? Let’s run the same command just to be sure.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/608/1*L51oVK0xqiK3WpqH9DtCJw.png" /></figure><p>Kill confirmed. Now you’ve got all the ammo needed to hunt down those tasks that are either idle or spiraling out of control.</p><p>Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c4515293fca5" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tech-at-power/trust-the-process-es-c4515293fca5">Trust the Process(es)</a> was originally published in <a href="https://medium.com/tech-at-power">Tech at Power</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The Magnificent 7 — RESTful Routes]]></title>
            <link>https://medium.com/tech-at-power/the-magnificent-7-restful-routes-8fd25b949c14?source=rss-b884380f12bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/8fd25b949c14</guid>
            <category><![CDATA[sinatra]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[routing]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[web-app-development]]></category>
            <dc:creator><![CDATA[Kyle Fadigan]]></dc:creator>
            <pubDate>Fri, 17 May 2019 11:38:01 GMT</pubDate>
            <atom:updated>2019-05-17T11:43:01.359Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This post is written by a Power Code Academy Student Developer. To learn more about this program, follow the </em><a href="https://blog.techatpower.com/benefits-of-training-your-own-entry-level-developers-3d23103df7af"><strong><em>Training Your Own Entry Level Developers</em></strong></a><strong><em> </em></strong><em>blog series on </em><strong><em>Tech at Power</em></strong><em>.</em></p><h3>The Magnificent 7 — RESTful Routes</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/600/1*Kf3j0SsnafXNA8Ma7B7TjQ.jpeg" /></figure><p>We use the act of resting in many ways through the means of getting sleep, powering down between difficult coding sessions, or maybe just taking a cat nap while the kids are asleep for hopefully many minutes. In any case, it’s beneficial. Well during our coding sessions, we can use RESTful routes in our web apps to communicate with the user to output what they want. What does REST have anything to do with coding though?</p><p>REST, or REpresentational State Transfer, is an architectural style for providing standards between computer systems on the web. It makes it easier for computers to talk to one another. It also makes it easier for us to use the system that’s already in place, instead of reinventing it every time we build or modify an app.</p><p>We will look at 7 different routes for CRUD verbs and actions. CRUD stands for Create, Read, Update and Delete. These actions are basically what we do with the data that gets entered in and displayed with our web app. Below is an overview of the different routes we’ll be interacting with.</p><p>The routes we’ll start with will allow us to gather data and present it.</p><ol><li>Index</li><li>New</li><li>Create</li><li>Show</li></ol><p>The last 3 routes will mutate that data.</p><p>5. Edit</p><p>6. Update</p><p>7. Destroy</p><p>Let’s, for example, use a web app that needs to store pet information to then be able to put up pets for adoption. Then adopting them out to owners. We’ll need to list all of our pets to see how many we have. We may need to create new pets. We’ll have to show users specific pet info we have, not just a master list. Then we’ll need to edit/update our pet info based on the pets being adopted. Finally, we can delete a pet’s info.</p><p>Let’s go over each CRUD action and which routes go with that action.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*wI-6p2e4i9FTdJ1nHZWbUQ.jpeg" /></figure><h3>Creating a new pet:</h3><pre>get &quot;/pets/new&quot; do<br>  erb :&#39;/pets/new&#39;<br>end</pre><h4>new RESTful Route</h4><p>What’s happening above in the first controller action, is the user is going to be rendered an HTML form, housed in the pets directory in the new.erb file. That form will contain the proper HTML code to display fields in which a user can enter a new pet’s information. The form will look like this…</p><pre>&lt;h1&gt;Create a new Pet&lt;/h1&gt;</pre><pre>&lt;form action=&quot;/pets&quot; method=&quot;POST&quot;&gt;</pre><pre>  &lt;label&gt;Name:&lt;/label&gt;<br>  &lt;input type=&quot;text&quot; name=&quot;pet[name]&quot; id=&quot;pet_name&quot;&gt;</pre><pre>  &lt;% Owner.all.each do |owner| %&gt;<br>    &lt;input type=&quot;checkbox&quot; name=&quot;pet[owner_id]&quot; id=&quot;&lt;%= owner.id %&gt;<br>    &quot;value=&quot;&lt;%= owner.id %&gt;&quot;&gt;&lt;%= owner.name %&gt;&lt;/input&gt;<br>  &lt;% end %&gt;</pre><pre>  &lt;label&gt;name:&lt;/label&gt;<br>  &lt;input type=&quot;text&quot; name=&quot;owner[name]&quot; id=&quot;owner_name&quot;&gt;&lt;/input&gt;</pre><pre>  &lt;input type=&quot;submit&quot; value=&quot;Create Pet&quot;&gt;</pre><pre>&lt;/form&gt;</pre><p>So here we’re assigning the form to the action of “/pets”, which will point back to our controller to actually “create” our new pet in our database, and even assign the pet to a new owner. That method of post and the action of “/pets” tell our app where exactly to go back to…</p><p><strong>create RESTful route</strong></p><pre>post &quot;/pets&quot; do<br>  @pet = Pet.create(params[:pet])<br>  unless params[&quot;owner&quot;][&quot;name&quot;].empty?<br>    @pet.owner = Owner.create(name: params[&quot;owner&quot;][&quot;name&quot;])<br>  end<br>  @pet.save<br>  redirect &quot;pets/#{@pet.id}&quot;<br>end</pre><p>There’s our post method along with our pets action at the start of the route! The rest of the code will create the new pet and assign it an owner based on that checkbox code we had on our form. It saves the pet and redirects us to the page for that specific pet. When the pet is created, it is assigned a primary key id.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*pn5Er1hVCF8Xz5IiGzCl2w.jpeg" /></figure><h3>Reading our data:</h3><p>Now that we have instances of owners and pets in our database, we need to “show” the user the pet they’ve adopted! Yay! Also, maybe they’d like to see an “index” of the pets still up for adoption and take home a companion for their first pet. Let’s take a look at what the index controller action would look like…</p><pre>get &quot;/pets&quot; do<br>  @pets = Pet.all<br>  erb :&#39;/pets/index&#39;<br>end</pre><h4>index RESTful Route</h4><p>This one is as simple as it appears. We’ll assign a variable @pets to an array of all the instances of the class, Pet. Then we’ll render our page that houses the Ruby code to display all of the pets.</p><pre>&lt;% @pets.each do |pet| %&gt;<br>  &lt;a href=“/pets/&lt;%= pet.id %&gt;”&gt;&lt;%= pet.name%&gt;&lt;/a&gt;<br>&lt;% end %&gt;</pre><p>Our “show” route is going to do something similar to the index, but we’re only going to show an individual pet. We will redirect the user using the pet’s id. I’ll save us some scrolling up and remind us that at the end of our post request for /pets, we had…</p><pre>redirect &quot;pets/#{@pet.id}&quot;</pre><p>So our redirect will look back at the controller, with the unique id provided by ActiveRecord in the params, and hit the show route in the controller.</p><pre>get “/pets/:id” do <br>  @pet = Pet.find(params[:id]) <br>  erb :’/pets/show’<br>end</pre><p>So we’re beginning to see a pattern here. The controller points us in the direction of a certain page. It’s rendered in a combination of Ruby and HTML code. We fill a form out to create data, then get redirected to different controller actions. FUN, right!?</p><p>Let’s take a look at what the code in the “show” page looks like to show an individual pet.</p><pre>&lt;h1&gt;&lt;%= @pet.name %&gt;&lt;/h1&gt;<br>&lt;h2&gt;&lt;%= @pet.breed %&gt;&lt;/h2&gt;<br>&lt;h3&gt;&lt;%= @pet.age %&gt;&lt;/h3&gt;</pre><p>This is a very simplified version of what a user could encounter, but the idea stays the same.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*e4AH1Y4gvIecd3RGfe-Pyw.jpeg" /></figure><h3>Updating our data:</h3><p>Our edit &amp; update actions are going to be very similar to the new &amp; create actions.</p><p>The edit controller action displays a form for the user to fill out, while the update action takes the parameters from the form and updates the existing database instance.</p><p><strong>edit RESTful Route</strong></p><p>So now that we have our data created, linked and viewable, what about when we have a pet that gets adopted? We would need to update the data to reflect the new owner.</p><pre>get &quot;/pets/:id/edit&quot; do<br>  @pet = Pet.find(params[:id])<br>  erb :&#39;/pets/edit&#39;<br>end</pre><p>Pretty straight forward, our controller action will find the pet that we wish to edit, usually through a button/link on the individual pet’s page. We’ll then load up another view that has a similar look to the new page, except we’re going to give this one a hidden input to route our controller.</p><pre>&lt;input id=&quot;hidden&quot; type=&quot;hidden&quot; name=&quot;_method&quot; value=&quot;patch&quot;&gt;</pre><p>An example of the code for the entire HTML page:</p><pre>&lt;form method=&quot;post&quot; action=&quot;/pets/&lt;%= @pet.id %&gt;&quot;&gt;<br>  &lt;input id=&quot;hidden&quot; type=&quot;hidden&quot; name=&quot;_method&quot; value=&quot;patch&quot;&gt;</pre><pre>  &lt;% Owner.all.each do |owner| %&gt;<br>    &lt;input type=&quot;checkbox&quot; name=&quot;pet[owner_id]&quot; id=&quot;&lt;%= owner.id %&gt;<br>    &quot;value=&quot;&lt;%= owner.id %&gt;&quot;&gt;&lt;%= owner.name %&gt;&lt;/input&gt;<br>  &lt;% end %&gt;</pre><pre>  &lt;label&gt;name:&lt;/label&gt;<br>  &lt;input type=&quot;text&quot; name=&quot;owner[name]&quot; id=&quot;owner_name&quot;&gt;&lt;/input&gt;</pre><pre>  &lt;input type=&quot;submit&quot; value=&quot;Edit Pet&quot;&gt;<br>&lt;/form&gt;</pre><p>Then when the user hits the submit button here, the new pet owner’s name will be saved and the data is sent back to the controller and to that patch action in our controller to actually be updated.</p><p><strong>update RESTful Route</strong></p><p>Just like the “create” method, the “update” method in the controller file is where all the code exists to take the user-edited info &amp; save it to the database. The code for the update method will look like…</p><pre>patch &quot;/pets/:id&quot; do<br>  @pet = Pet.find(params[:id])<br>  @pet.update(params[&quot;pet&quot;]) <br>  unless params[&quot;owner&quot;][&quot;name&quot;].empty?<br>    @pet.owner = Owner.create(name: params[&quot;owner&quot;][&quot;name&quot;])<br>  end <br>  @pet.save<br>  redirect &quot;pets/#{@pet.id}&quot;<br>end</pre><p>Here we see the controller finding the Pet object based on the :id sent from the form. We then actually “update” the info for the pet, saving it and redirecting the user back to that pet’s id page.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1000/1*rOyFkIO-p3egDpg_uvuT2A.jpeg" /></figure><h3>Deleting our data:</h3><p>Delete is the most direct of the CRUD actions and is only going to use one route to get its job done.</p><p>We’re going to find that desired instance of a pet, for this example the pet has been transferred to a farm upstate where they’ll have lots of room to run and play… it’s going to run the .destroy or .clear method on it and it’s out of our database.</p><p><strong>destroy RESTful Route</strong></p><p>This route will delete the information of a certain pet in our database. Let’s take a look at the controller action, then we’ll add a hidden form to the pet’s show page to accomplish the action.</p><p>Below is our controller action. After finding the pet’s info we assign it a variable, it deletes the info in that variable and redirects the user to the index page to see the remaining pets.</p><pre>delete &#39;/pets/:id/delete&#39; do<br>  @pet = Pet.find_by_id(params[:id])<br>  @pet.delete<br>  redirect to &#39;/pets&#39;  <br>end</pre><p>The code for the delete form is below. It’s attached to the bottom our show page when viewing a pet’s info.</p><pre>&lt;h1&gt;&lt;%= @pet.name %&gt;&lt;/h1&gt;<br>&lt;h2&gt;&lt;%= @pet.breed %&gt;&lt;/h2&gt;<br>&lt;h3&gt;&lt;%= @pet.age %&gt;&lt;/h3&gt;</pre><pre>&lt;form method=&quot;post&quot; action=&quot;/pets/&lt;%=@pet.id%&gt;/delete&quot;&gt; <br>  &lt;input id=&quot;hidden&quot; type=&quot;hidden&quot; name=&quot;_method&quot; value=&quot;delete&quot;&gt;<br>  &lt;input type=&quot;submit&quot; value=&quot;delete&quot;&gt;<br>&lt;/form&gt;</pre><h3>Recap:</h3><p>These 7 routes will get any web app started. You’ve got all the basics to use CRUD actions and the RESTful routes to create, read, and manipulate data. Conventions like these make open source coding a fantastic place to work in.</p><p>These routes make it so you don’t have to build a new road to drive on when you want to go somewhere, they give you the infrastructure to then be more creative with your app and what it’s doing. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8fd25b949c14" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tech-at-power/the-magnificent-7-restful-routes-8fd25b949c14">The Magnificent 7 — RESTful Routes</a> was originally published in <a href="https://medium.com/tech-at-power">Tech at Power</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Refactoring == A Simpler Life.]]></title>
            <link>https://medium.com/tech-at-power/refactoring-a-simpler-life-e4b9fd1bbd3f?source=rss-b884380f12bd------2</link>
            <guid isPermaLink="false">https://medium.com/p/e4b9fd1bbd3f</guid>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[test-driven-development]]></category>
            <category><![CDATA[ruby]]></category>
            <category><![CDATA[first-post]]></category>
            <category><![CDATA[software-development]]></category>
            <dc:creator><![CDATA[Kyle Fadigan]]></dc:creator>
            <pubDate>Thu, 25 Apr 2019 01:40:55 GMT</pubDate>
            <atom:updated>2019-04-29T13:53:40.196Z</atom:updated>
            <content:encoded><![CDATA[<p><em>This post is written by a Power Code Academy Student Developer. To learn more about this program, follow the </em><a href="https://blog.techatpower.com/benefits-of-training-your-own-entry-level-developers-3d23103df7af"><strong><em>Training Your Own Entry Level Developers</em></strong></a><strong><em> </em></strong><em>blog series on </em><strong><em>Tech at Power</em></strong><em>.</em></p><h3>Refactoring == A Simpler Life.</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZvgWeq4871xqP2Ek-k5qDw.jpeg" /></figure><p>Looking at coding as not just a job, a series of algorithms or just a bunch of letters and numbers is usually difficult, at least for me. From an inside or outside perspective it can be a bit daunting and honestly, confusing sometimes. That’s something I admire about us as humans, though. Taking something seemingly as frustrating as living in a world of problems that need fixing, and doing something about it. Not just getting by, but working towards getting by together, even when it’s tough. That’s what’s awesome to me about refactoring, especially in coding.</p><p>We are often given a problem to solve and need to write lines of code to execute the plan we put into place. It sounds pretty simple, and complex, all at the same time somehow. What’s been beautiful about putting that into action, are the creative and different ways one can come to the solutions!</p><p>Refactoring is basically the process of taking existing code, changing how the code is structured, but without changing its external behavior. What it aims to do is improve the nonfunctional aspects of the application. You and others will be able to read the code better and even though the change may add code, it’s usually less complex after refactoring. So what you’re saying is, you can make mine and others’ lives better with refactoring?! That’s awesome! Let’s take a sample of Ruby code that I scraped from a website, and see what it looks like prior to refactoring.</p><pre>name = tournament.css(“em”).map {|t_n| t_n.text.strip}.first<br>date = tournament.css(“.t-date”).map {|t_d| t_d.text.strip}.first<br>tier = tournament.css(“.info.ts”).map {|t_t| t_t.text.strip}.first<br>tier = “Unsanctioned” if tier == “”</pre><p>So here we have 3 different variables, all assigned a line of code that strangely, looks very similar in many ways. It’s usually best practices that we try to make our code very “DRY”, which stands for “don’t repeat yourself.” Currently, these lines of code are in the same method, which isn’t entirely shown, but when called on runs the commands housed inside. Even though those lines of code will run successfully, it’s awfully difficult to read. What are those lines of code even doing? They retrieve 3 separate, specific lines of text from a website to display for the user. That brings me to my next point, The Single Responsibility Principle.</p><p>The Single Responsibility Principle is a computer programming principle that states that every function should have responsibility for a single part of the functionality provided by the software. So if my method is grabbing 3 different sets of data from that website, but storing everything all in one method, it sounds like we’re fully ignoring that principle. Let’s refactor that section out to its own separate methods. With those separated we’ll be able to call inside that existing method they’re in and maybe even elsewhere if we need or want to.</p><pre>def get_info(tournament, selector)<br>  tournament.css(selector).first.text.strip<br>end</pre><pre>def get_name(tournament)<br>  get_info(tournament, “em”)<br>end</pre><pre>def get_date(tournament)<br>  get_info(tournament, “.t-date”)<br>end</pre><pre>def get_tier(tournament)<br>  tier = get_info(tournament, “.info.ts”)<br>  if tier == “”<br>    “Unsanctioned”<br>  else<br>    tier<br>  end<br>end</pre><p>Now, these methods are refactored, DRY, and much easier to read! They even go about to describe what each function is doing, which implementing inside one method would be even less readable than we had it originally. We also give the ‘tier’ method functionality that only applies to that piece of data being scraped. I’d call that a success!</p><p>We can use this strategy of refactoring for many different advantages. We mentioned a few already, the last one I’ll leave you with is the Extensibility that refactoring gives our code, according to Wikipedia. <a href="https://en.wikipedia.org/wiki/Extensibility">“Extensibility is a software engineering and systems design principle where the implementation takes future growth into consideration. The term extensibility can also be seen as a systemic measure of the ability to extend a system and the level of effort required to implement the extension.”</a> There’s a lot of big words in there, but having extensibility in our code allows us to go back and accomplish a few things. We get to review our code more easily, we create a more compartmentalized way to update and make things better, and it allows us to talk for multiple minutes about how interesting this process is.</p><p>In conclusion, we took our code that was working fine and refactored it. We gave ourselves, our peers and our clients a greater advantage at succeeding. Happy coding!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e4b9fd1bbd3f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/tech-at-power/refactoring-a-simpler-life-e4b9fd1bbd3f">Refactoring == A Simpler Life.</a> was originally published in <a href="https://medium.com/tech-at-power">Tech at Power</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>