summaryrefslogtreecommitdiffstats
path: root/talks/sheriff/all.lt
blob: e465dc3a32b4f21f3d8699dd9b8bfb0708b9b559 (plain) (blame)
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
@Include { myslides }

@OverheadTransparencies
	@Title {
Validation using Erlang's type system

with Sheriff
}
	@RunningTitle { sheriff }
	@Author { Lo�c Hoguin }
	@Institution { Dev:Extend }
	@DateLine { Yes }
	@InitialLanguage { English }
	@PageOrientation { Landscape }
//

@Overhead
	@Title { Good Erlang code }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-type year() :: 1900..2011.
-type age() :: 0..111.

-spec main() -> no_return().
main() ->
    Year = 1984,
    Age = calculate_age(Year),
    io:format("~b years old~n", [Age]),
    main().

-spec calculate_age(year()) -> age().
calculate_age(Year) ->
    2011 - Year.
} }

@End @Overhead

@Overhead
	@Title { Dialyzer is awesome, isn't it? }
	@Begin

@BulletList
@ListItem { Dialyzer statically checks function specifications }
@ListItem { All type errors will be reported by Dialyzer }
@ListItem { Okay, code isn't that good, it's just a dumb example }
@EndList

@End @Overhead

@Overhead
	@Title { Good Erlang code receiving external data }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-type year() :: 1900..2011.
-type age() :: 0..111.

-spec main() -> no_return().
main() ->
    receive {year_of_birth, Year} ->
        Age = calculate_age(Year),
        io:format("~b years old~n", [Age])
    end,
    main().

-spec calculate_age(year()) -> age().
calculate_age(Year) ->
    2011 - Year.
} }

@End @Overhead

@Overhead
	@Title { We lost Dialyzer }
	@Begin

@BulletList
@ListItem { Dialyzer doesn't help here }
@ListItem { @F Year could be any integer in this code!
	@BulletList
	@ListItem { Even year 3001 }
	@ListItem { Or it could be a binary, a list, a tuple, a pid... }
	@EndList
}
@ListItem { This value wouldn't match the type we defined }
@EndList

@End @Overhead

@Overhead
	@Title { What is external data? }
	@Begin

@BulletList
@ListItem { Anything that doesn't come directly from your process }
@ListItem { This includes:
	@BulletList
	@ListItem { Process messages }
	@ListItem { Shared resources }
	@ListItem { Sockets, files }
	@ListItem { Return values from NIFs, ports, linked-in drivers }
	@EndList
}
@ListItem { Real-world applications are all about external data! }

@End @Overhead

@Overhead
	@Title { Why check external data? Just let it crash! }
	@Begin

@BulletList
@ListItem { WHAT?! }
@ListItem { Are you sure it'll crash? }
@ListItem { Maybe it's going to crash an unrelated process
	@BulletList
	@ListItem { Like a central gen_server of your application }
	@EndList
}
@ListItem { Maybe it's going to be stored in a database or a file }
@ListItem { Maybe it's going to be sent directly to connected clients }
@EndList

@End @Overhead

@Overhead
	@Title { Really? }
	@Begin

@BulletList
@ListItem { A person registering on your website today can't be born in 1492! }
@ListItem { Think about it, are you really crashing on this kind of data? }
@ListItem { Also think about XSS, SQL injection, and friends }
@EndList

@End @Overhead

@Overhead
	@Title { Always validate external data }
	@Begin

@BulletList
@ListItem { Either print a nice error message to the user
	@BulletList
	@ListItem { HTML forms, for example }
	@EndList
}
@ListItem { Or crash as soon as possible
	@BulletList
	@ListItem { Don't crash anywhere! Crash on the system boundaries }
	@ListItem { Don't let bad data crash your core processes }
	@ListItem { Don't let external attacks or user error bring down your app }
}
@EndList

@End @Overhead

@Overhead
	@Title { Data validation without Sheriff }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-type year() :: 1900..2011.

-spec is_valid_year(year()) -> boolean().
is_valid_year(Y)
        when is_integer(Y), Y >= 1900, Y =< 2011 ->
    true;
is_valid_year(_Y) ->
    false.
} }

@End @Overhead

@Overhead
	@Title { All this has happened before... }
	@Begin

@BulletList
@ListItem { I feel like I'm repeating myself there }
@EndList

@End @Overhead

@Overhead
	@Title { And all this will happen again }
	@Begin

@BulletList
@ListItem { I did write the same constraint twice }
@ListItem { Dialyzer already checks it for most of the program }
@ListItem { Why not use the @F year() type directly? }
@EndList

@End @Overhead

@Overhead
	@Title { I can't type }
	@Begin

@BulletList
@ListItem { But I can't use Erlang's types from runtime code! }
@EndList

@End @Overhead

@Overhead
	@Title { Who do you call when you need help? }
	@Begin

@CD 3.0 @Scale @IncludeGraphic badge.eps

@End @Overhead

@Overhead
	@Title { I am the law }
	@Begin

@BulletList
@ListItem { Sheriff is a runtime type checker }
@ListItem { It uses Erlang's type system for validation }
@ListItem { You don't need to duplicate constraints to validate data anymore! }
@ListItem { So just be lazy and validate all external data with a single LoC }
@EndList

@End @Overhead

@Overhead
	@Title { Data validation with Sheriff }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
true = sheriff:check(Y, year()).
} }

@End @Overhead

@Overhead
	@Title { Tuple validation with Sheriff }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-type subject() :: 'I' | you | he.
-type verb() :: like | ignore | hate.
-type object() :: me | you | him.

-type grammar() :: {subject(), verb(), object()}.

%% ...

sheriff:check({you, like, me}, grammar()). %% true
sheriff:check({'I', love, you}, grammar()). %% false
} }

@End @Overhead

@Overhead
	@Title { Recursive type validation with Sheriff }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-type rtype() :: {leaf | rtype(), leaf | rtype()}.

%% ...

sheriff:check({leaf, {leaf, leaf}}, rtype()). %% true
sheriff:check({{flower, flower}, leaf}, rtype()). %% false
sheriff:check(<<"flower">>, rtype()). %% false
} }

@End @Overhead

@Overhead
	@Title { Parameterized type validation with Sheriff }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-type a() :: 0..65535.
-type b(T) :: undefined | T.
-type c() :: b(a()).

%% ...

sheriff:check(undefined, c()). %% true
sheriff:check(42, c()). %% true
sheriff:check(-1, c()). %% false
sheriff:check(1234567890, c()). %% false
sheriff:check(defined, c()). %% false
} }

@End @Overhead

@Overhead
	@Title { Record validation with Sheriff }
	@Begin

@CD @F @Box paint { lightyellow } { @Verbatim {
-record(packet, {
	id :: 1 | 2 | 3,
	num = 0 :: non_neg_integer(),
	data = <<>> :: binary()
}).
-type packet() :: #packet{}.

%% ...

sheriff:check(#packet{id=1, data= <<0:32>>}, packet()). %% true
sheriff:check(#packet{id=undefined}, packet()). %% true
sheriff:check({packet, 2, 1, <<>>}, packet()). %% true
sheriff:check(#packet{id=0, num=7}, packet()). %% false
sheriff:check(#http_req{}, packet()). %% false
} }

@End @Overhead

@Overhead
	@Title { Digging in }
	@Begin

@BulletList
@ListItem { Sheriff is a parse_transform }
@ListItem { It first generates validation functions for all the types you defined }
@ListItem { It then replaces the @F @Verbatim { sheriff:check/2 } calls
with the proper validation calls }
@ListItem { It's fast and is only a compilation option away }
@EndList

@End @Overhead

@Overhead
	@Title { Don't fall into lava }
	@Begin

@BulletList
@ListItem { There are limitations }
@ListItem { Exported types can only work on modules that were compiled
using the sheriff parse_transform
	@BulletList
	@ListItem { Excluding basic types like @F { integer() } of course }
	@EndList
}
@ListItem { It's only as good as Erlang's type system
	@BulletList
	@ListItem { It can't check element order in lists }
	@ListItem { It can't check the content of binaries, only size }
	@ListItem { This can probably be fixed later }
	@EndList
}
@ListItem { Dialyzer will print out some warnings if analyzing from source }
@EndList

@End @Overhead

@Overhead
	@Title { Sheriff got deputies }
	@Begin

@BulletList
@ListItem { Sheriff, today, is only a proof of concept }
@ListItem { Code was written by two @I { Dev:Extend } interns
	@BulletList
	@ListItem { William Dang }
	@ListItem { Hamza Mahmood }
	@EndList

	@ID 0.5 @Scale @IncludeGraphic wilza.eps
}
@ListItem { They only had one month to learn Erlang and do the project }
@ListItem { So there's probably many bugs! }
@EndList

@End @Overhead

@Overhead
	@Title { To infinity, and beyond! }
	@Begin

@BulletList
@ListItem { We'll cleanup the codebase }
@ListItem { We'll add PropEr tests }
@ListItem { We'll add a few missing features }
@ListItem { First release is planned for December 2011 }
@EndList

@End @Overhead

@Overhead
	@Title { Wanted }
	@Begin

@BulletList
@ListItem { You can help! }
@ListItem { Source code is already available on blue @Color {
"https://github.com/extend/sheriff" @ExternalLink { "https://github.com/extend/sheriff" } } }
@ListItem { Try it out }
@ListItem { Suggest improvements }
@ListItem { File bug reports
	@BulletList
	@ListItem { Wait for the code cleanup though }
	@EndList
}
@EndList

@End @Overhead

@Overhead
	@Title { Suit up! }
	@Begin

@BulletList
@ListItem { We'll ping Kostis Sagonas and the red ties at some point }
@ListItem { They have experience, they can probably help }
@EndList

@End @Overhead

@Overhead
	@Title { Fin }
	@Begin

@BulletList
@ListItem { Any questions? }
@EndList

@End @Overhead