<?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>Forem: charliekroon</title>
    <description>The latest articles on Forem by charliekroon (@charliekroon).</description>
    <link>https://forem.com/charliekroon</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%2F388536%2F39c39819-f3fc-47a0-a7df-e5afe0e46e67.png</url>
      <title>Forem: charliekroon</title>
      <link>https://forem.com/charliekroon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/charliekroon"/>
    <language>en</language>
    <item>
      <title>Closures and callbacks in Javascript</title>
      <dc:creator>charliekroon</dc:creator>
      <pubDate>Mon, 24 Apr 2023 15:27:15 +0000</pubDate>
      <link>https://forem.com/charliekroon/closures-and-callbacks-in-javascript-5d5f</link>
      <guid>https://forem.com/charliekroon/closures-and-callbacks-in-javascript-5d5f</guid>
      <description>&lt;p&gt;&lt;em&gt;In the month of April, I'll publish a small blog series called "Questions from the Past". I'll be going through my old notebooks from my first year as a developer and answering the questions I think are most important. Today we will go into Closures and Callbacks in Javascript.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Closures&lt;/strong&gt;&lt;br&gt;
Closures are functions that have access to variables from an outer function, even after the outer function has finished executing. This might sound a bit confusing, but let's go through it together.&lt;br&gt;
Imagine you have a function that defines a variable, 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;function outerFunction() {
  let outerVariable = "Hiiii!";

  function innerFunction() {
    console.log(outerVariable);
  }

  return innerFunction;
}

const inner = outerFunction();
inner(); // logs "Hiiii!" to the console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;innerFunction&lt;/code&gt; has access to &lt;code&gt;outerVariable&lt;/code&gt;, even though it's not defined inside the &lt;code&gt;innerFunction&lt;/code&gt; itself. This is because &lt;code&gt;innerFunction&lt;/code&gt; closes over &lt;code&gt;outerVariable&lt;/code&gt;, creating a closure that allows it to access the variable's value even after &lt;code&gt;outerFunction&lt;/code&gt; has finished executing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Callbacks&lt;/strong&gt;&lt;br&gt;
A callback is a function that is passed as an argument to another function and is executed at a later time. Here's an example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;function doSomething(num1, num2, callback) {
  let result = num1 + num2;
  callback(result);
}

function logResult(result) {
  console.log(`The result is ${result}`);
}

doSomething(3, 4, logResult); // logs "The result is 7" to the console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, &lt;code&gt;logResult&lt;/code&gt; is a callback function that's passed as an argument to &lt;code&gt;doSomething&lt;/code&gt;. When &lt;code&gt;doSomething&lt;/code&gt; is executed, it performs an operation on &lt;code&gt;num1&lt;/code&gt; and &lt;code&gt;num2&lt;/code&gt; and then calls the callback function, passing in the result as an argument. In this case, &lt;code&gt;logResult&lt;/code&gt; is executed and the result is logged to the console.&lt;/p&gt;

&lt;p&gt;That's all there is to it! Once you get a handle on closures and callbacks, you'll be able to write better and more efficient code.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How does useEffect really work?</title>
      <dc:creator>charliekroon</dc:creator>
      <pubDate>Wed, 12 Apr 2023 14:38:14 +0000</pubDate>
      <link>https://forem.com/charliekroon/how-does-useeffect-really-work-5496</link>
      <guid>https://forem.com/charliekroon/how-does-useeffect-really-work-5496</guid>
      <description>&lt;p&gt;&lt;em&gt;In the month of April I’ll publish a small blog series called “Questions from the past”. I’ll be going through my old notebooks from my first year as a developer and answer the questions I think are most important. Today we will discuss the workings of the &lt;code&gt;useEffect&lt;/code&gt; hook.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;&lt;code&gt;useEffect&lt;/code&gt; is a React Hook that can be both useful and messy at the same time. I know there are already a bunch of blog posts on this, but I want to make it super clear for you when, how and why you can use it. Let's go into it!&lt;/p&gt;

&lt;p&gt;You can use &lt;code&gt;useEffect&lt;/code&gt; to perform various tasks, such as fetching data, updating the DOM, optimising performance or cleaning up resources when the component is unmounted*. &lt;/p&gt;

&lt;p&gt;Let’s take a look at the &lt;code&gt;useEffect&lt;/code&gt; hook, and analyse bit by bit what it does. To begin, here's the basic code for the &lt;code&gt;useEffect&lt;/code&gt; hook:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;useEffect(() =&amp;gt; {
// code to run on every render
return () =&amp;gt; {
// clean up!
}
  }, [dependencyArray]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The dependency array
&lt;/h3&gt;

&lt;p&gt;The dependency array is the second parameter of &lt;code&gt;useEffect&lt;/code&gt;. It’s called literally ‘dependency array’ because this is the array that &lt;code&gt;useEffect&lt;/code&gt; is dependent on. With that I mean that depending on what's inside the dependency array, your &lt;code&gt;useEffect&lt;/code&gt; will either run or not run.&lt;/p&gt;

&lt;p&gt;There are three things that you can do with the dependency array:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Leave it empty.
&lt;code&gt;useEffect&lt;/code&gt; will run only once when the component loads for the first time. This is because there are no dependencies that could trigger a re-run of the code.
&lt;pre&gt;
useEffect(() =&amp;gt; {
console.log("The component was loaded for the first time!")
}, []); 
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Don’t include it at all. &lt;code&gt;useEffect&lt;/code&gt; is called on every single state change.&lt;/p&gt;

&lt;pre&gt;
useEffect(() =&amp;gt; {
console.log("I ran because there was a state change!")
});
&lt;/pre&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Put a variable inside the dependencyArray. &lt;code&gt;useEffect&lt;/code&gt; will run every time that variable changes.&lt;/p&gt;

&lt;pre&gt;
const [dogCount, setDogCount] = useState(0);
useEffect(() =&amp;gt; {
console.log(`There are now ${dogCount} dogs!`)
}, [dogCount]);
&lt;/pre&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The return function
&lt;/h3&gt;

&lt;p&gt;The return function (aka the Clean Up function) is called when the component is about the be removed from the screen, or when the &lt;code&gt;useEffect&lt;/code&gt; needs to cancel the previous effect.&lt;/p&gt;

&lt;p&gt;For example, in this code snippet, the &lt;code&gt;useEffect&lt;/code&gt; will fetch user data every time the value of &lt;code&gt;userId&lt;/code&gt; changes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const [user, setUser] = useState(null);

useEffect(() =&amp;gt; {
  let isRendered = true;

  fetchUserData(userId).then(user =&amp;gt; {
    if(isRendered) setUser(user);
  });

  return () =&amp;gt; {
    isRendered = false;
  }; 
}, [userId]);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the component is first mounted, it uses the &lt;code&gt;fetchUserData&lt;/code&gt; function to get the data from the initial &lt;code&gt;userId&lt;/code&gt;. If the &lt;code&gt;userId&lt;/code&gt; changes while the component is still showing, the &lt;code&gt;useEffect&lt;/code&gt; hook makes sure that component only displays the data for the current &lt;code&gt;userId&lt;/code&gt; and not the old data.&lt;/p&gt;

&lt;p&gt;When the &lt;code&gt;fetchUserData&lt;/code&gt; function returns with the user data, it uses &lt;code&gt;setUser&lt;/code&gt; to update the component with the new data. This makes sure that the component always shows the correct user data.&lt;/p&gt;

&lt;p&gt;So, to sum it up: if you're using React, it's important to know how &lt;code&gt;useEffect&lt;/code&gt; works, the dependency array, and the return function. I know it can be confusing at first, but once you get the hang of it, it can really help make your React app better and faster. &lt;/p&gt;

&lt;p&gt;&lt;em&gt;*Component mounting is when React creates and adds a new component to the page for the first time. It sets up the component's initial state, properties, and children, and assigns it a unique identifier. After it's mounted, it can be updated with new information.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>react</category>
      <category>frontend</category>
      <category>beginners</category>
    </item>
    <item>
      <title>What is hoisting in Javascript?</title>
      <dc:creator>charliekroon</dc:creator>
      <pubDate>Thu, 06 Apr 2023 15:50:34 +0000</pubDate>
      <link>https://forem.com/charliekroon/what-is-hoisting-in-javascript-4ppp</link>
      <guid>https://forem.com/charliekroon/what-is-hoisting-in-javascript-4ppp</guid>
      <description>&lt;p&gt;&lt;em&gt;In the upcoming three weeks, I’ll publish a small blog series called “Questions from the past”. I’ll be going through my old notebooks from my first year as a developer and answer the questions I think are most important. For this first blog in the series, let’s discuss the concept of hoisting in Javascript.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Despite its name, hoisting in Javascript has nothing to do with lifting something up or moving something around. All it means is that JS sets up memory space for variables and functions that you create in your code. This way, when your code starts running and gets executed line by line, JS can access these variables and functions.&lt;/p&gt;

&lt;p&gt;However, when Javascript sets aside that memory space, it doesn’t know what the value of the variables and functions will be. Therefore, it sets a placeholder called undefined.&lt;/p&gt;

&lt;p&gt;What’s interesting about hoisting is that the Javascript engine may execute your code in a different order than the way it’s written. That’s because it can access variables and functions that were hoisted before they were even declared in the code. But since the value of hoisted variables and functions is undefined, relying too heavily on hoisting can lead to unexpected results.&lt;/p&gt;

&lt;p&gt;Let’s take a look at this code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;console.log(a)
console.log(b)

var a = "I am a variable!"

function b() {
  console.log("I am a function!")
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In most programming languages, you might expect an error as soon as you try to run this code, but with Javascript, it’s different. Because the memory space is already set aside for the variable and the function before the code is executed, it will not throw an error. What it will do is return undefined. So, while you can technically call functions and variables that are declared later, it’s generally not a good idea to rely too much on hoisting.&lt;/p&gt;

&lt;p&gt;So, in summary:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Hoisting in Javascript simply means JS sets up memory space for variables and functions that you create in your code. This way, when your code starts running and gets executed line by line, JS can access these variables and functions.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Variables and function declarations can be hoisted.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It’s best to declare variables and functions before accessing them. This way you’ll avoid weird bugs and undefined warnings in your console.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>beginners</category>
      <category>programming</category>
    </item>
    <item>
      <title>How to test with RSpec: an extensive beginners guide</title>
      <dc:creator>charliekroon</dc:creator>
      <pubDate>Fri, 03 Mar 2023 18:10:41 +0000</pubDate>
      <link>https://forem.com/charliekroon/how-to-test-with-rspec-an-extensive-beginners-guide-93d</link>
      <guid>https://forem.com/charliekroon/how-to-test-with-rspec-an-extensive-beginners-guide-93d</guid>
      <description>&lt;p&gt;I had only been coding for a year when I started working at HackerOne. At HackerOne, we primarily use Ruby on Rails on the backend. However, I only knew JavaScript and TypeScript, so I had to learn Ruby on Rails. On top of that, I had to learn how to write tests with RSpec. Nothing confused me more than the official RSpec documentation, and there was little online content. Because I don’t want you to struggle as I have, I’ve written a beginner’s guide to help you get started with RSpec.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is RSpec&lt;/strong&gt;&lt;br&gt;
RSpec is a testing framework used to test Ruby on Rails code. It is designed for behavior-driven development (BDD), which originates from Test Driven Development (TDD). In simpler terms, RSpec allows you to write tests that describe how you want your code to behave. RSpec is intended to test the backend of your app only, as it is solely Rails-focused.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What to test for&lt;/strong&gt;&lt;br&gt;
Before writing any tests, it’s important to know what to test for. A good rule of thumb is to always look at the implementation first, as it provides guidance when writing a test. As a general rule, you should always test for the following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happens in the happy path?&lt;/li&gt;
&lt;li&gt;What happens when I create an error or add invalid input? (check the validation)&lt;/li&gt;
&lt;li&gt;Am I the right person to make this action? (check the authorization)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, imagine we would be creating a second Instagram app. One of the most important models for that app would be the user model. Before we get into testing with Rspec, let’s write a very simple user model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# models/user.rb
class User &amp;lt; ApplicationRecord
  validates :name, presence: true
  validates :email, presence: true, uniqueness: true
  has_secure_password
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Great! Now that we’ve got our user model we are ready to write our test. Remember what we just spoke about? To know what we want to test for we want to look at our implementation first. If you had to explain in human language what our model does, you could say that:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;it is valid when it has a name, email, and password&lt;/li&gt;
&lt;li&gt;it is invalid without a name&lt;/li&gt;
&lt;li&gt;it is invalid without an email&lt;/li&gt;
&lt;li&gt;it is invalid without a password&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Without realizing it, you already wrote half of our test. Let’s finish writing the test that covers the requirements we just described:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# spec/models/user_spec.rb
require 'rails_helper

RSpec.describe User, type: :model do
  it 'is valid with a name, email, and password' do
    user = User.new(
      name: 'Tanya McQuoid',
      email: 'tanya@thewhitelotus.com',
      password: 'yougotthis'
    )
    expect(user).to be_valid
  end

  it 'is invalid without a name' do
    user = User.new(name: nil)
    user.valid?
    expect(user.errors[:name]).to include("can't be blank")
  end

  it 'is invalid without an email' do
    user = User.new(email: nil)
    user.valid?
    expect(user.errors[:email]).to include("can't be blank")
  end

  it 'is invalid without a password' do
    user = User.new(password: nil)
    user.valid?
    expect(user.errors[:password]).to include("can't be blank")
  end

  it 'is invalid with a duplicate email address' do
    User.create!(
      name: 'Tanya McQuoid',
      email: 'tanya@thewhitelotus.com',
      password: 'yougotthis'
    )
    user = User.new(
      name: 'Shane',
      email: 'tanya@thewhitelotus.com',
      password: 'pineappleroom'
    )
    user.valid?
    expect(user.errors[:email]).to include('has already been taken')
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let’s break down step by step what we just wrote.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;'require 'rails_helper'&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
RSpec needs to know where your Rails app is and where to find its components. With &lt;code&gt;'require 'rails_helper'&lt;/code&gt; we are loading the Rails application environment, and require access to the rails app.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;describe&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;describe&lt;/code&gt; outlines the general functionality of the class or feature you're testing. The &lt;code&gt;describe&lt;/code&gt; method has a set of expectations, for example, what a model should look like.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;it&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
With every line that starts with &lt;code&gt;it&lt;/code&gt; a new, individual test case begins. &lt;code&gt;it&lt;/code&gt; takes a string argument that describes what the test is testing.&lt;/p&gt;

&lt;p&gt;If you want to temporarily exclude a test example for your test without deleting it, you can use &lt;code&gt;xit&lt;/code&gt; instead of &lt;code&gt;it&lt;/code&gt; .&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;User.create&lt;/code&gt; vs. &lt;code&gt;User.new&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
In Ruby on Rails &lt;code&gt;User.new&lt;/code&gt; and &lt;code&gt;User.create&lt;/code&gt; are both methods that can be used to create a new instance of the User class. Though they are similar, they do work slightly differently. &lt;code&gt;User.new&lt;/code&gt; creates a completely new instance of the User model, but does not save it to the database. To save it to the database, we need to run a separate user.save command. With &lt;code&gt;User.create!&lt;/code&gt; you create a new user record and immediately save it to the database in one go.&lt;/p&gt;

&lt;p&gt;Within the block &lt;code&gt;it "is invalid with a duplicate email address"&lt;/code&gt; we are testing if the email already exists. First, we create a new user record and save it directly to the database with the &lt;code&gt;User.create!&lt;/code&gt; command. The &lt;code&gt;!&lt;/code&gt; at the end of the method will help us by raising an error if the user can’t be saved.&lt;/p&gt;

&lt;p&gt;Next, we use &lt;code&gt;User.new&lt;/code&gt; to create a new instance of the User model with a different name and password, but with the same email. We don’t use &lt;code&gt;User.create!&lt;/code&gt; here, because we don’t want to actually save the record. We are only interested in testing its validation.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;expect&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
The &lt;code&gt;expect&lt;/code&gt; method allows us to make assertions about the expected code behavior. For example, by writing, &lt;code&gt;expect(user).to be_valid&lt;/code&gt; we are making the assertion that the user object is valid.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional things&lt;/strong&gt;&lt;br&gt;
When starting out with RSpec, there were things that confused me, and that I couldn’t find a lot of information about online. We didn’t use them in our code example earlier, but I think they are important to discuss. Let’s take a look at them together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;describe&lt;/code&gt; vs. &lt;code&gt;context&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
In RSpec, &lt;code&gt;context&lt;/code&gt; is a synonym for describe. The only difference between the two is that &lt;code&gt;context&lt;/code&gt; is meant to indicate that the tests inside it are related to a specific context, while &lt;code&gt;describe&lt;/code&gt; is more general. Some people prefer to use &lt;code&gt;context&lt;/code&gt; when they're describing a specific scenario, while others use &lt;code&gt;describe&lt;/code&gt; for everything. It's mostly a matter of personal preference.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;subject&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;subject&lt;/code&gt; lets you declare a single test subject, that is reused throughout our tests. For example, we might write:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RSpec.describe User do
  subject { User.new(
    name: 'Tanya McQuoid', 
    email: 'tanya@thewhitelotus.com', 
    password: 'yougotthis') }

  it 'is valid with valid attributes' do
    expect(subject).to be_valid
  end

  it 'is invalid without a name' do
    subject.name = nil
    expect(subject).to_not be_valid
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, we are using &lt;code&gt;subject&lt;/code&gt; to create a new valid User object that will be used in our tests. We can then reference &lt;code&gt;subject&lt;/code&gt; in our test assertions without having to create a new User object each time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;before&lt;/code&gt; and &lt;code&gt;after&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;before&lt;/code&gt; and &lt;code&gt;after&lt;/code&gt; are hooks that allow you to run your code before and after each example in your test. The &lt;code&gt;before&lt;/code&gt; hook is typically used to set up data or variables that are required for your test examples to run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RSpec.describe User do
  before do
    @user = User.create(
      name: "Armond", 
      email: "armond_themanager@thewhitelotus.com", 
      password: "password")
  end

  it "validates the presence of a name" do
    @user.name = nil
    expect(@user).to_not be_valid
  end

  it "validates the presence of an email" do
    @user.email = nil
    expect(@user).to_not be_valid
  end
end
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;after&lt;/code&gt; hook is used to clean up (e.g. deleting database records or closing network connections) the data that was created during our test.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;let&lt;/code&gt; and &lt;code&gt;let!&lt;/code&gt;&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;let&lt;/code&gt; is an RSpec method that is lazily defined. Meaning that this method won't be assigned before it's called somewhere in the test. &lt;code&gt;let&lt;/code&gt; memoizes the content within its block, which means it’s not going to change the context and it's only going to call and evaluate the context once.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;let!&lt;/code&gt; is not lazily defined and thus once you define it, the method will be created immediately, even though it's not being called.&lt;/p&gt;

&lt;p&gt;We are using &lt;code&gt;let&lt;/code&gt; by default, but when you’re testing multiple things, and you need to update that test variable multiple times with different expectations, you can use &lt;code&gt;let!&lt;/code&gt;. Then again, a counterargument is that the thing you’re testing is deserving of its own test case.&lt;/p&gt;

&lt;p&gt;RSpec is a super powerful tool, but it can be overwhelming at first. It will take some time to learn how to use the framework but by practicing you can learn how to use it effectively. But once you get used to it, writing tests with Rspec will feel like second nature.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;What are your experiences with RSpec? How did you learn to use it effectively? And what are your approaches to writing great specs with it?&lt;/em&gt;&lt;/p&gt;

</description>
      <category>bluesky</category>
      <category>discuss</category>
      <category>socialmedia</category>
    </item>
    <item>
      <title>How to think together; a way to better pair programming sessions</title>
      <dc:creator>charliekroon</dc:creator>
      <pubDate>Fri, 03 Mar 2023 16:37:23 +0000</pubDate>
      <link>https://forem.com/charliekroon/how-to-think-together-a-way-to-better-pair-programming-sessions-57ah</link>
      <guid>https://forem.com/charliekroon/how-to-think-together-a-way-to-better-pair-programming-sessions-57ah</guid>
      <description>&lt;p&gt;Software Engineering relies heavily on communication with others; teammates, product owners, designers, engineering managers, stakeholders. Yet so often we are trained to work and think alone. This is a shame because working together can lead to higher quality code, a better product, and — in the end — a happier end user.&lt;/p&gt;

&lt;p&gt;Software Engineers can collaborate in many ways and forms. One of them is Pair Programming. Pair programming is a software development technique where two developers work together on one machine.&lt;/p&gt;

&lt;p&gt;Though this sounds easy, it is not that simple. Pair programming involves a lot of communication and collaboration. It requires empathy and interpersonal skills and it can be difficult to manage a successful session. Pairing is an amazing tool, but it is a tool that we need to practice together before we can reap its benefits.&lt;/p&gt;

&lt;p&gt;Let’s go over some ways to learn how to think together.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Set a clear goal and remember it&lt;/strong&gt;&lt;br&gt;
At the start of a pairing session, you need to discuss the scope of the session. What is it you want to achieve? When is that goal met? How much time do we want to spend? Make it clear for both of you what it is you want to get out of the session before you start.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Your way is not the only way&lt;/strong&gt;&lt;br&gt;
There are thousands of ways to look at a problem, implement code, or debug something. The key is that we must never think our way is the only correct way. Pairing is not a race where only one person can win. You both want to solve a problem, which is finishing the task you’re working on. You’re both on the same team. Take time to analyze your colleague’s solution before pushing yours onto them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be aware of the knowledge gap&lt;/strong&gt;&lt;br&gt;
One of my early pairing experiences was with a programmer who was much better than me. They bulldozed through the problem and seemed to have forgotten there was a huge knowledge gap between us. Seeing this much more experienced programmer taking up all the space made me feel intimidated and I got scared to speak up, ask questions, and share ideas.&lt;/p&gt;

&lt;p&gt;Though it’s understandable that I shut down, it’s unnecessary. I learned later on that — when my pair has more experience on a topic — asking questions is beneficial for both of you. It can lead to fruitful discussions and better solutions. Even though your co-worker has more experience, explaining certain topics is just as valuable to them as it is to you.&lt;/p&gt;

&lt;p&gt;When you’re the more experienced one in a pair, don’t assume that the other person can’t contribute. You might be wearing blinders when approaching a problem, and a different view can lead to a better solution.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Reflect&lt;/strong&gt;&lt;br&gt;
Pair programming can be mentally exhausting and draining. Reflect during the session to assess if the pace is manageable and sustainable. If it’s not, adjust accordingly.&lt;/p&gt;

&lt;p&gt;When we learn how to explain ourselves, work well together, listen actively and treat each other with respect and kindness, we can get the most out of pair programming. It’s just a skill that we need to learn, that’s all.&lt;/p&gt;

</description>
      <category>bluesky</category>
      <category>discuss</category>
      <category>socialmedia</category>
    </item>
    <item>
      <title>A step-by-step guide on debugging</title>
      <dc:creator>charliekroon</dc:creator>
      <pubDate>Fri, 23 Dec 2022 16:59:15 +0000</pubDate>
      <link>https://forem.com/charliekroon/a-step-by-step-guide-on-debugging-48bn</link>
      <guid>https://forem.com/charliekroon/a-step-by-step-guide-on-debugging-48bn</guid>
      <description>&lt;p&gt;When I started working as a Software Engineer 2.5 years ago, bugs were intimidating to me. I did not know where or how to start debugging and it felt like an enormous task I would never be able to tackle alone.&lt;/p&gt;

&lt;p&gt;I often wished for a step-by-step guide on how to approach the debugging process. I never found one, so I decided to create my own.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 1&lt;/strong&gt;&lt;br&gt;
Slow down and be methodical&lt;br&gt;
When debugging, it’s of great importance that you work as methodically and cleanly as you possibly can. Change one thing at a time, write down the steps you’re taking, and note down your thoughts. By working as clean and controlled as possible, you avoid digging yourself into a hole of bugs, and it’s less likely that you’ll introduce a new bug by making an unnecessary change.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 2&lt;/strong&gt;&lt;br&gt;
Investigate&lt;br&gt;
To solve a bug, you need to understand it. To understand the bug, you need to ask yourself a lot of questions, such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Is the code being called?&lt;/li&gt;
&lt;li&gt;Am I missing a file?&lt;/li&gt;
&lt;li&gt;Is this route correct?&lt;/li&gt;
&lt;li&gt;What does that parameter do?&lt;/li&gt;
&lt;li&gt;What is the value of that variable?&lt;/li&gt;
&lt;li&gt;Is the function in the server, in the client, or, — when debugging a test — in the test?&lt;/li&gt;
&lt;li&gt;Did I introduce the bug with my latest change, or was it there all along?&lt;/li&gt;
&lt;li&gt;Is that function doing what I think it’s doing?&lt;/li&gt;
&lt;li&gt;Am I working on the correct file?&lt;/li&gt;
&lt;li&gt;Which function is returning that incorrect value?&lt;/li&gt;
&lt;li&gt;What is the error message actually telling me?&lt;/li&gt;
&lt;li&gt;Is this happening in my code or in the library that I am using?&lt;/li&gt;
&lt;li&gt;Can I write a test to reproduce the bug?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 3&lt;/strong&gt;&lt;br&gt;
Trust, but verify&lt;br&gt;
Oftentimes I make assumptions that I wholeheartedly believe and don’t even think of questioning. I tell myself things like: ‘I wrote the variable name correctly’, ‘the bug was introduced by the last change I made’ or ‘this function does X’. Hours go by until I figure out that that variable name had a typo, that the bug was always there, but my last change had brought it to the surface, or that the function was, in fact, not doing X. You can trust your assumptions, but verify them.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Step 4&lt;/strong&gt;&lt;br&gt;
Set a timer and ask for help&lt;br&gt;
The difference between working in a team with other engineers, and working on your personal projects, is that there is no unlimited amount of time to spend on bugs. When you put your name on a ticket in the sprint, you basically say: ‘hey, I take responsibility for this issue. I’ll make sure it’s finished.’ Taking that responsibility doesn’t mean that you need to do everything alone. It means you’ll finish the issue and make it ready to be shipped to production in a timely manner. Sometimes that means that you need the expertise from someone else.&lt;/p&gt;

&lt;p&gt;A way to find the balance between asking immediately for help and trying to fix an issue alone for days is to set a timer for an hour. If the bug is still not fixed by then, you can message your coworkers.&lt;/p&gt;

&lt;p&gt;Here is a template of the message you can send:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is what I am trying to do&lt;/li&gt;
&lt;li&gt;I did … and I expected … to happen, but instead … happened&lt;/li&gt;
&lt;li&gt;I have tried …, … and … to fix it, but …&lt;/li&gt;
&lt;li&gt;I think the reason why this is happening is that…&lt;/li&gt;
&lt;li&gt;This seems to be possible/impossible, because …&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Step 5&lt;/strong&gt;&lt;br&gt;
Take a break&lt;br&gt;
Investigating a bug requires a lot of focus. The longer you work on a bug, the more likely it is that you’re missing things. Take a break, go outside, cook, clean the kitchen, take a shower, work out, hang out with your friends, take a nap, watch The Office. When you come back to your bug, chances are that everything is a lot clearer.&lt;/p&gt;

&lt;p&gt;There is always a logical reason behind a bug. Sure, some bugs take a bit more time to figure out, but nothing is ever unexplainable and thus solvable. The nastier the bug is, the better of an engineer you’ll become, I promise.&lt;/p&gt;

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