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

Kotlin in 2 Days

WeRockStar
February 02, 2019

Kotlin in 2 Days

WeRockStar

February 02, 2019
Tweet

More Decks by WeRockStar

Other Decks in Programming

Transcript

  1. K O T L I N F O R A

    N D R O I D B E I N G K O T L I N E R S
  2. @werockstar Kotlin is a statically typed programming language By Jetbrains

    Kotlin supports both object-oriented and functional programming styles
  3. @werockstar W h y y o u s h o

    u l d s t a r t u s i n g K o t l i n f o r A n d ro i d t o d a y
  4. @werockstar 1. Language and environment are mature (Production ready) 2.

    It makes Android development much easier 3. Seamlessly with IDE (Mature language with great IDE) 4. Modern language feature 5. From Startup to Fortune 500 already used in production 6. Kotlin is Multiplatform 7. Work very well with Java code base or Java libraries 8. Google fully support Source: https://antonioleiva.com/reasons-kotlin-android/
  5. @werockstar Null safety is a favorite feature for everyone who

    ever had a NullPointerException in Java - i.e. every Android developer . Source: https://pusher.com/state-of-kotlin#kotlinverse
  6. @werockstar Ty p e I n f e re n

    c e You don’t even have to declare the type
  7. @werockstar Va r i a b l e Immutable or

    not Prefer to immutable String java = "Java Language" Java , Kotlin val kotlin = "Kotlin Language" ⭐ By default, you should strive to declare all variables in Kotlin with the val keyword
  8. @werockstar C o n t ro l F l o

    w when (with argument, without argument) and combine multiple values in the same branch if-else, if-else-if
  9. @werockstar C o n c e p t s o

    f r a n g e s L o o p +
  10. @werockstar B a s i c F u n c

    t i o n Start with fun
  11. @werockstar O b j e c t k e y

    w o rd o b j e c t d e c l a r a t i o n ( s i n g l e t o n l i k e s t a t i c ) c o m p a n i o n o b j e c t s o b j e c t e x p re s s i o n ( a n a l o g o u s t o J a v a a n o n y m o u s c l a s s e s )
  12. @werockstar Java Kotlin ⭐ C o n t a i

    n i n g o n l y d a t a b u t n o c o d e a re o f t e n c a l l e d v a l u e o b j e c t s
  13. @werockstar V i s i b i l i t

    y m o d i f i e r Modifier Class Member Top-Level Declaration Public (Default) Visible Everywhere Visible Everywhere Internal Visible In a Module Visible in a Module Protected Visible in Subclasses -- Private Visible in a Class Visible in a File
  14. @werockstar I n h e r i t a n

    c e o r I m p l e m e n t K o t l i n u s e s t h e c o l o n ` : ` a f t e r t h e c l a s s n a m e t o re p l a c e b o t h t h e e x t e n d s a n d i m p l e m e n t s k e y w o rd s u s e d i n J a v a
  15. E F F E C T I V E J

    AVA B Y J O S H U A B L O C H ( A D D I S O N - W E S L E Y, 2 0 0 8 ) Design and document for inheritance or else prohibit it
  16. @werockstar I n h e r i t a n

    c e I n J a v a , O p e n b y d e f a u l t I n K o t l i n , F i n a l b y d e f a u l t
  17. @werockstar A c c e s s m o d

    i f i e r i n a c l a s s Modifier Corresponding Member Comments Final Can’t Be Overridden Used By Default For Class Members Open Can Be Overridden Should Be Specified Explicitly Abstract Must Be Overridden Can Be Used Only In Abstract Classes; Abstract Members Can’t Have An Implementation Override Overrides A Member In A Superclass Or Interface Overridden Member Is Open By Default, If Not Marked Final
  18. @werockstar O v e r r i d e @

    O v e r r i d e a n n o t a t i o n i n J a v a , I n K o t l i n u s e o v e r r i d e
  19. @werockstar O p e n C l a s s

    ( f i n a l b y d e f a u l t )
  20. @werockstar O p e n v s A b s

    t r a c t Open: Final by default Abstract: Open by default
  21. @werockstar I n t e r f a c e

    Similar interface in Java 8 (default methods implementation)
  22. @werockstar D e f a u l t i m

    p l e m e n t a t i o n i n I n t e r f a c e In Java 8, Mark with the default keyword In Kotlin, You just provide a method body
  23. @werockstar L a n g u a g e 1

    0 2 . 1 + Te s t i n g ✅
  24. @werockstar U n i t Te s t i n

    g i n K o t l i n
  25. @werockstar M o c k i n g d e

    p e n d e n c y i n K o t l i n
  26. @werockstar K o t l i n ❤ J a

    v a 100% compatibility
  27. @werockstar C o d e C o n v e

    n t i o n ? For example, PersonJava class
  28. @werockstar C o n v e r t J a

    v a t o K o t l i n w i t h I D E 1. Copy and past to automatic convert 2. Convert by shortcut
  29. @werockstar H o w t o d e a l

    w i t h P l a t f o r m Ty p e s Option 1: If we have control over the java code Option 2: If we don’t have control over the java code
  30. @werockstar I n i t i a l i z

    e r b l o c k s The primary constructor cannot contain any code
  31. @werockstar l a z y v s l a t

    e i n i t i n i t i a l i z a t i o n
  32. @werockstar N u l l S a f e t

    y a n d h o w t o d e a l w i t h 1. let 2. ? ⛑ 3. !! 4. ?: 5. as? ☢
  33. @werockstar S m a r t C a s t

    Combine a type check and a cast into one operation
  34. @werockstar C o l l e c t i o

    n s Immutable Mutable K o t l i n u s e s t h e s t a n d a rd J a v a c o l l e c t i o n c l a s s e s ✅ println(<collection>.javaClass)
  35. @werockstar C o l l e c t i o

    n o p e r a t i o n s ⮑ Filter, Map, FlatMap
  36. @werockstar D e c o n s t r u

    c t i n g v a l u e Unpack a single composite value into multiple variables
  37. @werockstar H i g h e r o rd e

    r f u n c t i o n ⮑ A higher order function is a function that accepts or return another function
  38. @werockstar E x t e n s i o n

    f u n c t i o n Ability to extend a class with new functionality without having to inherit from the class or use any type of design pattern such as Decorator Source: https://kotlinlang.org/docs/reference/extensions.html
  39. @werockstar I n l i n e f u n

    c t i o n I n l i n i n g i s a n a t t e m p t t o re m o v e ( o r re d u c e ) t h e r u n t i m e o v e r h e a d o f l a m b d a f u n c t i o n s b y m o v i n g ( o r i n l i n i n g ) t h e h i g h e r o rd e r f u n c t i o n a n d i t s l a m b d a a rg u m e n t b o d y t o t h e c a l l - s i t e S o u rc e : h t t p s : / / m e d i u m . c o m / t o m p e e / i d i o m a t i c - k o t l i n - i n l i n e - f u n c t i o n s - e 3 9 b 2 f 9 0 a 2 9 1
  40. @werockstar B y t e s C o d e

    V i e w e r ( I n l i n e )
  41. @werockstar A n d r o i d F r

    a m e w o r k U s e c a s e
  42. @werockstar R x J a v a ( R x

    K o t l i n ) i n A n d ro i d ? Work with UI thread
  43. @werockstar C o ro u t i n e s

    i n A n d ro i d ? Work with UI thread
  44. @werockstar S a y g o o d b y

    e f i n d V i e w B y I d W i t h A n d ro i d K o t l i n E x t e n s i o n ✋
  45. @werockstar C o m m u n i c a

    t e w i t h a c t i v i t y
  46. @werockstar C o m m u n i c a

    t e w i t h f r a g m e n t
  47. @werockstar P l a t f o r m t

    y p e s f ro m A n d ro i d S D K
  48. @werockstar A n d ro i d P ro j

    e c t S t r u c t u re ( S u g g e s t i o n )
  49. @werockstar U I Te s t i n g w

    i t h E s p re s s o
  50. @werockstar E s p re s s o I d

    l i n g re s o u rc e
  51. @werockstar I d l i n g re s o

    u rc e o n N e t w o r k i n g
  52. @werockstar M o c k We b S e r

    v e r ( S w a p w i t h m a n u a l )
  53. @werockstar M o c k We b S e r

    v e r ( S w a p w i t h D I ? )
  54. @werockstar R o b o t P a t t

    e r n ( S e p a r a t i n g t h e W h a t F ro m t h e H o w ) Source: https://speakerdeck.com/jakewharton/testing-robots-kotlin-night-may-2016 Page Object?
  55. @werockstar A r c h i t e c t

    u r e 1. MVP 2. MVVM 3. UI Testing 4. Robot Pattern
  56. @werockstar R e s o u r c e s

    1. Kotlin in Action (Book) 2. Kotlin for Android Developers (Book) 3. Kotlin official document 4. Android Developer Google official document