Hi there,

I have a question about lazy initialization accessor methods that always
keep me thinking in terms of performance and style.

For years I did something like:

  property isNil ifTrue: [ self initializeProperty ]. "It could be ifNil:
as well"

  "Returns the receiver, not the property."
   property := self foo collect: [:e | ... ].

But this made me think that maybe I'm performing extra operations, so I
started doing something like:

  ^property ifNil: [ self initializeProperty ]

  ^property := self foo collect: [:e | ... ]

The initialize method is often created by an "Extract Method" refactoring I
do in the accesor, where I do the initial implementation.

The messages makes me think that the second implementation is "faster", but
an initializationMethod returning the value instead of the receiver is a
code smell to me.

I early initialize everything I know will be be used right away, but for
cases it doesn't make sense.

What do you think/prefer? Why?

Maybe we could use the reified variables (slots) to implement the lazy
initialization themselves :)

Best regards!

Esteban A. Maringolo

Reply via email to