% Law of demeter?
\documentclass[USenglish,9pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel,textcomp}
\usepackage[style=alphabetic,backend=biber,doi=false,isbn=false]{biblatex}
\usepackage{babel,textcomp,csquotes}
\usepackage{xspace}

\usepackage{tikz}
\usetikzlibrary{shapes,snakes,trees,arrows,shadows,positioning,calc}

\usepackage{varioref}
%\usepackage[hidelinks]{hyperref}
\usepackage{cleveref}

\usepackage{amsthm}
\usepackage{mathtools}

\usepackage{verbatim}
\usepackage{minted}
\usepackage{multicol}
%\usemintedstyle{bw}
\usemintedstyle{default}

\usetheme{Ifi}

\theoremstyle{definition}
\newtheorem*{wordDef}{Definition}
\newtheorem*{mytheorem}{Theorem}
\newcommand{\mydefinition}[1]{\begin{wordDef}#1\end{wordDef}}

\newcommand{\citing}[1]{~\cite{#1}}

\newcommand{\code}[1]{\texttt{\textbf{#1}}}
\newcommand{\type}[1]{\code{#1}}
\newcommand{\method}[1]{\type{#1}}
\newcommand{\var}[1]{\type{#1}}

\newcommand{\name}[1]{#1}
\newcommand{\tit}[1]{\emph{#1}}
\newcommand{\refa}[1]{\emph{#1}}
\newcommand{\pattern}[1]{\emph{#1}}
\newcommand{\metr}[1]{\emph{#1}}
\newcommand{\ExtractMethod}{\refa{Extract Method}\xspace}
\newcommand{\MoveMethod}{\refa{Move Method}\xspace}
\newcommand{\ExtractAndMoveMethod}{\refa{Extract and Move Method}\xspace}

\newcommand{\mysection}[1]{
  \section{#1}
  \begin{frame}
    \begin{center}{\Huge #1}\end{center}
  \end{frame}
}

\def\mintedframesep{11pt}

\def\thetitle{Automated Composition of Refactorings}
\def\subtitle{Implementing and evaluating a search-based Extract and Move Method refactoring}
\title{\thetitle}
\author{Erlend Kristiansen}


%% HEADLINE %%
\setbeamertemplate{headline}{%
  \ifnum\insertframenumber>1
\leavevmode%
  \hbox{%
    \begin{beamercolorbox}[wd=\paperwidth,ht=5ex,dp=1.125ex]{palette 
      quaternary}%
    \insertsectionnavigationhorizontal{\paperwidth}{}{\hskip0pt plus1filll}
    \end{beamercolorbox}%
  }
  \fi
}

\makeatletter
\def\insertsectionnavigationhorizontal#1#2#3{%
 \hbox to #1{{%
   \def\slideentry##1##2##3##4##5##6{}%
   #2\hskip.3cm%
   \newdimen\tmp@parbox%
   \setlength\tmp@parbox{#1}%
   \addtolength\tmp@parbox{-.6cm}%
   \parbox{\tmp@parbox}{\raggedleft%
     \usebeamerfont{section in head/foot}\usebeamercolor[fg]{section in head/foot}%
     \setbox\beamer@sectionbox=\hbox{}%
     \ht\beamer@sectionbox=1.875ex%
     \dp\beamer@sectionbox=0.75ex%
     \hskip-1.875ex plus-1fill\dohead%
     \box\beamer@sectionbox}\hfil\hskip.3cm%
     #3}}}
\makeatother

%% Remove navigation 
\setbeamertemplate{navigation symbols}{}

\bibliography{bibliography/master-thesis-erlenkr-bibliography}

\begin{document}

\begin{frame}
  \frametitle{\thetitle}
  {\subtitle}
  \vspace*{\baselineskip}

  {\small \emph{Erlend Kristiansen, 2014}}
  \vspace*{\baselineskip}
\end{frame}

\section{Definitions}

\begin{frame}
  \frametitle{Refactoring, as defined in the literature}

  \begin{quote}
    \emph{Refactoring} (noun): a change made to the internal structure
    of software to make it easier to understand and cheaper to modify without 
    changing its observable behavior.~\cite[p.~53]{refactoring}
  \end{quote}
\end{frame}

\begin{frame}
  \frametitle{An alternative definition of refactoring}

  \begin{definition}
    A \emph{refactoring} is a transformation
    done to a program without altering its external behavior.
  \end{definition}
\end{frame}

\begin{frame}
  \frametitle{Primitive and composite refactorings}
  \begin{definition}
    A \emph{primitive refactoring} is a refactoring that cannot be expressed in 
    terms of other refactorings.

  \end{definition}

  \begin{definition}
    A \emph{composite refactoring} is a refactoring that can be expressed in 
    terms of two or more other refactorings.
  \end{definition}
\end{frame}

\mysection{Motivation}

\begin{frame}[fragile]
\def\charwidth{4.5pt}
\def\indent{2*\charwidth}
\def\rightColX{32*\charwidth*0}
\def\lineheight{\baselineskip}
\def\mintedtop{22.5pt}

\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Bad
  \draw[overlaybox] (\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,2*\lineheight);
\end{tikzpicture}
\begin{minted}[linenos,samepage,frame=topline,label={Bad},framesep=\mintedframesep]{java}
class C {
  A a; B b; X x;
  void method() {
    x.y.foo();
    x.y.bar();
  }
}
class X {
  Y y;
}
class Y {
  void foo(){/*...*/}
  void bar(){/*...*/}
}
\end{minted}
\end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Good
  \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,\lineheight);

  \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*8) rectangle 
  +(19*\charwidth,4*\lineheight);

  \draw[-latex,red] (-3,2.5) -- (-0.1,\mintedtop+\lineheight*9);
  
\end{tikzpicture}
\begin{minted}[linenos,samepage,frame=topline,label={Good},framesep=\mintedframesep]{java}
class C {
  A a; B b; X x;
  void method() {
    x.fooBar();
  }
}
class X {
  Y y;
  void fooBar() {
    y.foo();
    y.bar();
  }
}
class Y {
  void foo(){/*...*/}
  void bar(){/*...*/}
}
  \end{minted}
\vfill
\end{minipage}
\end{frame}

\begin{frame}
  \begin{itemize}
    \item Get rid of long navigation paths.
    \item Move operations closer to the data they manipulate.
    \item Reduce coupling.
    \item Increase maintainability.
  
  \end{itemize}

\end{frame}

\mysection{The primitive refactorings}

\begin{frame}[fragile]
  \frametitle{The Extract Method refactoring}
  Extract a fragment of code into a new method.

\def\charwidth{4.5pt}
\def\indent{2*\charwidth}
\def\rightColX{32*\charwidth*0}
\def\lineheight{\baselineskip}
\def\mintedtop{6.5pt}

\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Bad
  \draw[overlaybox] (\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,2*\lineheight);
\end{tikzpicture}
\begin{minted}[linenos,samepage]{java}
class C {
  A a; B b; X x;
  void method() {
    x.y.foo();
    x.y.bar();
  }
}
\end{minted}
\end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Good
  \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,\lineheight);

  \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*5) rectangle 
  +(19*\charwidth,4*\lineheight);

  \draw[-latex,red] (-3,1.8) -- (-0.1,\mintedtop+\lineheight*6);
  
\end{tikzpicture}
\begin{minted}[linenos,samepage]{java}
class C {
  A a; B b; X x;
  void method() {
    fooBar();
  }
  void fooBar() {
    x.y.foo();
    x.y.bar();
  }
}
  \end{minted}
\vfill
\end{minipage}

\end{frame}


\begin{frame}[fragile]
  \frametitle{The Move Method refactoring}
  Move a method from one class to another.
  
\def\charwidth{4.5pt}
\def\indent{2*\charwidth}
\def\rightColX{32*\charwidth*0}
\def\lineheight{\baselineskip}
\def\mintedtop{6.5pt}

\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Bad
  \draw[overlaybox] (0,\mintedtop+\lineheight*5) rectangle 
  +(19*\charwidth,4*\lineheight);
\end{tikzpicture}
\begin{minted}[linenos,samepage]{java}
class C {
  A a; B b; X x;
  void method() {
    fooBar();
  }
  void fooBar() {
    x.y.foo();
    x.y.bar();
  }
}
class X {
  Y y;
}
\end{minted}
\end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Good
  \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,\lineheight);

  \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*8) rectangle 
  +(19*\charwidth,4*\lineheight);

  \draw[-latex,red] (-2.8,2.9) -- (-0.1,\mintedtop+\lineheight*9.5);
  
\end{tikzpicture}
\begin{minted}[linenos,samepage]{java}
class C {
  A a; B b; X x;
  void method() {
    x.fooBar();
  }
}
class X {
  Y y;
  void fooBar() {
    y.foo();
    y.bar();
  }
}
  \end{minted}
\vfill
\end{minipage}

\end{frame}


\mysection{The Extract and Move Method refactoring}

\begin{frame}[fragile]
\def\charwidth{4.5pt}
\def\indent{2*\charwidth}
\def\rightColX{32*\charwidth*0}
\def\lineheight{\baselineskip}
\def\mintedtop{22.5pt}

\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % Before
  \draw[overlaybox] (\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,2*\lineheight);
\end{tikzpicture}
\begin{minted}[linenos,samepage,frame=topline,label={Before},framesep=\mintedframesep]{java}
class C {
  A a; B b; X x;
  void method() {
    x.y.foo();
    x.y.bar();
  }
}
class X {
  Y y;
}
class Y {
  void foo(){/*...*/}
  void bar(){/*...*/}
}
\end{minted}
\end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  % After
  \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,\lineheight);

  \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*8) rectangle 
  +(19*\charwidth,4*\lineheight);

  \draw[-latex,red] (-3,2.5) -- (-0.1,\mintedtop+\lineheight*9);
  
\end{tikzpicture}
\begin{minted}[linenos,samepage,frame=topline,label={After},framesep=\mintedframesep]{java}
class C {
  A a; B b; X x;
  void method() {
    x.fooBar();
  }
}
class X {
  Y y;
  void fooBar() {
    y.foo();
    y.bar();
  }
}
class Y {
  void foo(){/*...*/}
  void bar(){/*...*/}
}
  \end{minted}
\vfill
\end{minipage}
\end{frame}

\begin{frame}
  \def\secondlevelmark{\textbullet}
  \begin{itemize}
    \item Composed of \ExtractMethod and \MoveMethod.
    \item Conceptually, one ``atomic'' operation.
      \vspace{2.5pt}
    \item Implemented as an Eclipse plugin.
      \vspace{2pt}
      \begin{itemize} \itemsep3pt
        \item[\secondlevelmark] The primitive refactorings are supplied by the 
          Eclipse JDT.
        \item[\secondlevelmark] The composition work had to be done by us.
        \item[\secondlevelmark] Not seamless (find the extracted method, move 
          target etc.).

      \end{itemize}

  \end{itemize}

\end{frame}

\mysection{Research questions}

\begin{frame}
  Main research question:
  \begin{quote}
    Is it possible to automate the analysis and execution of the 
    \ExtractAndMoveMethod refactoring, and do so for all of the code of a larger 
    project?
  \end{quote}
  
  Secondary questions:
  \begin{itemize}
    \item Can we do this efficiently?
    \item Can we perform changes safely?
    \item Can we improve the quality of source code?
    \item How can the automation of the refactoring be helpful?
  
  \end{itemize}
  
\end{frame}


\mysection{Automating the refactoring}

\begin{frame}[fragile]
  For any given method: We want to find the best candidate for the 
  \ExtractAndMoveMethod refactoring, if any exist.

  \begin{minipage}[b]{0.33\textwidth}
  \hfill\vspace{0.5cm}
  \end{minipage}\begin{minipage}[t]{0.5\textwidth}
\def\charwidth{4.5pt}
\def\indent{2*\charwidth}
\def\rightColX{32*\charwidth*0}
\def\lineheight{\baselineskip}
\def\mintedtop{6.5pt}

\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  \draw[overlaybox] (0,\mintedtop+\lineheight*3) rectangle 
  +(16*\charwidth,2*\lineheight);

  \node[draw opacity=0, text=red] at (20*\charwidth,\mintedtop+\lineheight*4) 
  {\fontsize{40}{60}\selectfont ?};
\end{tikzpicture}
\begin{minted}[samepage]{java}
void method() {
  statement_1;
  statement_2;
  statement_3;
  statement_4;
  statement_5;
}
\end{minted}
  \end{minipage}

\end{frame}


\begin{frame}[fragile]
\begin{minipage}[t]{0.5\textwidth}
\def\charwidth{4.75pt}
\def\indent{2*\charwidth}
\def\lineheight{\baselineskip}
\def\mintedtop{5.8pt}

\begin{tikzpicture}[overlay, yscale=-1]
  \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
  \draw[overlaybox] (2*\indent,\mintedtop+12*\lineheight) rectangle 
  +(14*\charwidth,2*\lineheight);

  \draw[overlaybox] (9*\charwidth,\mintedtop+\lineheight) rectangle 
  +(\charwidth,\lineheight);

  \node[draw opacity=0, text=red] at (12.1*\indent,\mintedtop+12.9*\lineheight) 
  {text selection};

  \node[draw opacity=0, text=red] at (11.5*\indent,\mintedtop+4*\lineheight) 
  {move target};

  \draw[-latex,red] (11.5*\indent-5*\charwidth,\mintedtop+3.7*\lineheight) -- 
  (10.2*\charwidth,\mintedtop+1.5*\lineheight);

\end{tikzpicture}
\begin{minted}[linenos,samepage]{java}
class C {
  A a; B b; boolean bool;
  void method(int val) {
    if (bool) {
      a.foo();
      a = new A();
      a.bar();
    }
    a.foo();
    a.bar();
    switch (val) {
    case 1:
      b.a.foo();
      b.a.bar();
      break;
    default:
      a.foo();
    }
  }
}
\end{minted}
\end{minipage}\begin{minipage}[t]{0.5\textwidth}
  \pause
  \vspace{1.2cm}
  A \textbf{candidate} consists of a \emph{text selection} and a \emph{move 
  target}.\\~\\

  A \textbf{valid text selection} is a text selection that contains all of one 
  or more consecutive program statements. It is the input to the \ExtractMethod 
  refactoring.\\~\\

  A \textbf{move target} is a variable (local or field), whose type is the 
  destination class in the \MoveMethod refactoring.

\end{minipage}
  
\end{frame}



\begin{frame}
  \frametitle{Searching}
  Usually, search-based refactoring is based on metrics.
  \begin{itemize}
    \item Refactor a lot.
    \item Choose the best candidate based on measurements.
  \end{itemize}\\~\\

  Our refactoring is based on heuristics.
  \begin{itemize}
    \item Up-front analysis.
    \item A set of assumptions defining what is considered the best candidate.
    \item No need to actually perform changes (before deciding).
    \item Search through all valid selections to find the best candidate.
  \end{itemize}

\end{frame}


\begin{frame}
  \frametitle{Choosing a refactoring candidate}
  \begin{itemize}
    \item Search through all selections to find the possible candidates.
    \item Find the best move target for all the candidates.
    \item Choose the best among the possible candidates.
    \item Based on the lengths of the navigation paths and the occurrence 
      counts.
  
  \end{itemize}
\end{frame}


\mysection{Demonstration}

\mysection{Case studies}

\begin{frame}
  Case studies performed on the \code{org.eclipse.jdt.ui} and 
  \code{no.uio.ifi.refaktor} projects. The resulting code was analyzed with 
  SonarQube.
  \vspace*{\baselineskip}

  The Eclipse JDT UI project:
  \begin{itemize}
    \item Over 300,000 lines of code.
    \item 2,552 methods out of 27,667 methods chosen to be refactored.
    \item Approx. 100 minutes.
  
  \end{itemize}

\end{frame}


\begin{frame}
  \frametitle{The case studies are inconclusive}
  \begin{itemize}
    \item Measurements show some deterioration regarding coupling.
    \item All improvement not measured, only strict coupling between classes.
    \item Examples exists where coupling is improved.
    \item More examples exists where dependencies are introduced.

  \end{itemize}
\end{frame}

\mysection{Demonstration continued}

\mysection{Conclusions}

\begin{frame}
  \begin{itemize}
    \item Automation is possible.
    \item Efficient enough for some kinds of use.
    \item Difficult not to break source code.
    \item Code is not improved in most cases.
    \item Not particularly useful in its current state.
  
  \end{itemize}
\end{frame}

\mysection{Future work}

\begin{frame}
\begin{itemize}
  \item Complete analysis.
  \item Make refactoring safer.
  \item Improve heuristics to avoid introducing new dependencies.

\end{itemize}
\end{frame}

\begin{frame}
  \frametitle{References}
  \printbibliography
\end{frame}

\end{document}
