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

100 lines or die

mattak
January 15, 2016

100 lines or die

KotlinAndroid勉強会 LT

mattak

January 15, 2016
Tweet

More Decks by mattak

Other Decks in Programming

Transcript

  1. (less than)
    100 lines or die.
    "OESPJEͰ,PUMJOษڧձ
    !NBUUBL

    View full-size slide

  2. とつぜんですが、

    View full-size slide

  3. Javaで
    こんなコードありませ
    んか?

    View full-size slide

  4. ͍ͭͷ·ʹ͔௕͘ͳ͍ͬͯΔߦ਺ɻɻɻ

    View full-size slide

  5. つらい…!!

    View full-size slide

  6. もし、
    10⾏のコードなら?

    View full-size slide

  7. 1໨ͰΘ͔Δʂ

    View full-size slide

  8. 100⾏のコードなら?

    View full-size slide

  9. ਺ेඵ͋Ε͹
    ཧղͰ͖Δʂ

    View full-size slide

  10. 1000⾏のコードなら?

    View full-size slide

  11. ਺෼ͳ͍ͱ
    ಡΊͳ͍ʂ

    View full-size slide

  12. 1万⾏のコードなら?

    View full-size slide

  13. もう⾟くて読めない…
    でも実在します.
    https://github.com/android/platform_frameworks_base/blob/master/core/java/android/widget/TextView.java#L10190

    View full-size slide

  14. Մಡੑ (ཧղʹ͔͔Δ࣌ؒ)
    ߦ਺
    ʢ஫: Πϝʔδਤʣ

    View full-size slide

  15. Τϯόά͢Δ֬཰
    ߦ਺
    ʢ஫: Πϝʔδਤʣ

    View full-size slide

  16. ۤ೰͢Δ࣌ؒ
    ߦ਺
    ʢ஫: Πϝʔδਤʣ

    View full-size slide

  17. ΞχϝΛݟΔ࣌ؒ
    ߦ਺
    ʢ஫: Πϝʔδਤʣ

    View full-size slide

  18. ⻑いコードを
    メンテナンスしたくな
    い!!

    View full-size slide

  19. というわけで

    View full-size slide

  20. 100 lines or die.

    View full-size slide

  21. 95%のソースコードを
    100⾏未満にする!

    View full-size slide

  22. というミッションで
    アプリを書いてみまし
    た。

    View full-size slide

  23. プロジェクト概要

    View full-size slide

  24. ⾔語:
    ΞʔΩςΫνϟ: 3૚ΞʔΩςΫνϟϕʔε
    σʔλόΠϯσΟϯά: RxJava
    ϥΠϒϥϦ: kotterknife, retrofit, okhttp, …

    View full-size slide

  25. 97%
    (ϥΠϒϥϦͳͲαϒϓϩδΣΫτͱςετআ͘)
    100ߦະຬ 529 files / ߹ܭ: 545 files

    View full-size slide

  26. 31#
    133#
    108#
    88#
    65#
    39#
    26#
    18#
    14#
    7# 5# 4# 4#
    1# 0# 0# 0# 1# 0# 0# 0# 0# 0# 0# 0# 0# 1#
    0#
    20#
    40#
    60#
    80#
    100#
    120#
    140#
    0# 10# 20# 30# 40# 50# 60# 70# 80# 90# 100# 110# 120# 130# 140# 150# 160# 170# 180# 190# 200# 210# 220# 230# 240# 250# 260#


    (16 files / 545 files)
    2.9%

    View full-size slide

  27. MainActivity΋ɺࠓͷͱ͜Ζ99ߦ

    View full-size slide

  28. ผϓϩδΣΫτͷΞϓϦ(Java)ͱൺֱ

    View full-size slide

  29. 0"
    5"
    10"
    15"
    20"
    25"
    0" 30" 60" 90"120"150"180"210"240"270"300"330"360"390"420"450"480"510"540"570"600"630"660"690"720"750"780"810"840"870"900"930"960"990"


    (39 files / 131 files)
    29.7%

    View full-size slide

  30. ※౰ࣾൺ 10ഒͷՄಡੑ޲্Λ࣮ݱʂ
    (͖ͯͱ͏Ͱ͢)

    View full-size slide

  31. Swift൛ͷΞϓϦͱൺֱ
    (΄΅ࣅͨΑ͏ͳΞʔΩςΫνϟͰͭͬͨ͘)

    View full-size slide

  32. 1"
    49"
    44"
    33"
    23"
    20"20"
    17"17"
    11"
    8" 8"
    10"
    2"
    3"
    4"
    3"
    4"
    5" 5"
    0"
    2"
    0"
    1"
    0"
    2"
    0"
    1" 1"
    2"
    1"
    0" 0"
    1"
    0"
    1" 1"
    0" 0" 0" 0" 0" 0" 0"
    1"
    0" 0" 0" 0" 0" 0" 0"
    1"
    0"
    10"
    20"
    30"
    40"
    50"
    60"
    0" 20" 40" 60" 80" 100" 120" 140" 160" 180" 200" 220" 240" 260" 280" 300" 320" 340" 360" 380" 400" 420" 440" 460" 480" 500" 520"


    (67 files / 307 files)
    21.8%

    View full-size slide

  33. Kotlin͍͢͝ʂ
    (࣮ࡍʹ͸ViewControllerͷ௕͕͞ओͳࠩ)

    View full-size slide

  34. 短く書く⼯夫
    (࣌ؒ͋Ε͹)

    View full-size slide

  35. ϓϩδΣΫτͷίʔυશମΛ
    ͖ͬ͢Γग़དྷͨཁҼΛ͍͔ͭ͘

    View full-size slide

  36. 3層アーキテクチャ
    ベース

    View full-size slide

  37. http://www.slideshare.net/mokemokechicken/iosandroidmodel
    ࢀߟ: iOS/AndroidΞϓϦΤϯδχΞ͕ཧղ͢΂͖ʮModelʯͷৼΔ෣͍

    View full-size slide

  38. "DUJWJUZ
    $VTUPN
    7JFX
    'SBHNFOU
    7JFX
    .PEFM
    .PEFM
    4FSWJDF
    Presentation Layer
    Business Layer
    20͘Β͍
    100͘Β͍
    100͘Β͍
    100͘Β͍
    10͘Β͍

    View full-size slide

  39. ΘΓͱ໾ׂ෼͚Ͱ͖͍ͯΔͷͰɺ
    ංେԽ͢ΔϘτϧωοΫfile΋গͳ͍

    View full-size slide

  40. CustomViewの活⽤

    View full-size slide

  41. Activity, Fragment, CustomView ͷ࢖͍෼͚
    - ϚονϣͳActivityʹ͞ΑͳΒ͢Δํ๏ -
    http://www.slideshare.net/yanzm/droid-kaigi2015-yanzm

    View full-size slide

  42. Activity << Fragment << CustomView
    ϑΝΠϧ਺͸

    View full-size slide

  43. খ͞ͳύʔπ(CustomView)Λ
    ୔ࢁͭ͘Γ࢖͍ճ͢ํ਑

    View full-size slide


  44. android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="vertical">


    android:id="@+id/text"

    android:layout_width="wrap_content"

    android:layout_height="wrap_content" />



    CustomViewͰxmlΛ૊Ή
    (ύʔπ͸খ͘͞খ෼͚)

    View full-size slide

  45. class CustomView : FrameLayout {

    val textView: TextView by bindView(R.id.text)


    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {}

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle) {}


    override fun onFinishInflate() {

    super.onFinishInflate()

    if (this.isInEditMode) { return }


    // model͔Βσʔλऔಘ

    val data = Observable.just("text")


    // observableͷ಺༰͕TextViewʹ൓ө͞ΕΔ

    TextViewModel(this.textView, data).subscribe()

    }

    }
    CustomView಺ͰσʔλόΠϯυ

    View full-size slide

  46. ⼩さな
    ViewModel構想

    View full-size slide

  47. ImageViewModel
    TextViewModel
    AudioButtonViewModel
    NextPageViewModel

    View full-size slide

  48. ViewModelͰ͍Ζ͍Ζ΍Γա͗ͳ͍
    1ͭͷViewModelͰ1ͭͷ͜ͱΛ΍Δ

    View full-size slide

  49. Extensionの活⽤

    View full-size slide

  50. // File: ScoreEntity.kt

    data class ScoreEntity(

    val param1: Int,

    val param2: Int

    )
    αʔόʔͱ௨৴͢Δࡍͷ
    Entity৘ใ͸ࣗಈੜ੒͞ΕΔ

    View full-size slide


  51. // File: ScoreEntityExtension.kt

    val ScoreEntity.score: Int

    get() = param1 * param2
    Entityʹ෇ਵ͢Δͪΐͬͱͨ͠ϩδοΫ͸
    Extensionͱͯ͠ผϑΝΠϧʹॻ͘

    View full-size slide

  52. ͪΐͬͱͨ͠ϩδοΫ͕
    γϣʔτΧοτͰ͖͖ͯͬ͢Γʂ

    View full-size slide

  53. まとめ
    KotlinͰίʔυΛ΋ͬͱ୹͘
    ϝϯς͠΍͘͢͠Α͏ʂ

    View full-size slide