eoneon123 Posted January 28, 2013 Posted January 28, 2013 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" )
Vaughan Posted January 29, 2013 Posted January 29, 2013 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. 1
eoneon123 Posted January 29, 2013 Author Posted January 29, 2013 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?
Raybaudi Posted January 29, 2013 Posted January 29, 2013 "~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" ) 1
eoneon123 Posted January 29, 2013 Author Posted January 29, 2013 Thanks, Raybaudi. But now I'm wondering if there is in fact a downside to using global variables aside from cluttering the data viewer?
jbante Posted January 30, 2013 Posted January 30, 2013 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.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now