• 20 hours
  • Easy

Free online content available in this course.

course.header.alt.is_video

course.header.alt.is_certifying

Got it!

Last updated on 6/17/24

Debug your function

What is debugging?

De-bug(g)-ing- is as you can see, literally means to eliminate bugs.

And what are bugs?

They aren't the little critters running around outside! Bugs are algorithmic and business logic mistakes in a program.

The origin of bugs
The origin of bugs

This is a cute story; however, software bugs are still pretty annoying. They are equally annoying if they were planted by you or other developers! 😉

Bugs are not compilation errors - at least those are always right in your face, and you know where to find and fix them even if you don't always know how.

Let's reiterate. Bugs are either algorithmic or business logic mistakes. They can cause crashes or produce incorrect results. Incorrect results are often more dangerous than crashes as they may continue to happen silently and without an immediate effect. 😱

There are a number of approaches to detect and eliminate bugs:

  • Random checks - poking around.

  • Using debugging tools.

Random checks

This is the tried and true method which is effective when you need to fix simple issues. And let's face it, you'll use it all the time for quick checks, but not as a tool to ensure the code quality. The most common way of implementing this method is using the Console.WriteLine function.

As an example, let's say you are developing a function that converts kilograms to pounds and then utilize it within the logic of your app:

class MagicWeight
{
   public static double KgToLb(double kg)
   {
      double k = 2.20462262185;
      double lb = kg / k;
      return lb;
   }
}

double weightInKg = 50.0;
double weightInLb = MagicWeight.KgToLb(weightInKg);

At some point, using the result of this function, you may suspect that something is not quite right. So you'd want to verify the value this function returns.

The most popular way of doing a quick check is using the Console.WriteLine function. So let's display the result after we've called the function:

class MagicWeight
{
   public static double KgToLb(double kg)
   {
      double k = 2.20462262185;
      double lb = kg / k;
      return lb;
   }
}

double weightInKg = 50.0;
double weightInLb = MagicWeight.KgToLb(weightInKg);
Console.WriteLine(weightInKg + " kg equals " + weightInLb + " lb"); // -> 50.0 kg equals 22.679618499987406 lb

Well, that doesn't look right 🤨.

Next, you may want to get closer to the calculation and log the values within the calculating function:

class MagicWeight
{
   public static double KgToLb(double kg) {
      double lb = kg / 2.2;
      Console.WriteLine("Converted " + kg + " kg to pounds and the result is " + lb + " lb") ;
      // -> Converting 50.0 kg to pounds and using conversion coefficient 2.20462262185 and getting the result 22.679618499987406 lb
      return lb; 
      }
}

double weightInKg = 50.0;
double weightInLb = MagicWeight.KgToLb(weightInKg);
Console.WriteLine(weightInKg + " kg equals " + weightInLb + " lb");

Here you can expose an additional value being used for the calculation, k, just to make sure it contains the correct value. The new output also shows a suspicious result. This allows you to conclude that you must look for an error within the calculated function. Of course, it's using division instead of multiplication. Let's fix that!

class MagicWeight
{
   public static double KgToLb(double kg)
   {
      double k = 2.20462262185;
      double lb = kg * k;
      Console.WriteLine("Converted " + kg + " kg to pounds and the result is " + lb + " lb"); 
      // -> Converting 50.0 kg to pounds and using conversion coefficient 2.20462262185 and getting the result 110.23113109250001 lb
      return lb;
    }
}

double weightInKg = 50.0;
double weightInLb = MagicWeight.KgToLb(kg: weightInKg);
Console.WriteLine(weightInKg + " kg equals " + weightInLb + " lb"); // -> 50.0 kg equals 110.23113109250001 lb

Now that looks like the right number!

You can see that having the values of variables logged during different steps of the instructions in the program helps you see the state of those values. You can then trace the mistake to its roots and fix it.

Using debugging tools

Debugging tools and processes depend on the programming language, platform, and environment you are utilizing. However, these tools share many common concepts.

The development tools

Visual Studio is the most common integrated development environment (IDE) for C# and has a free edition available on the internet, Visual Studio Code.

The process of debugging means going through the sequence of instructions step-by-step revealing the current state of the variables in scope. This refers to the variables declared within the code block which you are currently inspecting (during debugging). And revealing means that you can see the values of these variables at that moment. As you can imagine, it's a more complete way of checking out more complex problems.

Let's recap!

In this chapter, you learned the following:

  • Bugs are algorithmic and business logic mistakes.

  • Debugging is the process of detecting and eliminating bugs.

  • The most common ways to detect bugs are:

    • Random checks - good for quick validation.

    • Using debugging tools - for a more targeted precise process.

Example of certificate of achievement
Example of certificate of achievement