summaryrefslogtreecommitdiff
path: root/tutorials/module_2/notebook_2/debugging_code.ipynb
blob: 37b66c097e8766ca07f7066afe5a5a4d9a9d0d0a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Debugging Code\n",
    "\n",
    "## Introduction\n",
    "\n",
    "Have you ever had a piece of code not work the way you expected? What\n",
    "did you do? You may have, asked a friend or used an AI assistant. In\n",
    "this section, the following concepts are introduced - definition of a\n",
    "bug, common types of bugs and debugging techniques.\n",
    "\n",
    "A *software bug* is an unintentional mistake or defect with a program,\n",
    "this comes either from when the programmer makes a mistake in writing\n",
    "the code or the code works in a way which has consequences that were not\n",
    "foreseen by the programmer. Debugging is the act removing the bugs in\n",
    "the software. Debugging is a normal part of programming that even\n",
    "experiences developers spend a lot of time on.\n",
    "\n",
    "## Types of Bugs\n",
    "\n",
    "When writing code you are guaranteed to have bugs in your code. These\n",
    "bugs can be categorized in the following three groups.\n",
    "\n",
    "-   **Syntax errors** - this type of error occurs when the code fails\n",
    "    due to missing colons, missing indentation or a typo in code - some\n",
    "    languages like python are case sensitive meaning that the a capital\n",
    "    letter are different symbols.\n",
    "-   **Runtime errors** - e.g., dividing by zero or file not found.\n",
    "-   **Logical errors** - this may be the most dangerous that we need to\n",
    "    be careful with because this error can occur without any error\n",
    "    messages but it gives you the wrong result.\n",
    "\n",
    "## Debugging Techniques\n",
    "\n",
    "#### Print Debugging\n",
    "\n",
    "Insert print statements to check values of variables throughout the\n",
    "program.\n",
    "\n",
    "``` python\n",
    "def add(x, y):\n",
    "    print(f\"x = {x}, y = {y}\")\n",
    "    return x + y\n",
    "```\n",
    "\n",
    "In the example above the print statement gives us feedback on what the\n",
    "code is doing. The function in this example is obviously very simple,\n",
    "but when we start applying more complex equations or function then\n",
    "checking to see if the input variables are correct can indicate whether\n",
    "there is an issue lies within the `add()` function or if the function is\n",
    "given an incorrect input.\n",
    "\n",
    "#### Rubber Duck Debugging\n",
    "\n",
    "This is a technique by which you explaining your code line by line in\n",
    "natural language to someone else, yourself or an inanimate object like a\n",
    "rubber duck. This can help you spot your mistake in the code.\n",
    "\n",
    "#### Commenting Out Code\n",
    "\n",
    "Using comments to temporarily suppress parts of your code help you\n",
    "isolate and find the bug.\n",
    "\n",
    "#### IDE Debugging tools\n",
    "\n",
    "Depending if you use an IDE, they often come with some sort of debugging\n",
    "tools such as breakpoints, step into/over and variables explorers.\n",
    "\n",
    "#### AI Chat\n",
    "\n",
    "AI chat bots can help you find typo or fix logic in your code. You may\n",
    "find yourself going through the steps above when using an AI assistant\n",
    "to help you debug the code. However *never* assume that the code AI\n",
    "gives you works the way you intend it to work.\n",
    "\n",
    "## Interactive Debugging Activity\n",
    "\n",
    "In the following code snippets, debug the code and document the\n",
    "following: - What the bug is - How you found it (technique used) - What\n",
    "actions you took to fix the bug\n",
    "\n",
    "#### Code 1\n",
    "\n",
    "``` python\n",
    "def greet(name)\n",
    "    print(\"Hello, \" + Name)\n",
    "greet(\"John\")\n",
    "```\n",
    "\n",
    "#### Code 2\n",
    "\n",
    "``` python\n",
    "import numpy as np\n",
    "\n",
    "x = np.linspace(0,5,100)\n",
    "y = 1/x\n",
    "\n",
    "print(\"Result:\", y[0])\n",
    "```\n",
    "\n",
    "#### Code 3\n",
    "\n",
    "``` python\n",
    "def f(x):\n",
    "    return x**2 - 4  # Root at x = ±2\n",
    "\n",
    "def bisection(a, b, tol=1e-5, max_iter=100):\n",
    "    if f(a) * f(b) >= 0:\n",
    "        print(\"Bisection method fails. f(a) and f(b) should have opposite signs.\")\n",
    "        return None\n",
    "\n",
    "    for i in range(max_iter):\n",
    "        c = (a + b) / 2\n",
    "        if abs(f(c)) < tol:\n",
    "            return c\n",
    "        elif f(c) * f(b) < 0:\n",
    "            a = c\n",
    "        else:\n",
    "            b = c\n",
    "    return (a + b) / 2\n",
    "```\n",
    "\n",
    "## Reflection\n",
    "\n",
    "-   What was the most challenging bug you found?\n",
    "-   What debugging method did you find most useful?"
   ],
   "id": "a900965c-2ab1-4123-b5ac-6e2ae124dc7d"
  }
 ],
 "nbformat": 4,
 "nbformat_minor": 5,
 "metadata": {}
}