> Am 29.05.2018 um 17:49 schrieb sergio ruiz <[email protected]>: > > If a model has a list of things.. such as a user that can/may have lots of > pets, are there any real benefits to initializing the list of pets lazily? > > like: > > self pets := OrderedCollection new. > > vs. > > pets > pets ifNil: [self pets: OrderedCollection new] > ^ pets >
Lazy initialization is good if you only want to assign additional objects when needed. So if it would be a storage optimization you could make it in a way that only those who have pets get a collection assigned. The downside is on the one hand that it raises concurrency problems because if two processes call #pets at the same time it could be that two collections are created and the two processes have each one of them and only one is stored in the pets holder. On the other hand lazy init makes you use the getter even inside the object (to assure the collection is created). I usually like to avoid having getters if not really needed but a lazy init style makes this a (bad) habit. So my advize would be that if you don’t have a reason to do so make the initialization of this collection in the #initialize method and use the instVar accessin the methods of the pets holder. Try not to have a getter for the pets collection Norbert
