]>
Commit | Line | Data |
---|---|---|
7c28933b | 1 | \documentclass[USenglish]{ifimaster} |
571ef294 | 2 | \usepackage{import} |
7c28933b | 3 | \usepackage[utf8]{inputenc} |
9ff90080 EK |
4 | \usepackage[T1]{fontenc,url} |
5 | \urlstyle{sf} | |
571ef294 EK |
6 | \usepackage{babel,textcomp,csquotes,ifimasterforside,varioref,graphicx} |
7 | \usepackage[style=numeric-comp]{biblatex} | |
8 | %\usepackage[backend=biber,style=numeric-comp]{biblatex} | |
12c254af | 9 | \usepackage{amsthm} |
9ff90080 | 10 | |
12c254af EK |
11 | \theoremstyle{plain} |
12 | \newtheorem*{wordDef}{Definition} | |
13 | ||
14 | \newcommand{\definition}[1]{\begin{wordDef}#1\end{wordDef}} | |
9ff90080 | 15 | |
7c28933b EK |
16 | |
17 | \title{Refactoring} | |
18 | \subtitle{An essay} | |
19 | \author{Erlend Kristiansen} | |
20 | ||
21 | \bibliography{bibliography/master-thesis-erlenkr-bibliography} | |
9ff90080 EK |
22 | |
23 | \begin{document} | |
7c28933b | 24 | \ififorside{} |
9ff90080 | 25 | \frontmatter{} |
9ff90080 EK |
26 | |
27 | ||
28 | \chapter*{Abstract} | |
29 | Empty document. | |
30 | ||
31 | \tableofcontents{} | |
32 | \listoffigures{} | |
33 | \listoftables{} | |
34 | ||
35 | \chapter*{Preface} | |
36 | ||
37 | \mainmatter{} | |
7c28933b | 38 | |
9ff90080 EK |
39 | \part{Introduction} |
40 | ||
51a854d4 | 41 | \chapter{Refactoring in general} |
7c28933b | 42 | |
51a854d4 | 43 | \section*{What is refactoring?} |
7c28933b | 44 | |
51a854d4 EK |
45 | This question is best answered dividing the answer into two parts. First |
46 | defining the concept of a refactoring, then discuss what the dicipline of | |
47 | refactoring is all about. And to make it clear already from the beginning: The | |
48 | dicussions in this report must me seen in the context of object oriented | |
49 | programming languages. It may be obvious, but much of the material will not make | |
50 | much sense otherwise, although some of the techniques may be applicable to | |
51 | sequential languages, then possibly in other forms. | |
52 | ||
53 | \subsection*{Defining a refactoring} | |
12c254af | 54 | Well, this is the easy one. |
51a854d4 | 55 | \definition{A refactoring is a transformation |
12c254af EK |
56 | done to a program without altering its external behaviour.} |
57 | So a refactoring changes how the \emph{code} of a program is percepted by the | |
58 | \emph{programmer}, and not the behaviour experienced by any user of the program. | |
59 | The semantic meaning is preserved. To \emph{refactor} is (quoting Martin Fowler) | |
7c28933b EK |
60 | \begin{quote} |
61 | \ldots to restructure software by applying a series of refactorings without | |
9a55a5bc | 62 | changing its observable behaviour. \cite{refactoring} % page 54, definition |
7c28933b EK |
63 | \end{quote} |
64 | ||
51a854d4 | 65 | \subsection*{The dicipline of refactoring} |
12c254af EK |
66 | This one is a little harder. To get a grasp of what the dicipline of refactoring |
67 | is about, we better answer this question: \emph{Why do people refactor?} | |
68 | Possible answers could include: ``To remove duplication'' or ``to break up long | |
69 | methods''. Practitioners of the art of Design Patterns \cite{dp} could say that | |
70 | they do it to introduce a long-needed pattern to their program's design. So it's | |
71 | safe to say that peoples intentions is to make their programs \emph{better} in | |
72 | some sense. But what aspects of the programs are becoming improved? | |
51a854d4 EK |
73 | |
74 | As already mentioned, people often refactor to get rid of duplication. Moving | |
75 | identical or similar code into methods, and maybe pushing those around in their | |
76 | hierarchies. Making template methods for overlapping algorithms and so on. It's | |
12c254af EK |
77 | all about gathering what belongs together and putting it all in one place. And |
78 | the result? The code is easier to maintain, due to removing the implicit | |
51a854d4 EK |
79 | coupling between the code snippets and getting rid of the almost sure path to |
80 | bugs. | |
81 | ||
82 | The same people find out that their program contains a lot of long and | |
83 | hard-to-grasp methods. Then what do they do? They begin dividing their methods | |
84 | into smaller ones, using the \emph{Extract Method} \cite{refactoring}. All of a | |
85 | sudden they may discover something about their program that they weren't aware | |
86 | of before; revealing bugs they didn't know about or couldn't find due to the | |
87 | complex structure of their program. Making the methods smaller and giving good | |
88 | names to the new ones clarifies the algorithms and enhances the | |
89 | \emph{understandability} of the program. This makes simple refactoring an | |
90 | excellent method for exploring unknown program code, or code that you had | |
91 | forgotten that you wrote! | |
92 | ||
93 | The word \emph{simple} came up in the last section. In fact, most basic | |
94 | refactorings are simple. The true power of them are revealed first when they are | |
95 | combined into larger --- higher level --- refactorings. Often the goal of such a | |
96 | serie of refactorings is a design pattern. Thus the \emph{design} can be evolved | |
97 | throughout the lifetime of a program, opposed to designing up-front. It's all | |
98 | about being structured and taking small steps to improve the design. | |
99 | ||
100 | Many refactorings are aimed at lowering the coupling between different classes | |
101 | and different layers of logic. Say for instance that the coupling between the | |
102 | user interface and the business logic of a program is lowered. Then the business | |
103 | logic of the program could much easier be the target of automated tests, | |
0b0567f2 EK |
104 | increasing the productivity in the software development process. It would also |
105 | be much easier to distribute the different parts of the program if they were | |
106 | decoupled. | |
107 | ||
108 | Another effect of refactoring is that with the increased separation of concerns | |
9a55a5bc | 109 | coming out of many refactorings, the \emph{tunability} is improved. When |
0b0567f2 EK |
110 | profiling programs, the problem parts are narrowed down to smaller parts of the |
111 | code and optimization can be performed only where needed. | |
112 | ||
113 | Refactoring program code --- with a goal in mind --- give the code itself more | |
114 | value. That is in the form of robustness, understandability and maintainability. | |
115 | With the first as an obvious advantage, but with the following two being also | |
116 | very important in software development. By incorporating refactoring in the | |
117 | development process, bugs are found faster, new functionality is added more | |
118 | easily and code is easier to understand by the next person exposed to it, which | |
119 | might as well be the person who wrote it. So, refactoring can also add to the | |
120 | monetary value of a business, by increased productivity of the develompment | |
121 | process in the long run. | |
9ff90080 EK |
122 | |
123 | ||
12c254af | 124 | % \part{The project} |
9ff90080 | 125 | |
12c254af | 126 | % \chapter{Planning the project} |
9ff90080 EK |
127 | |
128 | ||
12c254af | 129 | %\part{Conclusion} |
9ff90080 | 130 | |
12c254af | 131 | %\chapter{Results} |
9ff90080 EK |
132 | |
133 | ||
9a55a5bc EK |
134 | |
135 | \section*{The impact on software quality} | |
136 | ||
137 | \subsection*{What is meant by quality?} | |
138 | The term \emph{software quality} can have many meanings. It all depends on the | |
139 | context we put it in. If we look at it with the eyes of a software developer, it | |
140 | usually mean that the software is easily maintainable and testable, or in other | |
141 | words, that it is \emph{well designed}. This often correlates with the | |
142 | management scale, where \emph{keeping the schedule} and \emph{customer | |
143 | satisfaction} is at the center. (Although refactoring isn't always recognized | |
144 | as the best way to achieve these goals.) From the customers point of view, in | |
145 | addition to good usability, | |
146 | \emph{performance} and \emph{lack of bugs} is always appreciated, measurements | |
147 | that are also shared by the software developer. (In addition, such things as | |
148 | good documentation could be measured, but this is out of the scope of this | |
149 | document.) | |
150 | ||
151 | \subsection*{The impact on performance} | |
152 | \begin{quote} | |
153 | Refactoring certainly will make software go more slowly, but it also makes the | |
154 | software more amenable to performance tuning. \cite{refactoring} % page 69 | |
155 | \end{quote} | |
156 | There is a common belief that refactoring compromises performance, due to | |
157 | increased degree of indirection and that polymorphism is slower than | |
158 | conditionals. | |
159 | ||
160 | In a survey, Demeyer \cite{demeyer2002} disproves this view in the case of | |
161 | polymorphism. He is doing an experiment on, what he calls, ``Transform Self Type | |
162 | Checks'' where you introduce a new polymorphic method and a new class hierarchy | |
163 | to get rid of a class' type checking of a ``type attribute``. He uses this kind | |
164 | of transformation to represent other ways of replacing conditionals with | |
165 | polymorphism as well. The experiment is performed on the C++ programming | |
166 | language and with three different compilators and platforms. Demeyer concludes | |
167 | that, with compiler optimization turned on, polymorphism beats middle to large | |
168 | sized if-statements and does as well as case-statements. (In accordance with his | |
169 | hypethesis, due to similarities between the way C++ handles polymorphism and | |
170 | case-statements.) | |
171 | \begin{quote} | |
172 | The interesting thing about performance is that if you analyze most programs, | |
173 | you find that they waste most of their time in a small fraction of the code. | |
174 | \cite{refactoring} | |
175 | \end{quote} | |
176 | So, although an increased amount of method calls could potentially slow down | |
177 | programs, one should avoid premature optimization and sacrificing good design, | |
178 | leaving the performance tuning until after profiling the software and having | |
179 | isolated the actual problem areas. | |
180 | ||
181 | ||
9ff90080 EK |
182 | \backmatter{} |
183 | \printbibliography | |
184 | \end{document} |