-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathslides_refactoring_rails.html
More file actions
163 lines (147 loc) · 7.26 KB
/
slides_refactoring_rails.html
File metadata and controls
163 lines (147 loc) · 7.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Slides for
Refactoring Rails — Ruby on Rails Guides
</title>
<link rel="stylesheet" type="text/css" href="stylesheets/style.css" data-turbo-track="reload">
<link rel="stylesheet" type="text/css" href="stylesheets/print.css" media="print">
<link rel="stylesheet" type="text/css" href="stylesheets/highlight.css" data-turbo-track="reload">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="stylesheets/reset.css">
<link rel="stylesheet" href="stylesheets/reveal.css">
<link rel="stylesheet" href="stylesheets/myslide.css" id="theme">
<link rel="stylesheet" href="stylesheets/code.css">
<script src="javascripts/clipboard.js" data-turbo-track="reload"></script>
<script src="javascripts/slides.js" data-turbo-track="reload"></script>
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<h1>Refactoring Rails</h1><p>This guide will show you how to refactor
your code. </p><p>By referring to this guide, you will be able to:</p>
<ul>
<li>Get to know some tools that help you find problematic aspects of your code</li>
<li>Learn general refactoring practice from concrete examples</li>
</ul>
<p><small>Slides - use arrow keys to navigate, esc to return to page view, f for fullscreen</small></p>
</section>
<section><a class='slide_break' href='refactoring_rails.html#slide-0'>▻</a>
<h2 id="refactoring"><a class="anchorlink" href="#refactoring"><span>1</span> Refactoring</a></h2><p>Never be ashamed of making you code better. On the contrary: recognizing code smells
in your old code means that you learnt something in the meantime.
Only very inexperienced people think that the code they wrote yesterday is
perfect.</p><p>Refactoring is </p>
<ul>
<li>restructuring an existing body of code</li>
<li>altering its internal structure</li>
<li>without changing its external behavior <a href="https://en.wikipedia.org/wiki/Refactoring">wikipedia</a></li>
</ul>
<p>We will use <strong>tests</strong> to ensure that we do not change the external behavior
of the code we are refactoring.</p><p>A <strong>code smell</strong> is a piece of bad code that we recognize.</p><p>Read the Ruby version of Fowlers refactoring book to
learn both code smells and refactorings:</p></section>
<section><a class='slide_break' href='refactoring_rails.html#slide-1'>▻</a>
<h2 id="code-smells"><a class="anchorlink" href="#code-smells"><span>2</span> Code Smells</a></h2><p>This is the list of code smells from
Fields, Harvie, Fowler(2010): Refactoring, Ruby Edition. Addison-Wesley.
In chapter 6 to 12 of that book they describe refactorings to handle
all these problems and more:</p>
<ul>
<li>Duplicated Code</li>
<li>Long Method</li>
<li>Large Class</li>
<li>Long Parameter List</li>
<li>Divergent Change</li>
<li>Shotgun Surgery</li>
<li>Feature Envy.</li>
<li>Data Clumps.</li>
<li>Primitive Obsession</li>
<li>Case Statements</li>
<li>Parallel Inheritance Hierarchies</li>
<li>Lazy Class.</li>
<li>Speculative Generality.</li>
<li>Temporary Field</li>
<li>Message Chains</li>
<li>Middle Man</li>
<li>Inappropriate Intimacy</li>
<li>Alternative Classes with Different Interfaces.</li>
<li>Incomplete Library Class</li>
<li>Data Class</li>
<li>Refused Bequest</li>
<li>Comments</li>
<li>Metaprogramming Madness</li>
<li>Disjointed API</li>
<li>Repetitive Boilerplate</li>
</ul>
</section>
<section><a class='slide_break' href='refactoring_rails.html#slide-2'>▻</a>
<h2 id="tools-for-code-quality"><a class="anchorlink" href="#tools-for-code-quality"><span>3</span> Tools for Code Quality</a></h2><p>Recognizing code that is problematic and should be refactored
is one of the main skills of a developer. Often it is not a
black and white situation: there might be several ways of writing
a certain piece of code, each with it's own pros and cons.<br>
A tool cannot help you make these decision.</p><p>But there is a role for tools in this process: especially when
faced with a lot of code there are tools that can help
you find places you should look at.</p><p>A <a href="https://en.wikipedia.org/wiki/Software_metric">code metric</a> is
a quantitive measure of the quality of a piece of code.</p><p>The Gem <code>metric_fu</code> combines some metrics. Install it in your
Gemfile, and add a task-file, and run it on the command line <code>metriy_fu</code>.
It will generate a report in <code>tmp/metric_fu/output/</code>. </p><p><strong>Saikuro</strong> is a good place to start reading the report: it
measures cyclomatic complexity, or how deep you nest your control structures.</p><p>See <a href="http://railscasts.com/episodes/166-metric-fu?view=asciicast">Rails Cast no
166</a> for a more
detailed introduction to metric_fu.</p><p>Another tool to help find spots where you can improve the quality of
your code is <code>rails_best_practices</code>. Install the gem, but don't put
it in your <code>Gemfile</code>. Just run <code>rails_best_practices -f html</code> in
the main directory of your app. The result will be written to
<code>./rails_best_practices_output.html</code>.</p></section>
<section><a class='slide_break' href='refactoring_rails.html#slide-3'>▻</a>
<h2 id="futher-reading"><a class="anchorlink" href="#futher-reading"><span>4</span> Futher Reading</a></h2>
<ul>
<li>Fowler, Beck, Brant, Opdyke, Roberts(1999). Refactoring: improving the design of existing code. Addison Wesley. ISBN: 0-201-48567-2.</li>
<li>Fields, Harvie, Fowler(2010): Refactoring, Ruby Edition. Addison-Wesley.</li>
<li><a href="https://github.com/facebook/codemod">codemod</a> a python script</li>
</ul>
</div></section>
</div>
</div>
<!-- End slides. -->
<!-- Required JS files. -->
<script src="javascripts/reveal.js"></script>
<script src="javascripts/search.js"></script>
<script src="javascripts/markdown.js"></script>
<script>
// Also available as an ES module, see:
// https://revealjs.com/initialization/
Reveal.initialize({
controls: false,
progress: true,
center: false,
hash: true,
// The "normal" size of the presentation, aspect ratio will
// be preserved when the presentation is scaled to fit different
// resolutions. Can be specified using percentage units.
width: 1000,
height: 600,
disableLayout: false,
// Factor of the display size that should remain empty around
// the content
margin: 0.05,
// Bounds for smallest/largest possible scale to apply to content
minScale: 0.2,
maxScale: 10.0,
keyboard: {
27: () => {
// do something custom when ESC is pressed
var new_url = window.location.pathname.replace('slides_', '') + window.location.hash.replace('/','slide-');
window.location = new_url;
},
191: 'toggleHelp',
13: 'next', // go to the next slide when the ENTER key is pressed
},
// Learn about plugins: https://revealjs.com/plugins/
plugins: [ RevealSearch, RevealMarkdown ]
});
</script>
</body>
</html>