Debugging
By now you have probably encountered some bugs in your application. Here’s a few tips and pointers on how to get your programing back to ‘working’.
Types of bugs
-
Syntactical
You typed something wrong.
-
Logical
You think the application should be doing one thing, when, actually, its doing something else entirely.
The developer console
In this class you should always have the developer console open. In chrome its under tools->javascript console.
Fixing syntatical bugs
Firstly, you need to memorize syntax or have a reference if you’re not confident you have it memorized.
- Check the developer console for the error. It will be in red. The error report will give you the exact line to look at. (You can also click on the line number on the far right to be taken (usually) to the line of code)
- Attention to detail (this comes with time)
- Make sure to look through all your code. Oftentimes your error will come from lines before where the exact error is reported.
- Indent your code correctly
Common syntactical errors:
- Opened a curly brace and did not close it in the right location. Look through your code and count opening and closings.
- Opened a string and did not close it. This is highly apparent in an editor because all your subsequent code will be highlighted like a string.
- Missing a closing or opening parenthesis around a loop or logical statment
- Forgetting an equals sign in variable assignment.
- Using a comma instead of a semi-colon inside of a for loop
Fixing logical bugs
Mitigation
Pseudocode
Using comments, write out in eglish the steps your application needs to go through. Here is an example.
Fixing
- Think like the computer Take each action one step at a time Ask yourself, “What happens here.. where does this lead?” If need be, comment out subsequent lines of code so it only goes as far as you are thinking
- Console.log Use console.log to check the value of variable throughout your code. This can also be used to check if a certain bit of code is reached/running
- Comment out as much as possible Go line by line if need be Make sure each step is working
Remember
Debugging is an iterative process. You will often fix errors just to encounter new ones. Don’t get frustrated - every error fixed gets you closer to working.