-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
287 lines (252 loc) · 8.12 KB
/
index.html
File metadata and controls
287 lines (252 loc) · 8.12 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>java.util.Optional: What and How</title>
<link rel="stylesheet" href="css/reveal.css">
<link rel="stylesheet" href="css/theme/solarized.css" id="theme">
<link rel="stylesheet" href="font-awesome/css/font-awesome.min.css">
<link rel="stylesheet" href="css/overrides.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
</head>
<body>
<div class="reveal">
<div class="slides">
<!-- Introduction -->
<section>
<h1>java.util.Optional</h1>
<h3>What it is and how to use it</h3>
<p>
<small>
Created by <a href="http://ginsberg.com" target="_blank">Todd Ginsberg</a>
for <a href="http://www.meetup.com/ChicagoJUG/events/230375291/" target="_blank">CJUG</a>
</small>
<br/>
<small>2016-06-02</small>
</p>
</section>
<section>
<section>
<h2>What is java.util.Optional?</h2>
<ul>
<li class="fragment">Introduced in Java 8</li>
<li class="fragment">Intended to reduce NullPointerExceptions</li>
<li class="fragment">A <u>signal</u> to other developers that a value may not be present</li>
<li class="fragment">Container with zero or one elements</li>
</ul>
</section>
<section>
<h2>What isn't java.util.Optional?</h2>
<ul>
<li class="fragment">Not capable of storing null</li>
<li class="fragment">Not serializable!</li>
<li class="fragment">Not widely used in the JDK</li>
</ul>
</section>
<section>
<blockquote cite="http://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type/26328555#26328555">
"Our intention was to provide a limited mechanism for library method return types where there needed to be a
clear way to represent "no result", and using null for such was overwhelmingly likely to cause errors."
</blockquote>
<br/>
-- Brian Goetz
</section>
<section>
<h3>How did it get here?</h3>
Optional is mainly here to make stream terminal operations possible for empty streams.
<pre><code data-trim>
new ArrayList<String>().stream().findFirst();
</code></pre>
</section>
<section>
<h3>Why not also add it to Map?</h3>
<pre><code data-trim>
public interface Map<K,V> {
// ...
default Optional<V> getOptionally(K k) {
// ...
}
}
</code></pre>
<div class="fragment">
Maps can contain null values!
<pre><code data-trim>
Map<String, String> map = new HashMap<>();
map.put("A",null);
// Both look the same - no difference between not present and null.
map.getOptionally("A");
map.getOptionally("B");
</code></pre>
</div>
</section>
</section>
<section>
<h3>States</h3>
<table>
<tr>
<th>value</th>
<th>isEmpty()</th>
<th>isPresent()</th>
<th>.get()</th>
</tr>
<tr>
<td>not null</td>
<td>false</td>
<td>true</td>
<td>value</td>
</tr>
<tr>
<td>null</td>
<td>true</td>
<td>false</td>
<td>NoSuchElementException</td>
</tr>
</table>
</section>
<section>
<h3>Creation</h3>
Use one of the two built-in Factory methods
<pre><code data-trim>
Optional<String> text = Optional.ofNullable("Hello, world!");
</code></pre>
<div class="fragment">
<h4>Or...</h4>
<pre><code data-trim>
Optional<String> text = Optional.of(somethingThatIsntNull);
</code></pre>
</div>
<div class="fragment">
<pre><code data-trim>
Optional<String> text = Optional.of(null);
// -> NullPointerException
</code></pre>
</div>
</section>
<section>
<h3>When to use it</h3>
If you use it at all, only use it for return types where there is no natural way to represent "no result"
<pre><code data-trim>
// Yes
public Optional<Customer> getCustomerById(long id);
</code></pre>
<div class="fragment">
<pre><code data-trim>
// No
public Optional<List<Customer>> getCustomersForMonth(final YearMonth when);
</code></pre>
</div>
<div class="fragment">
<pre><code data-trim>
// Prefer:
public List<Customer> getCustomersForMonth(final YearMonth when);
// -> Collections.emptyList();
</code></pre>
</div>
</section>
<section>
<section>
<h3>Don't call .get()!</h3>
<pre><code data-trim>
Optional<String> text = service.getText();
if(text.isPresent()) {
System.out.println(text.get());
} else {
System.out.println("Hello, world!");
}
</code></pre>
<div class="fragment">
Looks just like...
<pre><code data-trim>
String text = service.getText();
if(text != null) {
System.out.println(text);
} else {
System.out.println("Hello, world!");
}
</code></pre>
</div>
</section>
<section>
<blockquote cite="http://stackoverflow.com/questions/26327957/should-java-8-getters-return-optional-type/26328555#26328555">
"In retrospect, we should have called get something like getOrElseThrowNoSuchElementException
or something that made it far clearer that this was a highly dangerous method that undermined
the whole purpose of Optional in the first place. Lesson learned."
</blockquote>
<br/>
-- Brian Goetz
</section>
<section>
<h3>Use one of the safe methods instead!</h3>
<pre><code data-trim>
Optional<String> text = service.getText();
System.out.println(text.orElse("Hello, world!"));
</code></pre>
<div class="fragment">
<pre><code data-trim>
Optional<String> text = service.getText();
text.ifPresent(this::doSomethingWithText);
</code></pre>
</div>
<div class="fragment">
<pre><code data-trim>
Optional<String> text = service.getText();
System.out.println(text.orElseThrow(NoSuchElementException::new));
</code></pre>
</div>
</section>
</section>
<section>
<h3>When not to use it</h3>
<ul>
<li class="fragment">Method parameters</li>
<li class="fragment">Fields</li>
<li class="fragment">In any DTO or Entity, because it is not serializable</li>
<li class="fragment">If your customers are not using Java 8</li>
<li class="fragment">Possibly, don't use it at all!</li>
</ul>
</section>
<section>
<h3>Rules to remember</h3>
<ul>
<li class="fragment">When returning them, make <u>absolutely</u> sure they aren't null</li>
<li class="fragment">Don't call .get()</li>
</section>
<section data-transition="none">
<h1>Thanks!</h1>
<p>
<i class="fa fa-twitter"></i> <a href="https://twitter.com/ToddGinsberg" target="_blank">@ToddGinsberg</a>
</p>
<p>
<i class="fa fa-commenting"></i> <a href="http://bit.ly/FeedbackOptional" target="_blank">bit.ly/FeedbackOptional</a>
</p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
history: true,
//transition: 'none',
// More info https://github.com/hakimel/reveal.js#dependencies
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } }
]
});
</script>
</body>
</html>