Copenhagen Denmark
THE POWER OF TYPES DANNY PREUSSLER
@PreusslerBerlin
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
Copenhagen Denmark
@PreusslerBerlin
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 language, buggy code is impossible. The compiler simply refuses to build
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 language, buggy code is impossible. The compiler simply refuses to build
No language can prevent us from writing bugs!
But we can avoid as many as possible, as early as possible
Well-typed expressions do not go wrong
A theory of type polymorphism in programming. Journal of Computer and System Sciences, Robert Milner, 1978
abstraction mechanisms
Several Types of Types in Programming Languages Simone Martini, Universit`a di Bologna
A type is a concise, formal description of the behavior of a program fragment
Type systems for programming languages, Didier Rémy, INRIA Paris
THE HISTORY OF TYPES
1950 1960 1970 1980 1990 2000 2010 2020
Types are pretty much absent
1950 1960 1970 1980 1990 2000 2010 2020
1950 1960 1970 1980 1990 2000 2010 2020
Fortran
“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
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60
Types were born
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
the results of any program [..] should be comprehensible without knowing anything about the machine
Tony Hoare 1960
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60
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
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60 Algol W
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60 Algol W
the type system (including user-defined types) [should] guarantee that only the prescribed
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
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60 Algol W Pascal
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
The dawn of Object Orientation
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60 Algol W Pascal C++
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++
The age of the internet languages
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60 Algol W Pascal C++ Java
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
Dynamic Typed languages
1950 1960 1970 1980 1990 2000 2010 2020
Fortran Algol 60 Algol W Pascal C++ Java JS Ruby
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
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
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
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
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
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
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
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
KOTLIN AND TYPES
The type system is Kotlin’s super power
Nullability, 1st class functions, Nothing
If Kotlin uses the power of Types So can you!
PRIMITIVE OBSESSION
Usage of primitives instead of small objects for simple tasks
Usage of constants for coding information
Encoding the type [..] into the name [..] is brain damaged the compiler knows the types
Linus Torvalds about Hungarian notation
actually...
actually...
Don’t put in name what could be in type!
Express your Domain knowledge
Sample: At Soundcloud a URN uniquely identifies an entity: a user, track playlist…
soundcloud:tracks:1234
Anytime you find yourself writing code
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++
“type” properties/functions are a smell
Depending properties might be hint for hidden type
Factory methods are hint for different types
Booleans are a smell: 2 things in 1 place
Don’t let the creep in
COSTS
ALLOCATION COSTS
On JVM,
To avoid thread synchronization Eden is split into Thread Local Allocation Buffer
Allocating space in TLAB is often just a pointer bump
The compiler might even inline the whole class:
Allocation elimination with escape analysis
Urban performance legends, revisited, Brian Goetz 2005
Allocations on JVM are very cheap
A word on Android
ART is optimized for allocations
Allocations are OK Types are OK Yes, even enums
Modern Android development: Android Jetpack, Kotlin, and more (Google I/O 2018)
MEMORY COSTS
Memory
A plain Object takes 8 bytes
On Sun's JDK 1.3.1 for Windows
Memory
Any wrapper will be 8 bytes larger than wrapped primitive
On Sun's JDK 1.3.1 for Windows
But careful
Memory
An empty String takes 40 bytes
On Sun's JDK 1.3.1 for Windows
Memory
Boolean: header has 12 bytes + 1 for the boolean + 3 for the granularity
On HotSpot JVM 1.7.0_51
Memory
its "size" isn't something that's precisely defined
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Kotlin compiler translates to int
Kotlin compiler translates to java.lang.Integer
Kotlin compiler can use primitive int inside Age
Kotlin compiler translates to int
Kotlin keeps Age with int
GARBAGE COSTS
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
Short lived objects are cheap
SUM UP
(safety, machine checked documentation)
Sum up
Watch out for “hidden type” smells:
Sum up
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
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
call it my billion-dollar mistake
Tony Hoare about Algol W
Readability
Let the compiler do the work!
ALL THE THINGS!
Simone Martini, Universit`a di Bologna https://arxiv.org/pdf/1510.03726
Third International Conference, HaPoC 2015, Pisa, Italy
Didier Rémy, INRIA Paris, http://gallium.inria.fr/~remy/mpri/cours1.pdf
Journal of Computer and System Sciences, Robert Milner, 1978
https://homepages.inf.ed.ac.uk/wadler/papers/papers-we-love/milner-type-polymorphism.pdf
Laurence Tratt,Bournemouth University, 2009
https://tratt.net/laurie/research/pubs/html/tratt__dynamically_typed_languages/
Sources
History of types
http://blog.cleancoder.com/uncle-bob/2016/05/01/TypeWars.html
https://blog.ploeh.dk/2015/01/19/from-primitive-obsession-to-domain-modelling/
https://proandroiddev.com/phantom-types-in-kotlin-afd3f59fde10?
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html
Sources
Types
https://www.bettercodebytes.com/the-cost-of-object-creation-in-java-including-garbage-collection/
https://alidg.me/blog/2019/6/21/tlab-jvm
https://www.ibm.com/developerworks/java/library/j-jtp09275/index.html#artrelatedtopics
https://www.dynatrace.com/resources/ebooks/javabook/
https://www.javaworld.com/article/2077496/java-tip-130--do-you-know-your-data-size-.html
https://www.dynatrace.com/resources/ebooks/javabook/impact-of-garbage-collection-on-performance/
Sources
Costs
#KotlinConf
THANK YOU AND REMEMBER TO VOTE
Danny Preussler @ PreusslerBerlin