Debugging basics
When debugging, it's important to know when to expect errors, what kind of errors to expect, and what to do when errors occur.
This tutorial focuses on:
- When to expect errors
- Have patience
- Type of errors
- Seeking help
When to expect errors
You should expect errors whenever you are writing code (for any language). Computers do exactly as they're told, they don't know if you made a mistake or not. If you made a mistake they function on that mistake assuming that's how it's supposed to be.
Always watch out for errors, but be extra cautious when your code starts getting more and more complex. Complexity increases the chances of errors because more is going on and there is more of a chance of something not working as intended. There is a much higher chance of an error if you have 500 lines of code as opposed to 20.
Have patience
Debugging is not easy and requires patience. As sometimes the errors are obvious and sometimes they are more subtle. You can spend 2 minutes debugging something or you can spend 2 hours. Have patience, you will find those errors and fix them :)
Type of errors
There are several types of errors you will run across when writing code.
-
Syntax errors
Occur when you mistype something in your code that prevents it from working properly such as a missing closing quote, a missing semicolon, or misspelling the name of a command.
-
Runtime errors
Occur when the code is already executed. Example - some code that tries to open a file for the user to read, but there is one problem - the file doesn't exist! -
Logic errors
Occur when there is a mistake in your code that is causing undesired results. Example - you wrote some code that is supposed to calculate 8% sales tax on an item. You run the code and then wonder why the results are wrong. You check the code and see that you have it calculating not 8% but 80% sales tax. Logic errors, like runtime errors, occur when the code is already executed. -
Browser errors
Occur in specific browsers. When a browser is built in a way that conflicts with the way you want something to appear on a webpage. Example - in Internet Explorer 6 there is a problem with the CSS min-height property. The min-height property is supposed to set a minimum height for an element, but this does not work in Internet Explorer 6.
Seeking help
There are several sources from which you can seek help if you're having a hard time debugging.
Yourself
Trust in yourself to be able to find the errors. Check out our finding errors page if you are not familiar with the various methods used to find errors.
Search engines
You can copy and paste the error that's generated and search based on that criteria to see if anyone has discussed that same error on a webpage before. You can also type the name of the language you're working in together with a description of the error. Example - "PHP can't access form data"
Forums
Errors and how to fix them are often discussed on forums, particularly when it comes to errors that derive from the code of web software like ZenCart. If you are dealing with errors in an existing web software, you are most likely to find help in the forums for that particular software. Solutions to other errors can often be found on forums too.
Visit our debugging forums where you can discuss debugging with others.
Co-workers
If you work as a web developer together with other web developers, why not ask your co-workers for some help? Be nice about it - ask them if you can have a bit of their of time on something you're working on, show them where the problem is and it is likely that they will try to help you.