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

The Power of Types, KotlinConf 2019

The Power of Types, KotlinConf 2019

From Assembler, over Fortran and C to modern Kotlin, we came a long way and improved the way we can express our thoughts in code.

One thing that happened during this journey is that languages allow us to use types for our problem domain, independent of the underlying computer architecture. Types became a powerful tool.

Types improve readability which probably is the most important aspect of programming! But types also prevent you from making mistakes at compile time. This is why Kotlin translated the null problem into the type system.

We can use these powers in our daily lives. We can write better code by avoiding “primitive obsession”. I’ll show you how!

Danny Preussler

December 06, 2019
Tweet

More Decks by Danny Preussler

Other Decks in Technology

Transcript

  1. Copenhagen
    Denmark
    THE POWER OF TYPES
    DANNY PREUSSLER
    @PreusslerBerlin

    View Slide

  2. View Slide

  3. Do you enjoy
    debugging?

    View Slide

  4. In an ideal world,
    in the right language,
    buggy code is impossible.
    The compiler simply refuses
    to build

    View Slide

  5. In an ideal world,
    in the right language,
    buggy code is impossible.
    The compiler simply refuses
    to build

    View Slide

  6. In an ideal world,
    in the right language,
    buggy code is impossible.
    The compiler simply refuses
    to build

    View Slide

  7. In an ideal world,
    in the right language,
    buggy code is impossible.
    The compiler simply refuses
    to build

    View Slide

  8. No language can prevent
    us from writing bugs!

    View Slide

  9. But we can avoid
    as many as possible,
    as early as possible

    View Slide

  10. TYPES

    View Slide

  11. Well-typed expressions
    do not go wrong
    A theory of type polymorphism in programming.
    Journal of Computer and System Sciences, Robert Milner, 1978

    View Slide

  12. WHAT IS A TYPE?

    View Slide

  13. abstraction mechanisms
    over data
    Several Types of Types in Programming Languages
    Simone Martini, Universit`a di Bologna

    View Slide

  14. A type is a concise, formal
    description of the behavior of
    a program fragment
    Type systems for programming languages,
    Didier Rémy, INRIA Paris

    View Slide

  15. THE HISTORY OF TYPES

    View Slide

  16. 1950 1960 1970 1980 1990 2000 2010 2020

    View Slide

  17. Types are
    pretty much absent
    1950 1960 1970 1980 1990 2000 2010 2020

    View Slide

  18. 1950 1960 1970 1980 1990 2000 2010 2020
    Fortran

    View Slide

  19. “Two types of constants are permissible: fixed
    points (restricted to integers) and floating points”
    FORTRAN manual, 1956
    But at same time, “mode” is used instead
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran

    View Slide

  20. 1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Types
    were born

    View Slide

  21. Integers are of type integer.
    All other numbers are of type real.
    Algol 60
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60

    View Slide

  22. the results of any program [..]
    should be comprehensible
    without knowing anything about the machine
    or its storage layout.
    Tony Hoare 1960
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60

    View Slide

  23. I realised that [types] were essential
    not only for determining memory requirements,
    but also for avoiding machine-dependent error
    Tony Hoare 1960
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60

    View Slide

  24. 1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    1966
    Algol W

    View Slide

  25. One missing piece
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W

    View Slide

  26. the type system
    (including user-defined types)
    [should] guarantee that only the prescribed
    operations on a type could operate on its values
    James H. Morris. Types are not sets, New York, NY, USA, 1973. ACM
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W

    View Slide

  27. 1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal

    View Slide

  28. Types became
    a central feature of
    programming languages
    as we understand them today
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal

    View Slide

  29. The dawn of
    Object Orientation
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++

    View Slide

  30. Type structure is
    a syntactic discipline
    for enforcing levels of
    abstraction
    Types, abstraction and parametric polymorphism, John C. Reynolds, 1983
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++

    View Slide

  31. The age of
    the internet languages
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java

    View Slide

  32. Object-oriented languages
    had made static typing so
    painful that many preferred to
    give up and just wing it.
    Ben Lynn, https://benlynn.blogspot.com/
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java

    View Slide

  33. Dynamic
    Typed
    languages
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby

    View Slide

  34. It is important to note
    that this is different
    than being typeless
    Dynamically Typed Languages,
    Laurence Tratt, Bournemouth University
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby

    View Slide

  35. the chief technical difference
    between them being when
    types are enforced
    Dynamically Typed Languages,
    Laurence Tratt, Bournemouth University
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby

    View Slide

  36. After you ship, it costs you
    about 10,000 times as much
    to fix a software bug
    Bill Joy, Java-One, 1999
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby
    Java Generics

    View Slide

  37. Functional
    Programming
    arises from the grave
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby
    Scala
    Java Generics

    View Slide

  38. Functional programming languages
    are traditionally typed
    (Scheme and Erlang are exceptions)
    Type systems for programming languages,
    Didier Rémy, INRIA Paris
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby
    Scala
    Java Generics

    View Slide

  39. Anders Hejlsberg, 2012
    you can write large programs in
    JavaScript.
    You just can’t maintain them.
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby
    Scala
    Typescript
    Java Generics

    View Slide

  40. The typed v.s. untyped flavor [..]
    should not be confused
    with [..] whether types [..]
    are explicit or implicit.
    Type systems for programming languages, Didier Rémy, INRIA Paris
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby
    Scala
    Typescript
    Java Generics

    View Slide

  41. type reconstruction
    starts spreading
    1950 1960 1970 1980 1990 2000 2010 2020
    Fortran
    Algol 60
    Algol W
    Pascal
    C++
    Java
    JS
    Ruby
    Scala
    Kotlin
    Typescript
    Java Generics

    View Slide

  42. KOTLIN AND TYPES

    View Slide

  43. The type system is
    Kotlin’s super power
    Nullability,
    1st class functions,
    Nothing

    View Slide

  44. If Kotlin uses
    the power of Types
    So can you!

    View Slide

  45. PRIMITIVE OBSESSION

    View Slide

  46. Usage of primitives
    instead of small objects for simple tasks

    View Slide

  47. Usage of constants for coding information

    View Slide

  48. View Slide

  49. View Slide

  50. View Slide

  51. View Slide

  52. View Slide

  53. View Slide

  54. View Slide

  55. Don’t put in name
    what can be in type

    View Slide

  56. Encoding the type [..]
    into the name [..]
    is brain damaged
    the compiler knows the types
    Linus Torvalds about Hungarian notation

    View Slide

  57. View Slide

  58. View Slide

  59. View Slide

  60. View Slide

  61. View Slide

  62. actually...

    View Slide

  63. actually...

    View Slide

  64. View Slide

  65. Don’t put in name what could be in type!

    View Slide

  66. Express your Domain knowledge

    View Slide

  67. Sample:
    At Soundcloud a URN
    uniquely identifies an entity:
    a user, track playlist…
    soundcloud:tracks:1234

    View Slide

  68. View Slide

  69. Anytime you find yourself writing code
    of the form
    if the object is of type T1, then
    do something, but if it’s of type
    T2, then do something else,
    slap yourself.
    Scott Meyers, Effective C++

    View Slide

  70. “type” properties/functions are a smell

    View Slide

  71. Depending properties might be hint for hidden type

    View Slide

  72. Factory methods are hint for different types

    View Slide

  73. View Slide

  74. View Slide

  75. View Slide

  76. View Slide

  77. Booleans are a smell: 2 things in 1 place

    View Slide

  78. View Slide

  79. Don’t let the creep in

    View Slide

  80. View Slide

  81. View Slide

  82. View Slide

  83. View Slide

  84. View Slide

  85. View Slide

  86. View Slide

  87. COSTS

    View Slide

  88. ALLOCATION COSTS

    View Slide

  89. On JVM,
    objects allocated in Eden space

    View Slide

  90. To avoid thread synchronization
    Eden is split into
    Thread Local Allocation Buffer

    View Slide

  91. Allocating space in TLAB
    is often just a pointer bump

    View Slide

  92. The compiler might even
    inline the whole class:
    Allocation elimination
    with escape analysis
    Urban performance legends, revisited, Brian Goetz 2005

    View Slide

  93. Allocations on JVM
    are very cheap

    View Slide

  94. A word on Android
    ART is optimized for allocations

    View Slide

  95. Allocations are OK
    Types are OK
    Yes, even enums
    Modern Android development:
    Android Jetpack, Kotlin, and more (Google I/O 2018)

    View Slide

  96. MEMORY COSTS

    View Slide

  97. Memory
    A plain Object takes 8 bytes
    On Sun's JDK 1.3.1 for Windows

    View Slide

  98. Memory
    Any wrapper will be
    8 bytes larger
    than wrapped primitive
    On Sun's JDK 1.3.1 for Windows

    View Slide

  99. But careful

    View Slide

  100. Memory
    An empty String
    takes 40 bytes
    On Sun's JDK 1.3.1 for Windows

    View Slide

  101. Memory
    Boolean:
    header has 12 bytes
    + 1 for the boolean
    + 3 for the granularity
    On HotSpot JVM 1.7.0_51

    View Slide

  102. Memory
    its "size" isn't something
    that's precisely defined
    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

    View Slide

  103. Kotlin compiler translates to int

    View Slide

  104. Kotlin compiler translates to java.lang.Integer

    View Slide

  105. Kotlin compiler can use primitive int inside Age

    View Slide

  106. Kotlin compiler translates to int

    View Slide

  107. Kotlin keeps Age with int

    View Slide

  108. GARBAGE COSTS

    View Slide

  109. the performance of the garbage
    collector is not determined by the
    number of dead objects but rather by
    the number of live ones
    Analyzing Java Memory, Java enterprise performance book

    View Slide

  110. Short lived objects are cheap

    View Slide

  111. SUM UP

    View Slide

  112. ● Types improve readability (documentation)
    ● Types help compile time correctness
    (safety, machine checked documentation)
    ● Types improve abstraction (modularity)
    -> Types are enabler
    Sum up

    View Slide

  113. Watch out for “hidden type” smells:
    ● Types information in names
    ● Type attributes
    ● Booleans
    ● Primitives
    Sum up

    View Slide

  114. My goal was to ensure that all
    use of references should be
    absolutely safe,
    with checking performed
    automatically by the compiler
    Tony Hoare about Algol W

    View Slide

  115. But I couldn't resist the
    temptation to put in a null
    reference, simply because it
    was so easy to implement.
    Tony Hoare about Algol W

    View Slide

  116. call it my
    billion-dollar mistake
    Tony Hoare about Algol W

    View Slide

  117. Readability
    Let the compiler
    do the work!

    View Slide

  118. TYPE
    ALL THE THINGS!

    View Slide

  119. ● Several Types of Types in Programming Languages
    Simone Martini, Universit`a di Bologna https://arxiv.org/pdf/1510.03726
    ● History and Philosophy of Computing
    Third International Conference, HaPoC 2015, Pisa, Italy
    ● Type systems for programming languages
    Didier Rémy, INRIA Paris, http://gallium.inria.fr/~remy/mpri/cours1.pdf
    ● A theory of type polymorphism in programming.
    Journal of Computer and System Sciences, Robert Milner, 1978
    https://homepages.inf.ed.ac.uk/wadler/papers/papers-we-love/milner-type-polymorphism.pdf
    ● Dynamically Typed Languages
    Laurence Tratt,Bournemouth University, 2009
    https://tratt.net/laurie/research/pubs/html/tratt__dynamically_typed_languages/
    Sources
    History of types

    View Slide

  120. ● Type Wars, The Clean Code Blog, Uncle Bob
    http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html
    ● From Primitive Obsession to Domain Modelling, Mark Seemann
    https://blog.ploeh.dk/2015/01/19/from-primitive-obsession-to-domain-modelling/
    ● Phantom types in Kotlin, Danny Preussler
    https://proandroiddev.com/phantom-types-in-kotlin-afd3f59fde10?
    ● Primitive Data types, Oracle
    https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
    Sources
    Types

    View Slide

  121. ● The cost of object creation in Java, including garbage collection
    https://www.bettercodebytes.com/the-cost-of-object-creation-in-java-including-garbage-collection/
    ● Thread-Local Allocation Buffers in JVM
    https://alidg.me/blog/2019/6/21/tlab-jvm
    ● Urban performance legends, revisited
    https://www.ibm.com/developerworks/java/library/j-jtp09275/index.html#artrelatedtopics
    ● E-book: Java enterprise performance
    https://www.dynatrace.com/resources/ebooks/javabook/
    ● Do you know your data size?
    https://www.javaworld.com/article/2077496/java-tip-130--do-you-know-your-data-size-.html
    ● The impact of Garbage Collection on Application Performance
    https://www.dynatrace.com/resources/ebooks/javabook/impact-of-garbage-collection-on-performance/
    Sources
    Costs

    View Slide

  122. #KotlinConf
    THANK YOU
    AND
    REMEMBER
    TO VOTE
    Danny Preussler @ PreusslerBerlin

    View Slide