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

mutatingキーワードから学ぶCQS / 20180309 #tryswift_aftertalks

704056da9a4c4e075ad14479beaebab7?s=47 takasek
March 09, 2018

mutatingキーワードから学ぶCQS / 20180309 #tryswift_aftertalks

try!Swift Tokyo 2018 After talks Day 2 - connpass
https://tryswifttokyo-aftertalks.connpass.com/event/79690/
での発表資料です。

# 参考リンク

オブジェクト指向入門 第2版 方法論・実践 (IT Architects' Archiveクラシックモダン・コンピューティング) | バートランド・メイヤー, 酒匂 寛 |本 | 通販 | Amazon
https://www.amazon.co.jp/dp/4798111120

CommandQuerySeparation
https://martinfowler.com/bliki/CommandQuerySeparation.html

Swift.org - Documentation
https://swift.org/documentation/

Swift.org - API Design Guidelines
https://swift.org/documentation/api-design-guidelines/

704056da9a4c4e075ad14479beaebab7?s=128

takasek

March 09, 2018
Tweet

Transcript

  1. Learning CQS from mutating keyword by. 2018/3/9 try! Swift After

    Talks 1
  2. takasek iOS Developer @takasek OSS ActionClosurable౳ Xcode Extension PasteTheType 2

  3. CQS Command Query Separation ίϚϯυΫΤϦ෼཭ݪଇ 3

  4. Commands 1 • Change the state of a system •

    but do not return a value Queries • Return a result • do not change the observable state of the system • (are free of side effects) 1 https://martinfowler.com/bliki/CommandQuerySeparation.html 4
  5. Side effects ෭࡞༻ 5

  6. ෭࡞༻͸ɺιϑτ΢ΣΞͷਖ਼͠͞ͷ ࠜڌʢʹ਺ֶతଐੑʣΛࣦΘͤΔ Side effects ruin a ground for mathematical validity

    of the software. 6
  7. Referential transparency ࢀরಁաੑ Queries shouldn't change answers. ʮ໰͍Λൃ͢Δ͜ͱʹΑͬͯ ౴͕͑ม͑ΒΕΔ΂͖Ͱ͸ͳ͍ʯ 7

  8. Martin Fowler says... 1 It would be nice if the

    language itself would support this notion. I could imagine a language that would detect state changing methods, or at least allow the programmer to mark them. ʮCQSΛαϙʔτͯ͘͠ΕΔݴޠ͕͋Ε͹͍͍ͷʹʯ 1 https://martinfowler.com/bliki/CommandQuerySeparation.html 8
  9. 9

  10. mutating keyword “if you need to modify the properties of

    your structure or enumeration within a particular method, you can opt in to mutating behavior for that method” 3 func ͕஋ܕΦϒδΣΫτʹมߋΛՃ͑Δ͜ͱΛ໌ࣔ 3 "The Swift Programming Language (Swift 4.1)" 10
  11. 4 4 https://swift.org/documentation/api-design-guidelines/ 11

  12. Commands 1 • Change the state of a system •

    but do not return a value Queries • Return a result • do not change the observable state of the system • (are free of side effects) 1 https://martinfowler.com/bliki/CommandQuerySeparation.html 12
  13. Commands 1 • Change the state of a system •

    but do not return a value ! Queries • Return a result • do not change the observable state of the system • (are free of side effects) 1 https://martinfowler.com/bliki/CommandQuerySeparation.html 13
  14. find ./stdlib/public/core | grep .swift | xargs cat | grep

    "public mutating func" public mutating func append(_ newElement: Element) public mutating func insert(_ newElement: Element, at i: Int) public mutating func removeFirst() public mutating func sort() public mutating func subtract(_ other: Self) public mutating func multiply(by other: Self) public mutating func divide(by other: Self) public mutating func negate() public mutating func round(_ rule: FloatingPointRoundingRule) public mutating func write(_ other: String) public mutating func encode<T : Encodable>(_ value: T, forKey key: Key) throws 14
  15. Using Swift value types Swiftͷ஋ܕΛ࢖͏ = Geting along with CQS

    CQSʹ׳Ε਌͠Ή 15
  16. How about reference types (classes)? 16

  17. Do queries have side effects? ΫΤϦ͕෭࡞༻Λ͍࣋ͬͯͳ͍͔ʁ Do commands have return

    value? ίϚϯυ͕໭Γ஋Λ͍࣋ͬͯͳ͍͔ʁ 17
  18. Learn from Swift. Swiftʹֶ΅͏ Stand on the shoulders of giants.

    աڈͷڊਓʹֶ΅͏ 18
  19. ׬ 19

  20. ! 20

  21. find ./stdlib/public/core | grep .swift | xargs cat | grep

    "public mutating func" public mutating func popFirst() -> Element? public mutating func popLast() -> Element? public mutating func update(with newMember: Element) -> Element? public mutating func remove(at index: Int) -> Element public mutating func remove(_ member: Element) -> Element? public mutating func removeFirst() -> Element public mutating func removeValue(forKey key: Key) -> Value? public mutating func next() -> Element? public mutating func decode<I : IteratorProtocol>(_ input: inout I) -> UnicodeDecodingResult public mutating func superEncoder() -> Encoder public mutating func nestedContainer<NestedKey>(keyedBy keyType: NestedKey.Type) -> KeyedEncodingContainer<NestedKey> 21
  22. Exceptions. ྫ֎΋͋Δɻ 22

  23. Popping a stack is a good example of a query

    that modifies state. Meyer correctly says that you can avoid having this method, but it is a useful idiom. So I prefer to follow this principle when I can, but I'm prepared to break it to get my pop.1 ʮ෭࡞༻ආ͚ΒΕΔͬͯMeyer͸ݴͬͯΔ͚Ͳɺ popʹ໭Γ஋͕͋ͬͨΒਖ਼௚ศརͳͷͰ ʰCQSݪଇ͸कΕΔͳΒकΔʱʯ 1 https://martinfowler.com/bliki/CommandQuerySeparation.html 23
  24. कഁ཭ 24

  25. ͓͖ͯ͞ɺجຊతʹ͸༗ޮͳͷ͕ ʮݪଇʯ Principles work fine principally. 25

  26. ϝϦοτҙࣝͯ͠ ࢖͍͜ͳ͠·͠ΐ͏ Get along with them! 26