Hello all!
Fellow coding newbie here sharing some thoughts on debugging. I’d love to hear some of your thoughts on debugging, do you have any ideas, or tips you’d like to share?
De-bugging
Change your mindset
As a beginner to coding languages, you will come across errors and bugs in your code. Bugs and errors do not mean failure, nor do they imply you are bad at coding. It is another challenge to overcome, or opportunity to learn! Debugging is a key skill you will need to develop early. As with any skill, it will improve the more you practice. You should learn to enjoy the process, because the truth is, you will be spending a lot of time doing it.
We can spend hours in a state of frustration trying to get our code to work as we planned. Once our code works, we cheer, we share with our peers, job well done! Then, we are on to the next problem, implementation, or iteration, along with new bugs and errors. Fixing bugs and errors, you learn new things or remind yourself of something you have forgotten.
Types of errors
Before you think about techniques to deal with errors and bugs, you need to be aware of the mistakes you will make.
Syntax errors occur when you make a spelling or grammatical mistake in your code. Your program may crash or not run at all. By misspelling, and missing punctuation you will encounter Syntax errors. Modern code editors will highlight incorrect syntax in your code. This will help you with most common mistakes.
Runtime errors or 'bugs' do not appear until you attempt to run your program. Also called exceptions, the computer does not know how to handle the task defined in your code. An example of a runtime error would be attempting to divide a number by 0. Passing a 'falsy' value to some functions and methods can also result in runtime errors.
Semantic errors can be difficult to find. Your program will appear to run successfully. But the output returned is not what you were expecting. You have provided instructions that the computer understands. These instructions were not the instructions you intended to write. As the programmer, you will need to figure out what the program is doing.
Avoid de-bugging
With proper preparation and by taking things slow, you can avoid a lot of debugging.
First, it is important that you understand what you are trying to achieve. Imagine working toward a goal but you cannot recognise when you have succeeded, how do you know when to stop? You will not need a deep understanding of the entire problem. But you should have a good idea what the output should be, given a particular input. You can then adjust (debug) your program to achieve the correct solution.
Example
Write a program that demonstrates Pythagoras Theorem. This program should take two inputs from the user, a & b. These values represent the length of the sides of a right-angle triangle. It will the return to the user the length of the hypotenuse.
- Receive input values from the user
- Store these values
- Square each value (multiply by self)
- Add values together
- Return square root of sum
Do not get carried away writing line after line of code. Lengthy functions containing calls to functions using various methods are difficult to debug. Start small, this way when your code does go wrong, you know where to begin looking. The last iteration of your code executed as intended, you have added two lines, now the program fails to run. Where did the error come from? Running your code or displaying your web page provides immediate feedback. Achieving small victories helps to keep you motivated. The feeling of defeat and failure is one of the biggest hinderances to your development.
I have written the below code in python3.4, it attempts to solve the problem explained above. The code covers steps 1 – 3 and gives an example of a typical error message. To complete the solution, you will need to import math on line 1. This will give you access to extra mathematical functions. Once the solution is complete think about what else you can add to this program. How could you add logic to allow the user to find length a given b and c?
1 side_a = input(“Side 1 length?”)
2 side_b = input(“Side 2 length?”)
3
4 print(side_a)
5 print(side_b)
6
This iteration of the code covers steps 1 and 2 identified above.
- Receive input values from the user
- Store these values
1 side_a = input(“Side 1 length?”)
2 side_b = input(“Side 2 length?”)
3
4 print(side_a * side_a)
5 print(side_b * side_b)
6
This iteration of the code attempts to multiply each value by itself, then print the result to the user as a test. But, once the user inputs their values, the code returns a runtime error.
Traceback (most recent call last):
File "C:\Users\user\Programming\ex_py_theorem.py", line 4, in
print(side_a * side_a)
TypeError: can't multiply sequence by non-int of type 'str'
Fortunately the error message returned contains enough information to rectify the fault.
1 side_a = int(input(“Side 1 length?”)
2 side_b = int(input(“Side 2 length?”)
3
4 a_squ = side_a * side_a
5 b_squ = side_b * side_b
6
7 print(a_squ)
8 print(b_squ)
9
This resolves the error and allows the code to run. The values received from inputs are string values. We need them to be integer values for mathematical operations. The values are then stored into new variables for clarity and then printed as a test.
With a simple program working, you can begin making improvements . Again, make small adjustments and additions to your code. These changes will help you to move closer to your goal. Each step of the way you should test your changes and inspect the output. Repeating this process helps you to understand the solution to the problem on a deeper level. You will have a better understanding of the structure and flow of your program.
Why I like debugging
I have noticed the more debugging I do, I become better at writing code. It helps me to learn what works and what does not, and highlights areas for more study. The computer is never wrong. I cannot argue with a computer, I must be humble and acknowledge my code is incorrect. It is like playing a video game. I learn how to fail and then how to overcome. Each exercise in debugging scales in difficulty with my ability. The better I get the more challenging the bugs and errors become. I am a detective . Debugging allows me to use a different set of skills and allows me to embrace my inner Sherlock. Observation, deduction, and logical reasoning are all skills that develop with practice.
Tools and Aids
VSCode
This code editor is my favourite so far; it has a huge number of features built in to help you write working programs. Linters, and text highlighting to point out mistakes before you even test. Git revision management tools are very useful with larger projects. There are a lot of great code editors out there, find your favourites and learn to use it well. You may find yourself preferring different editors for different tasks.
DevTools
When working on any web development project, you will want to see the changes you are making. Testing the page and inspecting elements as you build helps you to remain in control. DevTools allows you to change CSS values so that you can see the effects before making changes to your code. DevTools also features a console for testing out JavaScript.
Console logs and print statements
Adding print statements allows you to check that your program is working as you intended. You can print values to the console and check they are as expected. You can also use a function to find the type of a variable, useful for dealing with type errors. You can remove these statements once you have proven the program.
Commenting out code
Commenting out the code in the error message can help you to locate the error. Comment tags tell the computer to ignore lines of code. Once you have an idea where the error lies, you can start investigating. Look at syntax, spelling, and what your program is doing as opposed to what you intend it to do.
Work backward from the error message
The error indicates where the program has stopped. The error will relate to either the line identified, or it will have come before. It is important you understand the flow of your program and follow the function calls. Sometimes an error message will identify an error in one file, but the mistake is in another linked file.
Style Guides
Clean code keeps your code tidy and readable, for yourself and others. The easier your code is to read the easier it is to spot errors and mistakes. Make use of white space and line breaks. Keeping your code tidy will help when you start refactoring to make your code more concise.
Documentation
Programming documentation should become your go to reference whilst writing and debugging. Within documentation you can look up various objects, their methods, and parameters. Not only will this help you fix mistakes but learn things you did not know were possible. Documentation contains syntax rules and code examples, the pages can be daunting. With practice finding the information you need becomes easier.
I hope this post helps fellow beginners with their coding journey. I look forward to hearing your thoughts on debugging. Stay motivated, don't quit, you got this!