[Q] do: ifNotEmpty: ifNotEmptyDo and A proposal for avoiding many bugs

Lic. Edgar J. De Cleene edgardec2001 at yahoo.com.ar
Sun Dec 5 08:56:47 UTC 2004


In image we have:

do: aBlock 
    "Evaluate aBlock with each of the receiver's elements as the argument."

ifNotEmpty: aBlock
    "Evaluate the given block unless the receiver is empty.

      If the block has an argument, eval with the receiver as its argument,
      but it might be better to use ifNotEmptyDo: to make the code easier to
      understand"

    ^self isEmpty ifFalse: [aBlock valueWithPossibleArgument: self].

ifNotEmptyDo: aBlock
    "Evaluate the given block with the receiver as its argument."

    ^self isEmpty ifFalse: [aBlock value: self].

Why we do not have ifNotEmptyDo: for each element ?

What you think of replacing.


addAll: aCollection
    "Include all the elements of aCollection as the receiver's elements.
Answer 
    aCollection. Actually, any object responding to #do: can be used as
argument."

    aCollection do: [:each | self add: each].
    ^ aCollection

with:

addAll: aCollection
    "Include all the elements of aCollection as the receiver's elements.
Answer 
    aCollection. Actually, any object responding to #do: can be used as
argument."

    aCollection ifNotEmptyDo: [:each | self add: each].
    ^ aCollection

I tired of fixing this kind or error .

Or why not have a doSmart: ?

"Evaluate aBlock with each of the receiver's elements as the argument always
argument and element are both not empty or nil"

Sure headaches are lowering.


Edgar




More information about the Squeak-dev mailing list