\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}
