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

Self-Documenting のススメ

Elvis Shi
September 20, 2018

Self-Documenting のススメ

Elvis Shi

September 20, 2018
Tweet

More Decks by Elvis Shi

Other Decks in Programming

Transcript

  1. } var employedBy = "YUMEMI Inc." var job = "iOS

    Developer" var favoriteLanguage = "Swift" var twitter = "@lovee" var qiita = "lovee" var github = "el-hoshino" var additionalInfo = """ iOSDC 2018 CfP શ෦མͪͨਏ͍ """ class Me: Developable, Talkable {
  2. let s = (0 ..< 50) .map({ _ in Int.random(in:

    0 ... 100) }) .filter({ $0 >= 60 }) .reduce(0, +)
  3. let s = (0 ..< 50) .map({ _ in Int.random(in:

    0 ... 100) }) .filter({ $0 >= 60 }) .reduce(0, +)
  4. private extension Int { var isPassed: Bool { return self

    >= 60 } } let numberOfStudents = 50 let studentsScoreList = (0 ..< numberOfStudents) .map({ _ -> Int in let score = Int.random(in: 0 ... 100) return score }) let passedStudentsScoreList = studentsScoreList .filter({ $0.isPassed }) let totalScoreOfAllPassedStudents = passedStudentsScoreList .reduce(0, +) ͳΔ΄Ͳɺςετʹ߹֨ͨ͠ੜెͷ
 ఺਺ͷ߹ܭͶʂ
  5. ͳͥ4FMG%PDVNFOUJOH$PEFʁ w ಉ͜͡ͱΛʮίʔυʯͱʮυΩϡϝϯτʯ྆ํͰॻ͘ ͱ͍͏ೋ౓ख͕ؒݮΔɻ w w w w w w

    w w ίʔυϨϏϡʔ͕ΑΓ͠΍͘͢ͳΔɻ w ίϛολʔͷҙਤ͕ಡΈऔΓ΍͘͢ͳΔ w w w w w w w w w w w อक͢Δ࣌ʹ໰୊ՕॴΛಛఆ͠΍͘͢ͳΔ w w w w w w
  6. ՄಡੑʹؾΛ͚ͭΔʢʣ class Player { // ... var allowsMagic: Bool {

    return // ຐ๏͕࢖͑Δ͔Ͳ͏͔ͷ൑ఆ } } ຐ๏Λ ڐՄʜʁ
  7. ՄಡੑʹؾΛ͚ͭΔʢʣ class Player { // ... var canUseMagic: Bool {

    return // ຐ๏͕࢖͑Δ͔Ͳ͏͔ͷ൑ఆ } } ຐ๏͕ ࢖͑Δ͔Ͳ͏͔͔ʂ
  8. ՄಡੑʹؾΛ͚ͭΔʢʣ func addSubview(_ subview: UIView, _ baseView: UIView) { baseView.addSubview(subview)

    } addSubview(avatarView, contentView) Ͳ͕ͬͪͲͬͪʹ ௥Ճ͞ΕΔͷʜʁ
  9. ՄಡੑʹؾΛ͚ͭΔʢʣ func addSubview(subview: UIView, baseView: UIView) { baseView.addSubview(subview) } addSubview(subview:

    avatarView, baseView: contentView) ଟ෼avatarView͕ࢠϏϡʔͰ contentView͕਌Ϗϡʔʁ
  10. ՄಡੑʹؾΛ͚ͭΔʢʣ func addSubview(_ subview: UIView, onto baseView: UIView, ifNeeded) {

    if !baseView.subviews.contains(where: { $0 === subview }) { baseView.addSubview(subview) } } addSubview(avatarView, onto: contentView, ifNeeded) ಡΈ΍͍͕͢
 จ๏ΤϥʔͰ͋Δ
  11. ՄಡੑʹؾΛ͚ͭΔʢʣ func addSubview(_ subview: UIView, onto baseView: UIView, ifNeeded) {

    if !baseView.subviews.contains(where: { $0 === subview }) { baseView.addSubview(subview) } } addSubview(avatarView, onto: contentView, ifNeeded)
  12. ՄಡੑʹؾΛ͚ͭΔʢʣ enum AddSubviewPostLabel { case ifNeeded } func addSubview(_ subview:

    UIView, onto baseView: UIView, _ label: AddSubviewPostLabel) { if !baseView.subviews.contains(where: { $0 === subview }) { baseView.addSubview(subview) } } addSubview(avatarView, onto: contentView, .ifNeeded) ߈ུ"ɿޙஔϥϕϧ༻ͷenumҾ਺Λ׶͑ͯ࡞Δ ɿར༻෦ΛಡΉͱ͔ͳΓಡΈ΍͍͢ ɿenumΛΘ͟Θ͟ఆٛ͠ͳ͍ͱ͍͚ͳ͍ɺ໘౗ष͍ɺ໊લিಥ͠΍͍͢
  13. ՄಡੑʹؾΛ͚ͭΔʢʣ func addSubview(_ subview: UIView, onto baseView: UIView, ifNeeded: ())

    { if !baseView.subviews.contains(where: { $0 === subview }) { baseView.addSubview(subview) } } addSubview(avatarView, onto: contentView, ifNeeded: ()) ߈ུ#ɿޙஔϥϕϧ༻ͷμϛʔҾ਺Λ࡞Δ ɿԿ΋௥ՃͰఆٛ͢Δඞཁ͕ͳ͍ɺָ ɿར༻෦ΛಡΉ࣌ඍົʹͳͥ͜ͷҾ਺౉ͯ͠Δͷ͔Λߟ͑ͯ͠·͍ͦ͏
  14. ՄಡੑʹؾΛ͚ͭΔʢʣ class SomeSequence { private var numbers: [Int] = [3,

    2, 1] private var isSorted: Bool = false // ... func getSorted() -> [Int] { self.numbers = self.numbers.sorted(by: <) self.isSorted = true return self.numbers } } let sequence = SomeSequence() // Կ͔ॲཧ... let sorted = sequence.getSorted() ฒͼସ͑ΒΕͨ഑ྻΛऔΖ͏ͱͨ͠
 ͚ͩͳͷʹsequenceͷঢ়ଶ͕มΘͬͨ
  15. ՄಡੑʹؾΛ͚ͭΔʢʣ class SomeSequence { private var numbers: [Int] = [3,

    2, 1] private var isSorted: Bool = false // ... @discardableResult func sort() -> [Int] { self.numbers = self.numbers.sorted(by: <) self.isSorted = true return self.numbers } } let sequence = SomeSequence() // Կ͔ॲཧ... let sorted = sequence.sort() ͜ͷϝιου͸໭Γ஋͕͋Δ͕
 ෭࡞༻΋͋Δϝιουͩʂ
  16. ࡉ͔͘ந৅Խ class SomeScreen { func presentWebPage() { // ΞυϨεόʔΛ... //

    ໭ΔϘλϯΛ... // ... // จࣈྻΛ URL ʹ... // URL ͔ΒϦΫΤετΛ... // ... // จࣈίʔυΛ... // ಺༰ͷϨΠΞ΢τΛ... // ... // } }
  17. ࡉ͔͘ந৅Խ class SomeScreen { func presentWebPage() { // ॳظ WebPage

    ը໘Λ࡞Δ // ࡞ͬͨ WebPage ը໘ʹભҠ͢Δ // ભҠ͕ऴΘͬͨΒ WebPage ը໘ͰಡΈࠐΉ } }
  18. ࡉ͔͘ந৅Խ class SomeScreen { func presentWebPage() { let screen =

    WebPageScreen() self.present(screen, completion: { screen.loadAddress("user_defined") }) } }
  19. ࡉ͔͘ந৅Խ class SomeScreen { // presentWebPage private func present(_ screen:

    WebPageScreen, completion: () -> Void) { // ભҠॲཧʹઐ೦ } } class WebPageScreen { // ը໘ߏ੒ʹઐ೦ func loadAddress(_ address: String) { // ಡΈࠐΈॲཧʹઐ೦ } }
  20. ໰୊Λࡉ͔͘ந৅Խ͢Δ w ͍͖ͳΓ۩ମతͳ࣮૷Λߟ͑ͳ͍ w ෳࡶͳ໰୊͸ͦ΋ͦ΋ߟ͑Δͷ͸೉͍͠ w ؆୯ͳ໰୊Ͱ΋͋Δఔ౓ͬ͘͟ΓͳΠϝʔδ͕͍͍ w ෳࡶͳػೳ΄Ͳɺͪΐͬͱͣͭ෼ׂ͢Δ w

    Ұ౓ʹߟ͑Δ໰୊ΛߜΕΔ w 5%%ʹ΋௨͡Δߟ͑ํ w ςετ΋ॻ͖΍͘͢ͳΔͱ͍͏خ͍͠෭࡞༻ w ෦඼ͷ੹຿ʹઐ೦͢Δ w ඞཁʹԠͯ͡%FMFHBUF΍$MPTVSFʹؙ౤͛
  21. ҉໧ͳલఏΛͳ͘͢ class SomeContainerView: UIScrollView { var childViews: [UIView] = []

    // ... func scrollToView(at index: Int) { self.contentOffset.x = self.bounds.width * CGFloat(index) } } த਎ಡΜͰΈͨΒ
 ͜Ε͸֤childViewͷ෯͕ࣗ਎ͷ෯ͱಉ͡
 ͱ͍͏લఏͰಈ͍ͯΔॲཧͩ
  22. ҉໧ͳલఏΛͳ͘͢ class SomeContainerView: UIScrollView { var childViews: [UIView] = []

    // ... func scrollToView(at index: Int) { self.contentOffset = self.childViews[index].frame.origin } } ͍ͭͲ͜Ͱ୭ʹݺ͹ΕΑ͏ͱඞͣ ֘౰childView͕දࣔ͞ΕΔॲཧͰ͋Δ 
 ʢchildViewͷϨΠΞ΢τͱ͍͏҉໧ͳલఏʹ ґଘ͠ͳ͍ʣ
  23. ҉໧ͳલఏΛͳ͘͢ class SomeContainerView: UIScrollView { private var childViews: [UIView] =

    [] // ... func scrollToChildView(at index: Int) { let childView = self.childViews[index] assert(childView.superview === self) self.contentOffset = childView.frame.origin } } Ͳ͏ͯ͠΋֎ͤͳ͍લఏ͸ ඞͣ໌ࣔ͢Δ