Difference between revisions of "Debugging"

From Computer Science Wiki
(Some Code to Get You Started)
 
(One intermediate revision by one other user not shown)
Line 1: Line 1:
[[File:square.png|right|frame|This a problem set for you to work through <ref>http://www.flaticon.com/</ref>]]
 
 
This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.
 
 
== Terminology ==  
 
== Terminology ==  
  
Line 60: Line 57:
  
 
<ref>http://www.makinggoodsoftware.com/2009/06/14/7-steps-to-fix-an-error/</ref>
 
<ref>http://www.makinggoodsoftware.com/2009/06/14/7-steps-to-fix-an-error/</ref>
 
== What is this problem set trying to do ==
 
 
'''You are going to create errors when you code!'''  Much of your work when you code is understanding where and error is in someone else's code. There is a process to follow when you are debugging. I expect EVERY student to follow the steps above when you are debugging. The purpose of this problem set is to encourage you to understand and apply your understanding of the debugging process to your code (and the code of a classmate).
 
 
== The Problem ==
 
 
There is some broken code below <ref>https://www.reddit.com/r/learnprogramming/comments/29olnm/what_is_wrong_with_my_python_code/</ref> Follow the debugging steps to make it work the way you think it should work. You must create a google document to track your steps. In order to meet the standard for this lesson:
 
 
# you must resolve the error so the program runs.
 
# you must answer the questions on [https://docs.google.com/document/d/1RGiFigErjkRZUgYYXLDSCQpKzK7G3xdzMSUju3cRONU/edit#heading=h.ile82dmotbr this google template (please COPY the document, share it with me and then edit it)]
 
 
== Some Code to Get You Started ==
 
 
A programmer was starting to write a program to calculate the cost for business trips for his company. The company was hoping to use this tool to make calculating the cost of business trips easier. Sadly, as the programmer was walking home one day, he was hit by a bus, and then an out-of-control taxi, followed by a stampede of wild horses. He was shaken up, and started limping home, only to be struck by a meteor. Thinking this might be the worse day ever, he was attacked by a tiger and a mountain lion.  After beating them, he limped up the stairs, only to be attacked by a group of ninja's. After beating them, he decided to never leave his house again. You must now fix his code.
 
 
 
The example below has three syntax errors:
 
 
<syntaxhighlight lang="python">
 
 
raw_input = ("Please enter your name: ")
 
password = raw_input("Please enter your password: ")
 
if password == "pass123"
 
    print("ok, you are allowed in")
 
else:
 
  print(sorry, I can't let you continue.")
 
 
 
</syntaxhighlight>
 
 
Below is a real-life example of some code:
 
 
<syntaxhighlight lang="python">
 
 
def hotel_cost(nights):
 
return 140 * nights
 
 
def plane_ride_cost(city):
 
if city == "Charlotte":
 
return 183
 
elif city == "Tampa":
 
return 220
 
elif city == "Pittsburgh":
 
return 222
 
elif city == "Los Angeles":
 
return 475
 
 
def rental_car_cost(days):
 
return 40 * days
 
if days >= 7:
 
rental_car_cost -= 50
 
elif days >= 3:
 
rental_car_cost -= 20
 
 
 
</syntaxhighlight>
 
 
Also try the code below:
 
 
<syntaxhighlight lang="python">
 
my_name = 'Zed A. Shaw'
 
my_age = 35 # not a lie
 
my_height = 74 # inches
 
my_weight = 180 # lbs
 
my_eyes = 'Blue'
 
my_teeth = 'White'
 
my_hair = 'Brown'
 
 
print "Let's talk about %s." % my_name
 
print "He's %d inches tall." % my_height
 
print "He's %d pounds heavy." % my_weight
 
print "Actually that's not too heavy."
 
print "He's got %s eyes and %s hair." % (my_eyes, my_hair)
 
print "His teeth are usually %s depending on the coffee." % my_teeth
 
 
# this line is tricky, try to get it exactly right
 
print "If I add %d, %d, and %d I get %d." % (
 
    my_age, my_height, my_weight, my_age + my_height + my_weight)
 
</syntaxhighlight>
 
 
== Take This Further ==
 
 
# In my opinion, this code is horrible. It is static (what happens if I fly to another city?), it is horribly documented, and it doesn't raise any exceptions. Please create a full-featured program that ASKS users how many nights, what cities, and how many days they need to rent a car for, and then returns a nice summary for you perhaps with a unique ITINERARY NUMBER for tracking.
 
 
== How you will be assessed ==
 
 
Every problem set is a formative assignment.  [[Media:Problem-setrubric.pdf|Please click here to see how you will be graded]]
 
  
 
== References ==
 
== References ==
Line 153: Line 62:
 
<references />
 
<references />
  
== See also ==
 
 
[http://www.openbookproject.net/thinkcs/python/english2e/app_a.html a short paper on the debugging process]
 
 
== A possible solution ==
 
 
 
<div class="toccolours mw-collapsible mw-collapsed">
 
Click the expand link to see one possible solution, but NOT before you have tried and failed at least three times
 
<div class="mw-collapsible-content">
 
 
<syntaxhighlight lang="python">
 
 
def hotel_cost(nights):
 
return 140 * nights
 
 
def plane_ride_cost(city):
 
if city == "Charlotte":
 
return 183
 
elif city == "Tampa":
 
return 220
 
elif city == "Pittsburgh":
 
return 222
 
elif city == "Los Angeles":
 
return 475
 
 
def rental_car_cost(days):
 
if days >= 7:
 
rental_car_cost -= 50
 
elif days >= 3:
 
rental_car_cost -= 20
 
        return 40 * days
 
</syntaxhighlight>
 
 
</div>
 
</div>
 
[[Category:problem set]]
 
[[Category:python]]
 
[[Category:strings]]
 
 
[[Category:debugging]]
 
[[Category:debugging]]

Latest revision as of 11:29, 7 January 2019

Terminology[edit]

In computers, debugging is the process of locating and fixing or bypassing bugs (errors) in computer program code or the engineering of a hardware device. To debug a program or hardware device is to start with a problem, isolate the source of the problem, and then fix it. A user of a program that does not know how to fix the problem may learn enough about the problem to be able to avoid it until it is permanently fixed. When someone says they've debugged a program or "worked the bugs out" of a program, they imply that they fixed it so that the bugs no longer exist.[1]

Debugging is the process of finding and resolving of defects that prevent correct operation of computer software or a system. Debugging tends to be harder when various subsystems are tightly coupled, as changes in one may cause bugs to emerge in another.[2]

A debugging process[edit]

This is an important process for you to understand [3]

6 Steps to fix an error

The correct approach to debug is to follow a very structured procedure based on repeating the same steps for every bug, the reason for this is that bugs usually are symptoms of a much bigger problem going on, so in order for us to uncover the real nature of the error we have to make sure we do an exhaustive and systematic revision of it.

The degree of formality when following the steps may vary depending on the bug, for critical errors it is better to document every single step in a document. For minor errors, or bugs found while coding it is just enough to follow them as a mental guideline.

Step 1. Identify the error.[edit]

This is an obvious step but a tricky one, sometimes a bad identification of an error can cause lots of wasted developing time.

A few tips to make sure you identify the bug correctly:

  1. See the error.
  2. Reproduce the error.

You never should say that an error has been fixed if you were not able to reproduce it.

  1. Understand what the expected behavior should be.
  2. Validate the identification.

Ask yourself: is this expected behavior or an actual error?


Step 2. Find the error.[edit]

Once we have an error correctly identified, it is time to go through the code to find the exact spot where the error is located, at this stage we are not interested in understanding the big picture for the error, we are just focused on finding it. A few techniques that may help to find an error are:

  1. Logging. It can be to the console, file… It should help you to trace the error in the code.
  2. Debugging. Debugging in the most technical sense of the word, meaning turning on whatever the debugger you are using and stepping through the code.
  3. Removing code.

Exclamation.png This is one of the most important strategies you can use to find errors:

Take out half of the code from the action causing the program to produce an error and keep splitting the code until you find the error.

Step 3. Analyze the error[edit]

This is a critical step, use a bottom-up approach from the place the error was found and analyze the code so you can see the big picture of the error, analyzing a bug has two main goals: to check that around that error there aren’t any other errors to be found (the iceberg metaphor), and to make sure what are the risks of entering any collateral damage in the fix.

Step 4. Document the error[edit]

At this stage you are almost ready to start coding the fix. If you are working on a complex application, please document your fix in an error reporting database.

Step 5. Fix the error.[edit]

That’s it, finally you can fix the error!

Step 6. Validate the solution.[edit]

Run the program again and reproduce the same steps that you discovered in step 1.

[4]

References[edit]

Provide an answer from a number of possibilities. Recognize and state briefly a distinguishing fact or feature.

Provide an answer from a number of possibilities. Recognize and state briefly a distinguishing fact or feature.

Follow and record the action of an algorithm.

Break down in order to bring out the essential elements or structure. To identify parts and relationships, and to interpret information to reach conclusions.

Break down in order to bring out the essential elements or structure. To identify parts and relationships, and to interpret information to reach conclusions.

Credit sources of information used by referencing (or citing) following one recognized referencing system. References should be included in the text and also at the end of the piece of work in a reference list or bibliography.