Search⌘ K
AI Features

Debugging  and  Refactoring

Explore how to identify and fix runtime bugs using Copilot chat, refactor complex functions into simpler parts, and reorganize procedural code into classes. Understand techniques for improving code readability and maintainability while preserving functionality through guided hands-on challenges.

Why debug and refactor?

Even with solid unit tests, bugs can still sneak into the runtime. Debugging is about finding and fixing those issues. Refactoring, however, focuses on improving messy or overly complex code, which helps prevent future bugs and makes the code easier to maintain.

Note: Refactoring isn’t just about cleaning code; it makes it faster for new developers to read, understand, and contribute. Well-organized code helps teams move quicker and onboard new teammates with less confusion.

Runtime error hunt

Let’s explore how to find and fix bugs affecting your program during runtime.

Bug scenario: Infinite loop in triple while nest

Here’s a classic example where a missing update creates an infinite loop—let’s debug it together.

Python
JavaScript
def has_triplet_sum(nums: list[int], target: int) -> bool:
i = 0
while i < len(nums):
j = 0
while j < len(nums):
k = 0
while k < len(nums):
if nums[i] + nums[j] + nums[k] == target:
return True
k += 1
j += 1
return False
print(has_triplet_sum([1, 2, 3], 6))
Infinite loop in triple while nest

Task:

    ...