Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Kotlin: MAX PAYNE (BKUG, July 2017)

Kotlin: MAX PAYNE (BKUG, July 2017)

Kotlin gives us a lot of neat features and just general developer satisfaction, but little is known about its hidden costs and pain it causes. This talk is a almost a 2 year combination of small (and no so small) issues we encountered when developing a full-Kotlin application started from the scratch. Bugs, features, performance, testing — it’s all there with our answers, conclusions and advises.

Presented at Belarus Kotlin User Group meetup on July 19, 2017.

Artur Dryomov

July 19, 2017
Tweet

More Decks by Artur Dryomov

Other Decks in Programming

Transcript

  1. K otlin: MA X PA Y NE A rtur D

    ryomov, J uno L ab @arturdryomov
  2. J uno K otlin K otlin J uno W ell,

    only sometimes, so no grudge here
  3. J uno R ider F u ll‑K ot lin +

    R xJ av a fr om t he st art 2 y ears K ot lin 1 . 1 - E A P in pr odu ct ion for 6+ mont hs K ot lin 1 . 2 - E A P r ight now 99.9 % cr as h‑fr ee 2 w eek spr ints dev elop ers, QA
  4. J uno R ider c l o c m a

    i n / k o t l i n — 40 K c l o c t e s t / k o t l i n — 38 K c l o c a n d r o i d T e s t / k o t l i n — 12 K
  5. S pecification mumbo‑jumbo J av a AST — A bstr

    act S y nt ax T r ee O nly J av a PSI — P r ogr am S tru ctur e I nt er face O nly IJ UAST — U niv ers al AST J av a and K ot lin A ndr oid L int is migr at ing t o it!
  6. C heckstyle H ow about not mapp ing it t

    o AST and inst ead map it t o UAST (U niv ers al A bstr act S y nt ax T r ee)? T hat w ay als o adding supp ort for K ot lin, w hich is an official t hing for A ndr oid now, can be supp ort ed. C hecksty le w or ks only w it h AST as w e dep end on ANTLR and J av a gr ammar.
  7. C heckstyle and S tatic A nalysis P ut your

    IJ config into repository You can somewhat run IJ headless D etekt works with PSI , so take it for a spin
  8. ∞ paths L et’s bu ild s omet hing! v

    a l g a m e B u i l d e r = S t r i n g B u i l d e r ( ) g a m e B u i l d e r . a p p e n d ( " t i c " ) g a m e B u i l d e r . a p p e n d ( " t a c " ) g a m e B u i l d e r . a p p e n d ( " t o e " ) v a l g a m e = g a m e B u i l d e r . t o S t r i n g ( )
  9. ∞ paths A ls o, a l s o .

    v a l g a m e : S t r i n g = S t r i n g B u i l d e r ( ) . a l s o { i t . a p p e n d ( " t i c " ) i t . a p p e n d ( " t a c " ) i t . a p p e n d ( " t o e " ) } . t o S t r i n g ( )
  10. ∞ paths H mm, l e t it be? v

    a l g a m e : S t r i n g = S t r i n g B u i l d e r ( ) . l e t { i t . a p p e n d ( " t i c " ) i t . a p p e n d ( " t a c " ) i t . a p p e n d ( " t o e " ) i t . t o S t r i n g ( ) }
  11. ∞ paths a p p l y it! v a

    l g a m e : S t r i n g = S t r i n g B u i l d e r ( ) . a p p l y { a p p e n d ( " t i c " ) a p p e n d ( " t a c " ) a p p e n d ( " t o e " ) } . t o S t r i n g ( )
  12. ∞ paths r u n s eems nice. v a

    l g a m e : S t r i n g = S t r i n g B u i l d e r ( ) . r u n { a p p e n d ( " t i c " ) a p p e n d ( " t a c " ) a p p e n d ( " t o e " ) t o S t r i n g ( ) }
  13. ∞ paths H ow about w i t h ?

    v a l g a m e : S t r i n g = w i t h ( S t r i n g B u i l d e r ( ) ) { a p p e n d ( " t i c " ) a p p e n d ( " t a c " ) a p p e n d ( " t o e " ) t o S t r i n g ( ) }
  14. ∞ paths B ett er br ew t hat coffee

    for long PR s ? : vs. l e t vs. i f ( i t = = n u l l )
  15. ∞ paths C O D E _ S T Y

    L E . m d I t does not r eally matt er, just go w it h it
  16. D estructuring N o joke, actu ally us efu l.

    v a l ( f i r s t , s e c o n d ) = p a i r v a l ( f i r s t , s e c o n d , t h i r d ) = t r i p l e O b s e r v a b l e . f r o m C a l l a b l e { c r e a t e P a i r ( ) } . s u b s c r i b e { ( f i r s t , s e c o n d ) - > p r i n t l n ( " 1 s t i s $ f i r s t , 2 n d i s $ s e c o n d " ) }
  17. D estructuring S upp orts d a t a c

    l a s s and su ggest ed by IJ . d a t a c l a s s P e r s o n ( f i r s t N a m e : S t r i n g , l a s t N a m e : S t r i n g ) v a l ( f i r s t N a m e , l a s t N a m e ) = p e r s o n
  18. D estructuring S omebody sw aps fields C omp iler

    is happy! d a t a c l a s s P e r s o n ( l a s t N a m e : S t r i n g , f i r s t N a m e : S t r i n g ) v a l ( f i r s t N a m e , l a s t N a m e ) = p e r s o n
  19. n u l l safety? T his w or ks

    just fine! f u n c r e a t e O b s e r v a b l e ( ) : O b s e r v a b l e < I n t > { r e t u r n O b s e r v a b l e . j u s t ( n u l l ) } OH HAI R xJ av a 2 R epro du ced on RxJ av a 2.1.1.
  20. n u l l safety!! T he langu age r

    elies on @ N o n N u l l annot at ions. Your code is fu ll of u nicor ns J av a code is fu ll of n u l l .
  21. n u l l safety!! R ead docs and s

    our ces car efu lly. P r omot e @ N u l l a b i l i t y t o y our fav or it e libr ar ies. H op efu lly A ndr oid SDK w ill cat ch up aft er annou ncements.
  22. R xJ ava 2 S tr eams p anic on

    n u l l v alu es. K ot lin has t o be fr iends w it h J av a. S ide r oad — S w ift vs. K ot lin S w ift: ? and n i l just su gar for e n u m O p t i o n a l . K ot lin: ? is a langu age help er, no gu ar ant ees t hou gh.
  23. K optional https://github.com/gojuno/koptional v a l s o m e

    = S o m e ( v a l u e ) v a l n o n e = N o n e v a l o p t i o n a l = n u l l a b l e V a l u e . t o O p t i o n a l ( ) v a l n u l l a b l e = o . t o N u l l a b l e ( ) v a l v a l u e S t r e a m = o p t i o n a l S t r e a m . f i l t e r S o m e ( ) v a l s i g n a l S t r e a m = o p t i o n a l S t r e a m . f i l t e r N o n e ( ) U s e ? : and l e t — embr ace t he langu age.
  24. SAM C onversion Singl e A bstr act M etho

    d / / R x J a v a 1 O b s e r v a b l e . z i p ( a , b ) { a , b - > a t o b } / / R x J a v a 2 O b s e r v a b l e . z i p ( a , b , B i F u n c t i o n < I n t , I n t > { a , b - > a t o b } )
  25. SAM C onversion / / R x J a v

    a 1 p u b l i c s t a t i c < T 1 , T 2 , R > O b s e r v a b l e < R > z i p ( O b s e r v a b l e < T 1 > o 1 , O b s e r v a b l e < T 2 > o 2 , F u n c 2 < T 1 , T 2 , R > z i p p e r ) / / R x J a v a 2 p u b l i c s t a t i c < T 1 , T 2 , R > O b s e r v a b l e < R > z i p ( O b s e r v a b l e S o u r c e < T 1 > o 1 , O b s e r v a b l e S o u r c e < T 2 > o 2 , B i F u n c t i o n < T 1 , T 2 , R > z i p p e r )
  26. SAM C onversion / / R x J a v

    a 1 p u b l i c i n t e r f a c e F u n c 2 < T 1 , T 2 , R > e x t e n d s F u n c t i o n { R c a l l ( T 1 t 1 , T 2 t 2 ) ; } / / R x J a v a 2 p u b l i c i n t e r f a c e B i F u n c t i o n < T 1 , T 2 , R > { R a p p l y ( T 1 t 1 , T 2 t 2 ) ; }
  27. SAM C onversion / / R x J a v

    a 1 p u b l i c c l a s s O b s e r v a b l e < T > { / / R e d a c t e d } / / R x J a v a 2 p u b l i c i n t e r f a c e O b s e r v a b l e S o u r c e < T > { v o i d s u b s c r i b e ( O b s e r v e r < T > o b s e r v e r ) ; }
  28. G son and R eflection d a t a c

    l a s s M o d e l { @ S e r i a l i z e d N a m e ( " m o d e l _ f i e l d " ) v a l f i e l d : S t r i n g } m o d e l . f i e l d . t o S t r i n g ( ) / / C r a s h !
  29. G son and R eflection G son is happy to

    write n u l l to properties R eflection is a thing in the J ava world...
  30. G son and R eflection F ail fast d a

    t a c l a s s M o d e l { @ S e r i a l i z e d N a m e ( " m o d e l _ f i e l d " ) v a l f i e l d : S t r i n g } : V a l i d a t a b l e { o v e r r i d e f u n v a l i d a t e ( ) { i f ( f i e l d = = n u l l ) t h r o w P a r s e E x c e p t i o n ( ) } }
  31. G son and R eflection T ake a look at

    M oshi or J ackson U ses K otlin native reflection
  32. i n l i n e or n o i

    n l i n e N eat, r edu ces met hods cou nt. OH HAI A ndr oid F eels like ker nel dev elop ment. N ot emr aced at all. M ost ly for ces @ S u p p r e s s ( " n o t h i n g _ t o _ i n l i n e " ) .
  33. i n l i n e or n o i

    n l i n e https://youtrack.jetbrains.com/issue/KT ‑16769 R evisit compiler and IDE warning about inline functions
  34. i n l i n e or n o i

    n l i n e K now about it I ’m looking at you, p r i v a t e f u n
  35. c o m p a n i o n o

    b j e c t and c o n s t / / K o t l i n c o m p a n i o n o b j e c t { p r i v a t e v a l N U M B E R = 4 2 } / / J a v a p u b l i c s t a t i c f i n a l c l a s s C o m p a n i o n { p r i v a t e f i n a l i n t N U M B E R = 4 2 ; p r i v a t e f i n a l i n t g e t N U M B E R ( ) { r e t u r n N U M B E R ; } }
  36. c o m p a n i o n o

    b j e c t and c o n s t S o y eah, + met hod w it hout c o n s t . M et hods w ill not cou nt t hems elv es! I nv ocat ion costs ar e r eal
  37. c o m p a n i o n o

    b j e c t and c o n s t / / K o t l i n c o m p a n i o n o b j e c t { p r i v a t e c o n s t v a l N U M B E R = 4 2 } / / J a v a p u b l i c s t a t i c f i n a l c l a s s C o m p a n i o n { p r i v a t e s t a t i c f i n a l i n t N U M B E R = 4 2 ; / / W i l l a c t u a l l y i n l i n e t h e v a l u e . }
  38. c o m p a n i o n o

    b j e c t and c o n s t N ot adv ocat ed enou gh. O nly pr imit iv es and S t r i n g . S hou ldn’t comp iler do it its elf?
  39. c o m p a n i o n o

    b j e c t and c o n s t K now about it
  40. H idden C osts IJ → T ools → S

    how K otlin B ytecode L ots of fun, also — scary!
  41. B ugs f u n m u l t i

    p l y ( d o u b l e : D o u b l e ) = d o u b l e * d o u b l e U p dat e K ot lin 1.1.2 1.1.3. j a v a . l a n g . I l l e g a l S t a t e E x c e p t i o n o r g . j e t b r a i n s . k o t l i n . k a p t 3 . d i a g n o s t i c . K a p t E r r o r E r r o r w h i l e a n n o t a t i o n p r o c e s s i n g J av a keyw or ds ar e su ddenly banned. Ch ang elog? Wh at ch ang elog?
  42. A ssemble C oming fr om J av a? +

    bu ild st ep. U s e D agger? + bu ild st ep. k a p t incr ement al comp ilat ion didn’t w or k for a long t ime... H av e a change? R E B U I L D . J ust r emember y ou ar e doing it t imes p er day.
  43. A ssemble C omp ilat ion is CPU int ens

    iv e. L apt ops hav e mobile CPU s. PC s ar e cheap, easy t o up gr ade...
  44. M ainframer https://github.com/gojuno/mainframer https://github.com/elpassion/mainframer‑intellij‑plugin 1. L apt op → Sour

    ce Co de → B u ild M achine. 2. B u ild M achine → Buil d R esults → L apt op.
  45. M ainframer $ . / g r a d l

    e w c l e a n a s s e m b l e D e b u g - - n o - d a e m o n W it hout MF : 4 minut es 10 s econds. L apt op ov er heat B as ically cannot us e y our comput er W it h MF : 1 minut e 10 s econds.
  46. A ssemble D o all optimizations you can M inimu

    m SDK t o L ollip op. G r adle bu ild cache. G r adle A ndr oid P lu gin DE X bu ild cache. M ainfr amer
  47. S pek @ R u n W i t h

    ( J U n i t P l a t f o r m : : c l a s s ) c l a s s S p e c : S p e k ( { v a l s y s t e m b y m e m o i z e d { S y s t e m ( ) } c o n t e x t ( " b o o t " ) { b e f o r e E a c h T e s t { s y s t e m . b o o t ( ) } i t ( " r e p o r t s b o o t s t a t u s " ) { a s s e r t T h a t ( s y s t e m . s t a t u s ( ) ) . i s E q u a l T o ( B O O T E D ) } } } )
  48. S pek: versions 1 . 0 . 8 9 D

    e s c r i b e B o d y → D s l 1 . 1 . 0 - b e t a 3 D s l → S p e c B o d y D ocs ment ion 1 . 0 , 1 . 1 . 1 9 . D o not ex ist. M ov ed fr om JU nit 4 t o JU nit 5 at s ome p oint
  49. S pek: m e m o i z e d

    caching @ R u n W i t h ( J U n i t P l a t f o r m : : c l a s s ) c l a s s O o p s S p e c : S p e k ( { v a l s y s t e m b y m e m o i z e d { S y s t e m ( ) } l i s t O f ( 1 , 2 , 3 ) . f o r E a c h { i n d e x - > c o n t e x t ( " b o o t " ) { i t ( " b o o t s " ) { } } } } S ame s y s t e m object for each ru n
  50. S pek: m e m o i z e d

    caching @ R u n W i t h ( J U n i t P l a t f o r m : : c l a s s ) c l a s s O k S p e c : S p e k ( { v a l s y s t e m b y m e m o i z e d { S y s t e m ( ) } l i s t O f ( 1 , 2 , 3 ) . f o r E a c h { i n d e x - > c o n t e x t ( " b o o t $ i n d e x " ) { i t ( " b o o t s " ) { } } } } U niqu e s y s t e m object for each ru n
  51. S pek: m e m o i z e d

    caching https://github.com/J etB rains/spek/issues/210 M emoized and same test names
  52. S pek: m e m o i z e d

    before and after @ R u n W i t h ( J U n i t P l a t f o r m : : c l a s s ) c l a s s J u s t S p e c : S p e k ( { v a l s y s t e m b y m e m o i z e d { S y s t e m ( ) } b e f o r e E a c h T e s t { p r i n t l n ( " b e f o r e $ s y s t e m " ) } a f t e r E a c h T e s t { p r i n t l n ( " a f t e r $ s y s t e m " ) } } ) B efor e and aft er s y s t e m ar e differ ent.
  53. S pek: m e m o i z e d

    before and after https://github.com/J etB rains/spek/issues/213 M emoized value is not the same before and after test
  54. S pek: JU nit A ctu ally JU nit 5

    fr om now on. U s e @ R u n W i t h ( J U n i t P l a t f o r m : : c l a s s ) t o w or k w it h JU nit 4. JU nit 5 is not r eleas ed, s o IJ may not w or k. OH HAI An droi d Stu dio 3 B et a. f o n and f i t w er e r emov ed, us e S p ek IJ p lu gin. Do es not work with J Unit 4. T his is fu n
  55. S pek K ind of adv ert iz ed by

    J etB r ains, but... C ommits ar e tw o t imes a mont h on av er age B r eaks comp at ibility eas ily D ocs feel out dat ed all t he t ime. T her e ar e bu gs. R emember v anilla JU nit st ability? A ls o, hav e 100 K t ests? H av e t o maint ain it
  56. B onus round You are on your own P repare

    that YouT rack account...
  57. B onus round ... K ot lin is v ery

    r efer ence s emant ics, it’s a t hin lay er on t op of J av a, and s o it p erp etu at es t hr ou gh a lot of t he J av ais ms in its model. ... I f w e had done an analog t o t hat for O bject iv e‑C it w ou ld be like, ev eryt hing is an NSO bject and it’s objc_ms g S end ev eryw her e, just w it h p ar ent hes es inst ead of squ ar e br ackets. ... Chris L attn er, Swift
  58. B onus round J av a: O r acle, R

    ed H at, I nt el, IBM , E clips e, T w itt er, J etB r ains, et c. K ot lin: J etB r ains. T his is fine actu ally, but do not exp ect a mir acle. F or a s ingle comp any a damn gr eat langu age
  59. C onclusion T her e w ill be dr agons

    K ot lin w ill not make y ou a bett er dev elop er. I t w ill giv e y ou t ools t hou gh R emember about J av a u nder neat h. S w ift can br eak t hings, K ot lin cannot T he langu age is t op‑not ch and w ell‑des igned. B e car efu l!