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

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

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. 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
  2. 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
  3. 9

  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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