<?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: Alec DuBois</title>
    <description>The latest articles on DEV Community by Alec DuBois (@afteralec).</description>
    <link>https://dev.to/afteralec</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%2F451041%2F548292b9-1150-4d0f-8678-ebf15d894e72.jpeg</url>
      <title>DEV Community: Alec DuBois</title>
      <link>https://dev.to/afteralec</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/afteralec"/>
    <language>en</language>
    <item>
      <title>Algorithm Toolkit #1: Modulo</title>
      <dc:creator>Alec DuBois</dc:creator>
      <pubDate>Fri, 04 Dec 2020 20:15:09 +0000</pubDate>
      <link>https://dev.to/afteralec/algorithm-toolkit-1-modulo-54a8</link>
      <guid>https://dev.to/afteralec/algorithm-toolkit-1-modulo-54a8</guid>
      <description>&lt;p&gt;Welcome to the part of this blog where I write about things I either:&lt;/p&gt;

&lt;p&gt;A) Wish I had known when I started programming&lt;br&gt;
B) Am really grateful someone taught me when I started programming&lt;/p&gt;

&lt;p&gt;Oh, who am I kidding? That's my entire blog.&lt;/p&gt;

&lt;p&gt;Let's talk about number manipulation for algorithms. More specifically, let's talk about the modulo, or remainder operator. &lt;em&gt;(Quick note here: these are different things, but not for the purposes of the use case described in this post)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Remainder"&gt;MDN says it best:&lt;/a&gt;&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The remainder operator (%) returns the remainder left over when one operand is divided by a second operand. It always takes the sign of the dividend.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So, &lt;code&gt;4 % 2&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt;, and &lt;code&gt;5 % 2&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Okay, we'll come back to that in a moment.&lt;/p&gt;

&lt;p&gt;Let's say you're given a common algorithm: reverse digits.&lt;/p&gt;

&lt;p&gt;The problem states that, given any integer as an argument, write an algorithm that returns the &lt;em&gt;reverse&lt;/em&gt; of the digits of that integer. As an example, this algorithm, given 123, would return 321.&lt;/p&gt;

&lt;p&gt;In JavaScript, you could achieve this using built-in methods:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Turn the number into a string&lt;/li&gt;
&lt;li&gt;Turn the string into an array&lt;/li&gt;
&lt;li&gt;Reverse the array with .reverse()&lt;/li&gt;
&lt;li&gt;Turn the array back into a string&lt;/li&gt;
&lt;li&gt;Turn that string back into a number&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;But this defeats the point of algorithms: the point here is to use a lightsaber, not a blaster: a civilized weapon for a more civilized age.&lt;/p&gt;

&lt;p&gt;So! Let's avoid all of this data manipulation entirely and leave the number as a number.&lt;/p&gt;

&lt;p&gt;To reverse the number, we need to be able to extract a single digit from the number at a time.&lt;/p&gt;

&lt;p&gt;It turns out that, given x being any integer, &lt;code&gt;x % 10&lt;/code&gt; always &lt;em&gt;returns&lt;/em&gt; the last digit of that number. To continue the exmaple, &lt;code&gt;123 % 10&lt;/code&gt; &lt;em&gt;returns&lt;/em&gt; 3. There's our last digit!&lt;/p&gt;

&lt;p&gt;If you wanted to simulate "popping" the digit off the number, you could, for example:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Disclaimer: this code assumes a &lt;strong&gt;positive&lt;/strong&gt; number&lt;/em&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;let&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;123&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="c1"&gt;// Here's our x % 10 operator, storing the last digit in r&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;x&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// This is the important part&lt;/span&gt;

  &lt;span class="c1"&gt;// Use the remainder for something here&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Pretty easy, right? Given that the remainder of &lt;code&gt;x % 10&lt;/code&gt; is the last digit of that number, &lt;em&gt;you can then just subtract that remainder from the original number and divide by 10&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Given 123, one iteration would leave 12, then 1, and finally &lt;code&gt;1 - 1 / 10&lt;/code&gt; is 0, which ends the loop.&lt;/p&gt;

&lt;p&gt;You can use this any time you need to manipulate a number without converting it into a string! Try it out the next time you need to manipulate a number directly.&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>javascript</category>
    </item>
    <item>
      <title>How Programming Changes the Way You Think</title>
      <dc:creator>Alec DuBois</dc:creator>
      <pubDate>Fri, 23 Oct 2020 18:54:48 +0000</pubDate>
      <link>https://dev.to/afteralec/programming-changes-the-way-you-think-3peb</link>
      <guid>https://dev.to/afteralec/programming-changes-the-way-you-think-3peb</guid>
      <description>&lt;p&gt;Programming changes the way you think.&lt;/p&gt;

&lt;p&gt;Let me qualify that: programming has changed the way &lt;strong&gt;I&lt;/strong&gt; think - and might have changed yours.&lt;/p&gt;

&lt;p&gt;This isn't to say I &lt;em&gt;know&lt;/em&gt; all the ways it's changed the way I think, though. I've had a hunch for a few weeks now that the brain action (that's a real thing, right?) of writing code has in turn reshaped the &lt;em&gt;way&lt;/em&gt; I think in a more far-reaching way than at the keyboard, and this is that: a hunch I think is worth exploring out loud.&lt;/p&gt;

&lt;h3&gt;
  
  
  Every Detail Matters
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FEgRlSFmU8AE_jKq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fpbs.twimg.com%2Fmedia%2FEgRlSFmU8AE_jKq.jpg" alt="Nevermind, I Misspelled a Variable"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever considered how you read? Do you skim? Do you read a single sentence, or devour lines at a time? Do you give pause at words you don't understand, or do you pick up meaning from context?&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.dictionary.com/e/typoglycemia/" rel="noopener noreferrer"&gt;Check this out&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is taht the frist and lsat ltteer be at the rghit pclae. The rset can be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the wrod as a wlohe.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;em&gt;This won't work in programming.&lt;/em&gt; Even though your brain can tell what's meant by &lt;code&gt;cnost&lt;/code&gt;,  it isn't &lt;code&gt;const&lt;/code&gt;. One works - and the other doesn't.&lt;/p&gt;

&lt;p&gt;Over time, I think this forces you to develop a different kind of thinking when reading: that of reading &lt;em&gt;characters&lt;/em&gt; over &lt;em&gt;words and sentences&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;The same kind of required-detail constraint applies to higher-level thinking as well. When you're designing an application interface, you now need to consider a host of details that must work in &lt;em&gt;specific and contextually correct&lt;/em&gt; ways - everything from the address of your calls to a server to what happens after it completes or fails.&lt;/p&gt;

&lt;p&gt;Personally, since I've started learning to code, I've stopped losing my keys and wallet.&lt;/p&gt;

&lt;h3&gt;
  
  
  It Isn't Working? Good News!
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.kym-cdn.com%2Fphotos%2Fimages%2Fnewsfeed%2F001%2F476%2F528%2Fd03" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.kym-cdn.com%2Fphotos%2Fimages%2Fnewsfeed%2F001%2F476%2F528%2Fd03" alt="This is fine."&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another candidate for this thought's title was &lt;strong&gt;big red bold THIS IS BAD text is actually my friend&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Seriously, before programming, when were errors a &lt;strong&gt;good&lt;/strong&gt; thing, really? Sure, &lt;em&gt;you either win or you learn&lt;/em&gt; or &lt;em&gt;every failure is an opportunity&lt;/em&gt; and other thoughts like that I could write entire blog posts about, but when has feedback ever been so thorough and downright &lt;strong&gt;useful&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;This is about more than reading errors, though. The simple act of programming is literally going from failure to failure until something works - and then breaking it again later and re-writing until it works a different way. Failure and making mistakes become your actual access to the end product.&lt;/p&gt;

&lt;p&gt;At some point along the way, I've (mostly) stopped kicking myself for mistakes in other parts of my life and started dealing with -  - forgive me for this - &lt;em&gt;debugging&lt;/em&gt; them instead.&lt;/p&gt;

&lt;h3&gt;
  
  
  It's (Almost) Always My Fault
&lt;/h3&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.kym-cdn.com%2Fphotos%2Fimages%2Ffacebook%2F001%2F240%2F012%2F1ed.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fi.kym-cdn.com%2Fphotos%2Fimages%2Ffacebook%2F001%2F240%2F012%2F1ed.jpg" alt="Some of your problems are your own fault | The Scroll of Truth"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Have you ever found yourself thinking to yourself &lt;em&gt;why isn't this working?&lt;/em&gt; (Don't you hate it when blog authors ask you rhetorical questions?) If you're like me, you've probably followed that with &lt;em&gt;this (insert test/technology/external library here) isn't working.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Maybe this is just me, but no matter how much I &lt;em&gt;know&lt;/em&gt; that the reason something isn't working is due to &lt;strong&gt;code I wrote&lt;/strong&gt;, I &lt;em&gt;always&lt;/em&gt; blame something else. Clearly Rails is doing something weird somewhere deep in the class inheritance tree, or this unit test has personally declared war on me and everything I hold dear.&lt;/p&gt;

&lt;p&gt;You already know the punchline: it was (99% of the time) actually something I wrote that caused the bug.&lt;/p&gt;

&lt;p&gt;I've gotten a bit better at short-circuiting this initial (freight) train of thought, but I find this one especially interesting because no matter how many times I've realized that I was actually the man behind the curtain, I'm still bent in this direction when something doesn't work.&lt;/p&gt;

&lt;h3&gt;
  
  
  This Is Not The End
&lt;/h3&gt;

&lt;p&gt;^ What he said. The best part of exploring this is what's left to explore. I don't know what I'm going to learn next (just kidding, I do know: it's TypeScript), and not knowing is exciting.&lt;/p&gt;

&lt;p&gt;Is there any way that programming has changed the way you think? Are squirrels secretly plotting their hostile takeover of society? Leave me a comment.&lt;/p&gt;

</description>
      <category>writing</category>
    </item>
    <item>
      <title>N + What? (Or: a brief introduction to n + 1 queries and how to avoid them)</title>
      <dc:creator>Alec DuBois</dc:creator>
      <pubDate>Tue, 29 Sep 2020 05:17:46 +0000</pubDate>
      <link>https://dev.to/afteralec/n-what-or-a-brief-introduction-to-n-1-queries-and-how-to-avoid-them-260h</link>
      <guid>https://dev.to/afteralec/n-what-or-a-brief-introduction-to-n-1-queries-and-how-to-avoid-them-260h</guid>
      <description>&lt;p&gt;Heads up: this is an extremely simplified introduction written for the perfect beginner.&lt;/p&gt;

&lt;p&gt;For more on this topic, I recommend leaving an offering at your local altar to Google and whispering your question into the search bar. Quietly, though! Don't want the neighbors to hear.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Extra heads up: This tutorial uses Ruby on Rails and ActiveRecord to demonstrate the topic, but the general idea applies in a much wider context&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Last heads up, I promise, please don't stop reading yet: notes for absolute beginners may appear throughout this article that look like this&lt;/em&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Jump to:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;N + What?&lt;/li&gt;
&lt;li&gt;How To Avoid Them&lt;/li&gt;
&lt;li&gt;In Closing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  N + What?
&lt;/h4&gt;

&lt;p&gt;To put it simply, n + 1 queries are when an action in an ORM generates (1) query for the object you're looking for, then (n) queries for the associations of that object.&lt;/p&gt;

&lt;p&gt;Pretty dry, right? Let me demonstrate:&lt;/p&gt;

&lt;p&gt;Let's say you're building a monster tamer application. (Does anyone else remember Azure Dreams?)&lt;/p&gt;

&lt;p&gt;You have a model for Tamer, a model for each Monster, and a MonsterTamer model as a join table - the MonsterTamer model keeps track of which Tamer owns which Monster - a &lt;em&gt;has-many-through&lt;/em&gt; relationship.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Bear with me: The models aren't the point here, the queries for the relationships are&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I want to call up my database for all of the Tamers and each of their Monsters. In a Ruby on Rails application, it might look like this in a Controller action:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="no"&gt;Tamer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;include: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="ss"&gt;:monsters&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The actual SQL query generated by this expression might look something like:&lt;/p&gt;

&lt;p&gt;&lt;em&gt;For the curious: I'm pulling these queries from the log generated by Ruby on Rails after running &lt;code&gt;rails server&lt;/code&gt;, and in this case executing a simple GET request by cheating and manually calling fetch() in the javascript console in my browser.&lt;/em&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Started GET "/tamers" for ::1 at 2020-09-28 21:42:35 -0700
   (0.6ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by TamersController#index as */*
  Tamer Load (0.7ms)  SELECT "tamers".* FROM "tamers"
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.8ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 11]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.6ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 12]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.6ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 13]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (1.8ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 14]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.6ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 15]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.7ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 16]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.8ms)  SELECT "monsters".* FROM "monsters" INNER JOIN "monster_tamers" ON "monsters"."id" = "monster_tamers"."monster_id" WHERE "monster_tamers"."tamer_id" = $1  [["tamer_id", 17]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
Completed 200 OK in 80ms (Views: 60.0ms | ActiveRecord: 15.6ms | Allocations: 23215)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Whoa whoa whoa.&lt;/strong&gt; Look at all those queries! Our tamers might only have a few monsters now, but what happens when they have 150? &lt;a href="https://en.wikipedia.org/wiki/List_of_Pok%C3%A9mon"&gt;893&lt;/a&gt;? This is only with a handful of Tamers, too!&lt;/p&gt;

&lt;p&gt;This is an example of an &lt;strong&gt;n+1 query&lt;/strong&gt;, where we have (1) query for the initial model plus (n) queries for its associations.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  How To Avoid Them
&lt;/h4&gt;

&lt;p&gt;We've looked at a bit of what a layer like ActiveRecord is doing under the hood: generating and executing queries to the database based on function calls written in another language - in this case, Ruby.&lt;/p&gt;

&lt;p&gt;Let's look more closely at this action I'm asking it to perform:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="no"&gt;Tamer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;include: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="ss"&gt;:monsters&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this, I'm asking for &lt;strong&gt;all&lt;/strong&gt; of the Tamers, &lt;strong&gt;then&lt;/strong&gt; asking for each of those Tamer's monsters when I express &lt;code&gt;include: [ :monsters ]&lt;/code&gt; further along in the line.&lt;/p&gt;

&lt;p&gt;The problem is that my application doesn't have the astoundingly powerful predictive capacity I do as a human being. When I ask it to draw &lt;code&gt;Tamer.all&lt;/code&gt; - &lt;em&gt;and this is an oversimplification&lt;/em&gt; - it has &lt;strong&gt;absolutely no idea&lt;/strong&gt; that I'm going to ask it for those Tamers' associated monsters a few characters later.&lt;/p&gt;

&lt;p&gt;Let's try something else:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;index&lt;/span&gt;
  &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="ss"&gt;json: &lt;/span&gt;&lt;span class="no"&gt;Tamer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;includes&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:monsters&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nf"&gt;all&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;include: &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt; &lt;span class="ss"&gt;:monsters&lt;/span&gt; &lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;See that &lt;code&gt;.includes(:monsters)&lt;/code&gt;? That's telling the query generator - ActiveRecord in this case - that I want to draw Tamer.all, &lt;em&gt;but include each of their associated monsters&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Here's the query that generates with three (3) Tamers:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Started GET "/tamers" for ::1 at 2020-09-28 21:54:02 -0700
   (5.0ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by TamersController#index as */*
  Tamer Load (0.6ms)  SELECT "tamers".* FROM "tamers"
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  MonsterTamer Load (1.0ms)  SELECT "monster_tamers".* FROM "monster_tamers" WHERE "monster_tamers"."tamer_id" IN ($1, $2, $3)  [["tamer_id", 18], ["tamer_id", 19], ["tamer_id", 20]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (0.5ms)  SELECT "monsters".* FROM "monsters" WHERE "monsters"."id" IN ($1, $2, $3)  [["id", 24], ["id", 25], ["id", 26]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
Completed 200 OK in 92ms (Views: 68.5ms | ActiveRecord: 13.6ms | Allocations: 18700)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Huh. That looks a lot lighter - it's only three queries!&lt;/p&gt;

&lt;p&gt;Let's &lt;em&gt;doubleplusone&lt;/em&gt; the number of Tamers to seven (7) and see what happens:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Started GET "/tamers" for ::1 at 2020-09-28 22:45:38 -0700
   (0.6ms)  SELECT "schema_migrations"."version" FROM "schema_migrations" ORDER BY "schema_migrations"."version" ASC
Processing by TamersController#index as */*
  Tamer Load (0.7ms)  SELECT "tamers".* FROM "tamers"
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  MonsterTamer Load (1.0ms)  SELECT "monster_tamers".* FROM "monster_tamers" WHERE "monster_tamers"."tamer_id" IN ($1, $2, $3, $4, $5, $6, $7)  [["tamer_id", 24], ["tamer_id", 25], ["tamer_id", 26], ["tamer_id", 27], ["tamer_id", 28], ["tamer_id", 29], ["tamer_id", 30]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
  Monster Load (1.2ms)  SELECT "monsters".* FROM "monsters" WHERE "monsters"."id" IN ($1, $2, $3)  [["id", 30], ["id", 31], ["id", 32]]
  ↳ app/controllers/tamers_controller.rb:3:in `index'
Completed 200 OK in 72ms (Views: 50.6ms | ActiveRecord: 15.0ms | Allocations: 20559)
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Hold up.&lt;/strong&gt; That's the &lt;strong&gt;same&lt;/strong&gt; amount of queries generated by &lt;em&gt;half&lt;/em&gt; the number of records!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;That's right.&lt;/strong&gt; We've now solved for n + 1 queries for this model in our application, meaning that &lt;em&gt;no matter how many records there are, the number of queries will never increase&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;While for a small application with a handful of records and few concurrent users you may see absolutely no actual difference in performance, this represents a mathematically significant decrease in the time complexity &lt;em&gt;&lt;a href="https://en.wikipedia.org/wiki/Time_complexity"&gt;(buzzword alert)&lt;/a&gt;&lt;/em&gt; of this action.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  In Closing
&lt;/h4&gt;

&lt;p&gt;There's no punchline; play with this the next time you find yourself drawing a resource out of a database and tell your barista tomorrow that you learned about n + 1 queries. &lt;em&gt;(Probably don't do that last bit, actually)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Leave a comment if you think I left anything out, or if you want to tell me why Nutella is better than peanut butter.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>sql</category>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Git Branches in Five Minutes</title>
      <dc:creator>Alec DuBois</dc:creator>
      <pubDate>Tue, 08 Sep 2020 18:06:31 +0000</pubDate>
      <link>https://dev.to/afteralec/git-branches-in-five-minutes-3e</link>
      <guid>https://dev.to/afteralec/git-branches-in-five-minutes-3e</guid>
      <description>&lt;p&gt;Let's talk about branches. We've touched on these &lt;em&gt;very briefly&lt;/em&gt; in previous posts in this series, but let's look at them on their own. (And do it in five minutes!)&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hi: notes for absolute beginners may show up like this.&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Jump To:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Overview&lt;/li&gt;
&lt;li&gt;Making a Branch&lt;/li&gt;
&lt;li&gt;Pull Requests&lt;/li&gt;
&lt;li&gt;Cleanup&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Overview
&lt;/h4&gt;

&lt;p&gt;In Git, branches are used to &lt;em&gt;isolate changes&lt;/em&gt; from the rest of the code base.&lt;/p&gt;

&lt;p&gt;This way, you can keep the spaghetti code you're using to build a new feature or fix a bug to yourself until you're ready to merge it with the main code.&lt;/p&gt;

&lt;p&gt;The &lt;em&gt;master&lt;/em&gt; branch is typically considered the &lt;em&gt;working&lt;/em&gt; branch, which means that any changes you make there may effect anyone - or anything - using the code. Not where you want to play around with a new feature!&lt;/p&gt;

&lt;p&gt;Here we go:&lt;/p&gt;

&lt;p&gt;&lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Making a Branch
&lt;/h4&gt;

&lt;p&gt;For the rest of this I'm going to assume you're using &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt;, but there are lots of other Git hosting services out there. Remember, Git and GitHub are two different things.&lt;/p&gt;

&lt;p&gt;First, you need to &lt;a href="https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn"&gt;create your repository&lt;/a&gt; and navigate there in your terminal.&lt;/p&gt;

&lt;p&gt;You can see which branch you're currently in by doing:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git status&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;On branch master
Your branch is up to &lt;span class="nb"&gt;date &lt;/span&gt;with &lt;span class="s1"&gt;'origin/master'&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;

nothing to commit, working tree clean
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;&lt;em&gt;If you don't, go back and make sure that your repository is pointed at the correct remote&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, make a new branch by doing:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout -b new-feature&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Remember flags? &lt;code&gt;-b&lt;/code&gt; is saying "make a &lt;strong&gt;new&lt;/strong&gt; branch!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You should now see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Switched to a new branch &lt;span class="s1"&gt;'new-feature'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;You've now created a new branch!&lt;/p&gt;

&lt;p&gt;Now, make some changes. I'm just going to create a file called &lt;code&gt;README.md&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;You can create a new file with &lt;code&gt;touch README.md&lt;/code&gt; in your terminal.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, do:&lt;br&gt;
&lt;code&gt;git add .&lt;/code&gt; and &lt;code&gt;git commit -m "Adds README"&lt;/code&gt;&lt;br&gt;
&lt;em&gt;This adds everything in the current directory &lt;code&gt;.&lt;/code&gt; and commits the changes. &lt;a href="https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn"&gt;More here.&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You should see:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;new-feature &lt;span class="o"&gt;(&lt;/span&gt;root-commit&lt;span class="o"&gt;)&lt;/span&gt; e18e06c] Adds README
 1 file changed, 0 insertions&lt;span class="o"&gt;(&lt;/span&gt;+&lt;span class="o"&gt;)&lt;/span&gt;, 0 deletions&lt;span class="o"&gt;(&lt;/span&gt;-&lt;span class="o"&gt;)&lt;/span&gt;
 create mode 100644 README.md
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Notice that the commit message went under the branch &lt;code&gt;new-feature&lt;/code&gt; in the first line of that message.&lt;/p&gt;

&lt;p&gt;Now, push to the &lt;em&gt;remote&lt;/em&gt; branch:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git push origin new-feature&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You'll see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Enumerating objects: 3, &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
Counting objects: 100% &lt;span class="o"&gt;(&lt;/span&gt;3/3&lt;span class="o"&gt;)&lt;/span&gt;, &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
Writing objects: 100% &lt;span class="o"&gt;(&lt;/span&gt;3/3&lt;span class="o"&gt;)&lt;/span&gt;, 216 bytes | 216.00 KiB/s, &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
Total 3 &lt;span class="o"&gt;(&lt;/span&gt;delta 0&lt;span class="o"&gt;)&lt;/span&gt;, reused 0 &lt;span class="o"&gt;(&lt;/span&gt;delta 0&lt;span class="o"&gt;)&lt;/span&gt;
To github.com:afteralec/git-blog.git
 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;new branch]      new-feature -&amp;gt; new-feature
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Done! Pretty easy, right?&lt;/p&gt;

&lt;h4&gt;
  
  
  Pull requests
&lt;/h4&gt;

&lt;p&gt;In a real project, you'd continue committing and pushing to your branch until your code was ready to see the light of day. &lt;em&gt;(Or, at least, be merged with the main code of the project in the&lt;/em&gt; master &lt;em&gt;branch!)&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Typically, this is done using a pull request.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://guides.github.com/activities/hello-world/#pr"&gt;GitHub has a great walk-through on it here&lt;/a&gt;, but again, use whichever platform you've been using until now.&lt;/p&gt;

&lt;p&gt;With GitHub, it's pretty easy:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;Open the pull request from the repository's page. Click the &lt;em&gt;pull requests&lt;/em&gt; tab at the top and then &lt;strong&gt;new pull request&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Follow the prompts and complete the form&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Open the pull request!&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;In this example, as the repository owner, you'll be able to merge your own pull request - this isn't always the case!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For our purposes, go ahead and merge your changes.&lt;/p&gt;

&lt;h4&gt;
  
  
  Cleanup
&lt;/h4&gt;

&lt;p&gt;From here, you have two options:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Delete your branch.&lt;/li&gt;
&lt;li&gt;Merge the new master code into your branch.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;For this, go ahead and delete the branch. Our new feature (the README file) has been added, and there's nothing else to do with it for now!&lt;/p&gt;

&lt;p&gt;Back in your terminal (in the &lt;em&gt;local repository&lt;/em&gt;) do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git checkout master&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Without the -b flag, this moves you around from branch to branch&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;and then:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git pull&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;This part's important - remember, you made changes in the&lt;/em&gt; new-feature &lt;em&gt;branch, not the&lt;/em&gt; master &lt;em&gt;branch, so this brings you back up-to-speed with the current master code.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You can also delete your branch locally!&lt;/p&gt;

&lt;p&gt;To see your branches, do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git branch&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Flags again! You can add &lt;code&gt;-r&lt;/code&gt; to see only the&lt;/em&gt; remote &lt;em&gt;branches, and add &lt;code&gt;-a-&lt;/code&gt; to see&lt;/em&gt; all &lt;em&gt;branches.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Do:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git branch -D new-feature&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Deleted branch new-feature &lt;span class="o"&gt;(&lt;/span&gt;was e18e06c&lt;span class="o"&gt;)&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;This has been Git Branches in (Less Than?) Five Minutes.&lt;/p&gt;

&lt;p&gt;If this helped you, you have something to add, or you or want to tell me why you think birds aren't real, leave a comment below.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>git</category>
    </item>
    <item>
      <title>A (Very) Simple Git Workflow</title>
      <dc:creator>Alec DuBois</dc:creator>
      <pubDate>Mon, 24 Aug 2020 19:17:30 +0000</pubDate>
      <link>https://dev.to/afteralec/a-very-simple-git-workflow-54dl</link>
      <guid>https://dev.to/afteralec/a-very-simple-git-workflow-54dl</guid>
      <description>&lt;p&gt;This is a quick on-the-court example of what a (very!) simple Git workflow might look like for two people working on a personal project together.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hey: You might see notes for absolute beginners like this&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Jump to:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Basics&lt;/li&gt;
&lt;li&gt;Adding a Collaborator&lt;/li&gt;
&lt;li&gt;A Note on Branches&lt;/li&gt;
&lt;li&gt;Common Pitfalls&lt;/li&gt;
&lt;li&gt;
A (Very) Simple Workflow &lt;a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Basics
&lt;/h4&gt;

&lt;p&gt;Before you do anything, you need to create your Git repository. &lt;a href="https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn"&gt;I wrote a tutorial on that here&lt;/a&gt;, but regardless of how you do this the rest of this tutorial assumes you've done this.&lt;/p&gt;

&lt;p&gt;Next, find someone you want to build something with!&lt;/p&gt;

&lt;p&gt;In setting up this workflow with your collaborator, you want to first define some rules. Here are some suggestions &lt;em&gt;(don't worry if anything here doesn't make complete sense yet)&lt;/em&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Only merge each other's pull requests&lt;/li&gt;
&lt;li&gt;Always test your new code before opening the pull request&lt;/li&gt;
&lt;li&gt;Always review your collaborator's code before merging the pull request&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;em&gt;Bottom line is: establish some groundwork here and agree on how to handle any conflicts or updates to the procedure as you go. Working with humans is different than working with computers!&lt;/em&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Adding a Collaborator
&lt;/h4&gt;

&lt;p&gt;Now, add the person you're working with as a Collaborator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.github.com/en/github/setting-up-and-managing-your-github-user-account/inviting-collaborators-to-a-personal-repository"&gt;Here's a quick run-down on how to do it on GitHub&lt;/a&gt;, but there are obviously other Git hosting services out there.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Just like in my previous tutorials on this, it's really not hard:&lt;/em&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Navigate to the repository in GitHub&lt;/li&gt;
&lt;li&gt;Click &lt;strong&gt;Settings&lt;/strong&gt;, &lt;strong&gt;Manage Access&lt;/strong&gt;, and &lt;strong&gt;Invite a Collaborator&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;You can send them an invitation by username, full name, or email address&lt;/li&gt;
&lt;li&gt;Finally, have your new collaborator &lt;strong&gt;Accept&lt;/strong&gt; your invitation by following the instructions in the invitation in their email&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You're in business! Have your collaborator &lt;code&gt;clone&lt;/code&gt; and then &lt;code&gt;pull&lt;/code&gt; the remote repository into their local repository. &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  A Note on Branches
&lt;/h4&gt;

&lt;p&gt;When working with someone else on a repository, it's extremely important to keep your main branch of code clean.&lt;/p&gt;

&lt;p&gt;&lt;a&gt;More on this later&lt;/a&gt;, but let's talk about branches for just a moment.&lt;/p&gt;

&lt;p&gt;Let's say you're working on an application and you want to add tags to your posts. You start work on the code in your local repository, but now you have a problem: the &lt;strong&gt;other&lt;/strong&gt; person on your project is also writing code. What if what you're doing starts to overlap?&lt;/p&gt;

&lt;p&gt;This is part of what &lt;em&gt;branches&lt;/em&gt; are for. As part of this workflow, you want to make sure that when you make any change to your code base, you &lt;strong&gt;don't&lt;/strong&gt; push up to the &lt;code&gt;master&lt;/code&gt; branch. Again, &lt;em&gt;more on that in a moment&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Instead, make sure you're creating a branch each time you add a feature or make a change. &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Common Pitfalls
&lt;/h4&gt;

&lt;p&gt;Let's handle some common pitfalls in working with a collaborator on a project.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Scene:&lt;/strong&gt; You're working on a killer new feature for your app. You need to change some of the basic structure of your app, but nothing major. Maybe you changed a custom property in the application's main stylesheet - no big deal, right?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;You push your change up to the master branch of your application. &lt;strong&gt;Oops&lt;/strong&gt;, your collaborator was using that custom property (or whatever structure you changed) in the features they built before. One angry phone call later, you're reversing some of your changes.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; not using pull requests to merge new code. In this case, you'd want to let your collaborator review the code, and they would catch the breaking change you made.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Scene:&lt;/strong&gt; You just fixed a gnarly bug in the comments section of your new app. You open the pull request and your collaborator merges it. You do &lt;code&gt;git checkout master&lt;/code&gt; to head back to the &lt;code&gt;master&lt;/code&gt; branch and start working on the next bug. No problem.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Uh oh. Problem.&lt;/strong&gt; You didn't &lt;code&gt;git pull&lt;/code&gt; to grab the changes you just made to the &lt;em&gt;remote&lt;/em&gt; repository and copy them to the master branch on your &lt;em&gt;local&lt;/em&gt; repository. If you branch from here, you'll wind up regressing the changes you just made.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;strong&gt;Pitfall:&lt;/strong&gt; not keeping your local master branch up-to-date. However you organize this is up to you, but you want to be aware of where your local master branch is against the remote master branch.&lt;/em&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  A (Very) Simple Workflow
&lt;/h4&gt;

&lt;p&gt;Here it is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;You start making changes to your code while checked out into your master branch&lt;/li&gt;
&lt;li&gt;You &lt;code&gt;git checkout -b branch-name&lt;/code&gt; to create a branch&lt;/li&gt;
&lt;li&gt;You finish making changes to your code (1 and 3 order is optional)&lt;/li&gt;
&lt;li&gt;You &lt;code&gt;git add .&lt;/code&gt;, &lt;code&gt;git commit -m 'adds this feature'&lt;/code&gt; and &lt;code&gt;git push&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;You open a pull request on GitHub and the other person reviews and merges it&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Finally, to start over:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;git checkout master&lt;/code&gt; to head back to your master branch and &lt;code&gt;git pull&lt;/code&gt;  to localize the changes you just made&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That's literally it! Keep in mind this is a &lt;strong&gt;very&lt;/strong&gt; basic example, but it could get you thinking on how to organize your workflow when it isn't just you working on a personal project.&lt;/p&gt;

&lt;p&gt;If this helped you, if you think I'm completely wrong, or think ducks should be allowed to cross the street without being filmed without their consent, leave a comment below.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>git</category>
    </item>
    <item>
      <title>Creating a New Git Repository for the Absolute Beginner</title>
      <dc:creator>Alec DuBois</dc:creator>
      <pubDate>Wed, 12 Aug 2020 04:57:47 +0000</pubDate>
      <link>https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn</link>
      <guid>https://dev.to/afteralec/creating-a-new-git-repository-for-the-absolute-beginner-25bn</guid>
      <description>&lt;p&gt;Exactly what it says on the tin. Here's a walk-through for starting a new Git repository locally using &lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt; and &lt;a href="https://www.github.com/"&gt;GitHub&lt;/a&gt;. This is intended for beginners, and I've tried to include some explanations to make this less of a monkey-see, monkey-do kind of tutorial.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Hey: notes for absolute beginners are sometimes included like this&lt;/em&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Jump To:
&lt;/h4&gt;

&lt;ul&gt;
&lt;li&gt;Before You Start&lt;/li&gt;
&lt;li&gt;Creating the Local Repository&lt;/li&gt;
&lt;li&gt;Creating the Remote Repository&lt;/li&gt;
&lt;li&gt;Commit and Push&lt;/li&gt;
&lt;li&gt;A Note on Branches&lt;/li&gt;
&lt;li&gt;
Sum Up &lt;a&gt;&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;h4&gt;
  
  
  Before You Start
&lt;/h4&gt;

&lt;p&gt;Here, I'm assuming you're familiar with the basics of moving around your terminal. If you aren't, go brush up on that first!&lt;/p&gt;

&lt;p&gt;In order to start a local &lt;em&gt;(on your computer)&lt;/em&gt; Git repository, you'll need an account with a git hosting service. I use &lt;a href="https://www.github.com/"&gt;GitHub&lt;/a&gt; from here on out, but there are other services out there.&lt;/p&gt;

&lt;p&gt;Before we do anything, we need to install &lt;a href="https://git-scm.com/"&gt;Git&lt;/a&gt;! You can find it with links to downloads &lt;a href="https://git-scm.com/downloads/'"&gt;here&lt;/a&gt;. Go on, I'll wait! &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the Local Repository
&lt;/h4&gt;

&lt;p&gt;Okay! First, open your terminal and find (or create) a directory &lt;em&gt;(folder)&lt;/em&gt; on your computer that you'd like to turn into a Git repository. &lt;em&gt;Use &lt;code&gt;cd &amp;lt;your/file/path&amp;gt;&lt;/code&gt; to change your directory and &lt;code&gt;mkdir &amp;lt;name&amp;gt;&lt;/code&gt; to make a directory.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Do &lt;code&gt;git init&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Initialized empty Git repository &lt;span class="k"&gt;in&lt;/span&gt; /home/khaleesi/plans/conquer_westeros/.git/
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;That's it! This directory is now a Git repository.&lt;/p&gt;

&lt;p&gt;To finish setup, you'll need to create the repository on whatever hosting platform you're using online. &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Creating the Remote Repository
&lt;/h4&gt;

&lt;p&gt;&lt;a href="https://docs.github.com/en/github/getting-started-with-github/create-a-repo"&gt;Here's the official GitHub documentation on creating a remote repository.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;(It isn't hard - log in to &lt;a href="https://github.com/"&gt;GitHub&lt;/a&gt; and click 'New'. Follow the prompts, and you're done!)&lt;/p&gt;

&lt;p&gt;But wait! How is the local repository going to know about the remote &lt;em&gt;(hosted online)&lt;/em&gt; repository? &lt;strong&gt;Great question.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;First, get the HTTPS link for your remote repository. On GitHub, you do this by clicking the green &lt;strong&gt;Code&lt;/strong&gt; button on the right of your repository page, making sure it says &lt;strong&gt;Https&lt;/strong&gt; and copy that link.&lt;/p&gt;

&lt;p&gt;Next, point the local repository at the remote repository.&lt;/p&gt;

&lt;p&gt;Do &lt;code&gt;git remote add origin &amp;lt;your-repository-link&amp;gt;&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Wait, did it work?&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;There's no response if this worked.&lt;/p&gt;

&lt;p&gt;Do &lt;code&gt;git remote -v&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;origin  https://github.com/khaleesi/conquer_westeros.git &lt;span class="o"&gt;(&lt;/span&gt;fetch&lt;span class="o"&gt;)&lt;/span&gt;
origin  https://github.com/khaleesi/conquer_westeros.git &lt;span class="o"&gt;(&lt;/span&gt;push&lt;span class="o"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;And that's it! Your local repository is now pointed at your remote repository.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;-v&lt;/code&gt; is what's called an option flag - it tells the command &lt;code&gt;git remote&lt;/code&gt; to print the &lt;code&gt;verbose&lt;/code&gt; response, which includes the remote link&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Notice &lt;code&gt;origin&lt;/code&gt; is the same as what you put in the &lt;code&gt;git remote add&lt;/code&gt; command - you could name this something else, though most places you go you see it called origin&lt;/em&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Commit and Push
&lt;/h4&gt;

&lt;p&gt;Ready for the next bit? Awesome. Now you're going to &lt;strong&gt;send stuff&lt;/strong&gt; from your local repository to the remote repository.&lt;/p&gt;

&lt;p&gt;First, you need to add something to push &lt;em&gt;(send)&lt;/em&gt;. How about a Readme file? Everyone knows writing good documentation makes you more attractive.&lt;/p&gt;

&lt;p&gt;You can be fancy and use the terminal for this:&lt;/p&gt;

&lt;p&gt;Do &lt;code&gt;touch README.md&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now for Git:&lt;/p&gt;

&lt;p&gt;Do &lt;code&gt;git add .&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Before you can send anything you need to &lt;code&gt;commit&lt;/code&gt; your changes.&lt;/p&gt;

&lt;p&gt;Do &lt;code&gt;git commit -m 'Added README.md'&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, &lt;code&gt;git push -u origin master&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Because the HTTPS link is being used for the remote repository, you'll be asked for your login credentials.&lt;/p&gt;

&lt;p&gt;After a moment, you should see something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight shell"&gt;&lt;code&gt;Counting objects: 3, &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
Writing objects: 100% &lt;span class="o"&gt;(&lt;/span&gt;3/3&lt;span class="o"&gt;)&lt;/span&gt;, 211 bytes | 0 bytes/s, &lt;span class="k"&gt;done&lt;/span&gt;&lt;span class="nb"&gt;.&lt;/span&gt;
Total 3 &lt;span class="o"&gt;(&lt;/span&gt;delta 0&lt;span class="o"&gt;)&lt;/span&gt;, reused 0 &lt;span class="o"&gt;(&lt;/span&gt;delta 0&lt;span class="o"&gt;)&lt;/span&gt;
To https://github.com/khaleesi/conquer_westeros.git
 &lt;span class="k"&gt;*&lt;/span&gt; &lt;span class="o"&gt;[&lt;/span&gt;new branch]      master -&amp;gt; master
Branch master &lt;span class="nb"&gt;set &lt;/span&gt;up to track remote branch master from origin.
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;There you have it! You can add files, remove files, change files, and as you go follow this same &lt;code&gt;git add&lt;/code&gt;, &lt;code&gt;git commit&lt;/code&gt;, &lt;code&gt;git push&lt;/code&gt; flow.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;touch&lt;/code&gt; creates a file and the &lt;code&gt;.md&lt;/code&gt; extension means it's a &lt;a href="https://www.markdownguide.org/"&gt;Markdown&lt;/a&gt; file, like this blog post!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;git add .&lt;/code&gt; tells Git to add everything in &lt;code&gt;.&lt;/code&gt;, the alias for the current directory&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;-m&lt;/code&gt; is an option flag on &lt;code&gt;git commit&lt;/code&gt; for 'add a message'&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;&lt;code&gt;-u&lt;/code&gt; is an option flag on &lt;code&gt;git push&lt;/code&gt; for &lt;code&gt;set upstream&lt;/code&gt;, which tells the local repository's &lt;code&gt;master&lt;/code&gt; branch to track the remote repository's &lt;code&gt;master&lt;/code&gt; branch - meaning that later you can just do &lt;code&gt;git push&lt;/code&gt; on its own and it will know which remote and branch to push to&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Notice &lt;code&gt;origin&lt;/code&gt; again? This is what we're calling the remote repository. &lt;code&gt;master&lt;/code&gt; is the main branch of that repository&lt;/em&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  A Note on Branches
&lt;/h4&gt;

&lt;p&gt;As a happy side-effect of creating your first repository, you've created your first &lt;em&gt;branch&lt;/em&gt;. I'm not going to go into depth here, but I'll just touch on what that means.&lt;/p&gt;

&lt;p&gt;Remember &lt;code&gt;master&lt;/code&gt; from &lt;code&gt;git remote add&lt;/code&gt; and &lt;code&gt;git push&lt;/code&gt;? Seemed kind of arrogant, right? Well, the master branch is the main branch of a repository. When you're pushing changes to the master branch, it's a pretty big deal!&lt;/p&gt;

&lt;p&gt;Most projects start out with just a master branch, and other branches get added and removed later as you and maybe others develop sections of code on its own before merging it with the master &lt;em&gt;(main)&lt;/em&gt; branch.&lt;/p&gt;

&lt;p&gt;Here's a resource if you want to learn more: &lt;a href="https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell"&gt;Git Branches in a Nutshell&lt;/a&gt; &lt;a&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h4&gt;
  
  
  Sum Up
&lt;/h4&gt;

&lt;p&gt;In summary, Git is easy:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git init&lt;/code&gt; to start your directory;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git remote add &amp;lt;your-remote-repository-link&amp;gt;&lt;/code&gt; to point it at a remote repository;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git add .&lt;/code&gt; to add &lt;em&gt;all&lt;/em&gt; files in the local repository to a commit;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git commit -m '&amp;lt;your-message&amp;gt;'&lt;/code&gt; to stage a commit;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;git push&lt;/code&gt; (using &lt;code&gt;-u origin master&lt;/code&gt; the first time) to push up to the remote repository, and...&lt;/p&gt;

&lt;p&gt;Done!&lt;/p&gt;

&lt;p&gt;If you made it to the end here, thanks for reading. Leave a comment if you've found this useful, it's good karma.&lt;/p&gt;

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