Krista's Coding Corner

07.03.2012

Bugs, bugs and even more bugs

Let’s create more complex program with logic bugs:

#!/usr/bin/env python
number_1 = 2
number_2 = 7
if number_1 > 5:
	print "First number is smaller than 5."
# let's sum number 1 and 2 together:
print "Addition of first and second number is:"
addition = number_1 + number_1
print addition
print "Adding 15 to first number and then multiplying the result with 3 gives us:"
number_1 = number_1 + 15 * 3
print number_1

First mistake is in first if-sentence: going to the first if-loop means that the number_1 is bigger than 5 but program prints the opposite on the screen.

Second mistake: addition is created by adding number_1 twice. No number_2 anywhere.

Third mistake: we actually multiply 15 with 3 AND AFTER that we add number_1. This is just basic math logic and programs also follow this.

Anyway, compiler or interpreter gives you error- and warning messages if something isn’t right but they won’t warn you about logic mistakes like above. Some of those messages are clear to understand and others not so much. Normally you have to use Google to find out what different error codes means. Tracking and fixing bugs is an art form of its own and hopefully I’ll one day write about those different methods... Bugs are a part of life, so get used to fixing them!

But remember that:

Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?

- Brian Kernighan

blog comments powered by Disqus