Debugging

Debugging

  • Definition: The process of identifying, isolating, and fixing errors or unexpected behavior in a program.
  • Goal: Understand why the program is not working and how to fix it.

Function

  • Definition: A named block of reusable code that performs a specific task. Functions take input (parameters), process it, and may return a result.
  • Example:
python

def add(a, b):
    return a + b

Performance

  • Definition: A measure of how efficiently a program runs in terms of time (speed) and resources (memory, CPU, etc.).
  • Goal: Write code that not only works but runs fast and efficiently at scale.

Write Your Programs to Facilitate Validation and Debugging

  • Meaning: Structure your code so it’s easier to:
    • Verify that it behaves correctly (validation)
    • Trace and fix errors when things go wrong (debugging)
  • Practices:
    • Use functions and modules
    • Write clear specifications
    • Include logging or debugging output
    • Use tests

Assert Statements

  • Definition: Statements used to test if a condition is true during execution. If false, the program crashes with an error message.
  • Purpose: Catch bugs early by verifying assumptions.
  • Example:
python


assert 2 + 2 == 4  # Passes
assert 2 + 2 == 5  # Fails and raises an AssertionError

Write Specification

  • Definition: A precise description of what a program or function is supposed to do, including:
    • Input and output types and formats
    • Expected behavior and edge cases
  • Purpose: Guides development and testing.

Modularize

  • Definition: Divide a program into small, manageable parts (modules or functions), each responsible for a single task.
  • Benefits: Easier to understand, test, debug, and reuse.

Test Input :: Output Pair to a Specification

  • Meaning: Check that for a given input, the function returns the expected output as defined by the specification.
  • Example:
python

# Specification: max(x, y) returns the greater of x and y
assert max(3, 5) == 5
assert max(7, 2) == 7

Debug - Events that Led to an Error

  • Definition: When debugging, trace the sequence of steps and variable values that led to a bug or error.
  • Tools: Print statements, debuggers, logs, stack traces.

Testing

  • Definition: Process of checking whether the program works as intended.
  • Unit Testing: Validate individual components (e.g., functions, classes).
    • Test small parts in isolation.
    • Example: Test if a sorting function works for different kinds of lists.

Integration Testing

  • Definition: Test how components work together as a complete system.
  • Note: Usually done after unit testing is complete.
  • Goal: Ensure the whole program behaves correctly.

Look at Something Small

  • Advice: When debugging or writing code, focus on small, manageable pieces instead of the whole program.

Code Review Example

python

Edit
def max(x, y):
    x & y float  # This line is invalid syntax.
  • Problem: x & y float is not valid Python and doesn’t specify types correctly.
  • Correct version (with type hints):
python

Edit
def max(x: float, y: float) -> float:
    return x if x > y else y

Billions of Options, Exhaustive Testing is Impossible

  • Meaning: For many programs, there are too many input combinations to test all of them.
  • Solution: Test a representative subset that covers:
    • Typical cases
    • Edge cases
    • Error cases

Test Suite

  • Definition: A collection of tests that verify different parts of a program.
  • Design Goals:
    • Small enough to run quickly.
    • Large enough to give confidence in correctness.


Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Vanilla Appliance - Powered by TurnKey Linux