Skip to content

補充 Paul 理解的 IoC 和 Temporal Coupling #4

Description

@ctwdtw

剛剛再去看了一下 IoC 和 Temporal Coupling:

Inversion of Control:

“The term Inversion of Control originally meant any sort of programming style where an overall framework or runtime controlled the program flow.”—Dependency Injection: Principles, Practices, and Patterns by Mark Seemann & Steven van Deursen

用自己的話講:

  1. 不是你的 code, 是別人的 code 決定 code 的 flow, 就叫做你的 control 被 invert 了。
  2. 例子: UIKit 的 life cycle 決定接下來你寫的哪行 code 會接著被執行。

Temporal Coupling:

“Temporal Coupling is a common problem in API design. It occurs when there’s an implicit relationship between two or more members of a class, requiring clients to invoke one member before the other. This tightly couples the members in the temporal dimension.”—Dependency Injection: Principles, Practices, and Patterns by Mark Seemann & Steven van Deursen

用自己的話講:

  1. code 要經由特別的順序被執行, 否則會出錯, 就叫做有 temporal coupling。
  2. 我們在寫測試的時候,如果測試的每一行有執行順序的要求的話 (例如稍微換 when 區塊的兩行程式碼, 測試就會崩潰),test case 就有所謂的 temporal coupling.
  3. 因為 UIKit 的 IoC 特性 (UIKit call your code , not your code call UIKit),
    所以我們的測試必須按照順序去 call sut.loadViewIfNeeded 和其他 UIKit 提供的 func (根據我們對 UIKit 的知識), 才能模擬 UIkit 的行為, 所以我們的測試就有 temporal coupling 了。
  4. 為了避免 3. 這種不好的 test case 的 smell, 老師在課堂中給了一些建議 (具體忘了...要去複習...orz.)

以上是我的理解, 大家可以補充或 debug XD.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions