# Debugging Code ^6dd4d7 ## Introduction Have you ever had a piece of code not work the way you expected? What did you do? You may have, asked a friend or used an AI assistant. In this section, the following concepts are introduced - definition of a bug, common types of bugs and debugging techniques. A *software bug* is an unintentional mistake or defect with a program, this comes either from when the programmer makes a mistake in writing the code or the code works in a way which has consequences that were not foreseen by the programmer. Debugging is the act removing the bugs in the software. Debugging is a normal part of programming that even experiences developers spend a lot of time on. ## Types of Bugs When writing code you are guaranteed to have bugs in your code. These bugs can be categorized in the following three groups. - **Syntax errors** - this type of error occurs when the code fails due to missing colons, missing indentation or a typo in code - some languages like python are case sensitive meaning that the a capital letter are different symbols. - **Runtime errors** - e.g., dividing by zero or file not found. - **Logical errors** - this may be the most dangerous that we need to be careful with because this error can occur without any error messages but it gives you the wrong result. ## Debugging Techniques #### Print Debugging Insert print statements to check values of variables throughout the program. ```python def add(x, y): print(f"x = {x}, y = {y}") return x + y ``` In the example above the print statement gives us feedback on what the code is doing. The function in this example is obviously very simple, but when we start applying more complex equations or function then checking to see if the input variables are correct can indicate whether there is an issue lies within the `add()` function or if the function is given an incorrect input. #### Rubber Duck Debugging This is a technique by which you explaining your code line by line in natural language to someone else, yourself or an inanimate object like a rubber duck. This can help you spot your mistake in the code. #### Commenting Out Code Using comments to temporarily suppress parts of your code help you isolate and find the bug. #### IDE Debugging tools Depending if you use an IDE, they often come with some sort of debugging tools such as breakpoints, step into/over and variables explorers. #### AI Chat AI chat bots can help you find typo or fix logic in your code. You may find yourself going through the steps above when using an AI assistant to help you debug the code. However *never* assume that the code AI gives you works the way you intend it to work. ## Interactive Debugging Activity In the following code snippets, debug the code and document the following: - What the bug is - How you found it (technique used) - What actions you took to fix the bug #### Code 1 ```python def greet(name) print("Hello, " + Name) greet("John") ``` #### Code 2 ```python import numpy as np x = np.linspace(0,5,100) y = 1/x print("Result:", y[0]) ``` #### Code 3 ```python def f(x): return x**2 - 4 # Root at x = ±2 def bisection(a, b, tol=1e-5, max_iter=100): if f(a) * f(b) >= 0: print("Bisection method fails. f(a) and f(b) should have opposite signs.") return None for i in range(max_iter): c = (a + b) / 2 if abs(f(c)) < tol: return c elif f(c) * f(b) < 0: a = c else: b = c return (a + b) / 2 ``` ## Reflection - What was the most challenging bug you found? - What debugging method did you find most useful?