Does anyone else have this pattern?

Piers Cawley pdcawley at
Fri Jun 29 14:56:26 UTC 2007

During my SmallLint directed exploration of the guts of the
OmniBrowser, I came across a chunk of code that looked (very
approximately like)

coll := self buildCollection.

coll ifEmpty: [^ self].
coll size = 1 ifTrue: [self browse: coll first].
self askUserToChooseFrom: coll.

Now, call me Mr Fussy, but I really don't like seeing function scoped
temporaries if I can help it, so it seems to me that one could write a
method on Collection along the lines of:

ifEmpty: emptyBlock ifSingular: singularBlock otherwise: otherwiseBlock

    self ifEmpty [^ emptyBlock value].
    ^self size = 1
        ifTrue: [singularBlock valueWithPossibleArgument: self first]
        ifFalse: [otherwiseBlock valueWithPossibleArgument: self]

Which would clean the original method up like so:

self buildCollection
    ifEmpty: []
    ifSingular: [:node| self browse: node]
    otherwise: [:coll| self askUserToChooseFrom: col]

Which, to my eyes at least, seems to express the intent more clearly.

The obvious rearrangements and subsets spring to mind, along with
otherwiseDo:, otherwiseInject:, otherwiseCollect:

So, am I alone in thinking something like this would be useful, and if
I'm not, what's the best way to go about getting it into the image?
Create a CollectionTesting package and add it to the dependencies of
any packages that use it? Submit a changeset somewhere? And enquiring
mind wants to know.

Thanks in advance for your consideration.

More information about the Squeak-dev mailing list