Long Ifs

What’s the best way of handling long if statements?

Let’s assume

if a=b
do something
else
do something else
end if

If “do something” is long and convoluted, then the script becomes difficult to read and fix.

I’ve been trying to use handlers; but then, should the handler produce more than one variable as result, they must be declared global, otherwise they’re not sent back.

How is this handled best? What would be the equivalent of a “go to” statement – what’s the AppleScript way?

Thanks.

You can allways return any number of vars from a handler:

set {x, y, z} to blah()

to blah()
	set a to 1
	set b to 2
	set c to 3
	return {a, b, c}
end blah

Anyway, you can use whatever makes life easier for you. I’ve read code from lots of people and everyone will make it different.
Depending on your final purposes, you can choose a way or another. For example, if you know you will be updating the script so frequently, and it’s long code, you should stick with some kind of modular design: use libraries, handlers, etc. Use a strong error tracking system, and perhaps some kind of log file, commented code… This way, the next time you return to your code, you will be able to read it easilly and you will improve your troubleshooting issues. (I think). :smiley:

Hello alexn,
Just to expand on jj’s suggestion and offer a specific tactic I have found very very useful - you should check out this article in the Unscripted section of MacScripter.net

http://macscripter.net/articles/206_0_10_0_C/

I used this Object-Switch technique to help my if…else…end statements in a long Adobe Illustrator script and it helped the project to become much more maintanable. (the bottom of the linked article summarizes how the Object-Switch is better than the if…else…end).

My situation is like this - the input could be either CMYK or RGB - so I had a huge if else statement

if (CMYK) 
-- lots and lots of code
else if (RGB) 
-- lots and lots of code
end

I broke these up into two separate script objects that inherited from a common parent (exactly as demonstrated in the link above) and the code is more modularized and easy to follow…its quite beautiful. And, if the object switches are placed in script libraries as JJ mentioned they can be accessed by any other script that might need the same functionality…

jON bEEBE

Thanks, that’s useful, as was the rest of your advice.

alex

Very interesting, thank you. That definitely looks like a practice I should adopt.

alex