Debugging
Jessica Xu
Sep 13, 2024
CSCI 110 - Lecture 14
Today’s Agenda
|
|
| |
| |
| |
| |
Recap
When you write code…
Think about all the test cases (inputs/outputs) you want to (unit) test!
You want your code to work for all cases you expect to work/fail gracefully for cases that shouldn’t work with the given input!
Edge cases
edge case: input value(s) requiring special consideration
also known as boundary condition or corner case.
common edge cases: testing 0 or empty string as arguments
test coverage: when testing a machine, need to identify a set of inputs such that if all inputs produced expected output, have confidence that machine works correctly
Review: Testing
What?
Trying different inputs and check output is correct
unit test: verify individual functions perform as expected; bugs are isolated
integration test: verify whole program performs as expected
Why?
Know whether code works correctly under all cases
If code (and behavior) get changed incorrectly, tests will catch it!
Unit testing vs. Integration testing
Unit tests
Integration tests
Both types of tests are essential for building reliable and maintainable software
Testing @ Google
Debugging
Objective: Debugging
What is it?
Why is it important?
How?
What's a bug?
debugging
Process of finding and fixing problems
in a program to make it work correctly.
Debugging strategies
Causes of bugs
Types of bugs
Compilation Errors
What happens?
print("My name is Jessica)
age = 8
age + 2 = 10
Print(age))
age = aeg + 9
print(age = 19)
SyntaxError
✗ print("My name is Jessica) # Missing quote
✓ age = 8
✗ age + 2 = 10 # Only the variable name on the left of the =
✗ Print(age)) # Should be lowercase p, extra )
✗ age = aeg + 9 # Typo - should be age, not aeg
✗ print(age = 19) # Can't assign variable inside of function call
Syntax errors
What happens?
price = 10
print(prices + 1)
print(price + 2)
NameError
Traceback (most recent call last):
File "python", line 2, in <module>
NameError: name 'prices' is not defined
What happens?
price = 10
if price < 10:
print(price)
print(price + 1)
IndentationError
Traceback (most recent call last):� File "python", line 3� print(price)� ^�IndentationError: expected an indented block
What happens?
price = 10
print(price)
print(price * 'hi')
IndentationError
Traceback (most recent call last):� File "python", line 1� price = 10� ^�IndentationError: unexpected indent
Runtime Errors
What happens?
fingers = “5”
toes = 5
print(fingers + toes)
print(toes / 0)
Runtime Error
✓ fingers = “5”
✓ toes = 5
✗ print(fingers + toes) # Can't add 5 to “5”
✗ print(toes / 0) # Division by 0
Runtime errors
What happens?
price = 10
print(price + '!')
TypeError
Traceback (most recent call last):� File "python", line 2, in <module>�TypeError: unsupported operand type(s) for +: 'int' and 'str'
What happens?
price = 'hi'
print(price * 'bye')
TypeError
Traceback (most recent call last):� File "python", line 2, in <module>�TypeError: can't multiply sequence by non-int of type 'str'
Semantic Errors
Semantic errors
What happens?
age = 2 ** 9
print("I am")
print("age")
print("years old")
Semantic Error
✗ age = 2 ** 9 # Should use *, unless you're 512
years old!
✓ print("I am")
✗ print("age") # This prints the word age, not
the variable!
✓ print("years old")
WORDLE
Let’s DEBUG: WORDLE!!
Practice: Debugging!
There are 3 bugs!
Can you find & fix them all?
Review: Debugging
What is it?
Finding bugs, or problems, with code
Why is it important?
Fix code to make it run the way it’s intended to run
How?
Questions?
Quiz
Time for Quiz 4!
Good luck!
Reminders
Reminder: TA’s & Jessica’s Office Hours!
Reminder: Project 1 (pt. 3)
Reminder: HW 4
Reminder: Midterm Exam 1
Next week! (Wednesday - 9/18)
Everything we have learned including functions (this week)!
Topics covered:
Reminder: Midterm Exam 1
Next week! (Wednesday - 9/18)
Everything we have learned including functions (this week)!
Topics covered:
Exam Review next Monday!
Please fill out this form so I know what you all want to review the most:
STEP Info Session!!
Reminder: Retakes!
To earn the chance to do a retake:
Do NOT tell me why you got it wrong! Instead pretend like you are writing the solution guide for the question :)
To take the retake quiz:
Google 20% TAs
Missed the first few lectures of class? Need some extra support?
Book virtual OH with some folks from Google!
https://csci110.page/staff/#googler-teaching-assistants
If you feel that you are falling behind and want more 1:1 attention (or if you just want to talk to more Googlers), schedule a meeting with a Googer TA that fits your schedule!