Re: Avoiding repetition
Re: Avoiding repetition
- Subject: Re: Avoiding repetition
- From: "John W. Baxter" <email@hidden>
- Date: Thu, 15 Jul 2004 17:39:58 -0700
On 7/15/2004 12:55, "Paul Berkowitz" <email@hidden> wrote:
>
On 7/15/04 12:14 PM, "Bernard Azancot" <email@hidden> wrote:
>
>
> till another basic newbie question:
>
>
>
> The following script has no problem. I just would like to know if it is
>
> possible to improve its "coding style" .
>
>
>
>
>
> (* Pseudo Script Start *)
>
>
>
> -- Note that "action" is the action performed, always the same
>
> (delete/copy), but in a different folder each different day of the week
>
> (backup purpose)
>
>
>
> If myday = Monday then
>
> action to folder A
>
>
>
> else If myday = Tuesday then
>
> action to folder B
>
> ...
>
> ...
>
> else if myday = Sunday then
>
> action to folder G
>
>
>
> (* Pseudo Script End *)
>
>
>
>
>
>
>
> Is there a way to avoid repeating 7 times the same boring code lines
>
> with 2 bound variables (day and folder) ?
>
>
>
If 'action' takes several lines, put it in a handler with a parameter (x)
>
where x gets replaced by A, B, c, or 'folder A, folder B, folder C.
>
Otherwise , if it's just a line, no need. You can replace the construction
>
this way:
>
>
-------------------
>
set weekDayList to {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday,
>
Sunday}
>
set folderList to {A, B, C, D, E, F, G}
>
>
repeat with i from 1 to 7
>
if myDay = item i of weekDayList then
>
action folder (item i of folderList)
>
exit repeat
>
end if
>
end repeat
>
>
----------------
>
>
>
However, this is actually going to be less efficient (not that it matters
>
much with something so simple) than your original. since two lists have to
>
be built (could be stored as properties, of course) and iterated. There's
>
nothing much wrong with your original method.
Well, there is something a little bit wrong with the original, inline series
of tests. It would be entirely possible to have an error in the actions for
one of the days but not the others. Suppose that day is Sunday, and instead
of folder G the source is typed to move into folder D, and only rarely is
the script run "for" Sunday.
The error might not be noticed until after one thinks the script is running
fine and is no longer checking the result each time.
--John
_______________________________________________
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.