Does anyone else have this pattern?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Does anyone else have this pattern?

Piers Cawley
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.

Reply | Threaded
Open this post in threaded view
|

Re: Does anyone else have this pattern?

Lukas Renggli
> 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

--
Lukas Renggli
http://www.lukas-renggli.ch

Reply | Threaded
Open this post in threaded view
|

RE: Does anyone else have this pattern?

J J-6
In reply to this post by Piers Cawley
I like this sort of pattern, but I wouldn't put it in the stock image.  I would make it an extension of whatever project uses it (i.e. it might be in Collection but under the protocol *Your-Project-Name), or maybe make a new project called "Collection extension" or something.

> Date: Fri, 29 Jun 2007 15:56:26 +0100
> From: [hidden email]
> To: [hidden email]
> Subject: Does anyone else have this pattern?
>
> 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.
>


Hotmail to go? Get your Hotmail, news, sports and much more! Check out the New MSN Mobile