Slide 1

Slide 1 text

T h e o r y i n P r a c t i c e GO BEYOND PARADIGMS Is Go object-oriented? Is that a good question? Understand language features, use the right patterns. Luciano Ramalho @ramalhoorg

Slide 2

Slide 2 text

2 Sometimes you need a blank template.

Slide 3

Slide 3 text

FLUENT PYTHON 3 5 stars at amazon.de Published in 9 languages: •Chinese (simplified) •Chinese (traditional) •English •French •Japanese •Korean •Polish •Brazilian Portuguese •Russian 2nd edition: late 2020

Slide 4

Slide 4 text

TECH REVIEWER OF THE BRAZILIAN EDITION OF GOPL GOPL:
 The Go Programming Language Donovan & Kernighan 4

Slide 5

Slide 5 text

WORKSHOP: INTRODUCING GO WITH TDD 5 https://tgo.li/wtm-tdd-go

Slide 6

Slide 6 text

EMPORIO CELESTIAL By Jorge Luis Borges 6

Slide 7

Slide 7 text

7

Slide 8

Slide 8 text

8

Slide 9

Slide 9 text

9

Slide 10

Slide 10 text

PARADIGMS Programming language categories 10

Slide 11

Slide 11 text

A GOOD SURVEY-STYLE TEXTBOOK Programming Language Pragmatics,
 4th edition (2015) Michael L. Scott 11

Slide 12

Slide 12 text

GCD ASM X86 Máximo divisor comum em assembly x86 (Scott, 2015) 12

Slide 13

Slide 13 text

GCD IN C, OCAML, PROLOG 13

Slide 14

Slide 14 text

GCD IN GO Imperative style Functional style 14

Slide 15

Slide 15 text

GCD IN GO Imperative style Functional style 15 Bad fit for Go:
 no tail-call optimisation

Slide 16

Slide 16 text

SCOTT’S CLASSIFICATION 16 Programming Language Pragmatics,
 4th edition (2015) Michael L. Scott

Slide 17

Slide 17 text

SCOTT’S CLASSIFICATION (ZOOM) 17

Slide 18

Slide 18 text

SCOTT’S CLASSIFICATION (ZOOM) 18 ???

Slide 19

Slide 19 text

ANOTHER BOOK Princípios de Linguagens de Programação
 (2003) Ana Cristina Vieira de Melo Flávio Soares Corrêa da Silva 19

Slide 20

Slide 20 text

20

Slide 21

Slide 21 text

21 Lógicas

Slide 22

Slide 22 text

THE GO FAMILY TREE 22 source:
 The Go Programming Language
 Donovan & Kernighan

Slide 23

Slide 23 text

CATEGORIES? Ontologies are so 1900’s 23

Slide 24

Slide 24 text

A CLASSIFICATION BASED ON HARD FACTS 24

Slide 25

Slide 25 text

A CLASSIFICATION BASED ON HARD FACTS 25 “Noble” gases!?

Slide 26

Slide 26 text

“Ontology is overrated.” Clay Shirky 26 https://tgo.li/onto-over

Slide 27

Slide 27 text

A BETTER APPROACH Fundamental Features of Programming Languages 27

Slide 28

Slide 28 text

HANDS-ON PROGRAMMING LANGUAGE THEORY 28

Slide 29

Slide 29 text

A PAPER PRESENTING THE APPROACH 29

Slide 30

Slide 30 text

A PAPER PRESENTING THE APPROACH 30

Slide 31

Slide 31 text

A PAPER PRESENTING THE APPROACH 31

Slide 32

Slide 32 text

A PAPER PRESENTING THE APPROACH 32

Slide 33

Slide 33 text

HANDS-ON WITH RACKET (A MODERN SCHEME DIALECT) 33

Slide 34

Slide 34 text

FEATURES Understand core features, not just syntax and libraries 34

Slide 35

Slide 35 text

SAMPLE FEATURES ✖ LANGUAGES 35 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 36

Slide 36 text

SAMPLE FEATURES ✖ LANGUAGES 36 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural Functions as objects Classes as objects

Slide 37

Slide 37 text

SAMPLE FEATURES ✖ LANGUAGES 37 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 38

Slide 38 text

SAMPLE FEATURES ✖ LANGUAGES 38 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 39

Slide 39 text

SAMPLE FEATURES ✖ LANGUAGES 39 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 40

Slide 40 text

SAMPLE FEATURES ✖ LANGUAGES 40 Common Lisp C Java Python Go First-class functions ✔ ∗ ✔ ✔ ✔ First-class types ✔ ✔ Iterators ∗ ✔ ✔ ∗ Variable model reference value* value and reference reference value* and reference* Type checking dynamic static static dynamic static Type expression structural nominal nominal structural structural

Slide 41

Slide 41 text

DESIGN PATTERNS When languages fall short 41

Slide 42

Slide 42 text

GOF: CLASSIC BOOK BY THE “GANG OF FOUR” Design Patterns: Elements of Reusable Object-Oriented Software (1995) Erich Gamma
 Richard Helm
 Ralph Johnson
 John Vlissides 42

Slide 43

Slide 43 text

NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 43

Slide 44

Slide 44 text

NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 44

Slide 45

Slide 45 text

NOT EVERY PATTERN IS UNIVERSAL Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides, Design Patterns: Elements of Reusable Object-Oriented Software (Addison-Wesley, 1995), p. 4. 45

Slide 46

Slide 46 text

46

Slide 47

Slide 47 text

47

Slide 48

Slide 48 text

48

Slide 49

Slide 49 text

ITERABLES & ITERATORS An excellent abstraction 49

Slide 50

Slide 50 text

ITERATION IN C 50 #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }

Slide 51

Slide 51 text

ITERATION IN GO 51

Slide 52

Slide 52 text

ITERATION IN C AND GO 52 #include int main(int argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }

Slide 53

Slide 53 text

FOREACH IN BARBARA LISKOV'S CLU 53 CLU Reference Manual — B. Liskov et. al. — © 1981 Springer-Verlag — also available online from MIT: http://publications.csail.mit.edu/lcs/pubs/pdf/MIT-LCS-TR-225.pdf © 2010 Kenneth C. Zirkel — CC-BY-SA

Slide 54

Slide 54 text

FOREACH IN CLU 54 year: 1975 CLU Reference Manual, p. 2 B. Liskov et. al. — © 1981 Springer-Verlag

Slide 55

Slide 55 text

ITERABLE OBJECTS: THE KEY TO FOREACH • Python, Java, CLU etc. let programmers define iterable objects
 
 
 
 
 
 
 • In Go, only array, slice, string, map, channel are supported by for/range 55 for item in an_iterable: process(item) for item in an_iterable: process(item)

Slide 56

Slide 56 text

ITERATOR PATTERN The classic recipe 56

Slide 57

Slide 57 text

THE ITERATOR FROM THE GANG OF FOUR Design Patterns
 Gamma, Helm, Johnson & Vlissides
 ©1994 Addison-Wesley 57

Slide 58

Slide 58 text

58 Head First Design Patterns Poster
 O'Reilly
 ISBN 0-596-10214-3

Slide 59

Slide 59 text

CLASSIC
 ITERATOR IN GO The classic recipe in Go 59

Slide 60

Slide 60 text

USING A CLASSIC ITERATOR IN GO for/range not supported 60

Slide 61

Slide 61 text

IMPLEMENTING A CLASSIC ITERATOR IN GO Not too bad
 (if you like Java) 61

Slide 62

Slide 62 text

CHANNEL-BASED ITERATOR Leveraging Go features 62

Slide 63

Slide 63 text

for/range supported! USING A CHANNEL-BASED ITERATOR 63

Slide 64

Slide 64 text

USAGE COMPARISON 64

Slide 65

Slide 65 text

IMPLEMENTING A CHANNEL-BASED ITERATOR 65

Slide 66

Slide 66 text

IMPLEMENTATION COMPARISON 66

Slide 67

Slide 67 text

CONSIDERING THE OPTIONS A classic two-method iterator is more cumbersome to use and to implement. It is used in the standard library. Ex: buffio.Scanner (with methods called Scan and Text). Clients can quit at any time; iterator will be garbage-collected. A channel-based iterator is simpler to use and to implement. However, there is a context switch cost to run a coroutine, and the coroutine may leak if it does not run to completion. Allowing clients to cancel the iterator requires an additional channel for signaling. 67

Slide 68

Slide 68 text

STRATEGY IN GO Leveraging core features of Go 68

Slide 69

Slide 69 text

CHOOSE AN ALGORITHM AT RUN TIME 69

Slide 70

Slide 70 text

70 Context Strategy Concrete strategies CHOOSE AN ALGORITHM AT RUN TIME

Slide 71

Slide 71 text

STRATEGY IN GO “Classic” implementation with an interface Implementation with first-class functions 71

Slide 72

Slide 72 text

TESTS 72 interface/strategy_test.go https://tgo.li/gobeyond2019

Slide 73

Slide 73 text

TESTS (CONT.) 73 interface/strategy_test.go https://tgo.li/gobeyond2019

Slide 74

Slide 74 text

CUSTOMER AND LINE ITEM 74 interface/store.go https://tgo.li/gobeyond2019

Slide 75

Slide 75 text

CONTEXT AND STRATEGY 75 interface/strategy.go https://tgo.li/gobeyond2019 Context Strategy

Slide 76

Slide 76 text

CONCRETE STRATEGY 76 interface/store.go https://tgo.li/gobeyond2019

Slide 77

Slide 77 text

MORE CONCRETE STRATEGIES 77 interface/store.go https://tgo.li/gobeyond2019

Slide 78

Slide 78 text

STRATEGY WITH FIRST CLASS FUNCTIONS 78

Slide 79

Slide 79 text

TESTS WITH FUNCTIONS 79

Slide 80

Slide 80 text

CONCRETE STRATEGIES AS FUNCTIONS 80

Slide 81

Slide 81 text

WHICH IS MORE IDIOMATIC GO? Interfaces v. Functions 81

Slide 82

Slide 82 text

WHICH IS MORE GOISH? Interfaces v. Functions 82

Slide 83

Slide 83 text

INTERFACES V. FIRST CLASS FUNCTIONS In the standard library, one-method interfaces are common. 83

Slide 84

Slide 84 text

INTERFACES V. FIRST CLASS FUNCTIONS But the http package supports both ways*: 84 *Kumar Iyer (ThoughtWorks): Higher-order functions vs interfaces in golang http://bit.ly/2j39uKh

Slide 85

Slide 85 text

WRAPPING UP Why learn the fundamentals 85

Slide 86

Slide 86 text

INSTEAD OF PARADIGMS… 86 A B C D E F G

Slide 87

Slide 87 text

FOCUS ON FUNDAMENTAL FEATURES 87 A B C D E F G

Slide 88

Slide 88 text

…to decide whether a particular pattern or implementation makes sense. FEATURES ARE THE BEST GUIDE… A B C D E F G + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ ✔

Slide 89

Slide 89 text

WHY LEARN THE FUNDAMENTALS* • Learn new languages more easily • Make the most of language features • Know how to choose alternative ways of implementation • Make good use of design patterns • Debug complicated bugs • Mimic useful features in languages where they are missing 89 Inspired by Programming Language Pragmatics Michael L. Scott *

Slide 90

Slide 90 text

Luciano Ramalho [email protected]
 Twitter: @ramalhoorg Github: https://github.com/standupdev Slides: speakerdeck.com/ramalho VIELEN DANK !