summaryrefslogtreecommitdiff
path: root/book/module2
diff options
context:
space:
mode:
Diffstat (limited to 'book/module2')
-rw-r--r--book/module2/debugging.tex130
-rw-r--r--book/module2/module2.aux56
-rw-r--r--book/module2/module2.tex2
3 files changed, 188 insertions, 0 deletions
diff --git a/book/module2/debugging.tex b/book/module2/debugging.tex
new file mode 100644
index 0000000..e4c495d
--- /dev/null
+++ b/book/module2/debugging.tex
@@ -0,0 +1,130 @@
+\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. \#\#\#\# 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 \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 \#\#\#\# 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}
diff --git a/book/module2/module2.aux b/book/module2/module2.aux
new file mode 100644
index 0000000..54ad0e7
--- /dev/null
+++ b/book/module2/module2.aux
@@ -0,0 +1,56 @@
+\relax
+\providecommand\hyper@newdestlabel[2]{}
+\@writefile{toc}{\contentsline {chapter}{\numberline {3}Module 2}{15}{chapter.3}\protected@file@percent }
+\@writefile{lof}{\addvspace {10\p@ }}
+\@writefile{lot}{\addvspace {10\p@ }}
+\@writefile{toc}{\contentsline {section}{\numberline {3.1}Debugging Code}{15}{section.3.1}\protected@file@percent }
+\newlabel{debugging-code}{{3.1}{15}{Debugging Code}{section.3.1}{}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {3.1.1}Introduction}{15}{subsection.3.1.1}\protected@file@percent }
+\newlabel{introduction}{{3.1.1}{15}{Introduction}{subsection.3.1.1}{}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {3.1.2}Types of Bugs}{15}{subsection.3.1.2}\protected@file@percent }
+\newlabel{types-of-bugs}{{3.1.2}{15}{Types of Bugs}{subsection.3.1.2}{}}
+\@writefile{toc}{\contentsline {subsection}{\numberline {3.1.3}Debugging Techniques}{15}{subsection.3.1.3}\protected@file@percent }
+\newlabel{debugging-techniques}{{3.1.3}{15}{Debugging Techniques}{subsection.3.1.3}{}}
+\newlabel{print-debugging}{{3.1.3}{15}{Print Debugging}{paragraph*.50}{}}
+\@writefile{toc}{\contentsline {paragraph}{\nonumberline Print Debugging}{15}{paragraph*.50}\protected@file@percent }
+\@writefile{toc}{\contentsline {subsection}{\numberline {3.1.4}Interactive Debugging Activity}{16}{subsection.3.1.4}\protected@file@percent }
+\newlabel{interactive-debugging-activity}{{3.1.4}{16}{Interactive Debugging Activity}{subsection.3.1.4}{}}
+\newlabel{code-2}{{3.1.4}{16}{Code 2}{paragraph*.52}{}}
+\@writefile{toc}{\contentsline {paragraph}{\nonumberline Code 2}{16}{paragraph*.52}\protected@file@percent }
+\newlabel{code-3}{{3.1.4}{16}{Code 3}{paragraph*.54}{}}
+\@writefile{toc}{\contentsline {paragraph}{\nonumberline Code 3}{16}{paragraph*.54}\protected@file@percent }
+\@writefile{toc}{\contentsline {subsection}{\numberline {3.1.5}Reflection}{16}{subsection.3.1.5}\protected@file@percent }
+\newlabel{reflection}{{3.1.5}{16}{Reflection}{subsection.3.1.5}{}}
+\@setckpt{module2/module2}{
+\setcounter{page}{17}
+\setcounter{equation}{0}
+\setcounter{enumi}{3}
+\setcounter{enumii}{0}
+\setcounter{enumiii}{0}
+\setcounter{enumiv}{0}
+\setcounter{footnote}{0}
+\setcounter{mpfootnote}{0}
+\setcounter{part}{0}
+\setcounter{chapter}{3}
+\setcounter{section}{1}
+\setcounter{subsection}{5}
+\setcounter{subsubsection}{0}
+\setcounter{paragraph}{0}
+\setcounter{subparagraph}{0}
+\setcounter{figure}{0}
+\setcounter{table}{0}
+\setcounter{tcbbreakpart}{1}
+\setcounter{tcblayer}{0}
+\setcounter{tcolorbox@number}{6}
+\setcounter{caption@flags}{2}
+\setcounter{continuedfloat}{0}
+\setcounter{float@type}{8}
+\setcounter{parentequation}{0}
+\setcounter{FancyVerbLine}{17}
+\setcounter{section@level}{2}
+\setcounter{Item}{16}
+\setcounter{Hfootnote}{0}
+\setcounter{bookmark@seq@number}{0}
+\setcounter{LT@tables}{9}
+\setcounter{LT@chunks}{2}
+}
diff --git a/book/module2/module2.tex b/book/module2/module2.tex
new file mode 100644
index 0000000..d5e9785
--- /dev/null
+++ b/book/module2/module2.tex
@@ -0,0 +1,2 @@
+\chapter{Module 2}
+\input{module2/debugging}