Proper use of curly brackets in scripts

claystroup
Tera Guru

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.

1 ACCEPTED SOLUTION

Chuck Tomasi
Tera Patron

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;


View solution in original post

6 REPLIES 6

Orgarnizations typically have a style guide that defines things like use curly braces, indentation, spacing between operators and keywords, etc. Check with your senior developers to see if something exists. If not, get their input and start putting one together - they'll both love and hate you for it.


If I have answered your question, please mark my response as correct so that others with the same question in the future can find it quickly and that it gets removed from the Unanswered list.



If you are viewing this from the community inbox you will not see the correct answer button.   If so, please review How to Mark Answers Correct From Inbox View.



Thank you