Slide 1

Slide 1 text

Copenhagen Denmark THE POWER OF TYPES DANNY PREUSSLER @PreusslerBerlin

Slide 2

Slide 2 text

No content

Slide 3

Slide 3 text

Do you enjoy debugging?

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

No language can prevent us from writing bugs!

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

TYPES

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

WHAT IS A TYPE?

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

THE HISTORY OF TYPES

Slide 16

Slide 16 text

1950 1960 1970 1980 1990 2000 2010 2020

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

1950 1960 1970 1980 1990 2000 2010 2020 Fortran

Slide 19

Slide 19 text

“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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

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

Slide 25

Slide 25 text

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

Slide 26

Slide 26 text

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

Slide 27

Slide 27 text

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

Slide 28

Slide 28 text

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

Slide 29

Slide 29 text

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

Slide 30

Slide 30 text

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++

Slide 31

Slide 31 text

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

Slide 32

Slide 32 text

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

Slide 33

Slide 33 text

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

Slide 34

Slide 34 text

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

Slide 35

Slide 35 text

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

Slide 36

Slide 36 text

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

Slide 37

Slide 37 text

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

Slide 38

Slide 38 text

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

Slide 39

Slide 39 text

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

Slide 40

Slide 40 text

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

Slide 41

Slide 41 text

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

Slide 42

Slide 42 text

KOTLIN AND TYPES

Slide 43

Slide 43 text

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

Slide 44

Slide 44 text

If Kotlin uses the power of Types So can you!

Slide 45

Slide 45 text

PRIMITIVE OBSESSION

Slide 46

Slide 46 text

Usage of primitives instead of small objects for simple tasks

Slide 47

Slide 47 text

Usage of constants for coding information

Slide 48

Slide 48 text

No content

Slide 49

Slide 49 text

No content

Slide 50

Slide 50 text

No content

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

No content

Slide 53

Slide 53 text

No content

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

Don’t put in name what can be in type

Slide 56

Slide 56 text

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

Slide 57

Slide 57 text

No content

Slide 58

Slide 58 text

No content

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

No content

Slide 61

Slide 61 text

No content

Slide 62

Slide 62 text

actually...

Slide 63

Slide 63 text

actually...

Slide 64

Slide 64 text

No content

Slide 65

Slide 65 text

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

Slide 66

Slide 66 text

Express your Domain knowledge

Slide 67

Slide 67 text

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

Slide 68

Slide 68 text

No content

Slide 69

Slide 69 text

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++

Slide 70

Slide 70 text

“type” properties/functions are a smell

Slide 71

Slide 71 text

Depending properties might be hint for hidden type

Slide 72

Slide 72 text

Factory methods are hint for different types

Slide 73

Slide 73 text

No content

Slide 74

Slide 74 text

No content

Slide 75

Slide 75 text

No content

Slide 76

Slide 76 text

No content

Slide 77

Slide 77 text

Booleans are a smell: 2 things in 1 place

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

Don’t let the creep in

Slide 80

Slide 80 text

No content

Slide 81

Slide 81 text

No content

Slide 82

Slide 82 text

No content

Slide 83

Slide 83 text

No content

Slide 84

Slide 84 text

No content

Slide 85

Slide 85 text

No content

Slide 86

Slide 86 text

No content

Slide 87

Slide 87 text

COSTS

Slide 88

Slide 88 text

ALLOCATION COSTS

Slide 89

Slide 89 text

On JVM, objects allocated in Eden space

Slide 90

Slide 90 text

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

Slide 91

Slide 91 text

Allocating space in TLAB is often just a pointer bump

Slide 92

Slide 92 text

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

Slide 93

Slide 93 text

Allocations on JVM are very cheap

Slide 94

Slide 94 text

A word on Android ART is optimized for allocations

Slide 95

Slide 95 text

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

Slide 96

Slide 96 text

MEMORY COSTS

Slide 97

Slide 97 text

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

Slide 98

Slide 98 text

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

Slide 99

Slide 99 text

But careful

Slide 100

Slide 100 text

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

Slide 101

Slide 101 text

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

Slide 102

Slide 102 text

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

Slide 103

Slide 103 text

Kotlin compiler translates to int

Slide 104

Slide 104 text

Kotlin compiler translates to java.lang.Integer

Slide 105

Slide 105 text

Kotlin compiler can use primitive int inside Age

Slide 106

Slide 106 text

Kotlin compiler translates to int

Slide 107

Slide 107 text

Kotlin keeps Age with int

Slide 108

Slide 108 text

GARBAGE COSTS

Slide 109

Slide 109 text

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

Slide 110

Slide 110 text

Short lived objects are cheap

Slide 111

Slide 111 text

SUM UP

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

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

Slide 115

Slide 115 text

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

Slide 116

Slide 116 text

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

Slide 117

Slide 117 text

Readability Let the compiler do the work!

Slide 118

Slide 118 text

TYPE ALL THE THINGS!

Slide 119

Slide 119 text

● 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

Slide 120

Slide 120 text

● 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

Slide 121

Slide 121 text

● 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

Slide 122

Slide 122 text

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