Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Go Beyond Paradigms

Go Beyond Paradigms

Is Go object oriented without inheritance? It has closures, but is it functional? The modern approach to learning languages focuses on features, not paradigms. This talk names key language features, how they affect design and coding patterns, and shows in practice how features relate to idiomatic code.

27c093d0834208f4712faaaec38c2c5c?s=128

Luciano Ramalho

October 09, 2019
Tweet

Transcript

  1. 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
  2. 2 Sometimes you need a blank template.

  3. 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
  4. TECH REVIEWER OF THE BRAZILIAN EDITION OF GOPL GOPL:
 The

    Go Programming Language Donovan & Kernighan 4
  5. WORKSHOP: INTRODUCING GO WITH TDD 5 https://tgo.li/wtm-tdd-go

  6. EMPORIO CELESTIAL By Jorge Luis Borges 6

  7. 7

  8. 8

  9. 9

  10. PARADIGMS Programming language categories 10

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

    Michael L. Scott 11
  12. GCD ASM X86 Máximo divisor comum em assembly x86 (Scott,

    2015) 12
  13. GCD IN C, OCAML, PROLOG 13

  14. GCD IN GO Imperative style Functional style 14

  15. GCD IN GO Imperative style Functional style 15 Bad fit

    for Go:
 no tail-call optimisation
  16. SCOTT’S CLASSIFICATION 16 Programming Language Pragmatics,
 4th edition (2015) Michael

    L. Scott
  17. SCOTT’S CLASSIFICATION (ZOOM) 17

  18. SCOTT’S CLASSIFICATION (ZOOM) 18 ???

  19. ANOTHER BOOK Princípios de Linguagens de Programação
 (2003) Ana Cristina

    Vieira de Melo Flávio Soares Corrêa da Silva 19
  20. 20

  21. 21 Lógicas

  22. THE GO FAMILY TREE 22 source:
 The Go Programming Language


    Donovan & Kernighan
  23. CATEGORIES? Ontologies are so 1900’s 23

  24. A CLASSIFICATION BASED ON HARD FACTS 24

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

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

  27. A BETTER APPROACH Fundamental Features of Programming Languages 27

  28. HANDS-ON PROGRAMMING LANGUAGE THEORY 28

  29. A PAPER PRESENTING THE APPROACH 29

  30. A PAPER PRESENTING THE APPROACH 30

  31. A PAPER PRESENTING THE APPROACH 31

  32. A PAPER PRESENTING THE APPROACH 32

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

  34. FEATURES Understand core features, not just syntax and libraries 34

  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. DESIGN PATTERNS When languages fall short 41

  42. 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
  43. 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
  44. 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
  45. 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
  46. 46

  47. 47

  48. 48

  49. ITERABLES & ITERATORS An excellent abstraction 49

  50. ITERATION IN C 50 #include <stdio.h> int main(int argc, char

    *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }
  51. ITERATION IN GO 51

  52. ITERATION IN C AND GO 52 #include <stdio.h> int main(int

    argc, char *argv[]) { for(int i = 0; i < argc; i++) printf("%s\n", argv[i]); return 0; }
  53. 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
  54. FOREACH IN CLU 54 year: 1975 CLU Reference Manual, p.

    2 B. Liskov et. al. — © 1981 Springer-Verlag
  55. 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)
  56. ITERATOR PATTERN The classic recipe 56

  57. THE ITERATOR FROM THE GANG OF FOUR Design Patterns
 Gamma,

    Helm, Johnson & Vlissides
 ©1994 Addison-Wesley 57
  58. 58 Head First Design Patterns Poster
 O'Reilly
 ISBN 0-596-10214-3

  59. CLASSIC
 ITERATOR IN GO The classic recipe in Go 59

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

  61. IMPLEMENTING A CLASSIC ITERATOR IN GO Not too bad
 (if

    you like Java) 61
  62. CHANNEL-BASED ITERATOR Leveraging Go features 62

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

  64. USAGE COMPARISON 64

  65. IMPLEMENTING A CHANNEL-BASED ITERATOR 65

  66. IMPLEMENTATION COMPARISON 66

  67. 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
  68. STRATEGY IN GO Leveraging core features of Go 68

  69. CHOOSE AN ALGORITHM AT RUN TIME 69

  70. 70 Context Strategy Concrete strategies CHOOSE AN ALGORITHM AT RUN

    TIME
  71. STRATEGY IN GO “Classic” implementation with an interface Implementation with

    first-class functions 71
  72. TESTS 72 interface/strategy_test.go https://tgo.li/gobeyond2019

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

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

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

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

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

  78. STRATEGY WITH FIRST CLASS FUNCTIONS 78

  79. TESTS WITH FUNCTIONS 79

  80. CONCRETE STRATEGIES AS FUNCTIONS 80

  81. WHICH IS MORE IDIOMATIC GO? Interfaces v. Functions 81

  82. WHICH IS MORE GOISH? Interfaces v. Functions 82

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

    interfaces are common. 83
  84. 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
  85. WRAPPING UP Why learn the fundamentals 85

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

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

    F G
  88. …to decide whether a particular pattern or implementation makes sense.

    FEATURES ARE THE BEST GUIDE… A B C D E F G + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ + ✔ ✔ ✔ ✔ ✔
  89. 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 *
  90. Luciano Ramalho luciano.ramalho@thoughtworks.com
 Twitter: @ramalhoorg Github: https://github.com/standupdev Slides: speakerdeck.com/ramalho VIELEN

    DANK !