Debugging your code as a beginner

Debugging your code as a beginner

ยท

3 min read

Bugs are annoying, more frustrating if you are a beginner, sometimes they make you feel like you can never be enough or do not understand the subject matter but this is not true. In fact having code errors simply mean you are making progress, the more you know the more complicated your code error. In this article, you will learn more about errors and why they ain't that bad after all.

What do I have bugs in my code ๐Ÿ˜ข

Errors are the mistakes in our code that causes our program to behave unexpectedly, Programming errors are known as bugs and the process to remove them from a program is known as Debug/Debugging. There are several errors in different languages but In this article, I will be explaining the three common errors in programming. They are:

  1. SyntaxError
  2. TypeError
  3. ReferenceError.

SyntaxError

SyntaxError occurs when the compiler cannot interpret the code due to a typo or mistake in the code. This makes the code invalid. Major causes of syntax error include missing parentheses, missing brackets or semicolon, missing string etc. An example is displayed in the image below.

js1.PNG

TypeError

TypeError occurs when an operation cannot be passed because the value isn't the expected type, see the example below

js2.PNG

ReferenceError

Reference Error occurs when a variable or function in the code doesn't exist. in this case, you will need to declare the variable

js3.PNG

How to fix bugs in code.

  1. Firstly read the error message, taking a look at the image in the reference error above, we can see that the error is on line 94 and the error message is that drink isn't defined. Knowing how to read an error message is the first step towards debugging.

  2. Go to the line of error and try to find out what is happening.

  3. Fix the bug and run your code again. If you experience difficulty while fixing it, do a Google search or check Stackoverflow

  4. Repeat steps until you have eliminated all the error.

Sometimes our code is broken, it doesn't display any error but it isn't just giving the right values, to solve this it is advisable to print the statement line by line and check the expected value. Doing this makes us know the line where the bug is, this makes debugging a lot easier.

PS: If you know any other way to debug faster, I will like to know. Kindly drop your suggestions in the comment section.