

% 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{Message Sends are Plans for Reuse}
\subtitle{}
\author{Damien Cassou, Stéphane Ducasse and Luc Fabresse}
\institute{http://stephane.ducasse.free.fr}
\date{}
\slidesid{W6S04}

\begin{document}

\frame[plain]{\titlepage}



\begin{frame}[fragile]

\frametitle{About This Lecture}

Another design lecture:

\begin{itemize}
\item Next step of the \code{not} implementation lecture
\item Relevant to any object-oriented language
\item May change your view on design
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{What You Will Learn}

\begin{itemize}
\item Message sends are hooks for subclasses
\item \textit{\symbol{34}I like big methods because I can see all the code\symbol{34}} leads to bad design
\item Why writing small methods is a sign of good design
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Sending A Message Leads to a Choice}

\begin{itemize}
\item a message send leads to a choice
\item a class hierarchy defines the choices
\item \code{self} always represents the receiver
\item method lookup starts in the class of the receiver
\end{itemize}


\begin{center}
\includegraphics[width=0.95\textwidth]{figures/Design-FatVsDispatch.png}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{An Example}

\begin{listing}{}
Node >> setWindowWithRatioForDisplay
   | defaultNodeSize |
   defaultNodeSize := mainCoordinate / maximizeViewRatio.
   self window add:
      (UINode new
         with: bandWidth * 55 / defaultWindowSize).
   previousNodeSize := defaultNodeSize.
\end{listing}

We want to change the \code{defaultNodeSize} formula in a subclass
\end{frame}



\begin{frame}[fragile]

\frametitle{Duplication}

Duplicate the code in a subclass

\begin{listing}{}
Node subclass: OurSpecificNode
   ...
\end{listing}

\begin{listing}{}
OurSpecificNode >> setWindowWithRatioForDisplay
  | defaultNodeSize |
  defaultNodeSize :=
     (mainCoordinate / maximizeViewRatio) + 10.
  self window add:
      (UINode new
         with: bandWidth * 55 / defaultWindowSize).
  previousNodeSize := defaultNodeSize.
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Avoid Duplication}

\begin{itemize}
\item in Java-like languages, using \code{private} attributes makes duplication in subclasses impossible
\item duplication is not a good practice:
\begin{itemize}
\item duplication copies bugs
\item changing one copy requires changing others
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Solution}

\begin{columns}

\begin{column}{0.5\textwidth}

\begin{itemize}
\item send messages
\item define small methods
\end{itemize}

Subclasses can override such methods
\end{column}


\begin{column}{0.5\textwidth}


\begin{center}
\includegraphics[width=0.85\textwidth]{figures/SelfIsDynamic.png}\end{center}

\end{column}

\end{columns}

\end{frame}



\begin{frame}[fragile]

\frametitle{We can Refactor this}

\begin{listing}{}
Node >> setWindowWithRatioForDisplay
  | defaultNodeSize |
  defaultNodeSize := (mainCoordinate / maximizeViewRatio).
  self window add:
      (UINode new
         with: bandWidth * 55 / defaultWindowSize).
  previousNodeSize := defaultNodeSize.
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Better Design}

\begin{listing}{}
Node >> setWindowWithRatioForDisplay
   | defaultNodeSize |
   defaultNodeSize := self ratio.
   self window add:
      (UINode new
         with: bandWidth * 55 / defaultWindowSize).
   previousNodeSize := defaultNodeSize.

Node >> ratio
   ^ mainCoordinate / maximizeViewRatio
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Subclasses Reuse Superclass Logic}

\begin{listing}{}
Node >> ratio
   ^ mainCoordinate / maximizeViewRatio
\end{listing}

A subclass can refine the behavior

\begin{listing}{}
OurSpecificNode >> ratio
   ^ super ratio + 10
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Another Step}

\begin{listing}{}
Node >> setWindowWithRatioForDisplay
   | defaultNodeSize |
   defaultNodeSize := self ratio.
   self window add:
      (UINode new
         with: bandWidth * 55 / defaultWindowSize).
   previousNodeSize := defaultNodeSize.
\end{listing}

We can also extract the \code{UINode} instantiation.
\end{frame}



\begin{frame}[fragile]

\frametitle{Another Step}

\begin{listing}{}
Node >> setWindowWithRatioForDisplay
   | defaultNodeSize |
   defaultNodeSize := self ratio.
   self window add: self uiNode.
   previousNodeSize := defaultNodeSize.
\end{listing}

\begin{listing}{}
Node >> uiNode
   ^ UINode new
      with: bandWidth * 55 / defaultWindowSize
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Do Not Hardcode Class Use}

\begin{listing}{}
Node >> uiNode
   ^ UINode new
      with: bandWidth * 55 / defaultWindowSize
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Define Methods Returning Classes}

\begin{listing}{}
Node >> uiNode
   ^ self uiNodeClass new
      with: bandWidth * 55 / defaultWindowSize.
\end{listing}

\begin{listing}{}
Node >> uiNodeClass
   ^ UINode
\end{listing}
\end{frame}



\begin{frame}[fragile]

\frametitle{Many Small Messages}

\begin{itemize}
\item Some developers complain about all these small methods
\item They try to understand code line by line
\item This does not scale
\end{itemize}

Small messages are a sign of good design
\end{frame}



\begin{frame}[fragile]

\frametitle{Avoid Magic Numbers}

\begin{listing}{}
Node >> uiNode
   ^ self uiNodeClass new
      with: bandWidth * 55 / defaultWindowSize.
\end{listing}

\begin{itemize}
\item subclasses may want to change values
\begin{itemize}
\item do not hardcode magic numbers (55)
\end{itemize}

\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Use a Message Send}

\begin{listing}{}
Node >> uiNode
   ^ self uiNodeClass new
      with: bandWidth * self averageRatio / defaultWindowSize.

Node >> averageRatio
   ^ 55
\end{listing}

\begin{itemize}
\item this gives a name to a value
\item subclasses can override the value
\end{itemize}

How to let the class users change the value?
\end{frame}



\begin{frame}[fragile]

\frametitle{Use an Instance Variable}

\begin{listing}{}
Node >> averageRatio
   ^ averageRatio ifNil: [ self defaultAverageRatio ]
Node >> defaultAverageRatio
   ^ 55
Node >> averageRatio: aNumber
   averageRatio := aNumber
\end{listing}

\begin{itemize}
\item subclasses can override the value
\item class users can set the value
\end{itemize}
\end{frame}



\begin{frame}[fragile]

\frametitle{Gruyere-Oriented Programming}


\begin{center}
\includegraphics[width=0.95\textwidth]{figures/gruyereWithSlogan}\end{center}

\end{frame}



\begin{frame}[fragile]

\frametitle{Conclusion}

\begin{itemize}
\item Code can be reused and refined in subclasses
\item Sending a message in a class defines a hook:
\begin{itemize}
\item i.e., a place where subclasses can inject variations
\end{itemize}

\item Prefer small methods because:
\begin{itemize}
\item this gives names to expressions
\item this gives freedom to subclasses
\end{itemize}

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