- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 06:00 AM
I've been researching and I'm not able to find an explanation for how curly brackets are evaluated in a script. Ex:
Ex 1:
if (current.variable_pool.eh_roe_type == 'Other'){
typeval = current.variable_pool.eh_roe_type_other;
else
typeval = current.variable_pool.eh_roe_type;
}
Ex 2:
if (current.variable_pool.eh_roe_type == 'Other'){
typeval = current.variable_pool.eh_roe_type_other;
} else {
typeval = current.variable_pool.eh_roe_type;
}
I've seen them used both ways in numerous SN scripting examples without explanation of how they evaluate differently, if there is any actual difference. To me, it would seem like you want to put your entire if/else statement inside one set of brackets expressing that you want them evaluated as one statement, but there are numerous examples of the if and else being in separate sets of brackets, so I really can't tell exactly what the difference is, what the output would be. From my experience with other platforms and languages, this can make a significant difference in the way the script operates.
Solved! Go to Solution.
- Labels:
-
Best Practices
-
Scripting and Coding

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 06:09 AM
Ex 1 is an invalid statement. Your first opening curly brace on the if statement says "I've got one or more statements before you'll find a closing brace - treat them as a group". Then you have an else appear in the middle of that group.
Options:
if (condition)
statement1;
else
statement2;
This tests the condition and says "if the condition is true, run statement1, otherwise run statement 2"
if (condition) {
statement1;
} else {
statement2;
}
This is functionally the same, but uses curly braces to be more visible. This is purely a style thing. Some people like it because they can put a debug statement in easily without adding curly braces later. Some people like it for readability. Not if you have more than one statement, e.g. statement1, and statement1a before the else, you MUST group them together with curly braces.
if (condition) {
statement1;
statement1a;
} else {
statement2;
}
You can 'unbalance' the curly braces, but I don't encourage it for readability. Example:
if (condition) {
statement1;
statement1a;
} else
statement2;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 06:02 AM
I haven't seen your ex 1 used yet, but ex 2 is most common. for if/else statements if you have 1 line of code executing you don't even need brackets ie:
if (current.variable_pool.eh_roe_type == 'Other')
typeval = current.variable_pool.eh_roe_type_other;
else
typeval = current.variable_pool.eh_roe_type;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 06:05 AM
I would think the logic of your ex 1 would evaluate the if and if it never evaluates true your else is in the if which logically doesn't make sense.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 06:09 AM
Ex 1 is an invalid statement. Your first opening curly brace on the if statement says "I've got one or more statements before you'll find a closing brace - treat them as a group". Then you have an else appear in the middle of that group.
Options:
if (condition)
statement1;
else
statement2;
This tests the condition and says "if the condition is true, run statement1, otherwise run statement 2"
if (condition) {
statement1;
} else {
statement2;
}
This is functionally the same, but uses curly braces to be more visible. This is purely a style thing. Some people like it because they can put a debug statement in easily without adding curly braces later. Some people like it for readability. Not if you have more than one statement, e.g. statement1, and statement1a before the else, you MUST group them together with curly braces.
if (condition) {
statement1;
statement1a;
} else {
statement2;
}
You can 'unbalance' the curly braces, but I don't encourage it for readability. Example:
if (condition) {
statement1;
statement1a;
} else
statement2;
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-18-2017 06:26 AM
All of these answers go along with how it seemed to me to work, but as I said, I'd seen examples of both ways, and some of our developers who have been doing this far longer than I use the Ex 1 method, so I was just looking for some feedback on which way is best.
Thank you all, you answered my question!