January 28, 201313 yr I just edited this to make sense (I hope). Basically my objective is to reduce the number of global variables in a script when their conditions are overlapping. For instance, where the comment is in the below Let statement, can I add: $$Command2 = "Yield" given the same conditions? Again, I know that I can just create another global variable but I'm trying to keep it simple given the number of lines in the actual code. Thanks in advance! $$Command1 = Let([ ~A = Case( conditions 1, 2, 3; "Black"; conditions 3, 4, 5; "White" ) ]; Case( ~A = "Black"; "Stop"; //<--can a Let statement be added to create additional variables? ~A = "White"; "Go" )
January 29, 201313 yr Is there a reason you are using global variables in a script? Global variables need (should) only be used if the value needs to persist after the script has run. If the value does not need to persist then do not use global variables. Use single-$ variables instead. A good use of a global variable might be to remember whether a script has run in the current session. The script would check whether a global variable $$has_run=1. If it does it would exit the script, if not it would run the script steps then set the variable to 1 and exit.
January 29, 201313 yr Author The reason I use globals is because they survive from one companion script to next. Basically, I set up a separate script with a bunch of interdependent variables to reduce the leg work in separate primary scripts. What's the downside to using globals?
January 29, 201313 yr "~A = "Black"; "Stop"; //<--can a Let statement be added to create additional variables?" Yes, you could do that in two ways: ~A = "Black"; "Stop" & Let ( $$Command2 = "Yield" ; "" ) or ~A = "Black"; Let ( $$Command2 = "Yield" ; "Stop" )
January 29, 201313 yr Author Thanks, Raybaudi. But now I'm wondering if there is in fact a downside to using global variables aside from cluttering the data viewer?
January 30, 201313 yr After you've forgotten what a global variable is for, it's difficult to track down all calculations that reference it, which sets up a disincentive to editing it's value or choosing not to set it in the future or refactoring in general for fear of breaking something. Global state leads to more tightly coupled code that is difficult to maintain with confidence.
Create an account or sign in to comment