the power of types danny preussler
play

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


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

  2. Do you enjoy debugging?

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

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

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

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

  7. No language can prevent us from writing bugs!

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

  9. TYPES

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

  11. WHAT IS A TYPE?

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

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

  14. THE HISTORY OF TYPES

  15. 1950 1960 1970 1980 1990 2000 2010 2020

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

  17. 1950 1960 1970 1980 1990 2000 2010 2020 Fortran

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

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

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

  21. 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 or its storage layout. Tony Hoare 1960

  22. 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

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

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

  25. 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 operations on a type could operate on its values James H. Morris. Types are not sets, New York, NY, USA, 1973. ACM

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

  27. 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

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

  29. 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

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

  31. 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/

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

  33. 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

  34. 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

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

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

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

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

  39. 1950 1960 1970 1980 1990 2000 2010 2020 Fortran Algol 60 Algol W Pascal C++ Java JS Ruby Java Generics Scala Typescript 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

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

  41. KOTLIN AND TYPES

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

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

  44. PRIMITIVE OBSESSION

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

  46. Usage of constants for coding information

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

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

  49. actually...

  50. actually...

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

  52. Express your Domain knowledge

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

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

  55. “type” properties/functions are a smell

  56. Depending properties might be hint for hidden type

  57. Factory methods are hint for different types

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

  59. Don’t let the creep in

  60. COSTS

  61. ALLOCATION COSTS

  62. On JVM, objects allocated in Eden space

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

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

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

  66. Allocations on JVM are very cheap

  67. A word on Android ART is optimized for allocations

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

  69. MEMORY COSTS

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

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

  72. But careful

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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend