Re: Why save text item delimiters? Philosophical question....
Re: Why save text item delimiters? Philosophical question....
- Subject: Re: Why save text item delimiters? Philosophical question....
- From: Christopher Nebel <email@hidden>
- Date: Mon, 16 Jun 2003 13:18:52 -0700
On Sunday, June 15, 2003, at 03:09 PM, David Crowe wrote:
One of the common aspects of all scripts that use the AppleScript text
item delimiters is that first they are saved, then set to the desired
value, then used, then restored.
In my mind, the first and last steps are a waste of time. If every
script always sets the delimiters to the desired value, then there
should be no need to restore them, and the code to save/restore is
just a waste.
The only problem that I can see is that while using the text item
delimiters your script calls a function that sets them to a different
value, but even limiting this to the case where the code between
setting and usage of the delimiters has no function calls, clearly
this seems like an anachronism.
Well, yes and no. Strictly speaking, you're correct: if everyone sets
the delimiters to what they want before using them *and guarantees that
no one else will change them*, then it all works as desired. And, as
you point out, this method has its advantages: shorter scripts (mostly)
that run a bit faster. The problem is, it also requires clients to be
careful about what they do between setting the delimiters and using
them. "No worries," you say, "I'll just set them right before using
them every time." Then one day, you hit something like this:
repeat with i in some_really_big_list
set AppleScript's text item delimiters to ","
do_something_with(every text item of i)
end
Since part of the point was execution speed, you might be tempted to
hoist the "set" line out of the loop. But, depending on what else
happens inside the loop, that might introduce a bug. "Big deal," you
say, "I'm smart and I know what everything else in the code is doing."
True, maybe, but nevertheless the odds are that you'll screw up at some
point. Probably more than once.
Anyway, the point is that neither style is absolutely correct or
incorrect. Like most engineering tasks, it's a trade-off: always doing
the save/restore costs a bit more time writing boilerplate code up
front and a bit more execution time doing saves and restores that
aren't really necessary in exchange for more flexibility in client code
and easier maintenance, while the "always-set" approach makes the
reverse trade. Which you choose is a largely personal decision, unless
you're working with a team or writing a library. Save/restore is
guaranteed to not screw up clients, no matter what they're doing, so
it's the right choice for libraries and people who focus on long-term
maintenance. On the other hand, if you're going for speed and don't
plan to look at the script again once you're done, always-set is a
perfectly reasonable choice. (For an extreme case of the latter, see
The Story of Mel
<
http://www.jargon.net/jargonfile/t/TheStoryofMel.html>. It's a
personal favorite of mine.)
Of course, many such arguments could be avoided if you didn't have to
set the text item delimiters so bloody often in the first place.
Several bugs are already on file regarding this.
--Chris Nebel
Apple Development Tools
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.