<?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: rwparrish</title>
    <description>The latest articles on DEV Community by rwparrish (@rwparrish).</description>
    <link>https://dev.to/rwparrish</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%2F456931%2Fb9a82976-1a11-4748-91a3-503c05d5edfa.jpg</url>
      <title>DEV Community: rwparrish</title>
      <link>https://dev.to/rwparrish</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/rwparrish"/>
    <language>en</language>
    <item>
      <title>How to Ask for Support as a New Dev</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Fri, 29 Mar 2024 15:52:04 +0000</pubDate>
      <link>https://dev.to/rwparrish/how-to-ask-for-support-as-a-new-dev-3cmh</link>
      <guid>https://dev.to/rwparrish/how-to-ask-for-support-as-a-new-dev-3cmh</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;Remember your goal. You want to become a developer. Yes, you are building familiarity with the language you are working with (I'll reference JavaSript in this blog), but more importantly, you're also building your problem-solving process and honing your &lt;a href="https://betterprogramming.pub/the-mindset-of-a-tech-a-matter-of-perfecting-ones-self-everyday-bdee6b519add" rel="noopener noreferrer"&gt;developer mindset&lt;/a&gt;. &lt;strong&gt;Above all else, a good developer is a good problem solver.&lt;/strong&gt; Knowing when and how to ask for support from your fellow devs will be an important piece in your problem-solving process. At first, reaching out can feel intimidating, but if you follow these simple guidelines you can start building this skill knowing that you are going about it in an "industry standard" way. The great news is, in my experience, developers &lt;em&gt;want&lt;/em&gt; to help you along! With that said, let's take a look at some things to consider when reaching out for support.&lt;/p&gt;

&lt;h1&gt;
  
  
  When to Reach Out
&lt;/h1&gt;

&lt;p&gt;As you gain experience as a developer, your knowledge expands, and your problem-solving skills become more robust. For new developers, it's essential to attempt to solve the issue independently first to foster personal growth. However, waiting too long to seek help can hinder progress and lead to burnout. Setting a timer can be a helpful strategy for those who tend to delay seeking assistance like myself. When you get stuck, set a timer, throw your kitchen sink at it, and if you're still stuck when the timer goes off it's time to step away and think about reaching out. By then, you'll have valuable insights to include in your request for help.&lt;/p&gt;

&lt;h1&gt;
  
  
  Crafting the Ask
&lt;/h1&gt;

&lt;p&gt;So, how will you know if you have done enough on your own before you reach out? Consider the following guidelines when reaching out:&lt;/p&gt;

&lt;h3&gt;
  
  
  Be Specific:
&lt;/h3&gt;

&lt;p&gt;When formulating your question, ensure it's concise and clear. Apply your problem-solving skills to refine your question and uncover the specific issue you need to address. If you state your question and you get some questions in response, don't take it personally. Instead, try your best to answer each question as clearly and honestly as you can. The person helping you is trying to gain more context and gauge your understanding so they can provide good support. The idea of "good" support within the context of this conversation is important. Remember, you want to become more and more independent, and &lt;em&gt;good&lt;/em&gt; support should help you achieve this.&lt;/p&gt;

&lt;h3&gt;
  
  
  Don't Ask for the Fish, Ask &lt;em&gt;How&lt;/em&gt; to Fish:
&lt;/h3&gt;

&lt;p&gt;Instead of asking for the solution outright, seek guidance on how to solve the problem. Is there a resource you are unaware of, a debug tool that would be perfect for this issue, or a question you haven't yet considered? The answer to all these questions is likely, "yes". Sure, you want the answer but learning more about &lt;em&gt;how&lt;/em&gt; to find it is far more valuable and will serve you well throughout your career. As the saying goes, "Give a man a fish, and you feed him for a day. Teach a man to fish, and you feed him for a lifetime."&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;For example:&lt;/strong&gt; Instead of asking: "How does the &lt;code&gt;map()&lt;/code&gt; method work?" you could try asking, "Can someone point me towards some good documentation for the &lt;code&gt;map()&lt;/code&gt; method?" Though, you probably should've Googled for docs already. Or, "What practice problems helped you better understand how the &lt;code&gt;map()&lt;/code&gt; method works?"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Another example:&lt;/strong&gt; Instead of asking "Why won't this code work?" consider asking, "I've tried this thing and that tool, is there a debugging tool that could be helpful in this context that I haven't mentioned?" &lt;/p&gt;

&lt;h3&gt;
  
  
  Provide Context:
&lt;/h3&gt;

&lt;p&gt;While providing context is paramount, this doesn't mean you should share your entire project! Be considerate of others' time - nobody has the time to pour through all your code just to try and understand where you are stuck. You need to decide what information is relevant before you reach out. This is for your benefit and for the benefit of the person who might help you - they want to quickly get a sense of where your knowledge gaps are so they can provide &lt;em&gt;good&lt;/em&gt; support.&lt;/p&gt;

&lt;p&gt;I find that oftentimes people who haven't written all the code themselves provide bizarre context, if any, when reaching out. This is because they don't know enough about the code they are using in &lt;em&gt;their project&lt;/em&gt; - &lt;strong&gt;don't be this kind of person&lt;/strong&gt;. Provide only the relevant code, a brief explanation of your rationale regarding &lt;em&gt;your&lt;/em&gt; code, any relevant error messages, and any things you've already tried. Have you checked the documentation yet? What did you try Googling? Which debugging tools have you tried using and what did they reveal? &lt;/p&gt;

&lt;p&gt;Also, verbalizing your question before reaching out and/or using &lt;a href="https://en.wikipedia.org/wiki/Rubber_duck_debugging" rel="noopener noreferrer"&gt;rubber duck debugging&lt;/a&gt; can be extremely powerful too. Trust me, oftentimes by doing these things, you may find that your initial question improves, or you might even solve the problem on your own, bypassing the need for support.&lt;/p&gt;

&lt;p&gt;Of course, you're new at this and you may not know for sure if an error message is relevant or what exactly your code is doing (you probably wouldn't be stuck if you did) but that's OK, just do your best. Like all things, asking for support in this manner will get easier with practice. You'll begin to get a feel for the context other devs will want when you reach out with an issue.&lt;/p&gt;

&lt;h3&gt;
  
  
  Show Gratitude:
&lt;/h3&gt;

&lt;p&gt;This one is simple. When a fellow dev takes the time to try to understand your issue and all its context so they can provide &lt;em&gt;good&lt;/em&gt; support, show gratitude!&lt;/p&gt;

&lt;h1&gt;
  
  
  Practical Example with Code
&lt;/h1&gt;

&lt;p&gt;For reference, here is the project code so far:&lt;/p&gt;

&lt;p&gt;Inside the index.js file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const weaponCardContainer = document.getElementById("weapon-card-container")

const init = () =&amp;gt; {
    fetch("http://localhost:3000/weapons")
        .then(resp =&amp;gt; resp.json())
        .then(weapons =&amp;gt; renderWeaponsList(weapons))
}

const renderWeaponsList = (weapons) =&amp;gt; {
    const weaponsListContainer = document.getElementById('weapons-list')
    weapons.forEach(weapon =&amp;gt; {
        const weaponLi = document.createElement('li')
        const weaponNameSpan = document.createElement('span')
        weaponNameSpan.classList.add('weaponSpan')
        weaponLi.classList.add('weapon-li')
        weaponNameSpan.innerText = weapon.name
        weaponLi.id = weapon.id
        weaponLi.append(weaponNameSpan)
        weaponsListContainer.append(weaponLi)

        weaponNameSpan.addEventListener('mouseover', () =&amp;gt; {
            weaponNameSpan.classList = 'highlight-effect'
        })
        weaponNameSpan.addEventListener('mouseleave', () =&amp;gt; {
            weaponNameSpan.classList.remove('highlight-effect')
        })
        weaponNameSpan.addEventListener('click', () =&amp;gt; renderWeaponCard(weapon))
    })
}

const renderWeaponCard = (weapon) =&amp;gt; {
    // build weapon card to display details on right-hand side of the screen
    const weaponCard = document.createElement("div")
    weaponCard.classList.add("weapon-card")

    const title = document.createElement("h2")
    title.textContent = weapon.name
    weaponCard.appendChild(title)

    const type = document.createElement("p");
    type.textContent = `Type: ${weapon.type}`
    weaponCard.appendChild(type)

    const damage = document.createElement("p")
    damage.textContent = `Damage: ${weapon.damage}`
    weaponCard.appendChild(damage)

    const criticalChance = document.createElement("p")
    criticalChance.textContent = `Critical Chance: ${weapon.criticalChance}%`
    weaponCard.appendChild(criticalChance);

    const manufacturer = document.createElement("p")
    manufacturer.textContent = `Manufacturer: ${weapon.manufacturer}`
    weaponCard.appendChild(manufacturer)

    const description = document.createElement("p")
    description.textContent = weapon.description
    weaponCard.appendChild(description)

    weaponCardContainer.innerHTML = ""
    weaponCardContainer.appendChild(weaponCard)
}

init()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Inside the index.html file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!DOCTYPE html&amp;gt;
&amp;lt;html lang="en"&amp;gt;

&amp;lt;head&amp;gt;
    &amp;lt;script src="index.js"&amp;gt;&amp;lt;/script&amp;gt;
    &amp;lt;meta charset="UTF-8"&amp;gt;
    &amp;lt;meta name="viewport" content="width=device-width, initial-scale=1.0"&amp;gt;
    &amp;lt;title&amp;gt;CyberPunk Weapons&amp;lt;/title&amp;gt;
    &amp;lt;link rel="stylesheet" href="app.css"&amp;gt;
&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
    &amp;lt;div class="containerWrapper"&amp;gt;
        &amp;lt;div class="leftSide"&amp;gt;
            &amp;lt;ul class="weaponsList" id="weapons-list"&amp;gt;&amp;lt;/ul&amp;gt;
        &amp;lt;/div&amp;gt;

        &amp;lt;div class="rightSide" id="weapon-card-container"&amp;gt;&amp;lt;/div&amp;gt;
    &amp;lt;/div&amp;gt;

&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's look at a common error message in JavaScript, &lt;code&gt;Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')&lt;/code&gt;. You are building one of your first single-page web applications and things are going OK until you hit this error. Now, you are stuck. Being a beginner, you're not sure what to make of the error message. What do you do?&lt;/p&gt;

&lt;h3&gt;
  
  
  The Solid Outreach
&lt;/h3&gt;

&lt;p&gt;Here is an example of a request for support that is concise, doesn't ask for the answer outright, includes relevant context, and shows some gratitude:&lt;/p&gt;

&lt;p&gt;"I am trying to display the weapon details on the page after a user clicks on the weapon and I'm running into a specific error that I'm having trouble understanding: &lt;code&gt;Uncaught TypeError: Cannot set properties of null (setting 'innerHTML')&lt;/code&gt;. I &lt;em&gt;think&lt;/em&gt; it means that &lt;code&gt;weaponCardContainer&lt;/code&gt; is &lt;code&gt;null&lt;/code&gt; but I'm not sure.&lt;/p&gt;

&lt;p&gt;I know the callback for the click event is working - I've tested this for each weapon. I see that the error message is referencing line 60: &lt;code&gt;weaponCardContainer.innerHTML = ""&lt;/code&gt; but I'm confused because I've read about &lt;code&gt;innerHTML&lt;/code&gt; and I think I am using it correctly within the context of my project (property of an element) and I've tried querying the DOM for the &lt;code&gt;weaponCardContainer&lt;/code&gt; within the DevTools offered by the browser and I can grab it like so &lt;code&gt;document.getElementById("weapon-card-container")&lt;/code&gt; with no issues.&lt;/p&gt;

&lt;p&gt;I've been at this a bit now. Can someone point me in the right direction? Thank you in advance for any guidance!"&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;For new developers reaching out can feel intimidating but it is an essential tool in your toolbox. If you're new to this, I hope this little read helps you along your coding journey. Stay curious, keep coding, and enjoy the journey. Thanks for reading.&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>programming</category>
      <category>learning</category>
      <category>coding</category>
    </item>
    <item>
      <title>Supercharge Learning to Code with the Feynman Technique</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Fri, 22 Mar 2024 20:12:12 +0000</pubDate>
      <link>https://dev.to/rwparrish/supercharge-learning-to-code-with-the-feynman-technique-502o</link>
      <guid>https://dev.to/rwparrish/supercharge-learning-to-code-with-the-feynman-technique-502o</guid>
      <description>&lt;h1&gt;
  
  
  Intro
&lt;/h1&gt;

&lt;p&gt;As a software engineering instructor, I've observed a common challenge among adult students transitioning to coding careers. Learning to code can be particularly challenging for adults due to ingrained learning habits, such as note-taking, memorization, and pattern-matching. Pattern matching won't deepen your understanding and can create false confidence. Memorizing syntax and methods will come naturally with practice, so it shouldn't be a priority either. In this piece, I advocate for adopting the Feynman Technique as a more effective approach to learning programming.&lt;/p&gt;

&lt;p&gt;Richard Feynman was an American theoretical physicist known for his contributions to quantum mechanics, quantum electrodynamics, and particle physics. He was born on May 11, 1918, and passed away on February 15, 1988. Feynman made significant advancements in theoretical physics and won the Nobel Prize in Physics in 1965 for his work in quantum electrodynamics.&lt;/p&gt;

&lt;p&gt;Apart from his scientific achievements, Feynman was also known for his exceptional ability to explain complex scientific concepts in simple terms - this brings us to the Feynman Technique for learning complex topics.&lt;/p&gt;

&lt;h1&gt;
  
  
  Outline of The Feynman Technique
&lt;/h1&gt;

&lt;p&gt;So what is the Feynman Technique? It's actually quite a simple approach to learning that can be easily repeated:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose a Concept and Explain It&lt;/li&gt;
&lt;li&gt;Teach It to a Child&lt;/li&gt;
&lt;li&gt;Identify Gaps in Understanding&lt;/li&gt;
&lt;li&gt;Review, Simplify, and Repeat&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's walk through the process together using an example we're all very familiar with, tying our shoelaces.&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Choose a Concept and Explain it:
&lt;/h3&gt;

&lt;p&gt;Select a topic or concept you want to learn or understand better and explain it in simple terms. Use plain language and avoid jargon. Einstein said, "If you can't explain it in simple terms, you don't understand it well enough".&lt;/p&gt;

&lt;p&gt;"Tying shoelaces is a way to secure your shoes to your feet so they don't fall off while you walk. It involves making loops with the laces and then tying them together in a knot."&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Teach it to A Child:
&lt;/h3&gt;

&lt;p&gt;Pretend you're teaching the concept to a child who has no prior knowledge of the subject. This could be a child, a friend, or even an inanimate object like a &lt;a href="https://en.wikipedia.org/wiki/Rubber_duck_debugging" rel="noopener noreferrer"&gt;rubber ducky&lt;/a&gt;. Explain each step as you go along, making sure they understand the process. You may also want to demonstrate each step, especially in regard to coding.&lt;/p&gt;

&lt;p&gt;"First, you take one lace and make a loop by crossing it over itself. Then, you take the other lace and wrap it around the loop. Finally, you tuck the second lace through the hole underneath the first loop and pull it tight to make a knot."&lt;/p&gt;

&lt;h3&gt;
  
  
  3. Identify Gaps in Understanding:
&lt;/h3&gt;

&lt;p&gt;As you explain the concept, pay attention to areas where you struggle to explain or where your understanding is unclear. This helps identify gaps in your knowledge. Go back to your study materials (practice writing code and testing its behavior) and fill in these gaps. This step does require some humility and vulnerability - it's OK not to know everything!&lt;/p&gt;

&lt;p&gt;If you struggle to explain how to create the loops or tie the knot, you may need to revisit those steps and break them down into smaller, more manageable parts.&lt;/p&gt;

&lt;h3&gt;
  
  
  4. Review, Simplify, and Repeat:
&lt;/h3&gt;

&lt;p&gt;Review the concept and your explanations periodically to reinforce your understanding. If there are still areas you find difficult to explain, go back and review them again. Repetition helps solidify your understanding over time. Simplify complex ideas or terms until you can explain them in straightforward terms.&lt;/p&gt;

&lt;p&gt;"To tie your shoelaces, first make a loop with one lace, then wrap the other lace around it, and finally tuck it through the hole and pull tight."&lt;/p&gt;

&lt;h1&gt;
  
  
  Applying The Feynman Technique to Coding
&lt;/h1&gt;

&lt;p&gt;How can you apply this to coding? Let's take a look at an example and try to explain how the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map" rel="noopener noreferrer"&gt;&lt;code&gt;map()&lt;/code&gt;&lt;/a&gt; method in JavaScript works. To simplify the explanation I will use an analogy involving toy cars. &lt;/p&gt;

&lt;h3&gt;
  
  
  1. Choose a Concept and Explain it:
&lt;/h3&gt;

&lt;p&gt;"Imagine you have a toy box filled with different toy cars, and you want to take each toy car, paint it a different color, and put it into a new box. This is similar to what the map method does with arrays in JavaScript."&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Teach it to A Child:
&lt;/h3&gt;

&lt;p&gt;In my view, the teaching step of the Feynman Technique should involve writing code and testing its behavior - not just a verbal explanation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The Original Toy Box (Array):&lt;/strong&gt; "You start with your original toy box, which contains all the toys (elements) you want to work with.":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const orignalToyBox = [
  {
    id: 1,
    brand: "Hot Wheels",
    model: "Mustang",
    color: "Red"
  },
  {
    id: 2,
    brand: "Matchbox",
    model: "Corvette",
    color: "Blue"
  },
  {
    id: 3,
    brand: "Maisto",
    model: "Lamborghini",
    color: "Yellow"
  },
  {
    id: 4,
    brand: "Tonka",
    model: "Pickup Truck",
    color: "Green"
  },
  {
    id: 5,
    brand: "Disney Cars",
    model: "Lightning McQueen",
    color: "Red"
  }
];
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The Painter Function (Callback Function):&lt;/strong&gt; "Next, there is a painter who knows how to paint each toy a different color. This painter is like the callback function you provide to the map method. It takes each toy (element) from the original toy box and returns the painted version.":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function painterFunc(car) {
  car.color = "Orange"
  return car
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;The New Toy Box (New Array):&lt;/strong&gt; "As the painter finishes painting each toy, they put it into a new toy box. This new toy box represents the new array that the map method creates.":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let newToyBox
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Taking Each Toy and Painting It:&lt;/strong&gt; "The map method goes through each toy (element) in the original toy box (array) and calls the painter function (callback function) on it.":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function painterFunc(car) {
  console.log(car) // check each car is passed to callback
  console.log(car.color) // check each car's color 
  car.color = "Orange" // reassigning each car's color
  console.log(car.) // check to see that the color is now Orange
  return car // give the car to the map method so it will be added to the newToyBox when map is finished iterating
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Returning the New Toy Box:&lt;/strong&gt; "Once all the toys have been painted and placed into the new toy box, the map method returns this new box (new array) to you.":&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;let newToyBox = orignalToyBox.map(painterFunction)

// show the result in the console
console.log(newToyBox) // expected output:

[
  { id: 1, brand: 'Hot Wheels', model: 'Mustang', color: 'Orange' },
  { id: 2, brand: 'Matchbox', model: 'Corvette', color: 'Orange' },
  { id: 3, brand: 'Maisto', model: 'Lamborghini', color: 'Orange' },
  { id: 4, brand: 'Tonka', model: 'Pickup Truck', color: 'Orange' },
  { id: 5, brand: 'Disney Cars', model: 'Lightning McQueen', color: 'Orange'}
]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could try using different syntaxes for our callback function for extra practice with functions and conceptual reinforcement of how the &lt;code&gt;map()&lt;/code&gt; method works:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newToyBox = orignalToyBox.map(function painterFunc(car) {
  car.color = "Orange"
  return car
})

console.log(newToyBox)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newToyBox = orignalToyBox.map(function(car) {
  car.color = "Orange"
  return car
})

console.log(newToyBox)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const newToyBox = orignalToyBox.map(car =&amp;gt; {
  car.color = "Orange"
  return car
})

console.log(newToyBox)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you attempt to teach each step you will come to gaps in your understanding (&lt;strong&gt;Step 3. Identify Gaps in Understanding&lt;/strong&gt;). With more practice writing code and explaining how it works aloud, our ability to teach will improve and therefore our understanding will deepen (&lt;strong&gt;Step 4. Review, Simplify, and Repeat&lt;/strong&gt;).&lt;/p&gt;

&lt;h1&gt;
  
  
  Conclusion
&lt;/h1&gt;

&lt;p&gt;If I could start coding again, I'd use the Feynman Technique more and ditch the need to take exhaustive notes and memorize syntax. All the information I needed is readily available online, like the &lt;a href="https://developer.mozilla.org/en-US/" rel="noopener noreferrer"&gt;MDN Web Docs&lt;/a&gt; for JavaScript. Instead, I'd hone my ability to ask the right questions and find answers online by referencing the well-maintained documentation. Applying the Feynman Technique will help you become good at asking the right questions because it is effective at helping you find gaps in your understanding.&lt;/p&gt;

&lt;p&gt;There you have it. The next time you come up against a complex coding topic you're struggling to grasp, try the Feynman Technique. Stay curious, keep coding, and enjoy the never-ending learning journey. Thanks for reading.&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
      <category>learning</category>
    </item>
    <item>
      <title>Pagination in Rails</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sat, 16 Jan 2021 02:05:05 +0000</pubDate>
      <link>https://dev.to/rwparrish/pagination-in-rails-4379</link>
      <guid>https://dev.to/rwparrish/pagination-in-rails-4379</guid>
      <description>&lt;p&gt;In this blog, we will look at what pagination is and how to implement it into your Rails app.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;Everybody has done some shopping on Amazon at some point. We live in the age of information and convenience. Before the school year started, I was shopping on Amazon for a new backpack with my daughter. Of course, there were more options than one could dream of and it would have taken us three lifetimes to look through them all.&lt;/p&gt;

&lt;p&gt;This is where pagination comes in. With pagination, we can limit the number of items that are retrieved from the database and shown at a given point. It can be overwhelming to look at one page with hundreds or thousands of items on it. Why not show twenty or thirty items at a time instead? This is what pagination can do for us. Basically, it breaks up a large list into a sequence of smaller lists based on the specifications we give it.&lt;/p&gt;

&lt;h3&gt;
  
  
  Setting up
&lt;/h3&gt;

&lt;p&gt;A quick internet search of "will_paginate" finds &lt;a href="https://github.com/mislav/will_paginate" rel="noopener noreferrer"&gt;this&lt;/a&gt;. Before we continue, take a look at it. Setting up pagination is surprisingly simple. After you have read it, we walk through the setup step by step. We will reference &lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1
&lt;/h3&gt;

&lt;p&gt;First, we need to install the pagination gem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// in the Gemfile

gem 'will_paginate', '~&amp;gt; 3.1.0'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then run &lt;code&gt;bundle install&lt;/code&gt; from the terminal.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2
&lt;/h3&gt;

&lt;p&gt;In my app, I have a couple of places I want to be able to implement pagination. First, I will head to the controller(s) and make the necessary code changes there. We get this line of code from the &lt;a href="https://github.com/mislav/will_paginate" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; - &lt;code&gt;Post.paginate(page: params[:page], per_page: 30)&lt;/code&gt;. In the controller the code currently looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class ArticlesController &amp;lt; ApplicationController

def index
  @articles = Article.all
end
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code in the index action gets changed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def index
  @articles = Article.paginate(page: params[:page], per_page: 5)
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, the &lt;code&gt;@articles&lt;/code&gt; instance variable will hold only a certain number of items from the database according to our specifications. In this case, I specify that each page should show five items.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3
&lt;/h3&gt;

&lt;p&gt;Again, referring to the documentation, we are given this line of code - &lt;code&gt;&amp;lt;%= will_paginate @posts %&amp;gt;&lt;/code&gt;. So, in the appropriate view (articles/index.html.erb in this case):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1 class="text-center mt-4"&amp;gt;This is the index page&amp;lt;/h1&amp;gt;

&amp;lt;%= render 'article' %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The following code is added:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;h1 class="text-center mt-4"&amp;gt;This is the index page&amp;lt;/h1&amp;gt;

&amp;lt;div class="apple_pagination"&amp;gt;
  &amp;lt;%= will_paginate @articles, :container =&amp;gt; false %&amp;gt;
&amp;lt;/div&amp;gt;

&amp;lt;%= render 'article' %&amp;gt;

&amp;lt;div class="apple_pagination"&amp;gt;
  &amp;lt;%= will_paginate @articles, :container =&amp;gt; false %&amp;gt;
&amp;lt;/div&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I'm sure you noticed a couple of things. I wanted to render the pagination at the top and the bottom of the page. Also, I added some styling. You can do the same by following the link - &lt;em&gt;make those pagination links prettier&lt;/em&gt; - in the documentation to &lt;a href="http://mislav.github.io/will_paginate/" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;In this short and sweet read, pagination was covered. We learned:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Pagination breaks up a large list into a sequence of smaller lists based on the specifications we give it.&lt;/li&gt;
&lt;li&gt;It is pretty darn simple to implement!&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope you learned something new. Happy coding! &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>programming</category>
    </item>
    <item>
      <title>Changing sate before saving to the database in Rails</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sat, 09 Jan 2021 23:36:58 +0000</pubDate>
      <link>https://dev.to/rwparrish/changing-sate-before-saving-to-the-database-in-rails-50mi</link>
      <guid>https://dev.to/rwparrish/changing-sate-before-saving-to-the-database-in-rails-50mi</guid>
      <description>&lt;p&gt;In this blog, we're going to look at how to change an object's state immediately preceding saving it to the database in an RoR app.&lt;/p&gt;

&lt;p&gt;The method that we will be using is actually a callback that is provided by ActiveRecord called &lt;code&gt;before_save&lt;/code&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  So, what's a &lt;em&gt;callback&lt;/em&gt; then?
&lt;/h4&gt;

&lt;p&gt;The official Ruby on Rails &lt;a href="https://guides.rubyonrails.org/active_record_callbacks.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt; says, "Callbacks are methods that get called at certain moments of an object's life cycle. With callbacks, it is possible to write code that will run whenever an Active Record object is created, saved, updated, deleted, validated, or loaded from the database." &lt;/p&gt;

&lt;h4&gt;
  
  
  So, what's an &lt;em&gt;ActiveRecord&lt;/em&gt; callback then?
&lt;/h4&gt;

&lt;p&gt;Again, exploring the &lt;a href="https://api.rubyonrails.org/v6.1.0/classes/ActiveRecord/Callbacks.html" rel="noopener noreferrer"&gt;documentation&lt;/a&gt;, we read, "Callbacks are hooks into the life cycle of an Active Record object that allow you to trigger logic before or after a change in the object state."&lt;/p&gt;

&lt;p&gt;Take a look at the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  has_many :articles
  validates :username, presence: true, uniqueness: { case_sensitive: false }, 
                      length: { minimum: 3, maximum: 25 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i                 
  validates :email, presence: true, uniqueness: { case_sensitive: false }, 
                    length: { maximum: 105 },
                    format: { with: VALID_EMAIL_REGEX }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above code snippet, you can see that there is a constant assigned a some regex - &lt;code&gt;VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i&lt;/code&gt;. This constant is then used to validate the format of email address taken in from the user. &lt;/p&gt;

&lt;p&gt;For a neat little exercise, copy &lt;code&gt;\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/&lt;/code&gt; and paste it &lt;a href="https://rubular.com/" rel="noopener noreferrer"&gt;here&lt;/a&gt;. Be sure to add the "i" after the forward slash at &lt;a href="https://rubular.com/" rel="noopener noreferrer"&gt;Rubular&lt;/a&gt;. Try entering some example and non-example email addresses. For example, try entering "&lt;a href="mailto:janedoe@example.com"&gt;janedoe@example.com&lt;/a&gt;" and "email.com". Pretty cool, huh?&lt;/p&gt;

&lt;p&gt;So we can see that email addresses are being checked for proper email format. However, the letters' case in the email addresses is not being checked. This brings us to the &lt;code&gt;before_save&lt;/code&gt; callback.&lt;/p&gt;

&lt;p&gt;Check out the code below:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class User &amp;lt; ApplicationRecord
  before_save { self.email = email.downcase }
  has_many :articles
  validates :username, presence: true, uniqueness: { case_sensitive: false }, 
                      length: { minimum: 3, maximum: 25 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i                 
  validates :email, presence: true, uniqueness: { case_sensitive: false }, 
                    length: { maximum: 105 },
                    format: { with: VALID_EMAIL_REGEX }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice this line - &lt;code&gt;before_save { self.email = email.downcase }&lt;/code&gt;. Here we are referencing the email field associated with each object before saving said object so we must append the &lt;code&gt;self&lt;/code&gt; keyword to the &lt;code&gt;email&lt;/code&gt; we want to reference. This &lt;code&gt;self&lt;/code&gt; is referencing each object of the &lt;code&gt;User&lt;/code&gt; class so when the computer comes to the email field of any of these objects, it runs the &lt;code&gt;downcase&lt;/code&gt; method on that field and then saves the object. We can now be sure that the email address will have proper formatting AND letter case before being saved to the database!&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;This blog covered a bit of regex and introduced you to the ActiveRecord callback &lt;code&gt;before_save&lt;/code&gt;. With &lt;code&gt;before_save&lt;/code&gt;, we can change the state of an object right before the object is saved to the database and in this case, it was used to execute a block of code that downcased all the letters in email addresses. &lt;/p&gt;

&lt;p&gt;I hope you learned something and happy coding! &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
    </item>
    <item>
      <title>Using Bcrypt with Ruby</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sun, 03 Jan 2021 03:00:06 +0000</pubDate>
      <link>https://dev.to/rwparrish/using-bcrypt-with-ruby-242p</link>
      <guid>https://dev.to/rwparrish/using-bcrypt-with-ruby-242p</guid>
      <description>&lt;p&gt;In this blog, I will cover how to create a 'bcrypt" module for use in a Ruby on Rails project.&lt;/p&gt;

&lt;p&gt;Bcrypt uses the MD5 algorithm and adds in a salt to it in order to hash passwords. This is important because as developers we do not want passwords to be stored as regular old strings. Believe it or not, this used to be the case. However, this is no longer safe and NOT considered to be best practice.&lt;/p&gt;

&lt;p&gt;The code below is a module. I have called this module "Crud". The methods in this module can be easily called on from other classes or files:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Crud&lt;/span&gt;
  &lt;span class="nb"&gt;require&lt;/span&gt; &lt;span class="s1"&gt;'bcrypt'&lt;/span&gt;
  &lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="s2"&gt;"Module CRUD activated"&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_hash_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;BCrypt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;verify_hash_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="no"&gt;BCrypt&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Password&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_secure_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;list_of_users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;list_of_users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;user_record&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="n"&gt;user_record&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;create_hash_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_record&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="n"&gt;list_of_users&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nc"&gt;self&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;authenticate_user&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;list_of_users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;list_of_users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;each&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt; &lt;span class="o"&gt;|&lt;/span&gt;&lt;span class="n"&gt;user_record&lt;/span&gt;&lt;span class="o"&gt;|&lt;/span&gt;
      &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;user_record&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:username&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;verify_hash_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_record&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:password&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;user_record&lt;/span&gt;
      &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
    &lt;span class="s2"&gt;"Credentials were not correct"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice the "self" keyword on the methods creating class methods to be called on in another file. Class methods do not require an instance of an object to work. They can be called on the class itself, or a module in this case...&lt;/p&gt;

&lt;p&gt;In the file I want to use the Crud module containing the bcrypt methods:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s1"&gt;'crud'&lt;/span&gt;

&lt;span class="n"&gt;users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"ryan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"password1"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"jack"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"password2"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"maria"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"password3"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"michael"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"password4"&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt;
          &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="ss"&gt;username: &lt;/span&gt;&lt;span class="s2"&gt;"phill"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;password: &lt;/span&gt;&lt;span class="s2"&gt;"password5"&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;hashed_users&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Crud&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_secure_users&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;users&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hashed_users&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are two things to notice here. First, take a look at &lt;code&gt;require_relative 'crud'&lt;/code&gt;. This allows me to use the methods from the Crud module in this file. Second, notice the methods I wish to use from the Crud module must be appended with the &lt;code&gt;Crud&lt;/code&gt; class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="nb"&gt;require_relative&lt;/span&gt; &lt;span class="s1"&gt;'crud'&lt;/span&gt;

&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Student&lt;/span&gt;
  &lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Crud&lt;/span&gt;
  &lt;span class="nb"&gt;attr_accessor&lt;/span&gt; &lt;span class="ss"&gt;:first_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:last_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;:password&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;initialize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;firstname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;lastname&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@first_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;firstname&lt;/span&gt;
    &lt;span class="vi"&gt;@last_name&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;lastname&lt;/span&gt;
    &lt;span class="vi"&gt;@username&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;username&lt;/span&gt;
    &lt;span class="vi"&gt;@email&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;email&lt;/span&gt;
    &lt;span class="vi"&gt;@password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;password&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;to_s&lt;/span&gt;
    &lt;span class="s2"&gt;"First name: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@first_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Last name: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@last_name&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;, Username: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@username&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;,
                  email address: &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="vi"&gt;@email&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="n"&gt;ryan&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"Ryan"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Parrish"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ryan1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"ryan@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="s2"&gt;"password1"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;john&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;Student&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"John"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"Doe"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"john1"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s2"&gt;"john1@example.com"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
                      &lt;span class="s2"&gt;"password2"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;hashed_password&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;ryan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;create_hash_digest&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ryan&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;password&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="nb"&gt;puts&lt;/span&gt; &lt;span class="n"&gt;hashed_password&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If I include or "mixin" the module I want - &lt;code&gt;include Crud&lt;/code&gt;, then the methods in the module needn't be class methods so the "self" keyword can be removed from the methods in the module and the methods can then be called on instances of objects in the class I want. &lt;/p&gt;

&lt;p&gt;Happy coding! &lt;/p&gt;

</description>
      <category>ruby</category>
      <category>security</category>
      <category>rails</category>
    </item>
    <item>
      <title>JavaScript Scope and Hoisting</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sat, 26 Dec 2020 19:04:17 +0000</pubDate>
      <link>https://dev.to/rwparrish/javascript-scope-and-hoisting-h50</link>
      <guid>https://dev.to/rwparrish/javascript-scope-and-hoisting-h50</guid>
      <description>&lt;p&gt;If you are new to JavaScript and struggling to understand the concepts of scope and hoisting, you are not abnormal. I remember how long it took me to get a firm grasp on these two tricky concepts. In this blog, we'll take a look at a simple example to help clear things up.&lt;/p&gt;

&lt;p&gt;Check out this little write up on scope in JavaScript for a bit more on scope 👇&lt;br&gt;
&lt;/p&gt;
&lt;div class="ltag__link"&gt;
  &lt;a href="/rwparrish" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__pic"&gt;
      &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F456931%2Fb9a82976-1a11-4748-91a3-503c05d5edfa.jpg" alt="rwparrish"&gt;
    &lt;/div&gt;
  &lt;/a&gt;
  &lt;a href="https://dev.to/rwparrish/scope-in-javascript-js-1ca6" class="ltag__link__link"&gt;
    &lt;div class="ltag__link__content"&gt;
      &lt;h2&gt;Scope in JavaScript(JS)&lt;/h2&gt;
      &lt;h3&gt;rwparrish ・ Aug 23 '20&lt;/h3&gt;
      &lt;div class="ltag__link__taglist"&gt;
        &lt;span class="ltag__link__tag"&gt;#javascript&lt;/span&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/a&gt;
&lt;/div&gt;


&lt;p&gt;What will the browser's console show if the following code gets executed?&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var text = 'outside';
function logIt(){
    console.log(text);
    var text = 'inside';
};
logIt();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Stop. Don't scroll down and don't copy this into the DevTools Console, yet. Take a minute to look closely at the code and think about it.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  Solution
&lt;/h3&gt;

&lt;p&gt;The console will log &lt;strong&gt;undefined&lt;/strong&gt;. To understand this, let's look at hoisting and scope in JavaScript.&lt;/p&gt;

&lt;h3&gt;
  
  
  Function-level scope
&lt;/h3&gt;

&lt;p&gt;Functions create new scopes in JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function setVar(){
    // inside this function we have a new scope
    // so this variable, declared in this function's scope, won't be available outside the function
    var varInFunction = 'inside a function';
}
setVar();
console.log(varInFunction);  // throws 'ReferenceError: varInFunction is not defined'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Blocks like if statements and for loops do not create a new scope:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if (true) {
    // this if statement doesn't create a new scope
    // so varInIf is available in the global scope
    var block = 'inside an if statement';
}
console.log(block);  // logs 'inside an if statement'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Declaration vs. assignment
&lt;/h3&gt;

&lt;p&gt;A variable declaration simply tells the interpreter that a variable exists. By default it initializes the variable to &lt;strong&gt;undefined&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var dog;
console.log(dog);  // logs undefined (NOT a ReferenceError)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A variable assignment assigns a value to the variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;dog = "Lino";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can both declare and assign in the same line:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var dog = "Lino";
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Hoisting
&lt;/h3&gt;

&lt;p&gt;In Javascript, variable declarations are "hoisted" to the top of the current scope. Variable assignments, however, are not.&lt;/p&gt;

&lt;p&gt;Returning to the original problem:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var text = 'outside';
function logIt(){
    console.log(text);
    var text = 'inside';
};
logIt();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The declaration (but not the assignment) of text gets hoisted to the top of &lt;code&gt;logIt()&lt;/code&gt;. So our code gets interpreted as though it were:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;var text = 'outside';
function logIt(){
    var text;
    console.log(text);
    text = 'inside';
};
logIt();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So we have a new variable text inside of &lt;code&gt;logIt()&lt;/code&gt; that is initialized to undefined, which is what it holds when we hit our log statement.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;When you declare a variable in JavaScript (using "var"), that variable declaration is "hoisted" to the top of the current scope — meaning the top of the current function or the top of the script if the variable isn't in a function.&lt;/p&gt;

&lt;h3&gt;
  
  
  Next step
&lt;/h3&gt;

&lt;p&gt;Try looking into the differences when using "let" and "const" instead of "var" to declare variables in JavaScript. Also, what is a lexical environment and what does it have to do with the concepts covered in this blog?&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>javascript</category>
    </item>
    <item>
      <title>Validations and displaying messages in Rails</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Thu, 17 Dec 2020 18:58:51 +0000</pubDate>
      <link>https://dev.to/rwparrish/validations-and-displaying-messages-in-rails-2d47</link>
      <guid>https://dev.to/rwparrish/validations-and-displaying-messages-in-rails-2d47</guid>
      <description>&lt;p&gt;In this blog, I would like to share my process for setting validations and displaying messages in Rails. Here are the two general topics I will cover:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How can we make sure that instances of objects get saved to the database the way we want them to? The answer 👉 validations&lt;/li&gt;
&lt;li&gt;Once validations are working, how can we display messages letting us know about errors and/or successes? The answer 👉  there are a couple of ways to do this so keep reading to learn more!&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Active Record
&lt;/h3&gt;

&lt;p&gt;First, what is Active Record? From the official docs &lt;a href="https://guides.rubyonrails.org/active_record_basics.html" rel="noopener noreferrer"&gt;here&lt;/a&gt;, it is the M in the &lt;a href="https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93controller" rel="noopener noreferrer"&gt;MVC&lt;/a&gt; (Model-View-Controller) software design pattern. Active Record deals with persisting data, more specifically rich objects. Active record is an &lt;a href="https://en.wikipedia.org/wiki/Object%E2%80%93relational_mapping" rel="noopener noreferrer"&gt;ORM&lt;/a&gt; (Object Relational Mapping) framework. The &lt;a href="https://guides.rubyonrails.org/active_record_basics.html" rel="noopener noreferrer"&gt;docs&lt;/a&gt; put it well - "Using ORM, the properties and relationships of the objects in an application can be easily stored and retrieved from a database without writing SQL statements directly and with less overall database access code." Basically, Active Record makes connecting to the database much easier and efficient for Rails developers.&lt;/p&gt;

&lt;p&gt;Ok, that bit of background info helps us understand why it makes sense that Active Record would also deal with validating objects before they can be saved to the database to be persisted.&lt;/p&gt;

&lt;h3&gt;
  
  
  A bit on &lt;code&gt;params&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Below I have my &lt;a href="https://guides.rubyonrails.org/action_controller_overview.html" rel="noopener noreferrer"&gt;&lt;code&gt;params&lt;/code&gt;&lt;/a&gt; setup to whitelist the data I want. First, I require the top-level key of my &lt;code&gt;params&lt;/code&gt; - &lt;code&gt;:article&lt;/code&gt; and then I specify which keys within the &lt;code&gt;params&lt;/code&gt; I want to permit - &lt;code&gt;:title&lt;/code&gt; and &lt;code&gt;:description&lt;/code&gt; in this case:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create
    @article = Article.new(params.require(:article).permit(:title, :description))
    render plain: @article
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the browser, we see something like this &lt;code&gt;#&amp;lt;Article:0x00007fa47fbe8770&amp;gt;&lt;/code&gt;. Let's use the &lt;code&gt;inspect&lt;/code&gt; method to take a closer look at this object:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create
    @article = Article.new(params.require(:article).permit(:title, :description))
    render plain: @article.inspect
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should see something like this in the browser now:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#&amp;lt;Article id: nil, title: "Testing some stuff", description: "testing some stuff", created_at: nil, updated_at: nil&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Notice that &lt;code&gt;id: nil&lt;/code&gt;. This is because we haven't saved this instance of an article object to the database yet. Let's try saving it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create
    @article = Article.new(params.require(:article).permit(:title, :description))
   article.save
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can hop into the Rails console by entering &lt;code&gt;rails c&lt;/code&gt; in the terminal to verify it has been saved. In the console, try &lt;code&gt;Article.all&lt;/code&gt; asking to see all instances of article objects and we should see the most recent article created at the bottom of the list with an &lt;code&gt;id:&lt;/code&gt; of some &lt;code&gt;number&lt;/code&gt; depending on its place in the database table.&lt;/p&gt;

&lt;p&gt;You could also do this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create
    @article = Article.new(params.require(:article).permit(:title, :description))
    @article.save
    render plain: Article.all.inspect
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, now we know that our &lt;code&gt;strong_params&lt;/code&gt; are functioning and we can save objects to the database. Nice! However, what if we have some validations in mind? Here come those built-in Active Record validation helpers I mentioned earlier.&lt;/p&gt;

&lt;h3&gt;
  
  
  Built in Active Record Validations
&lt;/h3&gt;

&lt;p&gt;In my model - &lt;code&gt;article.rb&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Article &amp;lt; ApplicationRecord
  validates :title, presence: true, length: { minimum: 6, maximum: 100 }
  validates :description, presence: true, length: { minimum: 10, maximum: 300 }
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above I specify that the value for the &lt;code&gt;:title&lt;/code&gt; and &lt;code&gt;:description&lt;/code&gt; keys cannot be empty and that those values should have a certain length.&lt;/p&gt;

&lt;p&gt;Back in the controller - &lt;code&gt;articles_controller.rb&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create
    @article = Article.new(params.require(:article).permit(:title, :description))
    if @article.save
      redirect_to @article
    else 
      render 'new'
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Ok, if the article can be saved, the user should be redirected to the &lt;code&gt;show&lt;/code&gt; view to see it. However, if the validations are not met and the article cannot be saved then the form should re-render - essentially prompting the user to try again. Wouldn't it be nice to display an error message here for the user to better understand what went wrong?&lt;/p&gt;

&lt;h3&gt;
  
  
  Displaying error messages
&lt;/h3&gt;

&lt;p&gt;First, we try creating a new article that we know cannot be saved. Remembering the validations we setup earlier - maybe the title is left empty and/or the description is too short. In the console try:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;article = Article.new
=&amp;gt; #&amp;lt;Article id: nil, title: nil, description: nil, created_at: nil, updated_at: nil&amp;gt;
article.save
=&amp;gt; false
article.errors.full_messages
=&amp;gt; =&amp;gt; ["Title can't be blank", "Title is too short (minimum is 6 characters)", "Description can't be blank", "Description is too short (minimum is 10 characters)"]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Chaining the &lt;code&gt;errors&lt;/code&gt; and &lt;code&gt;full_messages&lt;/code&gt; methods we get an array of messages. We want to iterate over this array and display each message. But where should we do this!? Well, since the "new article input form" is being displayed again when validations are not met we should display the error messages there for the user to see.&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;new.html.erb&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;% if @article.errors.any? %&amp;gt;
  &amp;lt;h3&amp;gt;The following errors prevented the article from being saved:&amp;lt;/h3&amp;gt;
  &amp;lt;ul&amp;gt;
    &amp;lt;% @article.errors.full_messages.each do |msg| %&amp;gt;
     &amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt; 
    &amp;lt;% end %&amp;gt;
  &amp;lt;/ul&amp;gt;
&amp;lt;% end %&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because of the instance variable &lt;code&gt;@articles&lt;/code&gt; we have access to each newly submitted article in our view. &lt;code&gt;&amp;lt;% if @article.errors.any? %&amp;gt;&lt;/code&gt; checks to see if there are any errors on each new instance - we needn't enter the iteration block unless it is necessary to do so. Then we simply iterate over the array of messages inside &lt;code&gt;erb&lt;/code&gt; tags. Be sure to use &lt;code&gt;=&lt;/code&gt; in the &lt;code&gt;erb&lt;/code&gt; tag where you want the information to be displayed in the browser - &lt;code&gt;&amp;lt;li&amp;gt;&amp;lt;%= msg %&amp;gt;&amp;lt;/li&amp;gt;&lt;/code&gt;. The above code produces this in the browser:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr8q37clkbyid9t02oblr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fr8q37clkbyid9t02oblr.png" alt="Alt Text" width="800" height="222"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;h3&gt;
  
  
  Displaying messages using &lt;code&gt;flash&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;Now, what if the new article is saved successfully? How could we tell the user this?&lt;/p&gt;

&lt;p&gt;Back in the &lt;code&gt;articles_controller.rb&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def create
    @article = Article.new(params.require(:article).permit(:title, :description))
    if @article.save
      flash[:notice] = "This article was saved successfully"
      redirect_to @article
    else 
      render 'new'
    end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above we say if the article is saved successfully to the database, we use &lt;a href="https://api.rubyonrails.org/classes/ActionDispatch/Flash.html" rel="noopener noreferrer"&gt;flash&lt;/a&gt; to pass a temporary message from the &lt;code&gt;create&lt;/code&gt; action to the &lt;code&gt;show&lt;/code&gt; action. The &lt;code&gt;show&lt;/code&gt; action will automatically pass the massage to its view. However, we can do something like this:&lt;/p&gt;

&lt;p&gt;In the &lt;code&gt;application.html.erb&lt;/code&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;...

&amp;lt;body&amp;gt;
   &amp;lt;% flash.each do |name, msg| %&amp;gt;
     &amp;lt;%= msg %&amp;gt;
   &amp;lt;% end %&amp;gt;
   &amp;lt;%= yield %&amp;gt;
&amp;lt;/body&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;We learned how to:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Implement Active Record built-in validations&lt;/li&gt;
&lt;li&gt;Display error messages&lt;/li&gt;
&lt;li&gt;Display messages using &lt;code&gt;flash&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope you found this tutorial to be helpful. As always, please ask questions, leave feedback, and share. Happy coding!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Deploying Rails Apps with Heroku</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Fri, 11 Dec 2020 20:29:10 +0000</pubDate>
      <link>https://dev.to/rwparrish/deploying-rails-apps-with-heroku-9cm</link>
      <guid>https://dev.to/rwparrish/deploying-rails-apps-with-heroku-9cm</guid>
      <description>&lt;p&gt;Deploying your first app is exciting! Let's take a look at what it takes in a Rails case.&lt;/p&gt;

&lt;h3&gt;
  
  
  Intro
&lt;/h3&gt;

&lt;p&gt;First, a couple of key disctions between a local server and a production server:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;A production server needs to be accessible at all times so anyone can go check it out. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The database will not be transmitted so once deployed, you need to create your own &lt;em&gt;production database&lt;/em&gt; - only application code gets transmitted.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;blockquote&gt;
&lt;p&gt;Quick aside: &lt;strong&gt;all of the instructions covered will be for Mac users&lt;/strong&gt;. No worries Windows users! I will provide links to help guide you to the info you need along the way. This blog also assumes that you are currently using a popular version control system - &lt;a href="https://git-scm.com/" rel="noopener noreferrer"&gt;Git&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  Installing Heroku
&lt;/h3&gt;

&lt;p&gt;Once you create your free &lt;a href="https://signup.heroku.com/t/platform?c=70130000001xDpdAAE&amp;amp;gclid=Cj0KCQiAzsz-BRCCARIsANotFgOWl3QpR4aJdOE1AWvdAE3iQngj9JV70kxjDsctIMxyMiC8dGmtxZIaAheKEALw_wcB" rel="noopener noreferrer"&gt;Heroku&lt;/a&gt; account, you'll need the Heroku command-line interface (CLI) tools. It's time to install the Heroku CLI. To do so run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew tap heroku/brew &amp;amp;&amp;amp; brew install heroku
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can verify the installation went smoothly but running &lt;code&gt;heroku&lt;/code&gt; in the command line. If installed correctly you will get the version as well as a list of commands available to you. Windows users check &lt;a href="https://devcenter.heroku.com/articles/heroku-cli#download-and-install" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Next, you will need to log into the Heroku account you just created. Run this command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku login
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then follow the instructions in the terminal. It should take you to the browser to log in.&lt;/p&gt;

&lt;p&gt;Now, following along with the instructions which can be found &lt;a href="https://devcenter.heroku.com/articles/getting-started-with-rails6" rel="noopener noreferrer"&gt;here&lt;/a&gt; the next step is to create a new rails app. For this blog, I have already created a simple Rails app and I am assuming you have already made one too if you are reading this - I am skipping this step.&lt;/p&gt;

&lt;h3&gt;
  
  
  Creating Heroku App and Prepping Local Code for Deployment
&lt;/h3&gt;

&lt;p&gt;So, now it's time to deploy the application to Heroku. If you are following along &lt;a href="https://devcenter.heroku.com/articles/getting-started-with-rails6" rel="noopener noreferrer"&gt;here&lt;/a&gt;, you should scroll down until you find the title "Deploy your application to Heroku". Run the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku create
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Now we have a shell app created by Heroku with a link available to us. However, before our application code is deployed we need to get it production-ready which involves setting up our database gem for production.&lt;/p&gt;

&lt;p&gt;Ruby comes with &lt;a href="https://www.sqlite.org/index.html" rel="noopener noreferrer"&gt;SQLite&lt;/a&gt; which is fine for development (especially small apps) but not so great for production (it's not a production-ready database) and will not work with Heroku. Heroku uses &lt;a href="https://www.postgresql.org/" rel="noopener noreferrer"&gt;PostgreSQL&lt;/a&gt; as its default. To solve this we will change some things in the &lt;code&gt;Gemfile&lt;/code&gt; of our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }

ruby '2.6.1'

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~&amp;gt; 6.0.3', '&amp;gt;= 6.0.3.4'
# Use sqlite3 as the database for Active Record
gem 'sqlite3', '~&amp;gt; 1.4'
# Use Puma as the app server
gem 'puma', '~&amp;gt; 4.1'
# Use SCSS for stylesheets
gem 'sass-rails', '&amp;gt;= 6'
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem 'webpacker', '~&amp;gt; 4.0'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~&amp;gt; 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~&amp;gt; 2.7'
...

group :production do
  gem 'pg'
end

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The bit that was added to the &lt;code&gt;Gemfile&lt;/code&gt; was:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;group :production do
  gem 'pg'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gem for PostgreSQL (aka Postgre) was added.&lt;/p&gt;

&lt;p&gt;Now, look back at the &lt;code&gt;Gemfile&lt;/code&gt;, or the code snippet of mine above, and notice this line - &lt;code&gt;gem 'sqlite3', '~&amp;gt; 1.4'&lt;/code&gt;. At this position in the &lt;code&gt;Gemfile&lt;/code&gt;, this gem is made available universally in our app and we no longer want SQLite enabled for production. We want SQLite to be available only for &lt;em&gt;development and test environments&lt;/em&gt;. Cut &lt;code&gt;gem 'sqlite3', '~&amp;gt; 1.4'&lt;/code&gt; and move it:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;group :development, :test do
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
  gem 'sqlite3', '~&amp;gt; 1.4'
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because we have altered our &lt;code&gt;Gemfile&lt;/code&gt; it's time to run &lt;code&gt;bundle install&lt;/code&gt;. However, we do not want the gem we added for production - &lt;code&gt;gem 'pg'&lt;/code&gt; to be installed locally. So, we run this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;bundle install --without production
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This step makes the necessary updates to the &lt;code&gt;Gemfile.lock&lt;/code&gt; for production and our application code is ready to be deployed. &lt;strong&gt;Now is a good time to make a commit.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Deploy App!
&lt;/h3&gt;

&lt;p&gt;Now it's finally time to push our master/main branch to Heroku:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku main
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A quick note about the code above. If you see an error message that looks like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;error: src refspec main does not match any
error: failed to push some refs to 'https://git.heroku.com/sheltered-ocean-64484.git'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Try running:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;git push heroku master
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I won't go into exactly why this happens right now. Just know that some in the industry are changing terminology from "master" to "main". &lt;/p&gt;

&lt;p&gt;Once it is working, it is going to deploy to the Heroku app that we created! This step can take a minute or two and when finished you should see a line in the terminal that looks something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;remote:        https://sheltered-ocean-64484.herokuapp.com/ deployed to Heroku
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Go to that link to check out your deployed app!&lt;/p&gt;

&lt;p&gt;Now let's change the auto-generated name Heroku gave our app, "sheltered-ocean-64484" in my case, by running the following from the terminal while in your app's directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;heroku rename myappname
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now you can go to: &lt;code&gt;http://mynameapp.herokuapp.com/&lt;/code&gt; to view your app!&lt;/p&gt;

&lt;p&gt;That's it for now. If we added some more features like database tables and migration files we would need to perform a couple more steps from our terminal. Check &lt;a href="https://devcenter.heroku.com/articles/getting-started-with-rails6" rel="noopener noreferrer"&gt;here&lt;/a&gt; for that info. Scroll to the "Migrate your database" section.&lt;/p&gt;

&lt;h3&gt;
  
  
  Reacp
&lt;/h3&gt;

&lt;p&gt;This blog covered:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Installing Heroku&lt;/li&gt;
&lt;li&gt;Creating a Heroku app&lt;/li&gt;
&lt;li&gt;Making the necessary changes to our application code in preparation for deployment&lt;/li&gt;
&lt;li&gt;Deploying the app and changing its name&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I hope you found this tutorial to be useful. Please ask questions, leave feedback, and share.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>beginners</category>
      <category>tutorial</category>
      <category>ruby</category>
      <category>heroku</category>
    </item>
    <item>
      <title>Associations in Rails</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sat, 05 Dec 2020 22:51:32 +0000</pubDate>
      <link>https://dev.to/rwparrish/associations-in-rails-3gc1</link>
      <guid>https://dev.to/rwparrish/associations-in-rails-3gc1</guid>
      <description>&lt;p&gt;In this blog, I want to take a quick look at a very simple thing we can do to verify our associations have been set up properly at the start of a Rails project using some ActiveRecord magic.&lt;/p&gt;

&lt;p&gt;Once you have an idea of what your database table relationships will look like and have them defined in your project, it's a great idea to quickly create some dummy data in the rails console to verify your associations are working how you want them to.&lt;/p&gt;

&lt;p&gt;I was building an exercise journal and my relationships were as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Workout &amp;lt; ApplicationRecord
...
has_many :exercises
has_many :users, through: :exercises
...

class User &amp;lt; ApplicationRecord
...
has_many :workouts, through: :exercises
has_many :exercises
...

class Exercise &amp;lt; ApplicationRecord
...
belongs_to :workout
belongs_to :user
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We will be verifying these associations. So we head into the terminal and play in the Rails console &lt;code&gt;rails c&lt;/code&gt; to create a couple of users:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c = User.create(name: "Stan", email: "stan@stan.com", password: "password")
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;c&lt;/code&gt; is arbitrary here and is used to call the newly created user, if we do this we can confirm that the new user has been assigned a &lt;code&gt;user_id&lt;/code&gt; of 1, meaning it was successfully saved to the DB. We should be able to see the attributes that were passed in as well (&lt;code&gt;name&lt;/code&gt;, &lt;code&gt;email&lt;/code&gt;, &lt;code&gt;password&lt;/code&gt;). Repeat the process to create a second user which should save with a &lt;code&gt;user_id&lt;/code&gt; of 2. Great! We’ve got our users. &lt;/p&gt;

&lt;p&gt;Now on to workouts. It’s important to create the users and workouts first, given that our joins table is the &lt;code&gt;exercises&lt;/code&gt; table. This is because each exercise created will need to be associated with a user and a workout - the &lt;code&gt;exercises&lt;/code&gt; table will "join" the &lt;code&gt;users&lt;/code&gt; and &lt;code&gt;workouts&lt;/code&gt; tables. You can see this in the relationships code snippet provided above.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;c = Workout.create(name: "HIT", notes: "No rest")&lt;/code&gt;&lt;br&gt;
Very similar to the users example. This should save with a &lt;code&gt;workout_id&lt;/code&gt; of 1. You can confirm this by calling &lt;code&gt;c&lt;/code&gt; in the terminal. Make one more workout and then it’s time to create some exercises:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;c = Exercise.create(name: "Burpee", user_id: 1, workout_id: 1)&lt;/code&gt;&lt;br&gt;
Above I assigned foreign keys to the exercise object and then checked to see if it saved to the DB by calling &lt;code&gt;c&lt;/code&gt; in the terminal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;c = Exercise.create(name: "Pushup", user_id: 2, workout_id: 1)
c = Exercise.create(name: "Situp", user_id: 1, workout_id: 2)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we can call &lt;code&gt;User.all.first.workouts&lt;/code&gt; or &lt;code&gt;User.all.last.exercises&lt;/code&gt; or &lt;code&gt;Workout.all.last.exercises&lt;/code&gt; for example.&lt;/p&gt;

&lt;p&gt;I hope this helps you get started. It’s important to verify these relationships are working correctly before getting too deep into your project.&lt;/p&gt;

&lt;p&gt;Happy coding!&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How does React update the DOM?</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sat, 28 Nov 2020 22:23:44 +0000</pubDate>
      <link>https://dev.to/rwparrish/how-does-react-update-the-dom-1o0i</link>
      <guid>https://dev.to/rwparrish/how-does-react-update-the-dom-1o0i</guid>
      <description>&lt;p&gt;As I continue learning about React and working my way through this series on React, I find myself falling in love with it. In this blog, I would like to touch on React's virtual DOM.&lt;/p&gt;

&lt;h4&gt;
  
  
  What is the DOM?
&lt;/h4&gt;

&lt;p&gt;According to &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Document_Object_Model/Introduction" rel="noopener noreferrer"&gt;MDN&lt;/a&gt;, the Document Object Model (DOM) is a programming interface for HTML and XML documents. It represents the page so that programs can change the document structure, style, and content. The DOM represents the document as nodes and objects. That way, programming languages can connect to the page.&lt;br&gt;
...&lt;br&gt;
The DOM is an object-oriented representation of the web page, which can be modified with a scripting language such as JavaScript.&lt;/p&gt;

&lt;h4&gt;
  
  
  React's Virtual DOM
&lt;/h4&gt;

&lt;p&gt;React uses &lt;a href="https://reactjs.org/docs/faq-internals.html" rel="noopener noreferrer"&gt;virtual DOM(s)&lt;/a&gt;. A virtual DOM simply is a DOM representation in Javascript.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;render()&lt;/code&gt; method does not immediately render to the real DOM. &lt;/p&gt;

&lt;p&gt;Render is in fact, more a suggestion of what the HTML should look like, but &lt;code&gt;render()&lt;/code&gt; can very well be called and be the same as what was already displayed.&lt;/p&gt;

&lt;p&gt;Upon &lt;code&gt;render()&lt;/code&gt; being called, React compares virtual DOMs. It has an old virtual DOM and a re-rendered or a future virtual DOM. By comparing the "old" VDOM with the "future" VDOM, React can determine if there are any differences. If it detects differences, it reaches out to the real DOM and updates it--but not entirely!--it only changes the real DOM in the places where differences were detected.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faiey3drhqloe5cdxx1tm.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Faiey3drhqloe5cdxx1tm.jpg" alt="Alt Text" width="800" height="450"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This is important because as you might know, accessing the DOM is really slow. Specifically, every time the DOM changes, the browser needs to recalculate the CSS, layout, and repaint the page. This takes time and is something you want to do as little as possible.&lt;/p&gt;

&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;React's virtual DOM means speed in most cases and it helps alleviate us from having to think about when to render changes to the DOM and focus more on writing code creatively and solving problems.&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>react</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Jumping into Big O</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sun, 22 Nov 2020 01:59:42 +0000</pubDate>
      <link>https://dev.to/rwparrish/jumping-into-big-o-kn4</link>
      <guid>https://dev.to/rwparrish/jumping-into-big-o-kn4</guid>
      <description>&lt;p&gt;Not all programmers are math people and that is OK. Big O notation can be a little intimidating to approach if you are one of those people. In this blog, I would like to gently introduce you to the concept of Big O notation.&lt;/p&gt;

&lt;p&gt;Here is a &lt;a href="https://rithmschool.github.io/function-timer-demo/" rel="noopener noreferrer"&gt;link&lt;/a&gt; to an awesome widget that Colt Steele created. I highly recommend playing around with it to become more familiar with the concept of Big O. I will be using the same code snippets from the widget in this blog. Check it out!&lt;/p&gt;

&lt;h3&gt;
  
  
  So what is Big O Notation?
&lt;/h3&gt;

&lt;p&gt;It is simply a way for us to talk about the runtime of an algorithm as its input grows. That's it. Big O is expressed as O(n) where n is the size of the input. When input grows, how will that affect the time it takes for the algorithm to run? When discussing Big O notation, we are talking in very broad terms and considering the &lt;strong&gt;worst-case scenario&lt;/strong&gt; - the upper bound for runtime. So what does it mean to consider the worst-case scenario?&lt;/p&gt;

&lt;h3&gt;
  
  
  Simplifying Big O
&lt;/h3&gt;

&lt;p&gt;When considering the worst-case scenario we are thinking about what would happen when the input(n) becomes insanely large. This means constants don't matter and operations like addition, subtraction, multiplication, and division become basically irrelevant. In most cases, we can omit all constants and ignore lower powers of n - for example: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;O(1)&lt;/strong&gt; - O(500), O(10,000), and O(1,000,000,000) all simplify to O(1).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O(n)&lt;/strong&gt; - O(25n + n), O((57n / 3) * n), and O(1,000n) all simplify to O(n).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O(n²)&lt;/strong&gt; - O(35n + n²) simplifies to O(n²).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;O(n³)&lt;/strong&gt; - O(35n + n² + n³) simplifies to O(n³).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Take a look at the graph below. As programmers, we want to stay out of the "bad" and "horrible" zone.&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbnh1qprwovtuk8ipfoxs.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fbnh1qprwovtuk8ipfoxs.jpeg" alt="Alt Text" width="800" height="556"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;O(1)&lt;/strong&gt; - as you can see in the graphic above, as the input grows, runtime remains constant. This is awesome. The algorithm runs quickly regardless of the size of the input. This is the ideal and most operations that perform a single operation fall into this &lt;strong&gt;complexity&lt;/strong&gt; (in Computer Science the performance or runtime of an algorithm is also referred to as its complexity). Pushing to an array, getting an item at a particular index, adding a child element, etc, will take the same amount of time regardless of the array length. Algorithms with O(1) are ideal in their Big O complexity:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpToSecond(n) {
  return n * (n + 1) / 2;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;O(n)&lt;/strong&gt; - as input(n) grows, broadly speaking, runtime increases at a linear rate. This is because there is a 1:1 relationship between data size (input) and runtime. All loops fall into this big O complexity:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function addUpToFirst(n) {
  var total = 0;
  for (var i = 0; i &amp;lt;= n; i++) {
    total += i;
  }
  return total;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Look closely at the two functions above - &lt;code&gt;addUpToSecond&lt;/code&gt; and &lt;code&gt;addUpToFirst&lt;/code&gt;. They accomplish the same thing but have different Big O complexity. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;O(n²)&lt;/strong&gt; - from a worst-case perspective, an algorithm with this complexity will not perform well. As input(n) increases the runtime will increase exponentially - check out the graphic above again. Stay away from nested loops:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function printAllPairs(n) {
  for (var i = 0; i &amp;lt; n; i++) {
    for (var j = 0; j &amp;lt; n; j++) {
      console.log(i, j);
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Recap
&lt;/h3&gt;

&lt;p&gt;In this post, we dipped our toes into the Big O complexity pool. We learned that Big O has everything to do with "measuring" the time it takes an algorithm to run in the worst-case scenario. It is best practice to avoid writing code that includes nested loops and O(1) is the ideal.&lt;/p&gt;

&lt;p&gt;I hope this intro into Big O notation and complexity was gentle, clear, and helpful. There is more to the concept than was covered in this post, but this is a good start.&lt;/p&gt;

&lt;p&gt;Please feel free to school me if you think you can explain something more clearly. Leave feedback and ask questions! Happy coding!&lt;/p&gt;

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

</description>
      <category>algorithms</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Debugging in React</title>
      <dc:creator>rwparrish</dc:creator>
      <pubDate>Sun, 15 Nov 2020 03:46:55 +0000</pubDate>
      <link>https://dev.to/rwparrish/debugging-in-react-3op5</link>
      <guid>https://dev.to/rwparrish/debugging-in-react-3op5</guid>
      <description>&lt;p&gt;Some developers love to do it and some hate that part of the job. One thing is for sure. Every dev has to debug - a lot. So, we may as well learn how to be very effective at it. In this post, I would like to expose you to some debugging tools that are available for use in a React application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Chrome DevTools
&lt;/h2&gt;

&lt;p&gt;From the Chrome DevTools &lt;a href="https://developers.google.com/web/tools/chrome-devtools" rel="noopener noreferrer"&gt;page&lt;/a&gt;, "Chrome DevTools is a set of web developer tools built directly into the Chrome browser." This post will rely on these tools. If you are not familiar with Chrome DevTools yet, you are in for a treat. It offers lots of tools to help developers debug on the fly.&lt;/p&gt;

&lt;p&gt;There are some useful &lt;a href="https://developers.google.com/web/tools/chrome-devtools/shortcuts" rel="noopener noreferrer"&gt;keyboard shortcuts&lt;/a&gt; for launching the Chrome DevTools.&lt;/p&gt;

&lt;h5&gt;
  
  
  Mac:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;command+option+J - to open the DevTools in the &lt;strong&gt;Console&lt;/strong&gt; panel&lt;/li&gt;
&lt;li&gt;command+option+C - to open the DevTools in the &lt;strong&gt;Elements&lt;/strong&gt; panel&lt;/li&gt;
&lt;/ul&gt;

&lt;h5&gt;
  
  
  For Windows:
&lt;/h5&gt;

&lt;ul&gt;
&lt;li&gt;control+shift+J - to open the DevTools in the &lt;strong&gt;Console&lt;/strong&gt; panel&lt;/li&gt;
&lt;li&gt;control+shift+C - to open the DevTools in the &lt;strong&gt;Elements&lt;/strong&gt; panel&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;With that let's start looking at some different ways to debug in React. I'd like to cover three things:&lt;/p&gt;

&lt;h4&gt;
  
  
  1. Error Messages
&lt;/h4&gt;

&lt;h4&gt;
  
  
  2. Tackling Logical Errors
&lt;/h4&gt;

&lt;h4&gt;
  
  
  3. Checking State
&lt;/h4&gt;

&lt;h2&gt;
  
  
  Error Messages
&lt;/h2&gt;

&lt;p&gt;Take a look at the image below:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F613g5l85kom7mg23dtyj.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2F613g5l85kom7mg23dtyj.png" alt="Alt Text" width="800" height="874"&gt;&lt;/a&gt;&lt;br&gt;
React is kind enough to give us some powerful hints right in the browser even pointing to a line number in the code where the error occurred - &lt;code&gt;cannot read property 'value' of undefined&lt;/code&gt; simply means whatever comes before &lt;code&gt;value&lt;/code&gt; on line 28,&lt;code&gt;input&lt;/code&gt; in this case, is &lt;code&gt;undefined&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Below is an example of the same exact error displayed in the Chrome DevTools Console panel:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp3ej1p0e6w4rxytetwsw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fp3ej1p0e6w4rxytetwsw.png" alt="Alt Text" width="800" height="89"&gt;&lt;/a&gt;&lt;br&gt;
Again, notice it is pointing us to line 28 in the &lt;code&gt;App.js&lt;/code&gt; file. Isn't it awesome that we can so quickly pinpoint where the error in our code is!? At this point, we just have to figure out why &lt;code&gt;input&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt;. &lt;/p&gt;

&lt;h2&gt;
  
  
  Logical Errors
&lt;/h2&gt;

&lt;p&gt;Logical errors are more difficult to locate - we get no error messages and the app isn't functioning properly. For this, we can use the DevTools. Head to the &lt;strong&gt;Sources&lt;/strong&gt; panel then find and open the file your code is coming from:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frsvh4wwegqqyf3trbs2v.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Frsvh4wwegqqyf3trbs2v.png" alt="Alt Text" width="800" height="540"&gt;&lt;/a&gt;&lt;br&gt;
In there we can place a breakpoint (by clicking on a line number) where we think the problem is and poke around to see what is happening - walk through the code step by step. We can look at the data:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuuaq7vvaos7td3cbmdl.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxuuaq7vvaos7td3cbmdl.gif" width="600" height="366"&gt;&lt;/a&gt;&lt;br&gt;
Above you can see that &lt;code&gt;p.userId&lt;/code&gt; is &lt;code&gt;undefined&lt;/code&gt; and is being compared to &lt;code&gt;personId&lt;/code&gt;. Found it! The issue lies with &lt;code&gt;p.userId&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Using the Chrome DevTools with Source Maps that are generated for you automatically is a powerful feature for detecting logical errors. You can walk through your code as written by you, even though it is not the code running in the browser.&lt;/p&gt;

&lt;h2&gt;
  
  
  Checking State
&lt;/h2&gt;

&lt;p&gt;You can check the current state of your app in the DevTools but it can sometimes be a bit of pain. Thankfully, there is a handy extension that can be added to Chrome called "React Developer Tools."&lt;/p&gt;

&lt;p&gt;Once installed:&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzo2p6oug3unosbq1atj.gif" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fxzo2p6oug3unosbq1atj.gif" width="800" height="488"&gt;&lt;/a&gt;&lt;br&gt;
I recommend playing around in there to experience everything you can. A few things you can do are: log components to the console, inspect matching DOM elements, inspect the source code, and change state/props to see how things change without having to hardcode anything in your code.&lt;/p&gt;

&lt;h2&gt;
  
  
  Recap
&lt;/h2&gt;

&lt;p&gt;In this post, we learned that React gives us useful error messages in the browser, Chrome DevTools in your friend, and React Developer Tools is great for all things state-related.&lt;/p&gt;

&lt;p&gt;I hope you learned something and as always, leave feedback, ask questions, and share! Happy coding!✌️&lt;/p&gt;

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

</description>
      <category>react</category>
      <category>beginners</category>
      <category>tutorial</category>
      <category>codenewbie</category>
    </item>
  </channel>
</rss>
