1 of 56

Debugging

Jessica Xu

Sep 13, 2024

CSCI 110 - Lecture 14

2 of 56

Today’s Agenda

  • Recap (Testing Functions)
  • Debugging!
  • Quiz

3 of 56

Recap

4 of 56

When you write code…

Think about all the test cases (inputs/outputs) you want to (unit) test!

  • All the code paths/control flows
  • As many edge cases as you can think of!!

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!

5 of 56

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

6 of 56

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!

7 of 56

Unit testing vs. Integration testing

Unit tests

  • tests functions/isolated blocks of code
  • mock/stub dependencies
    • ex. Blackjack! (mock randint)
  • run everytime you make changes to code

Integration tests

  • tests how different parts of the program work together
    • ex. dependencies (databases, APIs)
  • run everytime code is pushed to production/major releases

Both types of tests are essential for building reliable and maintainable software

8 of 56

Testing @ Google

9 of 56

Debugging

10 of 56

Objective: Debugging

What is it?

Why is it important?

How?

11 of 56

What's a bug?

12 of 56

debugging

Process of finding and fixing problems

in a program to make it work correctly.

13 of 56

Debugging strategies

  • Read error message
  • Use IDE
  • Ask: What am I trying to do?
  • Ask: When did it last work?
  • Trace line by line
  • Comment out code
  • Print out variables and their values

14 of 56

Causes of bugs

  • Typos or invalid syntax
  • Trying to do something impossible or computer failures
  • Making a mistake in logic, so program doesn't do what you intended

  • Bugs happen to everybody
    • No matter which one...debug it!

15 of 56

Types of bugs

  • Compilation Errors
    • Your code cannot be parsed/understood
    • Program will not run
  • Runtime Errors
    • Your code can be parsed!
    • Program will crash when something bad happens
  • Semantic Errors
    • Your code can be parsed!
    • Your code will run without crashing
    • The output is not what you want…

16 of 56

Compilation Errors

17 of 56

What happens?

print("My name is Jessica)

age = 8

age + 2 = 10

Print(age))

age = aeg + 9

print(age = 19)

18 of 56

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

19 of 56

Syntax errors

  • Program won't run at all
  • Misspelling a variable name, forgetting or misplacing parentheses, quotes
  • Get error message with line of code the problem is on
  • Read over that line carefully - check the one before it too!

20 of 56

What happens?

price = 10

print(prices + 1)

print(price + 2)

21 of 56

NameError

Traceback (most recent call last):

File "python", line 2, in <module>

NameError: name 'prices' is not defined

22 of 56

What happens?

price = 10

if price < 10:

print(price)

print(price + 1)

23 of 56

IndentationError

Traceback (most recent call last):� File "python", line 3� print(price)� ^�IndentationError: expected an indented block

24 of 56

What happens?

price = 10

print(price)

print(price * 'hi')

25 of 56

IndentationError

Traceback (most recent call last):� File "python", line 1� price = 10� ^�IndentationError: unexpected indent

26 of 56

Runtime Errors

27 of 56

What happens?

fingers = “5”

toes = 5

print(fingers + toes)

print(toes / 0)

28 of 56

Runtime Error

fingers = “5”

toes = 5

print(fingers + toes) # Can't add 5 to “5”

print(toes / 0) # Division by 0

29 of 56

Runtime errors

  • Category of errors (ValueError/TypeError are both Runtime errors)
  • Program will start running, but will crash and stop part way through
  • Read error message for clues
  • When in doubt, Google it!
  • Common causes: a variable being empty or wrong type, dividing by 0, not handling failures when interacting with network or file system

30 of 56

What happens?

price = 10

print(price + '!')

31 of 56

TypeError

Traceback (most recent call last):� File "python", line 2, in <module>�TypeError: unsupported operand type(s) for +: 'int' and 'str'

32 of 56

What happens?

price = 'hi'

print(price * 'bye')

33 of 56

TypeError

Traceback (most recent call last):� File "python", line 2, in <module>�TypeError: can't multiply sequence by non-int of type 'str'

34 of 56

Semantic Errors

35 of 56

Semantic errors

  • Program will run fine and produce output - but not what you expect!
  • Double check logic
  • Tip: Add print statements to check that variables have values you expect
  • Rather than deleting code, comment it out

36 of 56

What happens?

age = 2 ** 9

print("I am")

print("age")

print("years old")

37 of 56

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")

38 of 56

WORDLE

39 of 56

Let’s DEBUG: WORDLE!!

40 of 56

Practice: Debugging!

  • Go to the WORDLE colab!
  • Scroll to the Debugging section and FIND THE BUGS!

41 of 56

There are 3 bugs!

Can you find & fix them all?

42 of 56

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?

  • Read error message
  • Use IDE
  • Ask: What am I trying to do?
  • Ask: When did it last work?
  • Trace line by line
  • Comment out code
  • Print out values stored in variables

43 of 56

Questions?

44 of 56

Quiz

45 of 56

Time for Quiz 4!

  • Here is the link to the quiz.
  • Do NOT use other resources. You should only be on Edstem.
  • If you have any questions, raise your hand!

46 of 56

Good luck!

47 of 56

Reminders

48 of 56

Reminder: TA’s & Jessica’s Office Hours!

  • Adaora’s OH TODAY
    • Fridays @ 4pm - 5pm

  • Jessica’s OHs TODAY
    • Fridays @ 3pm - 4pm

49 of 56

Reminder: Project 1 (pt. 3)

  • MORE Blackjack!
  • Put all the pieces together to build the entire game :)
  • Due on Sunday (9/15) at 11:59pm

50 of 56

Reminder: HW 4

  • Due next Wednesday @ 11:59 PM
  • This HW covers Functions!

51 of 56

Reminder: Midterm Exam 1

Next week! (Wednesday - 9/18)

Everything we have learned including functions (this week)!

Topics covered:

  • Data Types, Variables, Operators (Arithmetic, Comparison, Logical)
  • While Loops
  • Strings
  • Functions

52 of 56

Reminder: Midterm Exam 1

Next week! (Wednesday - 9/18)

Everything we have learned including functions (this week)!

Topics covered:

  • Data Types, Variables, Operators (Arithmetic, Comparison, Logical)
  • While Loops
  • Strings
  • Functions

53 of 56

Exam Review next Monday!

Please fill out this form so I know what you all want to review the most:

https://forms.gle/AjD35UzhUkPac4aK8

54 of 56

STEP Info Session!!

  • Applications open September 30th!! g.co/jobs/step
    • Note: They close on October 25th!!

  • STEP Info Session — NEXT TUESDAY!!
    • Spence Hall @ 5pm - 6:30pm

55 of 56

Reminder: Retakes!

To earn the chance to do a retake:

  • Email me by Sunday 11:59PM with explanations for the correct answers for ALL the questions you missed on the quiz

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:

  • Come Jessica’s OH the following week to take the quiz
  • You can earn up to 80% on the retakes

56 of 56

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!