[Pharo-project] Beginner question about "self" in block
otto at finworks.biz
Fri Jan 27 02:00:07 EST 2012
I like the "withBackLink:" one more because the other solution exposes
how books are implemented on Library by passing the collection
through. Better to let library manage books than to add it directly.
> Library>>addBook: aBook
> aBook addToLibrary: self.
> Book>>addToLibrary: aLibrary
> aLibrary addBook: self withBackLink: [ :backlinkValue | library :=
> backlinkValue ].
> Library>>addBook: aBook withBackLink: setBacklinkBlock
> books ifNil: [ books := OrderedCollection new ].
> books add: aBook.
> setBacklinkBlock value: self.
However, I'm not sure that you are adding *that* much value by using
the "withBackLink:" because this reads a bit easier:
aLibrary protectedAddBook: self.
library := aLibrary
books add: aBook
We build in a bit of an integrity framework that helps:
self assert: (library hasBook: self)
books do: [ :book | self assert: book library == self ]
After each test, in the tearDown, we call this on all objects created
in the test. Also, we run this daily on production databases and fix
failures (not often enough though!).
More information about the Pharo-dev