

% add the PATH to retrieve cls and sty beamer styles files
\makeatletter
\providecommand*{\input@path}{}
\edef\input@path{{../../support/beamer/}\input@path}% prepend
\makeatother

% \documentclass{pharoslides}
% \documentclass[aspectratio=169]{pharoslides} % format 16/9
\documentclass[withvideo]{pharoslides} % format 16/9 + incrustation video

\graphicspath{{./figures/}{../}{../../support/beamer/imgs/}}

%Information to be included in the title page:
\title{Common Errors}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W5S03}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{What You Will Learn}

Find and fix common mistakes faster
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
DiceHandle >> + aDiceHandle
   | res |
   res := DiceHandle new
   self dices do: [ :each | ... ].
\end{listing}


\begin{center}
\includegraphics[width=0.9\textwidth]{figures/DebugSelf2-cut.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Missing Period}

\begin{listing}{}
DiceHandle >> + aDiceHandle
   | res |
   res := DiceHandle new.
   self dices do: [ :each | ... ].
\end{listing}

\begin{itemize}
\item Separate instructions with period (\code{.})
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
x includes: 33
   ifTrue: [ self do something ]
\end{listing}

\textbf{Error:} Message \code{includes:ifTrue:} does not exist

\begin{listing}{}
self assert: players includes: aPlayer
\end{listing}

\textbf{Error:} Message \code{assert:includes:} does not exist
\end{frame}



\begin{frame}[fragile]

\frametitle{Keyword-Based Messages}

Solution

\begin{listing}{}
self assert: (players includes: aPlayer)
\end{listing}

\begin{itemize}
\item keyword messages are built out of fragments
\item the message is the longuest sequence of fragments
\item use parentheses to delimit multiple keyword messages
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
numbers := OrderedCollection new
             add: 35
\end{listing}

\textbf{Error:} \code{numbers} is the number 35 and not a collection

\begin{listing}{}
Dice >> setFaces: aNumber
   ^ faces := aNumber

Dice class >> new
   ^ super new setFaces: 6
\end{listing}

\textbf{Error:} \code{Dice new} returns 6 instead of a dice
\end{frame}



\begin{frame}[fragile]

\frametitle{Forgotten yourself}

\begin{listing}{}
numbers := OrderedCollection new
             add: 35;
             yourself
\end{listing}

is equivalent to

\begin{listing}{}
| numbers |
numbers := OrderedCollection new.
numbers add: 35.
numbers
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Forgotten yourself}

Solutions

\begin{listing}{}
numbers := OrderedCollection new
             add: 35;
             yourself
\end{listing}

\begin{listing}{}
Dice class >> new
   ^ super new setFaces: 6; yourself
\end{listing}

\begin{itemize}
\item \code{add:} and \code{setFaces:} return their argument, not the receiver
\item send \code{yourself} after a sequence of messages if you want the receiver
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Book>>borrow
   inLibrary ifFalse: [ ... ].
   ...
\end{listing}

\textbf{Error:} \code{nil} does not understand \code{ifFalse:}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Book>>initialize
   inLibrary := True

Book>>borrow
   inLibrary ifFalse: [ ... ].
  ...
\end{listing}

\textbf{Error:} Class \code{True} does not understand \code{ifFalse:}
\end{frame}



\begin{frame}[fragile]

\frametitle{True vs. true}

Solution

\begin{listing}{}
Book>>initialize
   inLibrary := true
\end{listing}

\begin{itemize}
\item \code{nil} is the unique instance of the class \code{UndefinedObject}
\item \code{true} is the unique instance of the class \code{True}
\item Class names start with an uppercase letter
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Dice >> roll
   faces atRandom
\end{listing}

\textbf{Error:} \code{aDice roll} returns \code{aDice} instead of a number
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Dice >> roll
   faces atRandom
\end{listing}

is equivalent to

\begin{listing}{}
Dice >> roll
   faces atRandom.
   ^ self
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Dice class >> new
   super new
      setFaces: 0;
      yourself
\end{listing}

\textbf{Error:} \code{Dice new} returns the class instead of the new instance
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Dice class >> new
   super new
      setFaces: 0;
      yourself
\end{listing}

is equivalent to

\begin{listing}{}
Dice class >> new
   super new
      setFaces: 0;
      yourself.
   ^ self
\end{listing}

\begin{itemize}
\item \code{new} is sent to a class
\item \code{self} is the class \code{Dice}
\item returns \code{Dice} and not its newly created instance
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Forgetting to Return the Result}

Solutions

\begin{listing}{}
Dice >> roll
   ^ faces atRandom

Dice class >> new
   ^ super new
      setFaces: 0;
      yourself
\end{listing}

\begin{itemize}
\item in a method, \code{self} is returned by default
\item do not forget the caret \code{\string^} to return something else
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{A Problem}

\begin{listing}{}
Dice class >> new
   ^ self new
      setFaces: 0;
      yourself
\end{listing}

\textbf{Error:} System is frozen
\end{frame}



\begin{frame}[fragile]

\frametitle{Infinite Loops in Overridden Methods}

Solution

\begin{listing}{}
Dice class >> new
   ^ super new
      setFaces: 0;
      yourself
\end{listing}

\begin{itemize}
\item use \code{super} in overridden methods
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{What You Should Know}

\begin{itemize}
\item How to identify common errors faster
\item Check periods \code{.}
\item Check parentheses \code{(} and \code{)}
\item Check carets \code{\string^}
\item Check \code{yourself}
\item Use the debugger to understand the problem
\end{itemize}
\end{frame}




% \setlength{\textwidth}{\originaltextwidth}
\setbeamertemplate{background canvas}{\insertBorderForHandout}

\begin{frame}[plain]

\begin{minipage}{\originaltextwidth}
\centering
\large


\vspace{2em}

A course by

\vspace{1em}

\hspace{-20px}\includegraphics[height=32px]{inriaLogo}\hspace{3em}\raisebox{7px}{and}\hspace{3em}\raisebox{-6px}{\includegraphics[height=30px]{logoutop}}

\vspace{2em}

in collaboration with

\vspace{1em}

\includegraphics[height=40px]{unisciellogo}\hskip 1.5em%
\raisebox{5px}{\includegraphics[height=30px]{unitlogo}}\hskip 2em%
\raisebox{8px}{\includegraphics[height=24px]{univLilleLogo}}\hskip 2em%
\raisebox{3px}{\includegraphics[height=29px]{logo-IMT}}\hskip 2em%
\raisebox{1px}{\includegraphics[height=32px]{minesDouaiLogo}}

\vspace{2.5em}

\includegraphics[width=50px]{by-nc-nd} \tiny \hskip 0.5em \raisebox{5px}{Inria 2016}

\medskip

\tiny
Except where otherwise noted, this work is licensed under CC BY-NC-ND 3.0 France

\url{https://creativecommons.org/licenses/by-nc-nd/3.0/fr/} 

\end{minipage}
\end{frame}

\end{document}

%%% Local Variables:
%%% mode: latex
%%% TeX-master: t
%%% End:
