% Motivate! (coupling)
% Rework examples
% Law of demeter?
\documentclass[USenglish,9pt]{beamer}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{babel,textcomp}
\usepackage[style=alphabetic,backend=biber]{biblatex}
\usepackage{babel,textcomp,csquotes}
\usepackage{xspace}

\usepackage{tikz}
\usepackage[beamer]{hf-tikz}
\usepackage{tikz-qtree}
\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{\whatsnext}[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}

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

\begin{document}

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

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

\begin{frame}
  \frametitle{Refactoring, as defined in the literature}
  In his book on refactoring\citing{refactoring}, Martin Fowler defines a
  refactoring like this:

  \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}
  If we leave the motivation behind refactoring out of the definition, it could 
  be rephrased like this:

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

\begin{frame}
  \frametitle{Composite refactorings}
  \emph{Primitive refactorings} are refactorings that cannot be expressed in 
  terms of other refactorings. From these, we can express \emph{composite
  refactorings}:

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

\whatsnext{Motivation\ldots}

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


\whatsnext{The product\ldots}

\begin{frame}
  \frametitle{The Extract and Move Method refactoring}
  The main product of this thesis work is a search-based composite refactoring 
  composed of the primitive refactorings \ExtractMethod and \MoveMethod. We call 
  this composition for the \ExtractAndMoveMethod refactoring.

\end{frame}

\whatsnext{The primitive refactorings\ldots}

\begin{frame}[fragile]
  \frametitle{The Extract Method refactoring}
  The \refa{Extract Method} refactoring is used to extract a fragment of code 
  out of its context and into a new method. A call to this new method is inlined 
  where the fragment was before. The refactoring is used to break code into 
  logical units, with names that explain their purpose.

  \begin{multicols}{2}
    \begin{minted}[samepage]{java}
  class C {
    void method() {
      // 1: Some code
      // 2: Fragment
      // 3: More code
    }
  }
    \end{minted}

    \columnbreak

    \begin{minted}[samepage]{java}
  class C {
    void method() {
      // 1: Some code
      extractedMethod();
      // 3: More code
    }

    void extractedMethod() {
      // 2: Fragment
    }
  }
    \end{minted}
  \end{multicols}

\end{frame}

\begin{frame}[fragile]
  \frametitle{The Move Method refactoring}
  The \refa{Move Method} refactoring is used to move a method from one class to 
  another. This is useful if a method is using more features of another class 
  than of the class which it is currently defined. 
  
  \begin{multicols}{2}
    \begin{minted}[samepage]{java}
  class C {
    void method() {
      X x = new X();
      iBelongInX(x);
    }
    void iBelongInX(X x) {
      x.foo(); x.bar();
    }
  }
  
  class X {
    void foo(){/*...*/}
    void bar(){/*...*/}
  }
    \end{minted}

    \columnbreak

    \begin{minted}[samepage]{java}
  class C {
    void method() {
      X x = new X();
      x.iBelongInX();
    }
  }

  class X {
    void iBelongInX() {
      foo(); bar();
    }
    void foo(){/*...*/}
    void bar(){/*...*/}
  }
    \end{minted}
  \end{multicols}

\end{frame}

\whatsnext{Composing the two\ldots}

\begin{frame}[fragile]
  \frametitle{The Composition}

  \begin{minted}[samepage]{java}
  // Before
  class C {
    void method() {
      X x = new X();
      x.foo(); x.bar();
    }
  }
  
  class X {
    void foo(){/*...*/}
    void bar(){/*...*/}
  }
  \end{minted}

\end{frame}

\begin{frame}[fragile]
  \frametitle{The Composition}

  \begin{minted}[samepage]{java}
  // Intermediate step
  class C {
    void method() {
      X x = new X();
      extractedMethod(x);
    }
    void extractedMethod(X x) {
      x.foo(); x.bar();
    }
  }
  
  class X {
    void foo(){/*...*/}
    void bar(){/*...*/}
  }
  \end{minted}

\end{frame}

\begin{frame}[fragile]
  \frametitle{The Composition}

  \begin{multicols}{2}
    \begin{minted}[samepage]{java}
  // Before
  class C {
    void method() {
      X x = new X();
      x.foo(); x.bar();
    }
  }
  
  class X {
    void foo(){/*...*/}
    void bar(){/*...*/}
  }
    \end{minted}

    \columnbreak

    \begin{minted}[samepage]{java}
  // After
  class C {
    void method() {
      X x = new X();
      x.extractedMethod();
    }
  }

  class X {
    void extractedMethod() {
      foo(); bar();
    }
    void foo(){/*...*/}
    void bar(){/*...*/}
  }
    \end{minted}
  \end{multicols}

\end{frame}

\whatsnext{Automation\ldots}

\begin{frame}
  \frametitle{The refactoring is search-based}
  The search is based on heuristics, not metrics.

\end{frame}


\begin{frame}
  \frametitle{The search space}
  \begin{itemize}
    \item All possible selections of statements in a method.
    \item Disqualifying illegal selections (avoiding problems).
  
  \end{itemize}

\end{frame}


\begin{frame}
  \frametitle{Choosing a refactoring candidate}
  \begin{itemize}
    \item Find the best move target for all selections.
    \item Choose the best candidate between the selections.
  
  \end{itemize}
\end{frame}


\begin{frame}
  \frametitle{Finding a move target for a selection} 
  
  \begin{itemize}
    \item Find possible move targets (prefixes).
    \item Find the prefixes not suitable as move targets (unfixes).
    \item Find the best move target among the suitable targets (prefixes minus 
      unfixes).

  \end{itemize}

\end{frame}

\whatsnext{What do we hope to achieve?}

\begin{frame}[fragile]
  \frametitle{The goal}
  We would like to minimize coupling between classes.
  \vspace*{\baselineskip}

  Example:
  \begin{minted}[samepage]{java}
  a.b.foo()
  \end{minted}

  should be
  \begin{minted}[samepage]{java}
  a.foo()
  \end{minted}

  plus
  \begin{minted}[samepage]{java}
  class A {  
    B b;
    /* ... */
    public void foo() {
      b.foo()
    }
  }
  \end{minted}

\end{frame}


\whatsnext{Demonstration\ldots}

\whatsnext{Is it any good?}

\begin{frame}
  \frametitle{Case studies performed}
  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}

\whatsnext{Demonstration continued\ldots}

\whatsnext{Conclusions\ldots}

\begin{frame}
  \frametitle{Conclusions}
  \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}



%\begin{frame}
%  \frametitle{Demonstration}
%  \begin{itemize}
%    \item {The      
%        \code{LastStatementOfSelectionEndsInReturnOrThrow\-Checker.visit(IfStatement 
%        node)} method}
%      \begin{itemize}
%        \item Extract and Move on selection
%        \item Extract and Move, search based, on method
%      \end{itemize}
%    \item The \code{no.uio.ifi.refaktor} project
%      \begin{itemize}
%        \item Extract and Move, search based, over whole project
%      \end{itemize}
%  \end{itemize}
%
%\end{frame}


%\begin{frame}
%  \frametitle{What is left}
%  \begin{itemize}
%    \item Write technical section
%    \item Write up argument for correctness
%    \item Define the final case study
%    \item Run unit tests before and after change
%    \item Make more examples
%    \item Metrics?
%    \item \ldots
%  \end{itemize}
% 
%\end{frame}

\whatsnext{The end.}

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

\end{document}
