-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
1212 lines (1079 loc) · 48.5 KB
/
index.html
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
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/styles/default.min.css">
<title>Groovy</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="shower/themes/ribbon/styles/styles.css">
<style>
.slide {
padding: 70px;
padding-top: 55px;
}
.hljs {
background-color: #fff3d7 !important;
}
.hljs.bad {
background-color: #f5dbda !important;
}
.slide {
padding-top: 40px;
}
.slide pre mark {
z-index: 0;
}
.slide pre mark.warning {
background-color: orange !important;
}
.slide pre {
margin-bottom: 15px;
}
.shower {
--slide-ratio: calc(16 / 9);
}
li {
font-size: 28px;
}
p {
font-size: 28px;
}
h2 {
top: -20px;
margin-bottom: 20px !important;
color: black;
text-align: center;
font-size: 100px;
}
h3 {
text-align: center;
font-size: 48px;
}
#cover h2 {
margin: 20px 0 0;
color: black;
text-align: center;
font-size: 100px;
}
.strike {
text-decoration: line-through;
}
</style>
</head>
<body class="shower list">
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/8.4/highlight.min.js"></script>
<script>hljs.initHighlightingOnLoad();</script>
<script>
time = new Date();
totalTime = 0;
hash = '';
window.addEventListener('hashchange', function() {
deltaTime = Math.round((new Date() - time) / 1000);
totalTime += deltaTime;
console.log("Slide #" + hash + ", " + deltaTime + " s" + ", Total time " + totalTime + "s");
time = new Date();
hash = window.location.hash.substr(1);
});
</script>
<section class="slide clear" id="cover">
<img class="place center" alt="Apache GROOVY-1875" src="images/groovy-logo.png" style="width: 60%;top: 260px;">
</section>
<section class="slide clear">
<h2>What is it? 🤔</h2>
<ul>
<li class="next">Designed to be an extension of Java and native to JVM</li>
<li class="next">«Java + dynamic types + closures»</li>
<li class="next">75% Java, 12% Ruby, 8% Python, 5% black f<span
style="background-color: black">-----</span>g magic and pain</li>
<li class="next">Very suitable for <a target=_blank
href="http://docs.groovy-lang.org/docs/latest/html/documentation/core-domain-specific-languages.html">DSL</a>
</li>
<li class="next">Used for scripting & backend</li>
<li class="next">High development speed</li>
</ul>
</section>
<section class="slide clear">
<h2>Known for</h2>
<ul>
<li><a target=_blank href="https://gradle.org/">Gradle</a> build system</li>
<li><a target=_blank href="https://grails.org/">Grails</a> web framework</li>
<li><a target=_blank href="https://micronaut.io/">Micronaut</a> microservice framework</li>
<li><a target=_blank href="https://jenkins.io/">Jenkins</a> CI/CD</li>
</ul>
</section>
<section class="slide clear">
<h2>Disclaimer 🗒</h2>
<ul>
<li class="next">Groovy 3.0 released in Feb 2020 🐣</li>
<li class="next">Currently, Jenkins supports Groovy 2.4</li>
<li class="next">This talk does not cover 3.0 features</li>
</ul>
<div class="next">
<p>Notations:</p>
<pre>
<code class="bad java">// don't do like this</code>
</pre>
<pre>
<code class="java">// better like this</code>
</pre>
</div>
</section>
<section class="slide clear">
<h2>Differences with Java</h2>
<div class="next">
<p>Not supported:</p>
<ul>
<li>Lambda functions syntax (added in 3.0)</li>
<li><code>do-while</code> loops (added in 3.0)</li>
<li>ARM blocks (added in 3.0)</li>
<li><a target=_blank href="https://groovy-lang.org/differences.html">other small things</a></li>
</ul>
</div>
</section>
<section class="slide clear">
<h2>Verbosity</h2>
<img src="images/java-vs-groovy.png" alt="Java vs Groovy: You vs the guy she told you not to worry about"
class="cover" style="top: 320px; height: 340px;">
</section>
<section class="slide clear">
<h2>Dynamic Typing</h2>
<pre>
<code>def a = 123
Integer b = 123
def c = "123" as Integer</code>
</pre>
<ul class="next">
<li>Agreement: local vars can be <code>def</code> or type-specific</li>
<li>Agreement: method signatures should be as type-specific as possible</li>
</ul>
<pre class="next java" style="margin-bottom: 0px;"><code class="bad">def calc(def x, def y)</code></pre>
<pre class="next java"
style="margin-bottom: 0px;"><code class="bad">def calc(x, y) // def in params can be skipped</code></pre>
<pre class="next java" style="margin-bottom: 0px;"><code>Float calc(Float x, Integer y)</code></pre>
</section>
<section class="slide clear">
<h2>Multimethods</h2>
<pre>
<code class="java">int method(String arg) { return 1; }
int method(Object arg) { return 2; }
Object o = "Object";
int result = method(o);</code></pre>
<pre class="next java">
<code class>assertEquals(2, result); // in Java</code>
</pre>
<pre class="next java">
<code class>assertEquals(1, result); // in Groovy ⚠️</code>
</pre>
</section>
<section class="slide clear">
<h2>Omitting <code>return</code> & <code>;</code></h2>
<br />
<pre>
<code>Integer fib(Integer n) {</code>
<code class="bad"> if (n < 2) return 1 <mark class="warning">;</mark></code>
<code class="bad"> <mark class="warning">return</mark> fib(n - 1) + fib(n - 2) <mark class="warning">;</mark></code>
<code>}</code>
</pre>
</section>
<section class="slide clear">
<h2>Omitting <code>return</code> & <code>;</code></h2>
<br />
<pre>
<code>Integer fib(Integer n) {</code>
<code> if (n < 2) <mark class="warning">return</mark> 1</code>
<code> fib(n - 1) + fib(n - 2)</code>
<code>}</code>
</pre>
</section>
<section class="slide clear">
<h2>Omitting <code>()</code></h2>
<pre>
<code>def sum(a, b) {
a + b
}</code>
<code>def a = sum(4, 5)</code>
<code>def b = sum 4, 5</code>
<code>println sum(4, 5)</code>
</pre>
<pre class="next">
<code>def noParam() { 123 }</code>
<code>def c = noParam()</code>
</pre>
</section>
<section class="slide clear">
<h2>Groovy Truth 👩⚖️</h2>
<pre><code>if (<expr>) { ... }</code></pre>
<table width="100%">
<tr>
<td width="50%">
<ul class="next">
<li><code>true</code>, <code>!false</code></li>
<li>non-empty lists: <code>[1, 2, 3]</code>, <code>![]</code></li>
<li>non-empty maps: <code>[abc: 123]</code>, <code>![:]</code></li>
<li>non-empty (G)strings: <code>'abc'</code>, <code>!""</code></li>
<li>non-zero numbers: <code>1</code>, <code>-5.4</code>, <code>!0</code>, <code>!0.0f</code>
</li>
<li>non-null objects: <code>new Object()</code>, <code>!null</code></li>
</ul>
</td>
<td width=10% class="next">
<!-- <div> -->
<pre><code style="padding-left: 20px;">def a = [1, 2, 3]
<strike>if (!a.empty) {</strike>
if (a) {
print '👍'
}</code></pre>
</td>
</tr>
</table>
</section>
<!-- CLOSURES -->
<section class="slide clear">
<h2>Closures</h2>
<blockquote style="margin-bottom: 15px;">
<p style="text-align: justify; margin-bottom: 0px !important;">
Closure is a piece of code that is defined and then executed at a later point.
It can take arguments, return a value and be assigned to a variable.
It may reference variables declared in its surrounding scope.
</p>
</blockquote>
<pre>
<code class="java next">def sum = { int a, int b -> a + b }</code>
<!-- <code class="java bad next">assert sum.call(2, 3) == 5</code> -->
<code class="java next">assert sum(2, 3) == 5</code>
</pre>
<pre>
<code class="java bad next">def greet = { name -> "Hello ${name}!" }</code>
<code class="java next">def greet = { "Hello ${it}!" } // ⚠️ implicit `it`</code>
<code class="java next">assert greet('John') == 'Hello John!'</code>
</pre>
</section>
<section class="slide clear">
<h2>Closures: Delegation 🤯</h2>
<ul>
<li style="font-size: 22px;"><code>owner</code> - enclosing object where the closure is defined (either a class or a closure)</li>
<li style="font-size: 22px;"><code>delegate</code> - an object where methods calls or properties are resolved if the receiver of the message is not defined</li>
<li style="font-size: 22px;">by default, <code>owner</code> == <code>delegate</code>.</li>
<li style="font-size: 22px;"><code>delegate</code> can be changed.</li>
</ul>
<pre class="next">
<code class="java" style="font-size: 22px;">class One {</code>
<code class="java" style="font-size: 22px;"> def value() { "A" }</code>
<code class="java" style="font-size: 22px;"> def cl = { "value = ${value()}" } </code>
<code class="java" style="font-size: 22px;">} </code>
<code class="java" style="font-size: 22px;">println new One().cl() <span class="next">// value = A</span></code>
</pre>
</section>
<section class="slide clear">
<h2>Closures: Delegation Strategies 🤯</h2>
<ul>
<li><code>OWNER_FIRST</code> - <b>default</b>. If a property/method exists on the owner, call it on owner. Otherwise use the delegate.</li>
<li><code>OWNER_ONLY</code> - call the property/method on the owner, ignore delegate.</li>
<li><code>DELEGATE_FIRST</code></li>
<li><code>DELEGATE_ONLY</code></li>
</ul>
</section>
<section class="slide clear">
<h2>Closures: Delegation Example</h2>
<pre>
<code class="java bad">def calendar = Calendar.instance
<mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.clear()
<mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.set <mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.YEAR, 2019
<mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.set <mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.MONTH, <mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.DECEMBER
<mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.set <mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.DAY_OF_MONTH, 2
println <span style="font-size: 7px;"> </span><mark class="warning">calendar</mark><span style="font-size: 5px;"> </span>.time // Mon Dec 02 00:00:00 CET 2019</code>
</pre>
</section>
<section class="slide clear">
<h2>Closures: Delegation Example</h2>
<pre>
<code class="java" style="font-size: 20px;">def closure = {</code>
<code class="java" style="font-size: 20px;"> clear()</code>
<code class="java" style="font-size: 20px;"> set YEAR, 2019</code>
<code class="java" style="font-size: 20px;"> set MONTH, DECEMBER</code>
<code class="java" style="font-size: 20px;"> set DAY_OF_MONTH, 2</code>
<code class="java" style="font-size: 20px;"> println time</code>
<code class="java" style="font-size: 20px;">}</code>
<code class="java next" style="font-size: 20px;">def calendar = Calendar.instance</code>
<code class="java next" style="font-size: 20px;">closure.delegate = <mark class="warning">calendar</mark></code>
<code class="java next" style="font-size: 20px;">closure.resolveStrategy = <mark class="warning">Closure.DELEGATE_ONLY</mark></code>
<code class="java next" style="font-size: 20px;">closure() // Mon Dec 02 00:00:00 CET 2019</code>
</code>
</pre>
</section>
<section class="slide clear">
<h2>Closures: <code>with</code></h2>
<pre>
<code class="java">def calendar = Calendar.instance
calendar.with {
clear()
set YEAR, 2019
set MONTH, DECEMBER
set DAY_OF_MONTH, 2
println time // Mon Dec 02 00:00:00 CET 2019
}</code>
</pre>
</section>
<section class="slide clear">
<h2>Closures as Method Params</h2>
<pre>
<code class="java">def method(Closure cl) { cl 'world' }</code>
<code class="java">def closure = { println "Hello $it!" }</code>
<code class="java">method(closure)</code>
</pre>
<pre>
<code class="java next bad">method({ println "Hello $it!" })</code>
<code class="java next">method { println "Hello $it!" }</code>
</pre>
<pre class="next">
<code class="java">method {</code>
<code class="java"> println "Hello $it!"</code>
<code class="java">}</code>
</pre>
</section>
<section class="slide clear">
<h2>Groovy Closures vs Java Lambdas</h2>
<ul>
<li>Lambdas cannot access non-final objects from surrounding scope</li>
<li>Lambdas do not have a delegation strategy that can be changed</li>
</ul>
</section>
<section class="slide clear">
<h2>DSL 🤘</h2>
<blockquote style="margin-bottom: 15px;">
<p style="text-align: justify; margin-bottom: 0px !important;">
A domain-specific language (DSL) is a computer language specialized to a particular application domain.
</p>
</blockquote>
<div class="columns two next">
<pre>
<code class="java" style="font-size: 20px; line-height: 28px; padding-right: 67px;">pipeline {
stage('preparation') {
sh './clone.sh'
sh './init.sh'
}
stage('build') {
sh './build.sh --env=prod'
echo 'DONE!'
}
}
</code>
</pre>
<pre>
<code clas="bash" style="font-size: 20px; padding-left: 53px; border-left: 2px solid orange;"># stage preparation
/bin/sh -c './clone.sh'
/bin/sh -c './init.sh'
# stage build
/bin/sh -c './build.sh --env=prod'
echo 'DONE!'
</code>
</pre>
</div>
</section>
<section class="slide clear">
<h2>DSL: Example</h2>
<div class="columns two">
<pre>
<code class="java" style="font-size: 20px; line-height: 27px; padding-left: 60px; padding-right: 25px;">class PipelineBuilder {
List<String> cmds = []
def stage(String name, Closure cl) {
cmds << "# stage ${name}"
cl()
}
def echo(String command) {
cmds << "echo '${command}'"
}
def sh(String command) {
cmds << "/bin/sh -c '${command}'"
}
}
</code>
</pre>
<pre>
<code class="java" style="font-size: 20px; line-height: 24px; padding-left: 20px; border-left: 2px solid orange;">def pipeline(Closure cl) {
def builder = new PipelineBuilder()
cl.delegate = builder
cl()
builder.cmds.each { println it }
}
pipeline {
stage('preparation') {
sh './clone.sh'
sh './init.sh'
}
stage('build') {
sh './build.sh --env=prod'
echo 'DONE!'
}
}
</code>
</pre>
</div>
</section>
<!-- /CLOSURES -->
<section class="slide clear">
<h2>Basic Types</h2>
<ul>
<li class="next"><b>Lists</b>: <code>def a = [1, 2, 3, 4]</code></li>
<li class="next"><b>Maps ⚠️</b>: <code>def b = [city: 'Berlin', state: 'DE']</code></li>
<li class="next"><b>GStrings</b>: <code>"x = ${b.city} + ${20 + 22}"</code> → <code>'x = Berlin 42'</code>
</li>
<li class="next"><b>Strings</b>: <code>'x = ${b.city}'</code> → <code>'x = ${b.city}'</code></li>
</ul>
</section>
<section class="slide clear">
<h2>Lists: Adding</h2>
<pre>
<code class="java">List a = [1, 2, 3]
a << 4 // a == [1, 2, 3, 4]
a << [5, 6] // a == [1, 2, 3, 4, [5, 6]]</code>
</pre>
<pre>
<code class="java">def a = [1, 2, 3]
a += [4, 5] // a == [1, 2, 3, 4, 5]</code>
</pre>
<pre>
<code class="java bad">List<Integer> a = [1, 2, 3]
a.addAll([4, 5]) // ⚠️ returns true, a == [1, 2, 3, 4, 5]</code>
</pre>
</section>
<section class="slide clear">
<h2>Lists: Iterating</h2>
<code>for</code> → <code>.each</code>
<pre><code>[1, 2, 3].each {
println "Item: $it"
}</code></pre>
<pre><code class="java">['a', 'b', 'c'].eachWithIndex { it, i ->
println "Index = $i, Item = $it"
}</code></pre>
</section>
<section class="slide clear">
<h2>Lists: Filtering and Transforming 🎩</h2>
<pre><code>[1, 2, 3].collect { it * 2 } // [2, 4, 6]
[1, 2, 3].find { it > 1 } // 2
[1, 2, 3].findAll { it > 1 } // [2, 3]</code></pre>
<pre>
<code class="java">['a', 'b', 'c'].collectEntries { [(it): it.toUpperCase()] }
// ['a': 'A', 'b': 'B', 'c': 'C']</code></pre>
<pre>
<code>(0..10).findAll { it % 2 == 0 }.collect { it ** 2 }.sum()
// 220 = 0 + 4 + 16 + 36 + 64 + 100</code></pre>
</section>
<section class="slide clear">
<h2>Lists: Slicing 🔪</h2>
<pre><code class="java">def a = [1, 2, 3, 4, 5]
assert a[0] == 1
assert a[-2] == 4
assert a[2..4] == [3, 4, 5]
assert a[4..<2, 0, 0, 4] == [5, 4, 1, 1, 5]</code></pre>
<pre class="next"><code class="java">assert 'hello'[0, 4..<0] == 'holle'
assert 'hello'.split('').first() == 'h' // ⚠️ fails if null/[]
assert 'hello'.split('').last() == 'o' // ⚠️ fails if null/[]</code>
</pre>
</section>
<section class="slide clear">
<h2>Maps: Adding/Merging</h2>
<pre>
<code class="java">Map a = [key1: 'val1']
assert a.key1 == 'val1'
assert a['key1'] == 'val1'</code>
</pre>
<pre class="next">
<code class="java">a << [key2: 'val20', key3: 'val3']
// a == [key1: 'val1', key2: 'val20', key3: 'val3']</code>
</pre>
<pre class="next">
<code class="java">a += [key3: 'val30']
// a == [key1: 'val1', key2: 'val20', key3: 'val30']</code>
</pre>
</section>
<section class="slide clear">
<h2>Maps: Iterating</h2>
<code>for</code> → <code>.each</code>
<pre><code>[a: 1, b: 2, c: 3].each {
println "k = ${it.key}, v = ${it.value}"
}</code></pre>
<pre><code>[a: 1, b: 2, c: 3].each { k, v ->
println "k = ${k}, v = ${v}"
}</code></pre>
</section>
<section class="slide clear">
<h2>Maps: Filtering and Transforming 🎩</h2>
<pre><code>[a: 1, b: 2, c: 3].collect { it.value * 2 } // [2, 4, 6]
[a: 1, b: 2, c: 3].collect { k, v -> v * 2 } // [2, 4, 6]
[a: 1, b: 2, c: 3].find { it.value > 1 }.value // 2 ⚠️
[a: 1, b: 2, c: 3].findAll { it.value > 1 } // [b: 2, c: 3]</code></pre>
<pre>
<code class="java">['a': 1, 'b': 2, 'c': 3].collectEntries {
k, v -> [(k): v ** 3]
}
// ['a': 1, 'b': 8, 'c': 27]</code></pre>
</section>
<section class="slide clear">
<h2><code>in</code> operator</h2>
<pre><code class='java'>'a' in ['a', 'b', 'c'] // true
'b' in [a: 1, b: 2, c: 3] // true
'a' in null // false</code></pre>
</section>
<section class="slide clear">
<h2>Ranges, Times</h2>
<pre>
<code>assert (2..5) == [2, 3, 4, 5]
assert (2..<5) == [2, 3, 4]
assert (-5..3).step(2) == [-5, -3, -1, 1, 3]</code>
</pre>
<pre class="next">
<code>assert ('d'..<'a') == ['d', 'c', 'b']</code>
</pre>
<pre class="next">
<code class="java">3.times { println "i = ${it}" }
// i = 0
// i = 1
// i = 2</code>
</pre>
</section>
<section class="slide clear">
<h2>Power Assertions 💪</h2>
<br />
<pre>
<code class="java">def expected = [0, 2, 4, 6]</code>
<code class="java">assert (0..10).findAll { it % 2 == 0 } == expected</code>
<span class="next">
<code> | | |</code>
<code> [0, 2, 4, 6, 8, 10] | [0, 2, 4, 6]</code>
<code> false</code>
</span>
</pre>
</section>
<section class="slide clear">
<h2>Getters, Setters</h2>
<p><b>Properties</b> — fields without access modifier</p>
<pre>
<code>class Company {
String name
}</code>
</pre>
<pre class="next">
<code class="bad">def name = company.getName()
company.setName("whatever")</code>
</pre>
<pre class="next">
<code>def name = company.name
company.name = "whatever"</code>
</pre>
</section>
<section class="slide clear">
<h2>Getters, Setters</h2>
<pre>
<code>class Company {
def getInformation() {
// implementation
}
}</code>
</pre>
<pre class="next">
<code>assert company.getInformation() == company.information</code>
</pre>
</section>
<section class="slide clear">
<h2>Getters, Setters</h2>
<pre>
<code>class A {</code>
<code> <mark class="warning">boolean</mark> <mark class="warning">is</mark><span style="font-size:10px;"> </span>Something() {</code>
<code> // implementation</code>
<code> }</code>
<code>}</code>
</pre>
<pre class="next">
<code>def a = new A()
assert a.isSomething() == a.something</code>
</pre>
</section>
<section class="slide clear">
<h2>Map Constructors 🗺</h2>
<pre>
<code class="java">class Person {</code>
<code class="java"> String firstName</code>
<code class="java"> String lastName</code>
<code class="java"> String fullName() {</code>
<code class="java"> "${firstName} ${lastName}"</code>
<code class="java"> }</code>
<code class="java">}</code>
</pre>
<pre class="next">
<code class="java">def p = new Person(firstName: 'John', lastName: 'Doe')</code>
<code class="java">assert p.fullName() == 'John Doe'</code>
</pre>
</section>
<section class="slide clear">
<h2>The <code>call</code> method 📞</h2>
<pre>
<code class="java" style="line-height: 37px;">class A {
def call(Integer x) { "got int" }
def call(Float x) { "got float" }
def call(Object x) { "got something else" }
def call() { "got nothing" }
}
def a = new A()
println a() // got nothing
println a(1) // got int
println a(2.5f) // got float
println a('abc') // got something else</code>
</pre>
<pre class="next">
<code class="java">def p = new Person(firstName: 'John', lastName: 'Doe')</code>
</pre>
</section>
<section class="slide clear">
<h2>Elvis Operator <code>?:</code></h2>
<pre>
<code class="bad">def displayName</code>
<code class="bad">if (user.name) {</code>
<code class="bad"> displayName = user.name</code>
<code class="bad">} else {</code>
<code class="bad"> displayName = 'Anonymous'</code>
<code class="bad">}</code>
</pre>
<pre class="next">
<code class="bad">def displayName = user.name ? user.name : 'Anonymous'</code>
</pre>
<pre class="next">
<code>def displayName = user.name ?: 'Anonymous'</code>
</pre>
</section>
<section class="slide clear">
<h2>Elvis Operator <code>?:</code></h2>
<h3 class="next"
style="transform: rotate(90deg); font-size: 200px; margin-top:-20px !important; margin-left: -300px !important;">
?:</h3>
<span class="next"><img src="images/elvis.jpg" alt="Elvis Presley" style="width: 240px; margin-left:100px;"
class="place center ">
</span>
</section>
<section class="slide clear">
<h2>Star-Dot Operator <code>*.</code></h2>
<pre>
<code>def a = new Company(name: 'ABC')
def b = new Company(name: 'BCD')
def c = new Company(name: 'DEF')</code>
</pre>
<pre class="next">
<code class="java bad">def names = [a, b, c].collect { it.name }
// ['ABC', 'BCD', 'DEF']</code>
</pre>
<pre class="next">
<code class="java">def names = [a, b, c]*.name</code>
</pre>
<pre class="next">
<code class="java">assert [a, b, c]*.name*.toLowerCase() == ['abc', 'bcd', 'def']</code>
</pre>
</section>
<section class="slide clear">
<h2>Null-Safe Navigation <code>?.</code></h2>
<pre>
<code>class Company {</code>
<code> Address address</code>
<code>}</code>
</pre>
<pre class="next">
<code class="bad">if (company != null && company.address != null && </code>
<code class="bad"> company.address.street != null) {</code>
<code class="bad"> println company.address.street.name</code>
<code class="bad">}</code>
</pre>
<pre class="next">
<code>println company?.address?.street?.name</code>
</pre>
</section>
<section class="slide clear">
<h2>Crouching <code>private</code>, hidden <code>public</code></h2>
<ul class="next">
<li><code>public</code> is default</li>
<pre>
<code><span class="strike">public</span> def sum(def a, def b) { a + b }</code>
</pre>
</ul>
<ul>
<li class="next">🤷♂️ <code>private</code> & <code>protected</code> are accessible by <a target=_blank
href="https://issues.apache.org/jira/browse/GROOVY-1875">anyone</a></li>
<li class="next">⚠️ IntelliJ doesn't detect usage of <code>private</code> methods in tests.
<br /><code>private</code> → <code>protected</code> 👍</li>
<li class="next">💡 <code>PPP</code> are <strike>access modifiers</strike> semantic notations</li>
</ol>
</section>
<section class="slide clear">
<h2>Power Switch 🔌</h2>
<pre>
<code class="java" style="font-size: 17px;">def testSwitch(val) {
def result
switch (val) {
case 60..90:
result = 'Range contains'; break
case [21, 'test', 9.12]:
result = 'List contains'; break
case { it instanceof Integer && it < 50 }:
result = 'Closure boolean'; break
}
result
}
assert testSwitch(21) == 'List contains'</code>
</pre>
</section>
<section class="slide clear">
<h2>RegExes</h2>
<pre>
<code class="java">def finder = ('groovy' <mark class="warning">==~</mark> /gr.*/)
assert finder == true</code>
</pre>
<pre><code class="java">def group = ('groovy and grails, ruby and rails' <mark class="warning">=~</mark>
/(\w+) and (\w+)/)
assert group instanceof java.util.regex.Matcher
assert 2 == group.size()
assert ['groovy and grails', 'groovy', 'grails'] == group[0]
assert ['ruby and rails', 'ruby', 'rails'] == group[1]</code></pre>
</section>
<section class="slide clear">
<h2>Example: JSON fetching & parsing</h2>
<pre><code class="java">import groovy.json.JsonSlurper
String url = 'https://jsonplaceholder.typicode.com/todos/1'
String response = url.toURL().text
def contents = new JsonSlurper().parseText(response)
assert contents.title == 'delectus aut autem'</code></pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-01.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java">for (int i = 0; i < changeLogSets.size(); i++) {</code>
<code class="java"> def entries = changeLogSets[i].items</code>
<code class="java"> for (int j = 0; j < entries.length; j++) {</code>
<code class="java"> def entry = entries[j]</code>
<code class="java"> echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java"> def files = new ArrayList(entry.affectedFiles)</code>
<code class="java"> for (int k = 0; k < files.size(); k++) {</code>
<code class="java"> def file = files[k]</code>
<code class="java"> body(jenkinsFileNames, entry, file)</code>
<code class="java"> }</code>
<code class="java"> }</code>
<code class="java">}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-01.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">for (int i = 0; i < changeLogSets.size(); i++) {</code>
<code class="java bad"> def entries = changeLogSets[i].items</code>
<code class="java bad"> for (int j = 0; j < entries.length; j++) {</code>
<code class="java bad"> def entry = entries[j]</code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java bad"> def files = new ArrayList(entry.affectedFiles)</code>
<code class="java bad"> for (int k = 0; k < files.size(); k++) {</code>
<code class="java bad"> def file = files[k]</code>
<code class="java bad"> body(jenkinsFileNames, entry, file)</code>
<code class="java bad"> }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-01.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">for (int i = 0; i < changeLogSets.size(); i++) {</code>
<code class="java bad"> def entries = changeLogSets[i].items</code>
<code class="java bad"> for (int j = 0; j < entries.length; j++) {</code>
<code class="java bad"> def entry = entries[j]</code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java bad"> def files = new ArrayList(entry.affectedFiles)</code>
<code class="java bad"> for (int k = 0; k < files.size(); k++) {</code>
<code class="java bad"> body(jenkinsFileNames, entry, files[k])</code>
<code class="java bad"> }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-02.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">for (int i = 0; i < changeLogSets.size(); i++) {</code>
<code class="java bad"> def entries = changeLogSets[i].items</code>
<code class="java bad"> for (int j = 0; j < entries.length; j++) {</code>
<code class="java bad"> def entry = entries[j]</code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java bad"> for (int k = 0; k < entry.affectedFiles.size(); k++) {</code>
<code class="java bad"> body(jenkinsFileNames, entry, entry.affectedFiles[k])</code>
<code class="java bad"> }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-02.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">for (int i = 0; i < changeLogSets.size(); i++) {</code>
<code class="java bad"> def entries = changeLogSets[i].items</code>
<code class="java bad"> for (int j = 0; j < entries.length; j++) {</code>
<code class="java bad"> def entry = entries[j]</code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java" > entry.affectedFiles.each { body(jenkinsFileNames, entry, it) }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-03.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">for (int i = 0; i < changeLogSets.size(); i++) {</code>
<code class="java bad"> def entries = changeLogSets[i].items</code>
<code class="java" > entries.each { entry -> </code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java" > entry.affectedFiles.each { body(jenkinsFileNames, entry, it) }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-04.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">changeLogSets.each { changeLogSet -> </code>
<code class="java bad"> def entries = changeLogSet.items</code>
<code class="java" > entries.each { entry -> </code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java" > entry.affectedFiles.each { body(jenkinsFileNames, entry, it) }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-05.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">changeLogSets.each { changeLogSet -> </code>
<code class="java bad" > changeLogSet.items.each { entry -> </code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java" > entry.affectedFiles.each { body(jenkinsFileNames, entry, it) }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-06.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java bad">def changeLogSets = currentBuild.changeSets ?: []</code>
<code class="java bad">changeLogSets*.items.each { entries -> </code>
<code class="java" > entries.each { entry -> </code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java" > entry.affectedFiles.each { body(jenkinsFileNames, entry, it) }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Example: Refactoring <img src="images/brain-07.jpg" style="height: 54px; width: 84px; vertical-align: middle;"></h2>
<pre style="font-size: 17px;">
<code class="java" >currentBuild.changeSets*.items.each { entries -> </code>
<code class="java" > entries.each { entry -> </code>
<code class="java" > echo "${entry.commitId} by ${entry.author} on ${entry.timestamp}: ${entry.msg}"</code>
<code class="java" > entry.affectedFiles.each { body(jenkinsFileNames, entry, it) }</code>
<code class="java" > }</code>
<code class="java" >}</code>
</pre>
</section>
<section class="slide clear">
<h2>Traits</h2>
<blockquote style="margin-bottom: 15px;">