Debugging 101: 9 steps to stress free coding!

Russell Japheth
PH DevConnect
Published in
3 min readJun 19, 2017

In this post, we’re going to review some guidelines that can make debugging easier (or at least less traumatic or terrifying whatever word suits you) or even help you avoid bugs in the first place.

  1. Every bug or error has a cause. Yes, the very first rule in debugging (in my rule book of course) is: Every bug has a cause. no bug magically appears, there are no gremlins that create them while you’re asleep.
  2. Follow best practices. more often than not, following best practices can save you a lot of stress especially when you’re upgrading or integrating with another project.
  3. Test your code often. Don’t wait until you’ve written hundreds of statements before you test your code. Testing it after a few statements or changes will make it easier to spot and resolve new errors. And remember to always use comments, it’ll help you remember what you we’re doing.
  4. Read the error message. At times once you get an error notice, you race off to your code looking for errors without first reading through the error message, needless to say, this approach (which I must admit I’m also guilty of) is counter-productive, as you’ll probably end up going in circles or even causing more errors. So remember: always read through the error message first.
  5. Get a second opinion. At times it helps to get a second opinion and by that, I don’t necessarily mean asking someone else, you could try a different method of doing the same thing, this often helps in spotting the problem. Or if for instance your program to insert a couple of records in a database doesn’t work, you could try accessing the database from a database administration software like phpmyadmin for mysql and sqlite db browser for sqlite just to be sure.
  6. Use the internet. More often than not the error you encountered or a variation of it already has a solution online. It could be in a blog or a forum or even in the documentation of the framework, library or language you’re using. So a quick Google search might save you hours of unnecessary bug hunting.
  7. Separate your code. It’s a good practice to separate your code, let’s say one class one file or one action per function, that way when errors appear its easier to spot compared to when everything is muddled up.
  8. Take a break. Now this is really important, after bug hunting for a while you should take a break, even If you think a solution is in sight. You might not feel tired but your brain might be sound asleep! Or, who knows, maybe your just hungry.
  9. Move on! At times the reason you’ve not been able to get that snippet to work is is because you’re not ready for it yet (like when I was trying to understand Object Oriented PHP without first understanding how functions work). So it might be wise to move on to something else, and I’m sure when you return to it, you’ll be a better developer and you’ll probably make it work!

Summary: In this post we’ve covered 9 tips that could make debugging easier. Did I miss any? Share tips that have worked for you in the comments section. Happy Coding and bug hunting!

--

--

Russell Japheth
PH DevConnect

Software Developer | StackOverflow Expert | General Food Advocate 😋 | Technical Writer