The process of correcting our errors makes our program incapable of running. It is a way of making a program free of bugs accessible. Every programmer must debug the code itself whenever they are stuck with any of the errors in the program. There are certain methods of debugging your program but it's your choice if you follow the traditional methods or abide by your tactics and techniques to solve the problems. It's your choice to debug any program in your way. The ways of debugging your program are:
Bruteforce Method
Backtracking
Program slice
Cause elimination method
The above-listed ways are the formal ways that are used by programmers to debug the code. So, let me discuss something about these methods.
- Bruteforce Method:
It is the traditional process of debugging your code by putting some of the print statements to know the intermediate values of the variables by which an error symptom can be sensed and the error can be resolved easily. This is the least used debugging technique as it is a less efficient method. It is used with the hope of getting the printed values that can help us to trace the error statements. Some programmers still use it. I sometimes use it to debug the code.
It becomes more specific and systematic in the case of the source code debugger as it is used to debug the code by using some of the breakpoints. The values of the variables can easily be checked and the breakpoints help to test the values of the variable.
Backtracking:
It is another process of debugging your code. It is the most common method of debugging your code. During this technique starting from the place at which the error symptom is sensed, the source code is derived backward until the error statement is countered and corrected. But this technique fails to find the error in the case of the program with more lines of code. More lines of code can lead to more backtracking functions which limit the use of this technique. This technique is more suited for a program with fewer lines of code.
Program Slice:
As the name depicts that the slicing is done first then try to counter the bugs and errors in your program. You can see an example
<?php // Start the session and get the data session_start(); if (isset($_SESSION['username'])) { echo "Welcome".$_SESSION['username']; echo "<br> Your favourite category is ".$_SESSION['favcat'] ; echo "<br>We have saved your session"; } else { echo "Please login to continue"; } ?>
Here this is a program in PHP to start a session for your login system. So if you have these types of programs then you can slice the program like this to counter the errors easily but I think here there is no error in this program. But let me explain here. Here is the first slice:
<?php // Start the session and get the data session_start(); if (isset($_SESSION['username'])) { echo "Welcome".$_SESSION['username']; echo "<br> Your favourite category is ".$_SESSION['favcat'] ; echo "<br>We have saved your session"; }
Here is the second slice:
else { echo "Please login to continue"; } ?>
here actually slicing helps us to reduce the search space . It is similar to backtracking but here is a slice of a program for a particular variable is the set of source lines that are proceeded by the statement and this influences the value of the variable.
Cause elimination method:
In this method of debugging, The possible errors are listed that can cause the program to stop executing or the previously encountered errors are listed at once place and it is tried by the programmer to not repeat the same type of errors that he has addressed already. A connected error from the before listed errors can be addressed in a similar manner that he has used before. And this type of technique can also be termed fault tree analysis.
Now, I have summarized the regular techniques of debugging.I am currently working with PHP right so I used to debug the errors actually using a function that is used mostly during MySQL transactions i.e mysqli_error().This function is actually the best function for programmers to know whether there is any error as mostly it is the hint that it provides before deployment into an actual server that here the variable is not global or the SQL script may be wrong etc . Different types of errors can easily be addressed.
mysqli_connect_error() is used to check whether the connection with the MySQL server is there or not. Let me explain it using an example:-
<?php
// Connecting to the database
$servername="localhost";
$username="root";
$password="";
//Create a connection object
$conn = mysqli_connect($servername,$username,$password);
//die if connection was not successful
if(!$conn){
die("Sorry we failed to connect:".mysqli_connect_error());
}
echo "Connection was successful";
?>
here you can observe that I have used mysqli_connect_error() to address if there is any error with the connection with the MySQL server.
Now, let me take another example:-
<?php
//Create a db
$sql= "CREATE DATABASE dbswarup";
$result=mysqli_query($conn,$sql);
//check for the database creation success
if($result){
echo "The db was created successfully";
}
else {
echo "The db was not created successfully because of this error". mysqli_error($conn);
}
echo "The result is";
echo var_dump($result);
echo "<br>";
?>
Here mysqli_error() is used to address the errors if there is any error such as:if we are executing a script to create a database but the database with a similar name is present already then the function mysqli_error() will print that already a database is there so change the name of the database. Another function is var_dump($variable) is actually used to know the type of data that is present within the variable and by knowing the type of data we can address the error that is there with the program.
CONCLUSION:
Errors and Bugs are part of a programmer it is actually the driver to convert a beginner to a legend or a pro programmer. So, Debugging is a habit that should be there in every programmer and the programmer must be hungry enough to gain enough knowledge about the preferred programming language and debug their code themselves.