13.07.2015 Views

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

Smalltalk Best Practice Patterns Volume 1: Coding - Free

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

OrderedCollectionYou need a dynamically sized Collection.How do you represent Collections whose size can’t be determined when they are created?Many of the hassles programs give users come from the need for flexibility. Primitive memorymanagement has lead to a lack of flexibility in the sizes of data. Every time I read “you can have upto 99 files” or “each function can have no more than 256 lines” I imagine a software engineerpreallocating memory to handle just that many items. While arbitrary limits like “99” or “256” aretypically chosen because the engineers can’t imagine the need for any more, just as typically theybecome a hindrance to a user somewhere down the road (sometimes years down the road).There is no excuse for arbitrary data size limitations in <strong>Smalltalk</strong>. Two factors work in your favoras you try to eliminate such limits. First, the Collection classes give you tremendous leverage tochange your mind about representing one-to-many relationships. Second, the garbage collectorfrees you from the drudgery of maintaining references as data structures change size.This flexibility comes at a cost. The implementation of OrderedCollection, the most commondynamically sized Collection, allocates more memory than it strictly needs at first, so that somegrowth can be accommodated at little cost. The implementation also uses indirection to accesselements. In some Collection-intensive code I have found this to be a bottleneck. While you’re justtrying to get the program working, though, you shouldn’t worry about such issues. Time enough toaddress them later.Use an OrderedCollection as your default dynamically sized Collection.Change to a Set if you need to ensure that every element appears only once. Change to a RunArrayif you want to compactly represent a collection with long runs of the same element. Change to anArray if you don’t need dynamic sizing.<strong>Coding</strong> <strong>Patterns</strong> page 87 of 147 9/30/2006

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!