Before we dive into debugging and the types of errors aka bugs in a computer program, first we will see what actually a bug is.
Have you heard of errors (bugs) in programming?
A bug is an error or flaw in a computer program due to which the program may produce incorrect results or to behave in unintended ways or may not even execute at all.
While programming every programmer with good intentions will try to write a program which can run as expected without any errors. But, in this process of creating a bug-free (error free) programs, we unintentionally introduce bugs into the program.
There is no program which is bug free all the time. A program may be bug free at the time it was written but as the user base increases at some point of time in the future that bug free program will have bugs in it.
That is the main reason why programmers will be anticipating future errors and will be improving their programs from time to time.
Types of errors in programming
There are 3 types of errors in programming. They are:
- Syntax errors
- Run time errors
- Semantic errors
A syntax error is an error which will occur when there is an error in the program, like maybe you missed a semicolon. When syntax errors are present you cannot even execute your program.
Your programming language’s compiler / interpreter will tell you where the syntax error is in the program along with the file name and the number. These are the easiest to be found.
Below is a gist to demonstrate what a syntax error is. In the gist, on line number 7, you can see that there is no semicolon.
When you try to run this program, you will be shown an error Parse error: syntax error, unexpected ‘echo’ in filename on line 10
A runtime error is the one which doesn’t have any syntax errors but will occur when a program is running. The compiler will tell you about the error and also tell in which file and the stack trace at which the error occurred.
As shown in the below gist, you can see that there is no syntax error in it. But, when you try to run this program you will be shown an error saying that Undefined variable: var_2 along with the filename and line number
A semantic error is an error in which there will be no syntax and runtime errors and your program will also execute. But, the result will not be the one you are expecting.
It is an error which is present in the logic of your program.
As shown in the below gist, you can see that there is no syntax error in it and when you try to execute it, the program will also execute successfully. But, the result is not the one which you might be expecting.
In the gist, as mentioned in the comments, the program should return the sum of other two variables, but the program is actually returning the difference of the two variables.
What is Debugging?
Debugging is the process of finding and resolving existing and potential errors (bugs) in a computer program that cause a program to behave unexpectedly, produce incorrect results and sometimes may crash the system too.
How to debug your program?
The process of debugging will require a programmer to check the code line by line and find where the error is present and then fix bugs present in that part of the program.
Now a days, there are debugger tools and extensions available for developers which will help you in debugging and make your life easy.
You can keep check points, turn on the debugger and then execute the program to check whether the data passed in your program from one part to another part is same as you are expecting.
IDEs (Integrated Development Environment) like VS Code (Visual Studio Code) will come with inbuilt debugger extension, which is Node.js debugger extension. You can install other debugger extensions for different languages and can configure the debugger according to your needs.