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.
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.
I have similar situations in mind where my code looked like the one above. I don't feel anything bad about that and this is also where SmallLint is sort of missing the context.
Similar issues have been discussed on the mailing list for things like #ifNil:ifTrue:ifFalse and friends. Implementing such methods would lead to an explosion of the interface, see the WAHtmlBuilder in ancient versions of Seaside.
Btw, I am not sure if you are aware of the Essential-Testing tools? The TestRunner of Squeak 3.9 origins from there. The package is available at http://source.lukas-renggli.ch/essential.html and it includes facilities to run SLint as part of SUnit tests. It allows to use pragmas to declare certain lint errors as expected or to ignore certain lint rules in the context. It also includes some additional rules I consider to be important.
Lukas
squeak-dev@lists.squeakfoundation.org