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

20210404 Kotlin Tech Talk発表『例外ではなくnullを返すKotlinらしいAPIデザイン』

20210404 Kotlin Tech Talk発表『例外ではなくnullを返すKotlinらしいAPIデザイン』

doyaaaaaken

April 05, 2021
Tweet

More Decks by doyaaaaaken

Other Decks in Programming

Transcript

  1. ྫ֎Ͱ͸ͳ͘nullΛฦ͢
    KotlinΒ͍͠APIσβΠϯ
    Kotlin Tech Talk
    2021/04/05 খࢁ݈ଠ(Kenta Koyama) @doyaaaaaaken

    View full-size slide

  2. ࣗݾ঺հ
    • ࢓ࣄ
    • גࣜձࣾsmartround ૑ۀϝϯόʔ / CTO
    • αʔόαΠυKotlinɾVueJSΛࠓ͸Α͘ॻ͍͍ͯ·͢
    • OSS׆ಈ
    • kotlin-csvͷ࡞ऀʢ⭐: ໿250ʣ
    • KtorɾExposedɾkotest౳΁ͷίϯτϦϏϡʔτ
    • झຯ
    • ອըɾήʔϜɾϓϩάϥϛϯάɾάϧϝɾςχεɾϘʔυήʔϜɾຑ੃ɾཱྀߦ…etc

    View full-size slide

  3. ͋Δ೔ײͨ͡՝୊
    • java.time.LocalDateͷparseϝιου͕࢖͍ͮΒ͍
    • LocalDate.parse(dateStr: String): LocalDate
    • ՝୊఺1: yyyy-MM-ddܗࣜͷจࣈྻ͔͠ड͚෇͚ͳ͍
    • ՝୊఺2: ྫ֎ͷ৔߹ʹඋ͑try-catchΛॻ͘ඞཁ͕͋Γ৑௕

    View full-size slide

  4. ՝୊΁ͷରॲ
    • ೔෇จࣈྻΛύʔε͢Δutilityؔ਺Λ࡞ͬͨ
    • fun parseLocalDate(dateStr: String): LocalDate?
    • ՝୊఺1: yyyy-MM-ddܗࣜͷจࣈྻ͔͠ड͚෇͚ͳ͍ 㱺 ෳ਺ͷ೔෇ϑΥʔϚοτΛύʔεՄೳʹɻղܾʂʂ
    • ՝୊఺2: ྫ֎ͷ৔߹ʹඋ͑try-catchΛॻ͘ඞཁ͕͋Γ৑௕ 㱺 ྫ֎Ͱ͸ͳ͘nullΛฦ͢Α͏ʹɻղܾʂʂ

    View full-size slide

  5. ίʔυϨϏϡʔ
    • ࣭໰಺༰
    • ࣭໰ɿʰ ྫ֎Ͱ͸ͳ͘nullΛฦ͢Α͏ʹͨ͠ͷ͸ͳͥʁʱ
    • ݩʑJavaඪ४ϥΠϒϥϦ͕ྫ֎Λฦ͢ͷ͸ཧ༝͕͋ΔͷͰ͸ͳ͍͔ʁ
    • ճ౴
    1. ྫ֎Λฦ͢ඞવੑ͕ͳ͍ͨΊ
    2. nullableʹ͢Δઃܭͷ΄͏͕try-catchΛॻ͔ͣʹࡁΈɺίʔυهड़ྔ͕গͳ͘ͳΔ
    3. Kotilnͷඪ४ϥΠϒϥϦͱ͔Λݟͯͯ΋ɺͪ͜Βͷ΄͏͕KotlinͬΆ͍͔Β

    View full-size slide

  6. ίʔυϨϏϡʔ
    • ࣭໰಺༰
    • ࣭໰ɿʰ ྫ֎Ͱ͸ͳ͘nullΛฦ͢Α͏ʹͨ͠ͷ͸ͳͥʁʱ
    • ݩʑJavaඪ४ϥΠϒϥϦ͕ྫ֎Λฦ͢ͷ͸ཧ༝͕͋ΔͷͰ͸ͳ͍͔ʁ
    • ճ౴
    1. ྫ֎Λฦ͢ඞવੑ͕ͳ͍ͨΊ
    2. nullableʹ͢Δઃܭͷ΄͏͕try-catchΛॻ͔ͣʹࡁΈɺίʔυهड़ྔ͕গͳ͘ͳΔ
    3. Kotilnͷඪ४ϥΠϒϥϦͱ͔Λݟͯͯ΋ɺͪ͜Βͷ΄͏͕KotlinͬΆ͍͔Β
    ओ؍తͳճ౴ͳͷͰɺ΋͏গ͖ͪ͠ΜͱݴޠԽͯ͠આ໌͍ͨ͠ͱײ͍ͯͨ͡

    View full-size slide

  7. ༗ࣝऀͷݟղ
    • JetbrainsͷKotlin։ൃϦʔυͷਓ͕ॻ͍ͨهࣄ͕ෲམͪ͢Δ಺༰ͩͬͨ
    • Kotlin and Exceptions
    • ཁ໿
    • KotlinΒ͍͠APIσβΠϯͱͯ͠͸…
    1. ϝιουͷฦΓ஋Λnull΍ઐ༻ܕʹ͢Δ͜ͱͰɺϝιουͷৼΔ෣͍Λදݱ͢΂͖
    2. ྫ֎͸༧ظͤ͵໰୊ൃੜ࣌ʹ౤͛ɺͦͷྫ֎͸ΞϓϦέʔγϣϯͷϧʔτϨϕϧͰHandling͢΂͖
    • JavaϥΠϒϥϦ͕ྫ֎Λฦ͢͜ͱ͕ଟ͍ͷ͸ݕࠪྫ֎͕͋Δ͔Β
    • Kotlinඪ४ϥΠϒϥϦ΋ OrNull ͕͍ͭͨ΋ͷ͕ଟ͍
    • ྫɿStringܕͷtoIntOrNull, ListܕͷgetOrNull, firstOrNull …

    View full-size slide

  8. ͦͷଞɿࢀߟهࣄ঺հ
    • ࠓճͷ಺༰ͷৄࡉ͸ҎԼهࣄʹ·ͱ·͍ͬͯ·͢
    • ʰKotlinΒ͍͠APIσβΠϯʹ͢Δʹ͸ྫ֎Ͱ͸ͳ͘nullΛฦ͢ʱ
    ※ ଞʹ΋Kotlinؔ࿈ͷTipsܥهࣄΛ৭ʑॻ͍ͯ·͢
    • ʰKotlinͷlazyʢ஗Ԇॲཧʣͷ࢓૊ΈΛௐ΂ͯࣗ෼Ͱ؆қʹ࣮૷ͯ͠Έͨʱ
    • ʰKotlinͷΫϥεҕৡͷ࢖͍ॴʱ
    • ʰKotlinʹ͓͚ΔFactoryϝιουͷ࣮૷ύλʔϯʱ
    • ʰKotlinͰωετߏ଄ͷDSLΛ࣮૷͢Δํ๏ʱ
    • ʰܕ҆શʹIDͷܕΛએݴ͢Δ࣮૷ύλʔϯʢKotlinͷ৔߹ʣʱ
    • ʰKotlinͷSequenceͱListͷڍಈͷҧ͍ʹ͍ͭͯʱ

    View full-size slide

  9. ձࣾએ఻
    • smartroundͰ͸ΤϯδχΞΛ
    ઈࢍืू͍ͯ͠·͢ʂʂ
    • ʰελʔτΞοϓɾ౤ࢿՈؒͷσʔλڞ༗ɾ؅ཧ
    ϓϥοτϑΥʔϜʱΛ։ൃ
    • ελʔτΞοϓ1500ࣾɾ౤ࢿՈ100͕ࣾొ࿥
    • αʔόαΠυKotlinɾVue.jsɾAWSΛར༻
    • ࣾһ਺10໊ఔͷϑΣʔζͰ͢
    ࿩͚ͩฉ͍͍ͨํ΋େ׻ܴͳͷͰWantedlyɾTwitter DM͔Β͓ؾܰʹ͝Ԡื͍ͩ͘͞ʂ
    ࢀߟ63-
    w αʔϏε-1ϖʔδ
    w ٕज़঺հࢿྉʰTNBSUSPVOEΛࢧ͑Δٕज़ʱ
    w XBOUFEMZ࠾༻ϖʔδ

    View full-size slide

  10. ͝ਗ਼ௌ͋Γ͕ͱ͏͍͟͝·ͨ͠
    @doyaaaaaken

    View full-size slide