Smalltalk Best Practice Patterns Volume 1: Coding - Free
Smalltalk Best Practice Patterns Volume 1: Coding - Free
Smalltalk Best Practice Patterns Volume 1: Coding - Free
- No tags were found...
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
IsEmptyHow do you test if a collection is empty?I wouldn’t have thought I’d have to point this one out, but over and over I see code that says:...aCollection size = 0 ifTrue: ...or...aCollection size > 0 ifTrue: ...Checking whether size is equal to zero or greater than zero is really an implementation of a higherlevel concept, namely “Is the collection empty?”The collection protocol provides simple messages that test collections for emptiness. Using themresults in more readable code.Send isEmpty to test whether a collection is empty (has no elements). Use notEmpty to testwhether a collection has elements.Note that while Visual<strong>Smalltalk</strong> and VisualAge also define notEmpty, VisualWorks 2 does not, soyou’ll have to add it yourself.The value of isEmpty is not that there is such a huge difference between:aCollection size = 0and:aCollection isEmptyalthough there is some difference. The important point is that I can read the idiomatic versioninstantly. If I see some explicitly checking the size I have to stop and figure out if there issomething special going on that isn’t obvious.All cultures develop their own vocabulary. If you were in a car repair shop and you heard someonesay “hand me that wrench with the knurled wheel that changes the width”, you wouldn’t want themworking on your car. You want a mechanic who knows it’s a monkey wrench.<strong>Coding</strong> <strong>Patterns</strong> page 102 of 147 9/30/2006