Re: Since Apple Listens to this List... [Case Statements]
Re: Since Apple Listens to this List... [Case Statements]
- Subject: Re: Since Apple Listens to this List... [Case Statements]
- From: Chris Page <email@hidden>
- Date: Thu, 14 Feb 2002 00:19:18 -0800
nigh on 2002-02-13 8:08 PM, Shane Stanley at email@hidden
wrote:
>
Which is four lines longer than:
>
>
if x is y1
>
-- Do this is x is y1
>
else if x is y2
>
-- Do this if x is y2
>
else if x is y3
>
-- Do this if x is y3
>
end
>
>
Methinks beauty is in the eye of the beholder...
It's not really about the number of lines (although I think the design
referred to -- which I've omitted -- is too verbose). It's about the number
of times "if x is" has to be typed and read. More repetition means more
chance for error and a greater maintenance burden. It makes it harder to see
the variations in all the repeated code.
Here's my suggestion:
select x
y1 then
-- do stuff
y2 then
-- do stuff
y3 then
-- do stuff
else
-- do stuff when there is no match
end select
This makes it easier to see the test values and minimizes the amount of
syntax needed.
In the above, "x equals" is applied to each test case until the first match.
If you want greater flexibility, also allow an option to specify a test
function instead of a test value:
select with f
y1 then
...
In this case, f is applied to each value in turn until f returns true.
If you want to really go all-out, it can be useful to have another clause,
or clauses, that are executed if any of the values match. It's sort of the
opposite of "else":
select x
before
-- if there is a match, do this before the "then" code
after
-- if there is a match, do this after the "then" code
y1 then
-- do stuff
else
-- do stuff when there is no match
end select
This eliminates the need to perform redundant tests or to set variables to
test after the select statement. An alternative to the "after" clause might
be for select to set "it" to the matching test value so it can be inspected
after the select.
To allow for multiple test cases sharing code without the burden of defining
extra functions, as someone else suggested, a "continue" keyword might be
useful:
select x
y1 then
-- do unique stuff
continue
y2 then
-- do stuff common to y1 and y2
...
A related, but simpler and perhaps more common extension is to allow
multiple values to select the same "then" code, e.g.:
select x
y1, y2 then
-- do stuff for y1 and y2
...
[I considered using "or" as in "y1 or y2 then", but I think that's more
ambiguous because it could be misread as "x equals (y1 or y2)" instead of
"(x equals y1) or (x equals y2)", and I think using comma makes it easier to
see the test values.]
--
Chris Page
Mac OS Lead, Palm Desktop
Palm, Inc.
This is software development we9re talking about here.
3Picky2 is our mantra.
_______________________________________________
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.