]> git.uio.no Git - ifi-stolz-refaktor.git/blob - nik2014/nik2014.tex
Actually add the JDT UI sources as well, not only the tests.
[ifi-stolz-refaktor.git] / nik2014 / nik2014.tex
1 % Law of demeter?
2 \documentclass[USenglish,9pt]{beamer}
3 \usepackage[utf8]{inputenc}
4 \usepackage[T1]{fontenc}
5 \usepackage{lmodern}
6 \usepackage{babel,textcomp}
7 \usepackage[style=alphabetic,backend=biber,doi=false,isbn=false]{biblatex}
8 \usepackage{babel,textcomp,csquotes}
9 \usepackage{xspace}
10
11 \usepackage{tikz}
12 \usetikzlibrary{shapes,snakes,trees,arrows,shadows,positioning,calc}
13
14 \usepackage{varioref}
15 %\usepackage[hidelinks]{hyperref}
16 \usepackage{cleveref}
17
18 \usepackage{amsthm}
19 \usepackage{mathtools}
20
21 \usepackage{verbatim}
22 \usepackage{minted}
23 \usepackage{multicol}
24 %\usemintedstyle{bw}
25 \usemintedstyle{default}
26
27 \usetheme{Ifi}
28
29 \theoremstyle{definition}
30 \newtheorem*{wordDef}{Definition}
31 \newtheorem*{mytheorem}{Theorem}
32 \newcommand{\mydefinition}[1]{\begin{wordDef}#1\end{wordDef}}
33
34 \newcommand{\citing}[1]{~\cite{#1}}
35
36 \newcommand{\code}[1]{\texttt{\textbf{#1}}}
37 \newcommand{\type}[1]{\code{#1}}
38 \newcommand{\method}[1]{\type{#1}}
39 \newcommand{\var}[1]{\type{#1}}
40
41 \newcommand{\name}[1]{#1}
42 \newcommand{\tit}[1]{\emph{#1}}
43 \newcommand{\refa}[1]{\emph{#1}}
44 \newcommand{\pattern}[1]{\emph{#1}}
45 \newcommand{\metr}[1]{\emph{#1}}
46 \newcommand{\ExtractMethod}{\refa{Extract Method}\xspace}
47 \newcommand{\MoveMethod}{\refa{Move Method}\xspace}
48 \newcommand{\ExtractAndMoveMethod}{\refa{Extract and Move Method}\xspace}
49
50 \newcommand{\mysection}[1]{
51   \section{#1}
52   \begin{frame}
53     \begin{center}{\Huge #1}\end{center}
54   \end{frame}
55 }
56
57 \def\mintedframesep{11pt}
58
59 \def\thetitle{Search-based Composed Refactorings}
60 \def\subtitle{Implementing and evaluating a search-based Extract and Move Method refactoring}
61 \title{\thetitle}
62 \author{Erlend Kristiansen, Volker Stolz}
63
64
65 %% HEADLINE %%
66 \setbeamertemplate{headline}{%
67   \ifnum\insertframenumber>1
68 \leavevmode%
69   \hbox{%
70     \begin{beamercolorbox}[wd=\paperwidth,ht=5ex,dp=1.125ex]{palette 
71       quaternary}%
72     \insertsectionnavigationhorizontal{\paperwidth}{}{\hskip0pt plus1filll}
73     \end{beamercolorbox}%
74   }
75   \fi
76 }
77
78 \makeatletter
79 \def\insertsectionnavigationhorizontal#1#2#3{%
80  \hbox to #1{{%
81    \def\slideentry##1##2##3##4##5##6{}%
82    #2\hskip.3cm%
83    \newdimen\tmp@parbox%
84    \setlength\tmp@parbox{#1}%
85    \addtolength\tmp@parbox{-.6cm}%
86    \parbox{\tmp@parbox}{\raggedleft%
87      \usebeamerfont{section in head/foot}\usebeamercolor[fg]{section in head/foot}%
88      \setbox\beamer@sectionbox=\hbox{}%
89      \ht\beamer@sectionbox=1.875ex%
90      \dp\beamer@sectionbox=0.75ex%
91      \hskip-1.875ex plus-1fill\dohead%
92      \box\beamer@sectionbox}\hfil\hskip.3cm%
93      #3}}}
94 \makeatother
95
96 %% Remove navigation 
97 \setbeamertemplate{navigation symbols}{}
98
99 \bibliography{bibliography/master-thesis-erlenkr-bibliography}
100
101 \begin{document}
102
103 \begin{frame}
104   \frametitle{\thetitle}
105 %  {\subtitle}
106 %  \vspace*{\baselineskip}
107
108   { \emph{Erlend Kristiansen, Universitetet i Oslo}}
109
110    {~~~~and}
111
112   { \emph{\underline{Volker Stolz, H\o gskolen i Bergen}}}
113   \vspace*{\baselineskip}
114 \end{frame}
115
116 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
117
118 \begin{frame}
119   \frametitle{Outline and Motivation}
120   \begin{itemize}
121   \item What are refactorings?
122   \item What are their problems?
123   \item How can we help?
124   \item Experiment and evaluation
125   \end{itemize}
126 \end{frame}
127 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
128
129 %\section{Definitions}
130
131 \begin{frame}
132   \frametitle{Refactoring, as defined in the literature}
133
134   \begin{quote}
135     \emph{Refactoring} (noun): a change made to the internal structure
136     of software to make it easier to understand and cheaper to modify without 
137     changing its observable behavior.~{\bf [Fowler]}
138   \end{quote}
139 \end{frame}
140
141 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
142 \begin{frame}
143   \frametitle{Why are they necessary?}
144   \vspace*{-2mm}
145   \includegraphics[scale=0.3]{abs-motivation.png}
146   
147   \vspace*{-2mm}
148   \begin{itemize}
149   \item anecdotal evidence from our own project
150   \item ``refactoring smells'' that should be avoided by programmers
151 %  \item (infrastructure developed here can be used for survey on repositories)
152   \end{itemize}
153 \end{frame}
154 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
155 \begin{frame}
156   \frametitle{An alternative definition of refactoring}
157
158   \begin{definition}
159     A \emph{refactoring} is a transformation
160     done to a program without altering its external behavior.
161   \end{definition}
162 \end{frame}
163
164 \begin{frame}
165   \frametitle{Primitive and composite refactorings}
166   \begin{definition}
167     A \emph{primitive refactoring} is a refactoring that cannot be expressed in 
168     terms of other refactorings.
169
170   \end{definition}
171
172   \begin{definition}
173     A \emph{composite refactoring} is a refactoring that can be expressed in 
174     terms of two or more other refactorings.
175   \end{definition}
176 \end{frame}
177
178 \section{Motivation}
179
180 \begin{frame}[fragile]
181 \def\charwidth{4.5pt}
182 \def\indent{2*\charwidth}
183 \def\rightColX{32*\charwidth*0}
184 \def\lineheight{\baselineskip}
185 \def\mintedtop{22.5pt}
186
187 \begin{minipage}[t]{0.45\textwidth}
188 \begin{tikzpicture}[overlay, yscale=-1]
189   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
190   % Bad
191   \draw[overlaybox] (\indent,\mintedtop+\lineheight*3) rectangle 
192   +(16*\charwidth,2*\lineheight);
193 \end{tikzpicture}
194 \begin{minted}[linenos,samepage,frame=topline,label={Bad},framesep=\mintedframesep]{java}
195 class C {
196   A a; B b; X x;
197   void method() {
198     x.y.foo();
199     x.y.bar();
200   }
201 }
202 class X {
203   Y y;
204 }
205 class Y {
206   void foo(){/*...*/}
207   void bar(){/*...*/}
208 }
209 \end{minted}
210 \end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
211 \begin{tikzpicture}[overlay, yscale=-1]
212   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
213   % Good
214   \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
215   +(16*\charwidth,\lineheight);
216
217   \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*8) rectangle 
218   +(19*\charwidth,4*\lineheight);
219
220   \draw[-latex,red] (-3,2.5) -- (-0.1,\mintedtop+\lineheight*9);
221   
222 \end{tikzpicture}
223 \begin{minted}[linenos,samepage,frame=topline,label={Good},framesep=\mintedframesep]{java}
224 class C {
225   A a; B b; X x;
226   void method() {
227     x.fooBar();
228   }
229 }
230 class X {
231   Y y;
232   void fooBar() {
233     y.foo();
234     y.bar();
235   }
236 }
237 class Y {
238   void foo(){/*...*/}
239   void bar(){/*...*/}
240 }
241   \end{minted}
242 \vfill
243 \end{minipage}
244 \end{frame}
245
246 \begin{frame}{What our refactoring is going to do for you}
247   \begin{itemize}
248     \item Get rid of long navigation paths.
249     \item Move operations closer to the data they manipulate.
250     \item Reduce coupling.
251     \item Increase maintainability.
252     \item []
253     \item Automatically!
254   
255   \end{itemize}
256
257 \end{frame}
258
259 \mysection{The primitive refactorings}
260
261 \begin{frame}[fragile]
262   \frametitle{The Extract Method refactoring}
263   Extract a fragment of code into a new method.
264
265 \def\charwidth{4.5pt}
266 \def\indent{2*\charwidth}
267 \def\rightColX{32*\charwidth*0}
268 \def\lineheight{\baselineskip}
269 \def\mintedtop{6.5pt}
270
271 \begin{minipage}[t]{0.45\textwidth}
272 \begin{tikzpicture}[overlay, yscale=-1]
273   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
274   % Bad
275   \draw[overlaybox] (\indent,\mintedtop+\lineheight*3) rectangle 
276   +(16*\charwidth,2*\lineheight);
277 \end{tikzpicture}
278 \begin{minted}[linenos,samepage]{java}
279 class C {
280   A a; B b; X x;
281   void method() {
282     x.y.foo();
283     x.y.bar();
284   }
285 }
286 \end{minted}
287 \end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
288 \begin{tikzpicture}[overlay, yscale=-1]
289   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
290   % Good
291   \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
292   +(16*\charwidth,\lineheight);
293
294   \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*5) rectangle 
295   +(19*\charwidth,4*\lineheight);
296
297   \draw[-latex,red] (-3,1.8) -- (-0.1,\mintedtop+\lineheight*6);
298   
299 \end{tikzpicture}
300 \begin{minted}[linenos,samepage]{java}
301 class C {
302   A a; B b; X x;
303   void method() {
304     fooBar();
305   }
306   void fooBar() {
307     x.y.foo();
308     x.y.bar();
309   }
310 }
311   \end{minted}
312 \vfill
313 \end{minipage}
314
315 \end{frame}
316
317
318 \begin{frame}[fragile]
319   \frametitle{The Move Method refactoring}
320   Move a method from one class to another.
321   
322 \def\charwidth{4.5pt}
323 \def\indent{2*\charwidth}
324 \def\rightColX{32*\charwidth*0}
325 \def\lineheight{\baselineskip}
326 \def\mintedtop{6.5pt}
327
328 \begin{minipage}[t]{0.45\textwidth}
329 \begin{tikzpicture}[overlay, yscale=-1]
330   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
331   % Bad
332   \draw[overlaybox] (0,\mintedtop+\lineheight*5) rectangle 
333   +(19*\charwidth,4*\lineheight);
334 \end{tikzpicture}
335 \begin{minted}[linenos,samepage]{java}
336 class C {
337   A a; B b; X x;
338   void method() {
339     fooBar();
340   }
341   void fooBar() {
342     x.y.foo();
343     x.y.bar();
344   }
345 }
346 class X {
347   Y y;
348 }
349 \end{minted}
350 \end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
351 \begin{tikzpicture}[overlay, yscale=-1]
352   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
353   % Good
354   \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
355   +(16*\charwidth,\lineheight);
356
357   \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*8) rectangle 
358   +(19*\charwidth,4*\lineheight);
359
360   \draw[-latex,red] (-2.8,2.9) -- (-0.1,\mintedtop+\lineheight*9.5);
361   
362 \end{tikzpicture}
363 \begin{minted}[linenos,samepage]{java}
364 class C {
365   A a; B b; X x;
366   void method() {
367     x.fooBar();
368   }
369 }
370 class X {
371   Y y;
372   void fooBar() {
373     y.foo();
374     y.bar();
375   }
376 }
377   \end{minted}
378 \vfill
379 \end{minipage}
380
381 \end{frame}
382
383
384 \mysection{The Extract and Move Method refactoring}
385
386 \begin{frame}[fragile]
387 \def\charwidth{4.5pt}
388 \def\indent{2*\charwidth}
389 \def\rightColX{32*\charwidth*0}
390 \def\lineheight{\baselineskip}
391 \def\mintedtop{22.5pt}
392
393 \begin{minipage}[t]{0.45\textwidth}
394 \begin{tikzpicture}[overlay, yscale=-1]
395   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
396   % Before
397   \draw[overlaybox] (\indent,\mintedtop+\lineheight*3) rectangle 
398   +(16*\charwidth,2*\lineheight);
399 \end{tikzpicture}
400 \begin{minted}[linenos,samepage,frame=topline,label={Before},framesep=\mintedframesep]{java}
401 class C {
402   A a; B b; X x;
403   void method() {
404     x.y.foo();
405     x.y.bar();
406   }
407 }
408 class X {
409   Y y;
410 }
411 class Y {
412   void foo(){/*...*/}
413   void bar(){/*...*/}
414 }
415 \end{minted}
416 \end{minipage}\hspace{0.1\textwidth}\pause\begin{minipage}[t]{0.45\textwidth}
417 \begin{tikzpicture}[overlay, yscale=-1]
418   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
419   % After
420   \draw[overlaybox] (\rightColX+\indent,\mintedtop+\lineheight*3) rectangle 
421   +(16*\charwidth,\lineheight);
422
423   \draw[overlaybox] (\rightColX,\mintedtop+\lineheight*8) rectangle 
424   +(19*\charwidth,4*\lineheight);
425
426   \draw[-latex,red] (-3,2.5) -- (-0.1,\mintedtop+\lineheight*9);
427   
428 \end{tikzpicture}
429 \begin{minted}[linenos,samepage,frame=topline,label={After},framesep=\mintedframesep]{java}
430 class C {
431   A a; B b; X x;
432   void method() {
433     x.fooBar();
434   }
435 }
436 class X {
437   Y y;
438   void fooBar() {
439     y.foo();
440     y.bar();
441   }
442 }
443 class Y {
444   void foo(){/*...*/}
445   void bar(){/*...*/}
446 }
447   \end{minted}
448 \vfill
449 \end{minipage}
450 \end{frame}
451
452 \begin{frame}
453   \def\secondlevelmark{\textbullet}
454   \begin{itemize}
455     \item Composed of \ExtractMethod and \MoveMethod.
456     \item Conceptually, one ``atomic'' operation.
457       \vspace{2.5pt}
458     \item Implemented as an Eclipse plugin.
459       \vspace{2pt}
460       \begin{itemize} \itemsep3pt
461         \item[\secondlevelmark] The primitive refactorings are supplied by the 
462           Eclipse JDT.
463         \item[\secondlevelmark] The composition work had to be done by us.
464         \item[\secondlevelmark] Not seamless (find the extracted method, move 
465           target etc.).
466
467       \end{itemize}
468
469   \end{itemize}
470
471 \end{frame}
472
473 %\mysection{Research questions}
474
475 \begin{frame}
476   Main research question:
477   \begin{quote}
478     Is it possible to automate the analysis and execution of the 
479     \ExtractAndMoveMethod refactoring, and do so for all of the code of a larger 
480     project?
481   \end{quote}
482   
483   Secondary questions:
484   \begin{itemize}
485     \item Can we do this efficiently?
486     \item Can we perform changes safely?
487     \item Can we improve the quality of source code?
488     \item How can the automation of the refactoring be helpful?
489   
490   \end{itemize}
491   
492 \end{frame}
493
494
495 \begin{frame}[fragile]{Automating the refactoring}
496   For any given method: We want to find the best candidate for the 
497   \ExtractAndMoveMethod refactoring, if any exist.
498
499   \begin{minipage}[b]{0.33\textwidth}
500   \hfill\vspace{0.5cm}
501   \end{minipage}\begin{minipage}[t]{0.5\textwidth}
502 \def\charwidth{4.5pt}
503 \def\indent{2*\charwidth}
504 \def\rightColX{32*\charwidth*0}
505 \def\lineheight{\baselineskip}
506 \def\mintedtop{6.5pt}
507
508 \begin{tikzpicture}[overlay, yscale=-1]
509   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
510   \draw[overlaybox] (0,\mintedtop+\lineheight*3) rectangle 
511   +(16*\charwidth,2*\lineheight);
512
513   \node[draw opacity=0, text=red] at (20*\charwidth,\mintedtop+\lineheight*4) 
514   {\fontsize{40}{60}\selectfont ?};
515 \end{tikzpicture}
516 \begin{minted}[samepage]{java}
517 void method() {
518   statement_1;
519   statement_2;
520   statement_3;
521   statement_4;
522   statement_5;
523 }
524 \end{minted}
525   \end{minipage}
526
527 \end{frame}
528
529
530 \begin{frame}[fragile]
531 \begin{minipage}[t]{0.5\textwidth}
532 \def\charwidth{4.75pt}
533 \def\indent{2*\charwidth}
534 \def\lineheight{\baselineskip}
535 \def\mintedtop{5.8pt}
536
537 \begin{tikzpicture}[overlay, yscale=-1]
538   \tikzstyle{overlaybox}=[fill=gray,opacity=0.2]
539   \draw[overlaybox] (2*\indent,\mintedtop+12*\lineheight) rectangle 
540   +(14*\charwidth,2*\lineheight);
541
542   \draw[overlaybox] (9*\charwidth,\mintedtop+\lineheight) rectangle 
543   +(\charwidth,\lineheight);
544
545   \node[draw opacity=0, text=red] at (12.1*\indent,\mintedtop+12.9*\lineheight) 
546   {text selection};
547
548   \node[draw opacity=0, text=red] at (11.5*\indent,\mintedtop+4*\lineheight) 
549   {move target};
550
551   \draw[-latex,red] (11.5*\indent-5*\charwidth,\mintedtop+3.7*\lineheight) -- 
552   (10.2*\charwidth,\mintedtop+1.5*\lineheight);
553
554 \end{tikzpicture}
555 \begin{minted}[linenos,samepage]{java}
556 class C {
557   A a; B b; boolean bool;
558   void method(int val) {
559     if (bool) {
560       a.foo();
561       a = new A();
562       a.bar();
563     }
564     a.foo();
565     a.bar();
566     switch (val) {
567     case 1:
568       b.a.foo();
569       b.a.bar();
570       break;
571     default:
572       a.foo();
573     }
574   }
575 }
576 \end{minted}
577 \end{minipage}\begin{minipage}[t]{0.5\textwidth}
578   \pause
579   \vspace{1.2cm}
580   A \textbf{candidate} consists of a \emph{text selection} and a \emph{move 
581   target}.\\~\\
582
583   A \textbf{valid text selection} is a text selection that contains all of one 
584   or more consecutive program statements. It is the input to the \ExtractMethod 
585   refactoring.\\~\\
586
587   A \textbf{move target} is a variable (local or field), whose type is the 
588   destination class in the \MoveMethod refactoring.
589
590 \end{minipage}
591   
592 \end{frame}
593
594
595
596 \begin{frame}
597   \frametitle{Searching}
598   Usually, search-based refactoring is based on metrics.
599   \begin{itemize}
600     \item Refactor a lot.
601     \item Choose the best candidate based on measurements.
602   \end{itemize}
603
604   Our refactoring is based on heuristics.
605   \begin{itemize}
606     \item Up-front analysis.
607     \item A set of assumptions defining what is considered the best candidate.
608     \item No need to actually perform changes (before deciding).
609     \item Search through all valid selections to find the best candidate.
610   \end{itemize}
611
612 \end{frame}
613
614
615 \begin{frame}
616   \frametitle{Choosing a refactoring candidate}
617   \begin{itemize}
618     \item Search through all selections to find the possible candidates.
619     \item Find the best move target for all the candidates.
620     \item Choose the best among the possible candidates.
621     \item Based on the lengths of the navigation paths and the occurrence 
622       counts.
623   
624   \end{itemize}
625 \end{frame}
626
627 \begin{frame}{Case studies}
628   Case studies performed on the \code{org.eclipse.jdt.ui} and 
629   \code{no.uio.ifi.refaktor} projects. The resulting code was analyzed with 
630   SonarQube.
631   \vspace*{\baselineskip}
632
633   The Eclipse JDT UI project:
634   \begin{itemize}
635     \item Over 300,000 lines of code.
636     \item 2,552 methods out of 27,667 methods chosen to be refactored.
637     \item Approx. 100 minutes.
638   
639   \end{itemize}
640
641 \end{frame}
642
643
644 \begin{frame}
645   \frametitle{The case studies are inconclusive}
646   \begin{itemize}
647     \item Measurements show some deterioration regarding coupling.
648     \item All improvement not measured, only strict coupling between classes.
649     \item Examples exist where coupling is improved.
650     \item More examples exist where dependencies are introduced.
651     \item[]
652     \item Needs more work!
653   \end{itemize}
654 \end{frame}
655
656 \section{Conclusions}
657
658 \begin{frame}{Conclusions and future work}
659   \begin{itemize}
660     \item Automation is possible.
661     \item Efficient enough for some kinds of use.
662     \item Difficult not to break source code.
663     \item Naive application introduces additional coupling.\\[2ex]
664     \item[] {\bf Future work:}
665   \item Better analysis (less crashes/broken output).
666   \item Improve heuristics to avoid introducing new dependencies.
667     \item Online suggestions?
668   \item Make refactoring safer.
669
670 \end{itemize}
671 \end{frame}
672
673 \end{document}