Slide 1

Slide 1 text

Moshi Code Gen Brian Plummer - Nike

Slide 2

Slide 2 text

Moshi Code Gen A Love Story Brian Plummer - Nike

Slide 3

Slide 3 text

Reflection Free Parsing & Immutable Data Model Moshi Code Gen =

Slide 4

Slide 4 text

ELI5 Definitions

Slide 5

Slide 5 text

No content

Slide 6

Slide 6 text

Immutable

Slide 7

Slide 7 text

Immutable Not Mutable

Slide 8

Slide 8 text

Immutable Not Mutable Hard to change

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Reflection Free

Slide 11

Slide 11 text

Reflection Free Not Dynamic

Slide 12

Slide 12 text

Reflection Free Not Dynamic Compiled

Slide 13

Slide 13 text

Why does this matter?

Slide 14

Slide 14 text

Mike’s first month at the New York Times Application startup time was horrible. He started digging and found some issues.

Slide 15

Slide 15 text

One big culprit was parsing a configuration file that we needed for Application start up

Slide 16

Slide 16 text

One big culprit was parsing a configuration file that we needed for Application start up Roughly 700ms wasted!

Slide 17

Slide 17 text

What was the issue? Why did it take so long?

Slide 18

Slide 18 text

What was the issue? Why did it take so long? Reflection!

Slide 19

Slide 19 text

How shall we fix this?

Slide 20

Slide 20 text

We could write our own type adapters How shall we fix this?

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

Annotation Processing

Slide 24

Slide 24 text

Annotation Processing •AutoValue

Slide 25

Slide 25 text

Annotation Processing •AutoValue •Immutables.org

Slide 26

Slide 26 text

Unfun stuff taken care of for us

Slide 27

Slide 27 text

Unfun stuff taken care of for us •toString()

Slide 28

Slide 28 text

Unfun stuff taken care of for us •toString() •equals()

Slide 29

Slide 29 text

Unfun stuff taken care of for us •toString() •equals() •hashCode()

Slide 30

Slide 30 text

Copy methods for mutate

Slide 31

Slide 31 text

Immutable Data Model

Slide 32

Slide 32 text

Immutable Data Model •Thread safety

Slide 33

Slide 33 text

Immutable Data Model •Thread safety •Reduced Complexity

Slide 34

Slide 34 text

Immutable Data Model •Thread safety •Reduced Complexity •No more bugs caused by mutation

Slide 35

Slide 35 text

Reflection free type adapters

Slide 36

Slide 36 text

All was well!

Slide 37

Slide 37 text

Kotlin came and we have data classes

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

Data classes give us most of what we needed

Slide 40

Slide 40 text

Data classes give us most of what we needed

Slide 41

Slide 41 text

Data classes give us most of what we needed •Unfun methods

Slide 42

Slide 42 text

Data classes give us most of what we needed •Unfun methods •Copy methods

Slide 43

Slide 43 text

Data classes give us most of what we needed •Unfun methods •Copy methods •Immutability

Slide 44

Slide 44 text

Data classes give us most of what we needed •Unfun methods •Copy methods •Immutability •Reflection free parsing

Slide 45

Slide 45 text

Moshi to the rescue!

Slide 46

Slide 46 text

data class Book ( val title: String?, val authors: List, val isbn: String )

Slide 47

Slide 47 text

@JsonClass(generateAdapter = true) data class Book ( val title: String?, val authors: List, val isbn: String )

Slide 48

Slide 48 text

That’s it!

Slide 49

Slide 49 text

Really really it..besides adding it as a dependency in gradle of course! That’s it!

Slide 50

Slide 50 text

implementation "com.squareup.moshi:moshi:$moshi_version" kapt("com.squareup.moshi:moshi-kotlin-codegen:$moshi_version") Really really it..besides adding it as a dependency in gradle of course!

Slide 51

Slide 51 text

That sounds too good to be true.

Slide 52

Slide 52 text

How does it work? That sounds too good to be true.

Slide 53

Slide 53 text

Annotation processing How does it work?

Slide 54

Slide 54 text

Type adapter resolves at runtime so you don’t have to do anything else

Slide 55

Slide 55 text

Uses reflection once to lookup, caches it for use. Good across modules!

Slide 56

Slide 56 text

gson? It’s time

Slide 57

Slide 57 text

Moshi is the way forward

Slide 58

Slide 58 text

http://blog.nimbledroid.com/ 2016/02/23/slow-Android- reflection.html

Slide 59

Slide 59 text

https://medium.com/ @ZacSweers/exploring-moshis- kotlin-code-gen-dec09d72de5e

Slide 60

Slide 60 text

https:// open.blogs.nytimes.com/ author/mike-nakhimovich/