summaryrefslogtreecommitdiff
path: root/book/module2/debugging_code.tex
blob: 0d244fb27d089e72c5fbd0c12ded7fe4a6dd4a5d (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
139
140
141
142
143
144
\section{Debugging Code}\label{debugging-code}

\subsection{Introduction}\label{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 \emph{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.

\subsection{Types of Bugs}\label{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.

\begin{itemize}
\tightlist
\item
  \textbf{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.
\item
  \textbf{Runtime errors} - e.g., dividing by zero or file not found.
\item
  \textbf{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.
\end{itemize}

\subsection{Debugging Techniques}\label{debugging-techniques}

\paragraph{Print Debugging}\label{print-debugging}

Insert print statements to check values of variables throughout the
program.

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ add(x, y):}
    \BuiltInTok{print}\NormalTok{(}\SpecialStringTok{f"x = }\SpecialCharTok{\{}\NormalTok{x}\SpecialCharTok{\}}\SpecialStringTok{, y = }\SpecialCharTok{\{}\NormalTok{y}\SpecialCharTok{\}}\SpecialStringTok{"}\NormalTok{)}
    \ControlFlowTok{return}\NormalTok{ x }\OperatorTok{+}\NormalTok{ y}
\end{Highlighting}
\end{Shaded}

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 \texttt{add()} function or if the
function is given an incorrect input.

\paragraph{Rubber Duck Debugging}\label{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.

\paragraph{Commenting Out Code}\label{commenting-out-code}

Using comments to temporarily suppress parts of your code help you
isolate and find the bug.

\paragraph{IDE Debugging tools}\label{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.

\paragraph{AI Chat}\label{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 \emph{never} assume that the code AI
gives you works the way you intend it to work.

\subsection{Interactive Debugging
Activity}\label{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

\paragraph{Code 1}\label{code-1}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ greet(name)}
    \BuiltInTok{print}\NormalTok{(}\StringTok{"Hello, "} \OperatorTok{+}\NormalTok{ Name)}
\NormalTok{greet(}\StringTok{"John"}\NormalTok{)}
\end{Highlighting}
\end{Shaded}

\paragraph{Code 2}\label{code-2}

\begin{Shaded}
\begin{Highlighting}[]
\ImportTok{import}\NormalTok{ numpy }\ImportTok{as}\NormalTok{ np}

\NormalTok{x }\OperatorTok{=}\NormalTok{ np.linspace(}\DecValTok{0}\NormalTok{,}\DecValTok{5}\NormalTok{,}\DecValTok{100}\NormalTok{)}
\NormalTok{y }\OperatorTok{=} \DecValTok{1}\OperatorTok{/}\NormalTok{x}

\BuiltInTok{print}\NormalTok{(}\StringTok{"Result:"}\NormalTok{, y[}\DecValTok{0}\NormalTok{])}
\end{Highlighting}
\end{Shaded}

\paragraph{Code 3}\label{code-3}

\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{def}\NormalTok{ f(x):}
    \ControlFlowTok{return}\NormalTok{ x}\OperatorTok{**}\DecValTok{2} \OperatorTok{{-}} \DecValTok{4}  \CommentTok{\# Root at x = ±2}

\KeywordTok{def}\NormalTok{ bisection(a, b, tol}\OperatorTok{=}\FloatTok{1e{-}5}\NormalTok{, max\_iter}\OperatorTok{=}\DecValTok{100}\NormalTok{):}
    \ControlFlowTok{if}\NormalTok{ f(a) }\OperatorTok{*}\NormalTok{ f(b) }\OperatorTok{\textgreater{}=} \DecValTok{0}\NormalTok{:}
        \BuiltInTok{print}\NormalTok{(}\StringTok{"Bisection method fails. f(a) and f(b) should have opposite signs."}\NormalTok{)}
        \ControlFlowTok{return} \VariableTok{None}

    \ControlFlowTok{for}\NormalTok{ i }\KeywordTok{in} \BuiltInTok{range}\NormalTok{(max\_iter):}
\NormalTok{        c }\OperatorTok{=}\NormalTok{ (a }\OperatorTok{+}\NormalTok{ b) }\OperatorTok{/} \DecValTok{2}
        \ControlFlowTok{if} \BuiltInTok{abs}\NormalTok{(f(c)) }\OperatorTok{\textless{}}\NormalTok{ tol:}
            \ControlFlowTok{return}\NormalTok{ c}
        \ControlFlowTok{elif}\NormalTok{ f(c) }\OperatorTok{*}\NormalTok{ f(b) }\OperatorTok{\textless{}} \DecValTok{0}\NormalTok{:}
\NormalTok{            a }\OperatorTok{=}\NormalTok{ c}
        \ControlFlowTok{else}\NormalTok{:}
\NormalTok{            b }\OperatorTok{=}\NormalTok{ c}
    \ControlFlowTok{return}\NormalTok{ (a }\OperatorTok{+}\NormalTok{ b) }\OperatorTok{/} \DecValTok{2}
\end{Highlighting}
\end{Shaded}

\subsection{Reflection}\label{reflection}

\begin{itemize}
\tightlist
\item
  What was the most challenging bug you found?
\item
  What debugging method did you find most useful?
\end{itemize}