Debugging on your machine
When you are writing code on your machine, and you want to debug an issue, try all of these:
Do not skip logs and just come to the conclusion “its not working”. Logs will, in 80% of the times contain crucial information. Make sure you start checking from couple of lines before the actual error line.
Google the error
This is very important, 90% of the times someone else has faced this issue and probably already resolved it. Try to google the main error line and read out at least 4–5 different pages. Sometimes an issue can be caused due to multiple reasons and the first solution might not be the solution you are looking for.
While doing this, make sure you remove the non-searchable text from the error line. This can include:
- Directory paths
If no results come up, try to trim down the error line you are searching by removing unimportant words.
Check Stack trace
If the above two attempts didn’t work out, try to find the stack trace and reach the actual line of code where the error happens. This will help you narrow down the number of possible issues of the error.
If this is not a fatal error (or even if it is), meaning the error happens in run time and just the outcome is wrong, the app doesn’t crash, the next thing you should try is running the app in debug mode. This might not be possible for all languages but most languages have a way to debug. The debug feature can also depend on the IDE you are using.
Put break points in and around the lines where the error occurs and check for the parameters and arguments (all related variables). If you figure out the variable which has the wrong variable, follow it back to where it originated.
This will require you to follow this cycle
- Break at a line
- Find the variable
- Find how this variable formed (params/args)
- Find the caller of this function, go to step 1
Not debugging on your machine
There are plenty of times when the issue does not come in your local development environment, but only in a sandbox or production machine. To debug such issues
Couple of language provide this nice feature of remote debugging where you can connect to a process on a remote machine. If possible, try to do this. I have only tried this with Java and it works very well. You will need to start the jvm with some special command line args, so this is possible only if you have the host machine in control.
If all of the above ways fail, this is one of the last option. You will still need to narrow down to the line where the error is possible happening and add print statements before them. Add as many print statements as possible so that you can follow the execution of a single thread.
Last option: hit and try
This will work very rarely, although it might give you good hints as to where you should look for the error. This way involves changing the initial variables randomly and try to observe the patterns of failure. If nothing else, this will help in narrowing down to the root cause.
Still not solved
This is a good time to call one of your colleague or hit up stackoverflow. Make sure you include as much information you got from all the previous steps.
Debugging is a acquired skill and you will get good at it as much as you practice.