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

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

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/

takasek

March 09, 2018
Tweet

More Decks by takasek

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  5. Side effects
    ෭࡞༻
    5

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

  9. 9

    View Slide

  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

    View Slide

  11. 4
    4
    https://swift.org/documentation/api-design-guidelines/
    11

    View Slide

  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

    View Slide

  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

    View Slide

  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(_ value: T, forKey key: Key) throws
    14

    View Slide

  15. Using Swift value types
    Swiftͷ஋ܕΛ࢖͏
    =
    Geting along with CQS
    CQSʹ׳Ε਌͠Ή
    15

    View Slide

  16. How about reference types
    (classes)?
    16

    View Slide

  17. Do queries have side effects?
    ΫΤϦ͕෭࡞༻Λ͍࣋ͬͯͳ͍͔ʁ
    Do commands have return value?
    ίϚϯυ͕໭Γ஋Λ͍࣋ͬͯͳ͍͔ʁ
    17

    View Slide

  18. Learn from Swift.
    Swiftʹֶ΅͏
    Stand on the shoulders of giants.
    աڈͷڊਓʹֶ΅͏
    18

    View Slide

  19. ׬
    19

    View Slide

  20. !
    20

    View Slide

  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(_ input: inout I)
    -> UnicodeDecodingResult
    public mutating func superEncoder() -> Encoder
    public mutating func nestedContainer(keyedBy keyType: NestedKey.Type)
    -> KeyedEncodingContainer
    21

    View Slide

  22. Exceptions.
    ྫ֎΋͋Δɻ
    22

    View Slide

  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

    View Slide

  24. कഁ཭
    24

    View Slide

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

    View Slide

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

    View Slide