Jump to content
Salesforce and other SMB Solutions are coming soon. ×

nested if statements


stuff

Recommended Posts

When you're writing a script with lots of if statements is it better to nest them e.g

 

If (whatever the first calculation is)

Perform script A

Else

If (whatever the second option is)

Perform script B

Else

If(whatever the third option is)

Perform script c

End if

End if

End if

 

or is better to have it as:

If (whatever the first option is)

Perform script A

End if

If (whatever the second option is)

End if

If (whatever the third option is)

End if

 

Is there a protocol for phrasing scripts using lots of if statements?

Is the script likely to run slightly differently depending on which of the two options are used?

any thoughts/explanations gratefully recieved.

Thanks

Stuff

Link to comment
Share on other sites

Hi stuff

 

I'd say the nested option has slightly quicker evaluation if you put the most likely result of the if at the top. Can't say if you will ever notice... depends on how complex the conditions are.

 

kjoe

smirk.gif

Link to comment
Share on other sites

I think that the first structure of if statments works quickly than the second. But the second structure is more readable. In some case this two structures is not equuvalent. And it is important for quickly works "put the most likely result of the if at the top" as say Kjoe.

Link to comment
Share on other sites

aaa wrote: [ QUOTE ]

In some case this two structures is not equivalent.

 

[/ QUOTE ]

 

that is correct. If you want to evaluate condition two no matter what the outcome of condition one is, a nested statement is no good.

 

kjoe

smirk.gif

Link to comment
Share on other sites

knowing that the nested statements will run through slightly quicker is very useful, thanks.

 

As the database is going to be done as a stand alone in developer I was also wondering whether either of the two options are more reliable/robost in the long run, do either of them run the risk of the script eventually getting confused halfway through and doing bizzare things or is this not an issue?

 

Also another question:

If you put a message to be displayed in the script with two options e.g. "ok" and "cancel" does it need two if statements below it e.g.

If (status(currentmessagechoice))=1

Perform script A

Else

If (status(currentmessagechoice))=2

perform script B

End if

End if

 

Or can you get away with

If (status(currentmessagechoice))=1

Perform script A

Else

perform script B

End if

 

The reason for asking is that I'm going through scripts I wrote while still getting used to filemaker so although they work I'm not sure whether they are likely to eventually breakdown and go pearshaped at some point the future.

 

thanks

stuff

Link to comment
Share on other sites

[ QUOTE ]

do either of them run the risk of the script eventually getting confused halfway through and doing bizzare things or is this not an issue?

 

[/ QUOTE ]

Don't think so. If it works the first time, then it will work the zillionth time. Bugs may emerge though, but this won't be because of system degradation but because of hidden flaws.

 

You can get away with the second procedure just fine if you only define two buttons.

 

kjoe

smirk.gif

Link to comment
Share on other sites

Thanks kjoe,

not only does that gives me peace of mind it also saves me a very tedious editing job on the old scripts!

thanks again,

stuff

Link to comment
Share on other sites

If scripts with a logical operator are always faster, where calculations are shorter.

Using the Else script step increases the efficiency of conditional scripts.

The indentation also makes the script easier to read.

Furthermore, using Else allows the script to Exit as soon as any condition is found to be true.

Link to comment
Share on other sites

I agree with Meekers about indentation.

 

Further, FileMaker has features some languages don't have such as the case and choose statements. In your first example, cass would be ideal.

Link to comment
Share on other sites

Hi Leader,

 

I'm afraid you're mixing up functions and script steps. Choose() and Case() are functions and are not available as script steps. Stuff's original question was about scripting.

 

kjoe

smirk.gif

Link to comment
Share on other sites

Hi Wenmeeker,

Thanks for the info on effeciency, to try to make the database more effecient would you recommend editing all the scripts to make them nested as in the first example rather than leaving them like they are in the second example?

Thanks

stuff

Link to comment
Share on other sites

Hi Stuff,

 

Evaluating scripted if-statements seldom takes a long time in my experience, it's loops, sorting, retrieving summary, max( ) and other relational values etcetera that takes time in scripts. These may be made more efficient by performing them on found sets if possible instead of the whole file.

In a large file,when your IF statements evaluate e.g. IF[max(some_relationship::some_field) > max(some_other_relationship::some_other_field)] , then looking into the script structure will probably be a good idea.

I think you need to consider the frequency of use of these scripts. Weigh the time you spend on tweaking them against the actual time saved when running the script. Always consider the adage "if it is not broken, don't try to fix it!".

 

kjoe

smirk.gif

Link to comment
Share on other sites

The most important thing to do is modular the script design to make the database more efficient.

 

Modular design is defined as breaking one large script into several sub-scripts and linking them together.

Creating a sub-script makes a typically static structure into something dynamic and capable of being reused multiple times.

This allows a single sub-script to be called from many other scripts. If modifications become necessary, only that sub-script needs to be changed.

 

Passing in formation between files/tables with Set Field, use open data channels (constant relationships) and links records, it is not always necessary that the match field is indexed.

Make the scripts conditional, use logical operators to make decisions.

See if you can use a calculation before using the script.

See if you can narrow down things to boolean level.

Use Freeze Window in looping scripts that switch between records, complex scripts that pass data between files using Perform Script or scripts that navigate to many different layouts, or when scripts has to change mode like switching between Find/Browse/Preview.

 

 

If the scripts working well, try to improve them....

Link to comment
Share on other sites

I said: "if it is not broken, don't try to fix it!".

 

John Wenmeekers said: "If the scripts working well, try to improve them.... "

 

Now you have two pieces of good advice. grin.gif

Seriously, John is telling you to keep the learning curve going up. Mine was the more pragmatic approach. You decide which way to go...

 

 

kjoe

smirk.gif

Link to comment
Share on other sites

Guest Yukon Cornelous

[ QUOTE ]

Hi Leader,

 

I'm afraid you're mixing up functions and script steps. Choose() and Case() are functions and are not available as script steps. Stuff's original question was about scripting.

 

kjoe

smirk.gif

 

[/ QUOTE ]

 

I think you an in error.

 

Case or Choose could be used in an If Statement?

Link to comment
Share on other sites

Sure. Any function could conceivably be used in an If statement. For example,

 

If [Case( "this", "that" )]

 

or

 

If [Choose( test - 1, result1, result2, result3 )]

Link to comment
Share on other sites

I misunderstood. IF[ ] is a script step, there is no case[ ] equivalent. sure you can put them into if[ ] like queue said.

 

kjoe

smirk.gif

Link to comment
Share on other sites

Oops, careless of me. kJoe is right!

 

Wishful thinking, I guess. That said, my DBs tend to have more than average calc fields, particularly when it comes to parsing. That tends to take a load off scripting.

 

Thanks for correcting, kjoe!

Link to comment
Share on other sites

[ QUOTE ]

Oops, careless of me. kJoe is right!

 

Wishful thinking, I guess. That said, my DBs tend to have more than average calc fields, particularly when it comes to parsing. That tends to take a load off scripting.

 

Thanks for correcting, kjoe!

 

[/ QUOTE ]

 

More than the average calc, will bog your db's down. You woudl be better off scripting.

Link to comment
Share on other sites

Chopper has a point, and it does take judicious use of calc fields. There's a matter of perception to consider as well: Overall, calc fields might be faster, but on the older OS/9 with a slow moving script, you could go off and do other tasks while the DB script churned away.

Link to comment
Share on other sites

The Case() function is specifically designed to handle a series of tests and results within a single function call.

 

There's no need for a scriptstep 'Case'.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.



×
×
  • Create New...

Important Information

Terms of Use