wnagrodzki / iOSProgrammingGuidelines

2 stars 0 forks source link

Lazy property #7

Open wnagrodzki opened 6 years ago

wnagrodzki commented 6 years ago

It should be used only to defer property costly initialization. Before using it check if the following rules are met:

To avoid cluttering the interface with lazy property initialization closure, extract it's content to a private method.

lazy var data: Data = self.provideFileData()

private func provideFileData() -> Data {
  // load data from disk
}
pwetrifork commented 6 years ago

The initialized property state should be independent from the state of the instance. The initialized property state should be independent from the time of it's initalization.

Consider a simpler phrasing for the above (I think that independence from the time of initialization is in fact independence from the state of the instance, which changes over time):

Property initialization outcome should be independent from the state of the owning instance at the time of the initialization

Then, what is the reasoning behind the rule about initialization not relying on other lazy properties?

Moskacz commented 5 years ago

What about a situation where initialising a property is costly but is not noticeable by the user? How should we measure this?

wnagrodzki commented 5 years ago

@Moskacz If it is not noticeable by the user we do not optimise it