Skip to content

Tips for Debugging Code without F-Bombs – Part 2

This post is a continuation of my previous tutorial about debugging code in which I discuss how preventing bugs is really the best way of debugging.  In this tutorial, we’re going to cover more debugging techniques and how to avoid bugs.

Types of Errors:

Ok, you’re testing frequently, and using good coding practices, but you’ve STILL got bugs.  What next??  Let’s talk about what kind of error you are encountering because that will determine the response.  Errors can be reduced to three basic categories: syntax errors, runtime errors, and the most insidious intent errors.  Let’s look at Syntax errors first.

Debugging Syntax Errors:

Syntax errors are a category of errors in which the programmer (you) has violated some rule of the coding language and the compiler or interpreter cannot convert your code into executable code.  Syntax errors are usually the easiest to fix because (in most instances) the compiler or interpreter will tell you not only what error you made, but on what line it occurred.  Consider the following code:
import Math
x = math.sqrt( 4 )
print( x )

This code produces the following error:


ImportError Traceback (most recent call last)
in ()
----> 1 import Math
2 x = math.sqrt( 4 )
3 print( x )
ImportError: No module named 'Math'

This is a simple example of a syntax error and this error actually produces an error message but the error message doesn’t actually describe the problem.  The error message states that there isn’t a Math module.  However, we know that Python ships with a math (lower case) module.

Use an Integrated Development Environment (IDE)

I wasn’t always a big believer in IDEs, but let me tell you that there is a reason that nearly every Java developer in the world uses Eclipse or IntelliJ to do their coding.  Most of the work I do is in Python, and inconsistent or incorrect indentation is a common source of bugs in Python code.  I’ve basically eliminated syntax errors from my code by using Spyder or Rodeo and really, using an IDE is simply the best way to prevent syntax errors.   Most IDEs feature things like:

  • Syntax highlighting:  This feature colors key words and other language reserved words to prevent errors.  Most IDEs will also do things like highlight unused variables, quoted sections etc.
  • Tab completion: This feature not only saves time, but also exposes you to many different options that you might not otherwise be aware of.
  • Error Flagging:  Most IDEs will flag syntax errors as you are typing them, thereby preventing a lot of aggravation later.  They also will catch things like variable type mismatches, misspellings, and can reformat your code to coding standards.
  • Variable exploration:  More on this later, but a good IDE will let you view the values of variables as you are executing code, which is extremely useful in debugging runtime errors.

Bottom line here, you don’t have to prove your nerdliness by coding in vim or emacs.  You’ll save yourself a lot of headaches and avoid syntax errors if you use a good IDE.

Decoding Error Messages

The ability to understand error messages is vital to fixing syntax errors.  The beauty of syntax errors is that the language will tell you where the error is, however this isn’t always as helpful as you might think.  Consider the following pseudo code:


1 print "Text"
2 print "More text
3 print "Even more text'
4 print x + 3

This code will likely throw some sort of Symbol expected error and point to line 3.  However the actual error is on line 2 when I failed to close the quote.  Fixing that error and rerunning the code will produce a second error also pointing to line 3 with a mismatched quote.  Again, an IDE with syntax highlighting will help you solve problems like this before you even execute the code.

Tl;dr

In conclusion, returning to the theme from the first article, the best way to fix syntax errors is to not make them in the first place and the best way to do that is to use an IDE.  In the next installment in this series, I’ll discuss how to debug runtime errors.

Share the joy

One Comment

  1. rohit aggarwal rohit aggarwal

    thanks for the information

Leave a Reply

Your email address will not be published. Required fields are marked *