THE POWER OF TYPES DANNY PREUSSLER @PreusslerBerlin Copenhagen - - PowerPoint PPT Presentation

the power of types danny preussler
SMART_READER_LITE
LIVE PREVIEW

THE POWER OF TYPES DANNY PREUSSLER @PreusslerBerlin Copenhagen - - PowerPoint PPT Presentation

THE POWER OF TYPES DANNY PREUSSLER @PreusslerBerlin Copenhagen Denmark Do you enjoy debugging? In an ideal world, in the right language, buggy code is impossible. The compiler simply refuses to build In an ideal world, in the right


slide-1
SLIDE 1

Copenhagen Denmark

THE POWER OF TYPES DANNY PREUSSLER

@PreusslerBerlin

slide-2
SLIDE 2
slide-3
SLIDE 3

Do you enjoy debugging?

slide-4
SLIDE 4

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

slide-5
SLIDE 5

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

slide-6
SLIDE 6

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

slide-7
SLIDE 7

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

slide-8
SLIDE 8

No language can prevent us from writing bugs!

slide-9
SLIDE 9

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

slide-10
SLIDE 10

TYPES

slide-11
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

slide-12
SLIDE 12

WHAT IS A TYPE?

slide-13
SLIDE 13

abstraction mechanisms

  • ver data

Several Types of Types in Programming Languages Simone Martini, Universit`a di Bologna

slide-14
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

slide-15
SLIDE 15

THE HISTORY OF TYPES

slide-16
SLIDE 16

1950 1960 1970 1980 1990 2000 2010 2020

slide-17
SLIDE 17

Types are pretty much absent

1950 1960 1970 1980 1990 2000 2010 2020

slide-18
SLIDE 18

1950 1960 1970 1980 1990 2000 2010 2020

Fortran

slide-19
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

slide-20
SLIDE 20

1950 1960 1970 1980 1990 2000 2010 2020

Fortran Algol 60

Types were born

slide-21
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

slide-22
SLIDE 22

the results of any program [..] should be comprehensible without knowing anything about the machine

  • r its storage layout.

Tony Hoare 1960

1950 1960 1970 1980 1990 2000 2010 2020

Fortran Algol 60

slide-23
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

slide-24
SLIDE 24

1950 1960 1970 1980 1990 2000 2010 2020

Fortran Algol 60 Algol W

1966 Algol W

slide-25
SLIDE 25

One missing piece

1950 1960 1970 1980 1990 2000 2010 2020

Fortran Algol 60 Algol W

slide-26
SLIDE 26

the type system (including user-defined types) [should] guarantee that only the prescribed

  • perations 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

1950 1960 1970 1980 1990 2000 2010 2020

Fortran Algol 60 Algol W Pascal

slide-28
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

slide-29
SLIDE 29

The dawn of Object Orientation

1950 1960 1970 1980 1990 2000 2010 2020

Fortran Algol 60 Algol W Pascal C++

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

slide-31
SLIDE 31

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

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

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

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

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

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

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

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

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

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

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

KOTLIN AND TYPES

slide-43
SLIDE 43

The type system is Kotlin’s super power

Nullability, 1st class functions, Nothing

slide-44
SLIDE 44

If Kotlin uses the power of Types So can you!

slide-45
SLIDE 45

PRIMITIVE OBSESSION

slide-46
SLIDE 46

Usage of primitives instead of small objects for simple tasks

slide-47
SLIDE 47

Usage of constants for coding information

slide-48
SLIDE 48
slide-49
SLIDE 49
slide-50
SLIDE 50
slide-51
SLIDE 51
slide-52
SLIDE 52
slide-53
SLIDE 53
slide-54
SLIDE 54
slide-55
SLIDE 55

Don’t put in name what can be in type

slide-56
SLIDE 56

Encoding the type [..] into the name [..] is brain damaged the compiler knows the types

Linus Torvalds about Hungarian notation

slide-57
SLIDE 57
slide-58
SLIDE 58
slide-59
SLIDE 59
slide-60
SLIDE 60
slide-61
SLIDE 61
slide-62
SLIDE 62

actually...

slide-63
SLIDE 63

actually...

slide-64
SLIDE 64
slide-65
SLIDE 65

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

slide-66
SLIDE 66

Express your Domain knowledge

slide-67
SLIDE 67

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

soundcloud:tracks:1234

slide-68
SLIDE 68
slide-69
SLIDE 69

Anytime you find yourself writing code

  • f 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

“type” properties/functions are a smell

slide-71
SLIDE 71

Depending properties might be hint for hidden type

slide-72
SLIDE 72

Factory methods are hint for different types

slide-73
SLIDE 73
slide-74
SLIDE 74
slide-75
SLIDE 75
slide-76
SLIDE 76
slide-77
SLIDE 77

Booleans are a smell: 2 things in 1 place

slide-78
SLIDE 78
slide-79
SLIDE 79

Don’t let the creep in

slide-80
SLIDE 80
slide-81
SLIDE 81
slide-82
SLIDE 82
slide-83
SLIDE 83
slide-84
SLIDE 84
slide-85
SLIDE 85
slide-86
SLIDE 86
slide-87
SLIDE 87

COSTS

slide-88
SLIDE 88

ALLOCATION COSTS

slide-89
SLIDE 89

On JVM,

  • bjects allocated in Eden space
slide-90
SLIDE 90

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

slide-91
SLIDE 91

Allocating space in TLAB is often just a pointer bump

slide-92
SLIDE 92

The compiler might even inline the whole class:

Allocation elimination with escape analysis

Urban performance legends, revisited, Brian Goetz 2005

slide-93
SLIDE 93

Allocations on JVM are very cheap

slide-94
SLIDE 94

A word on Android

ART is optimized for allocations

slide-95
SLIDE 95

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

MEMORY COSTS

slide-97
SLIDE 97

Memory

A plain Object takes 8 bytes

On Sun's JDK 1.3.1 for Windows

slide-98
SLIDE 98

Memory

Any wrapper will be 8 bytes larger than wrapped primitive

On Sun's JDK 1.3.1 for Windows

slide-99
SLIDE 99

But careful

slide-100
SLIDE 100

Memory

An empty String takes 40 bytes

On Sun's JDK 1.3.1 for Windows

slide-101
SLIDE 101

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

Memory

its "size" isn't something that's precisely defined

https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html

slide-103
SLIDE 103

Kotlin compiler translates to int

slide-104
SLIDE 104

Kotlin compiler translates to java.lang.Integer

slide-105
SLIDE 105

Kotlin compiler can use primitive int inside Age

slide-106
SLIDE 106

Kotlin compiler translates to int

slide-107
SLIDE 107

Kotlin keeps Age with int

slide-108
SLIDE 108

GARBAGE COSTS

slide-109
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

slide-110
SLIDE 110

Short lived objects are cheap

slide-111
SLIDE 111

SUM UP

slide-112
SLIDE 112
  • 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

Watch out for “hidden type” smells:

  • Types information in names
  • Type attributes
  • Booleans
  • Primitives

Sum up

slide-114
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

slide-115
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

slide-116
SLIDE 116

call it my billion-dollar mistake

Tony Hoare about Algol W

slide-117
SLIDE 117

Readability

Let the compiler do the work!

slide-118
SLIDE 118

TYPE

ALL THE THINGS!

slide-119
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

slide-120
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

slide-121
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

slide-122
SLIDE 122

#KotlinConf

THANK YOU AND REMEMBER TO VOTE

Danny Preussler @ PreusslerBerlin