[Pharo-project] Beginner question about "self" in block

Otto Behrens otto at finworks.biz
Fri Jan 27 08:00:07 CET 2012


Hi,

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:

Book>>addToLibrary: aLibrary
  aLibrary protectedAddBook: self.
  library := aLibrary

Library>>protectedAddBook: aBook
  books add: aBook

We build in a bit of an integrity framework that helps:

Book>>checkIntegrity
  self assert: (library hasBook: self)

Library>>checkIntegrity
  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!).

Cheers
Otto



More information about the Pharo-project mailing list