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

複雑さに立ち向かうためのコードリーディング入門

 複雑さに立ち向かうためのコードリーディング入門

iOSDC Japan 2023登壇資料

[email protected]

September 03, 2023
Tweet

Other Decks in Programming

Transcript

  1. 2023/09/03 iOSDC J a p a n 2023 shiz ෳࡶ͞ʹཱͪ޲͔͏ͨΊͷ

    ίʔυϦʔσΟϯάೖ໳ 01 010 0101 0 01 010 0101 0
  2. ௐ΂Δ 📖 👀 💦 👀 💦 📃 📝 🖥 📄

    👨💻 ίʔυͷಡΈํʹয఺Λ౰ͯͨಈػ
  3. ΋ͬͱௐ΂Δ 📖 👀 💦 👀 💦 📃 📝 🖥 📄

    👨💻 👀 💦 👀 💦 📖 📃 🖥 👩💻 ίʔυͷಡΈํʹয఺Λ౰ͯͨಈػ
  4. ͜Μͳঢ়گΛଧഁ͍ͨ͠ 😔 ࣗ෼ͬͯμϝͩͳ͋…. 🪫 😵💫 Θ͔Βͳ͍ͳ͋ 📖 👀 💦 👀

    💦 📃 📝 🖥 📄 👨💻 😨 Ͱ΋͕࣌ؒ…. ·ͩΘ͔Βͳ͍…. 📖 👀 💦 👀 💦 📃 📝 🖥 📄 👨💻 👀 💦 👀 💦 🖥 📖 👩💻 📃 ίʔυͷಡΈํʹয఺Λ౰ͯͨಈػ
  5. 05 04 03 02 01 00 Կ͕ग़ྗ͞ΕΔʁ MOo MOo MOo

    moO MOo moO MOo moO moO MoO moO MoO moO moO MoO MOO MoO MoO MoO MoO MOO moO MoO MoO MoO MOO moO MoO MoO MoO MoO moO MOo MOo moO MoO MoO MoO mOo mOo mOo MOo moo mOo MOo moo mOo MoO MoO MoO moo moO moO moO Moo moO MOo MOo moO MOo Moo moO Moo Moo MoO moO MoO MoO MoO MoO moO MoO MoO MoO Moo MoO moO MOo MOo moO MOO moO MOo Moo mOo mOo moo 00: ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  6. ԿΛ͍ͯ͠Δʁ MOo MOo MOo moO MOo moO MOo moO moO

    MoO moO MoO moO moO MoO MOO MoO MoO MoO MoO MOO moO MoO MoO MoO MOO moO MoO MoO MoO MoO moO MOo MOo moO MoO MoO MoO mOo mOo mOo MOo moo mOo MOo moo mOo MoO MoO MoO moo moO moO moO Moo moO MOo MOo moO MOo Moo moO Moo Moo MoO moO MoO MoO MoO MoO moO MoO MoO MoO Moo MoO moO MOo MOo moO MOO moO MOo Moo mOo mOo moo https://tio.run/##fY7BDcAgDANXYSHkIXhX fj J@KggpSYN4nGzFwrixi1SwGA@RdPqfmq9Y6oHLbzfr8X/7bNwWmh986Np7tYu5f77hlyuXbQGeNwOx23aTIi8 Hello World! ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  7. ԿΛ͍ͯ͠Δʁ MOo MOo MOo moO MOo moO MOo moO moO

    MoO moO MoO moO moO MoO MOO MoO MoO MoO MoO MOO moO MoO MoO MoO MOO moO MoO MoO MoO MoO moO MOo MOo moO MoO MoO MoO mOo mOo mOo MOo moo mOo MOo moo mOo MoO MoO MoO moo moO moO moO Moo moO MOo MOo moO MOo Moo moO Moo Moo MoO moO MoO MoO MoO MoO moO MoO MoO MoO Moo MoO moO MOo MOo moO MOO moO MOo Moo mOo mOo moo https://tio.run/##fY7BDcAgDANXYSHkIXhX fj J@KggpSYN4nGzFwrixi1SwGA@RdPqfmq9Y6oHLbzfr8X/7bNwWmh986Np7tYu5f77hlyuXbQGeNwOx23aTIi8 Hello World! ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  8. func doItNow() { let result = doSomething1() if result {

    doSomething2() } else { doSomething3() } } ԿΛ͍ͯ͠Δʁ 05 04 03 02 01 00 00: ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  9. func doItNow() { let result = doSomething1() if result {

    doSomething2() } else { doSomething3() } } ԿΛ͍ͯ͠Δʁ ͜Ε͚ͩͩͱΘ͔Βͳ͍ ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  10. Կ͕ग़ྗ͞ΕΔʁ let asciiRange = 32...126 var asciiValues: [UInt8] = [105,

    79, 83, 68, 67, 32, 74, 97, 112, 97, 110, 33] var result = "" while !asciiValues.isEmpty { let first = asciiValues.first! for value in asciiRange { if value == first { result.append(Character(UnicodeScalar(value)!)) asciiValues.removeFirst() } } } print(result) 05 04 03 02 01 00 00: ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  11. let asciiRange = 32...126 var asciiValues: [UInt8] = [105, 79,

    83, 68, 67, 32, 74, 97, 112, 97, 110, 33] var result = "" while !asciiValues.isEmpty { let first = asciiValues.first! for value in asciiRange { if value == first { result.append(Character(UnicodeScalar(value)!)) asciiValues.removeFirst() } } } print(result) ݁Ռ͸ʁ iOSDC Japan! ͳͥෳࡶʹײ͡Δͷ͔ʁ > ΫΠζ
  12. ৘ใΛҰ࣌తʹอ࣋͠ ͦͷ৘ใΛૢ࡞͢Δ ϫʔΩϯάϝϞϦ ྫ: ಄ͷதͰϧʔϓॲཧΛ࣮ߦ͢Δ for i in 0...9 {

    print(i * 2) } i = 0 → “0” i = 1 → “2” … ͳͥෳࡶʹײ͡Δͷ͔ʁ > ೝ஌ϓϩηε > ϫʔΩϯάϝϞϦ
  13. ୹ظهԱ ϫʔΩϯά ϝϞϦ ௕ظهԱ σʔλ͸͋Γ·ͤΜ σʔλ͍ͩ͘͞ ❷ ❶ ❸ ༰ྔͷݶք

    Ұͭͷ໰୊͕ଞͷ໰୊Λى͜͢ ֮͑ͱ͔ͳͯ͘͸… ͳͥෳࡶʹײ͡Δͷ͔ʁ > ೝ஌ϓϩηεͷ࿈ܞ
  14. νϟϯΫԽ ෳ਺ͷ৘ใΛҰͭͷνϟϯΫ ͱͯ͠άϧʔϐϯά͢Δ͜ͱ i O S D C J iOSDC

    Japan a p a n ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > νϟϯΫԽ
  15. func slowSort(_ i: Int, _ j: Int, _ numberList: inout

    [Int]) { guard i < j else { return } let m = (i+j)/2 slowSort(i, m, &numberList) slowSort(m+1, j, &numberList) if numberList[j] < numberList[m] { let temp = numberList[j] numberList[j] = numberList[m] numberList[m] = temp } slowSort(i, j-1, &numberList) } 30 29 28 27 26 25 24 23 22 21 20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 00: ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > νϟϯΫԽ ޮՌԻɿϙέοτα΢ϯυ – https://pocket-se.info/
  16. 30 29 28 27 26 25 24 23 22 21

    20 19 18 17 16 15 14 13 12 11 10 09 08 07 06 05 04 03 02 01 00 00: ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > νϟϯΫԽ ޮՌԻɿϙέοτα΢ϯυ – https://pocket-se.info/
  17. ৼΓฦΓ • ؆୯ʹਖ਼͘͠࠶ݱͰ͖ͨॴ͸Ͳ͜ʁ • ࠶ݱͰ͸ͳͯ͘΋ਖ਼͍͠ॴ͸͋ͬͨʁ • ·ͬͨ͘࠶ݱͰ͖ͳ͔ͬͨॴ͸͋ͬͨʁ • ͦͷݪҼ͸Կʁ •

    ݟ׳Εͳ͍ϓϩάϥϛϯά֓೦͸͋ͬͨʁ • ݟ׳Εͳ͍ϏδωεྖҬͷ֓೦͸͋ͬͨʁ ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > νϟϯΫԽ
  18. ๨٫ۂઢ ֮ ͑ ͯ ͍ Δ ׂ ߹ (%) ܦա࣌ؒ

    ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > ௕ظهԱ΁ͷΠϯϓοτ > ௕ظهԱΛ׆༻͢Δίπ
  19. 1ճ 2ճ 3ճ 4ճ ๨٫ۂઢ ֮ ͑ ͯ ͍ Δ

    ׂ ߹ (%) ܦա࣌ؒ ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > ௕ظهԱ΁ͷΠϯϓοτ > ௕ظهԱΛ׆༻͢Δίπ
  20. ୹ظهԱˠ௕ظهԱ ײ֮هԱ ୹ظهԱ ❶ ֎෦͔ΒೖΔ৘ใͷස౓͕૿͑Δ ❷୹ظهԱʹૹΒΕΔ৘ใͷස౓͕૿͑Δ 👀 👂 ✍ ❸

    ௕ظهԱ΁৘ใ͕܁Γฦ͠ೖΔ ௕ظهԱ ϫʔΩϯάϝϞϦ ❸ ❶ ❷ ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > ௕ظهԱ΁ͷΠϯϓοτ > ௕ظهԱΛ׆༻͢Δίπ
  21. •ϦετԽɺݟग़͠෇͚ ੵۃతͳνϟϯΫԽ Swift Concurrency - async/await - Structured Concurrency -

    actor return registeredIDs .insert(id) .inserted IDొ࿥(ॏෳ͸NG) ෳࡶ͞΁ͷରࡦ > ୹ظهԱ > ௕ظهԱ΁ͷΠϯϓοτ > ௕ظهԱΛ׆༻͢Δίπ
  22. •ม਺ͷมԽΛ௥͍ͬͯ͘ ঢ়ଶભҠਤ func fibonacci(_ x: Int) -> Int { if

    x < 2 { return x } var first: Int = 0 var second: Int = 1 for _ in 0...(x - 2) { let temp = second second = first + second first = temp } return second } x f irst second temp init 4 0 1 loop1 2 1 1 loop2 1 3 1 loop3 3 4 3 ෳࡶ͞΁ͷରࡦ > ϫʔΩϯάϝϞϦ > ໰୊ࣗମ͕೉͍͠৔߹ > ৘ใΛ಄ͷத͔Β֎ʹग़͢
  23. • ಈ࡞Λมߋͤͣɺࣗ෼͕ಡΈ΍͍͢ίʔυʹॻ͖׵͑Δ͜ͱ ྫ: ϝιου΍ม਺ͷΠϯϥΠϯԽ map → for in ϧʔϓ •

    ཧղͷͨΊ͚ͩͳͷͰผʹಈ͔ͳͯ͘΋Α͍ ೝ஌తϦϑΝΫλΫϦϯά ෳࡶ͞΁ͷରࡦ > ௕ظهԱ > ޡೝࣝ໰୊
  24. • ಈ࡞Λมߋͤͣɺࣗ෼͕ಡΈ΍͍͢ίʔυʹॻ͖׵͑Δ͜ͱ ྫ: ϝιου΍ม਺ͷΠϯϥΠϯԽ map → for in ϧʔϓ •

    ཧղͷͨΊ͚ͩͳͷͰผʹಈ͔ͳͯ͘΋Α͍ ೝ஌తϦϑΝΫλΫϦϯά ✖︎ ෳࡶ͞΁ͷରࡦ > ௕ظهԱ > ޡೝࣝ໰୊
  25. ޡೝ͕ࣝىͦ͜͠͏ͳ໰୊ func increment( _ value: Int ) -> Int {

    value+1 } func increment( _ value: Double ) -> Double { value+0.1 } ෳࡶ͞΁ͷରࡦ > ௕ظهԱ > ޡೝࣝ໰୊
  26. ޡೝ͕ࣝىͦ͜͠͏ͳ໰୊ func increment( _ value: Int ) -> Int {

    value+1 } ෳࡶ͞΁ͷରࡦ > ௕ظهԱ > ޡೝࣝ໰୊
  27. ޡೝ͕ࣝىͦ͜͠͏ͳ໰୊ var currentValue = 1.0 if 2 != increment(currentValue) {

    assertionFailure(“ͦΜͳόΧͳ…”) } func increment( _ value: Int ) -> Int { value+1 } func increment( _ value: Double ) -> Double { value+0.1 } 1 ͔ͳʁ ෳࡶ͞΁ͷରࡦ > ௕ظهԱ > ޡೝࣝ໰୊
  28. func max(in numbers: [Int]) -> Int? { guard let firstNumber

    = numbers.first else { return nil } var maxNumber = firstNumber for number in numbers { if number >= maxNumber { maxNumber = number } } return maxNumber } ෳ਺ͷཁૉΛ֨ೲ ෳ਺ͷཁૉΛ֨ೲ͢Δ΋ͷ Fundamentals of Programming: The Role of Variables ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > ม਺ͷ໾ׂΛߟ͑Δ
  29. func max(in numbers: [Int]) -> Int? { guard let firstNumber

    = numbers.first else { return nil } var maxNumber = firstNumber for number in numbers { if number >= maxNumber { maxNumber = number } } return maxNumber } ࠷΋ॏཁͳ஋ ࠷΋ॏཁͳ஋Λอ࣋͢Δ΋ͷ ෳ਺ͷཁૉΛ֨ೲ͢Δ΋ͷ Fundamentals of Programming: The Role of Variables ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > ม਺ͷ໾ׂΛߟ͑Δ
  30. func max(in numbers: [Int]) -> Int? { guard let firstNumber

    = numbers.first else { return nil } var maxNumber = firstNumber for number in numbers { if number >= maxNumber { maxNumber = number } } return maxNumber } ௚ۙͷ஋Λอ࣋͢Δ ࠷΋ॏཁͳ஋Λอ࣋͢Δ΋ͷ ࠷΋௚ۙͷ஋Λอ࣋͢Δ΋ͷ ෳ਺ͷཁૉΛ֨ೲ͢Δ΋ͷ Fundamentals of Programming: The Role of Variables ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > ม਺ͷ໾ׂΛߟ͑Δ
  31. func max(in numbers: [Int]) -> Int? { guard let firstNumber

    = numbers.first else { return nil } var maxNumber = firstNumber for number in numbers { if number >= maxNumber { maxNumber = number } } return maxNumber } ௚ۙͷ஋Λอ࣋͢Δ ࠷΋ॏཁͳ஋Λอ࣋͢Δ΋ͷ ࠷΋௚ۙͷ஋Λอ࣋͢Δ΋ͷ ෳ਺ͷཁૉΛ֨ೲ͢Δ΋ͷ Fundamentals of Programming: The Role of Variables ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > ม਺ͷ໾ׂΛߟ͑Δ
  32. func max(in numbers: [Int]) -> Int? { guard let firstNumber

    = numbers.first else { return nil } var maxNumber = firstNumber for number in numbers { if number >= maxNumber { maxNumber = number } } return maxNumber } ͭ·Γ ࠷΋ॏཁͳ஋Λอ࣋͢Δ΋ͷ ࠷΋௚ۙͷ஋Λอ࣋͢Δ΋ͷ ෳ਺ͷཁૉΛ֨ೲ͢Δ΋ͷ ഑ྻͷ࠷େ஋Λ ฦ͍ͯ͠Δͷ͔ͳʁ Fundamentals of Programming: The Role of Variables ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > ม਺ͷ໾ׂΛߟ͑Δ
  33. func max(in numbers: [Int]) -> Int? { guard let firstNumber

    = numbers.first else { return nil } var maxNumber = firstNumber for number in numbers { if number >= maxNumber { maxNumber = number } } return maxNumber } ม਺ͷ໾ׂΛߟ͑Δ ࠷΋ॏཁͳ஋Λอ࣋͢Δ΋ͷ ࠷΋௚ۙͷ஋Λอ࣋͢Δ΋ͷ ෳ਺ͷཁૉΛ֨ೲ͢Δ΋ͷ Fundamentals of Programming: The Role of Variables ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > ม਺ͷ໾ׂΛߟ͑Δ
  34. จষಡղͷࣣͭͷश׳ https://www.researchgate.net/publication/27474121_The_Seven_Habits_of_Highly_E ff ective_Readers จষಡղͷࣣͭͷश׳ ίʔυΛಡΉςΫχοΫ ༧උ஌ࣝͷ׆ੑԽ શମΛͬ͟ͱಡΉ த৺ͱͳΔ෺ޠͷ୳ࡧ ॏཁͩͱࢥ͏ϙΠϯτΛϦετΞοϓ͢Δ

    ਪ࿦ ࡞ΒΕͨഎܠ΍࡞ऀͷҙਤΛߟ͑Δ ࢹ֮Խ ঢ়ଶભҠਤΛඳ͍ͨΓɺґଘؔ܎ΛઢͰ݁Ϳ طଘͷ஌ࣝͱͷϦϯΫ աڈʹࣅͨΑ͏ͳίʔυΛݟͨ͜ͱͳ͍͔ࢥ͍ग़͢ ࣗ໰ࣗ౴ ٙ໰ʹࢥͬͨ͜ͱΛϦετΞοϓͯ͠ճ౴͢Δ ಺༰ͷཁ໿ ιʔευΩϡϝϯτΛॻ͘ ෳࡶ͞΁ͷରࡦ > ίʔυͷҙਤ͕͔ͭΊͳ͍ > ରࡦ > จষಡղज़ΛԠ༻͢Δ
  35. ࢀߟࢿྉ • ϓϩάϥϚʔ೴ ~ ༏ΕͨϓϩάϥϚʔʹͳΔͨΊͷೝ஌Պֶʹجͮ͘Ξϓϩʔν • Hacking Your Head :

    Managing Information Overload (extended) • Microservices: Software that Fits in Your Head • Demine your codebase in 30min with Exploratory Refactoring • The Cognitive Load Theory in Software Development • The Seven Habits of Highly E ff ective Readers • Measuring Program Comprehension: A Large-Scale Field Study with Professionals • νϟϯΫԽνΣοΫͷιʔεݩ • ϫʔΩϯάϝϞϦͱνϟϯΫʹؔ͢Δઆ໌ • ৘ใࣾձͷ৽ͨͳSOSɻ৘ใաଟγϯυϩʔϜͱ͸ ࢀߟࢿྉ