This post originated from an RSS feed registered with Agile Buzz
by James Robertson.
Original Post: Scripting in Workspaces
Feed Title: Travis Griggs - Blog
Feed URL: http://www.cincomsmalltalk.com/rssBlog/travis-rss.xml
Feed Description: This TAG Line is Extra
Over the years, I've observed Smalltalkers take advantage of the "workspace" to a variety of degrees. Some use it lots; some don't. This is not a post arguing whether you should or shouldn't.
I use it primarily for system maintenance/introspection scripts. Little code fragments that I don't feel warrant making new objects and models from. Doing so, there's two nifty tools I really like.
WorkspaceFormatting
Any script with a couple of do:'s or nested ifTrue:ifFalse: statements can get funny to read/modify quickly. WorkspaceFormatting is a goodie in the Open Repository which allows you to highlight a chunk of workspace code and "format" from the context menu, the same way you would a method in a class. I'm pretty sure it was Peter Hatch who authored it.
For me, this little tool is a big win, especially with any multiline workspace script. Years ago, I gave up wasting time formatting my own Smalltalk code and have never felt better. And this allows me to write readable scripts.
Blocks as Functions
If you written any normal scripts, you know that any appreciably complex script will want to be able to use "functions", usually because your performing the same unit of work from multiple call sites. One can accomplish this by using blocks stored in script variables. For example:
The first line declares a block which can act like a function for later uses in the script. Kind of like a poor man's method for scripts.
We could have inlined that. Until we have multiple call sites in the script it's probably not worth it except maybe for maintainability. What cannot be inlined though would be a recursive function. One of the not as often used properties of the full closure block, is that it can refer to and call itself. As in:
wholePrereqTree :=
[:pkg |
| all |
all := Set with: pkg.
pkg deploymentPrerequisites do:
[:coll |
(Store.Registry packageNamed: coll first)
ifNotNil: [:subPkg | all addAll: (wholePrereqTree value: subPkg)]].
all].
Store.Registry allPackages
groupedBy: [:each | (wholePrereqTree value: each) size]
In this one, the wholePrereqTree block invokes itself to recursively walk the prereq tree. For tree structure models like store and the class hierarchies, I've gotten a lot of mileage out of this technique.