Redundancy in the usage of LET and SET on already declared variables & GC
What I mean by the title is in regards to the many Qlik Language script examples I've seen online and at work, where variables are declared once with either the LET or SET keyword and used repeatedly on the same variable through the script, where I am not sure if this is a case of redundancy, taking into account that I do know the usage and purpose of both keywords. Now the question is, is there really any need for anyone to do this?
SET vVariableA = ""; LET vVariableA = someExpression;
LET vVariableA = someExpression;
LET vVariableA = aDifferentExpression;
LET vVariableA = "";
vVariableA = someExpression;
LET vVariableA = someExpression;
vVariableA = aDifferentExpression;
Where we want the variable to hold an empty string if the expression returns null in the first example and a different expression in the second.
Although it appears that once you declare a variable with the SET keyword and called again with SET omitted it appears that the variable is evaluated as an expression (but I am not sure about this), which would mean SET would be required every time you want the value inside to be a string - I would like for this to be confirmed so correct me if I'm wrong, but so far from what I've tried and tested it appears that the latter is accepted by the Language Compiler (or whatever it is or called).
Also I wanted to know if the Qlik Language has any Garbage Collection in place, or do we always have to release memory by doing:
It's not quite clear for me which points of the Qlik variable-handling isn't clear enough. Therefore here a short try:
SET is assigning everything which is on the right side of the =
LET evaluates what is on the right side of the = and assigned this result to variables
Also I recommend of always using the SET/LET keywords for assigning a variable to be able to differ between both types and to increase the readability of the script. Each re-assigning of any value to this variable overwrites the older ones unless it is NULL which deletes the variable.
Further each variable/field/table which is created and won't be needed in the end must be explicitly removed with a drop-statement or assigning NULL to be deleted.
Many more to these things could you find here: Variables
Thank you for the answer but I am not exactly asking how to use LET and SET or what the purpose of each is, what I am asking is a bit more complicated, basically I'm asking if we can cut the usage of SET and LET on the same variable after its declaration, except when we want to re-assign some value to a SET variable, you'd have to re-read my post, still thank you for trying.
Let is optional. If the Let/Set is omitted Let is assumed. Set is mandatory, so if you want Set behaviour, you must specify Set.
QV script does not require variables to be declared, so the rules are the same whether its the first reference or a subsequent reference to the variable. The downside of this is that QV/QS will not throw an error if you reference an unassigned variable (it will be implicitly set to null).
Logic will get you from a to b. Imagination will take you everywhere. - A Einstein
Qlik Script language is interpreted, not compiled. Try imagining how a compiler should handle $-sign expansion and you know where I got stuck when trying to create a JIT-compiler (and gave up in the end) in order to just improve performance and add traceability.
Qlik Script language is not an OOPS. It was invented probably 25 years ago and largely reflects ideas from SQL and Visual Basic before it became VB.NET. I think it still does.
From a language design perspective, Qlik Script language and execution environment are indeed sometimes inconsistent. So please do not get annoyed about weird constructs or behavior. The important thing is: although being far from perfect, it works and is lightning fast. (As far as I know, the latter was design rule No.1.) The price we pay: nobody will tell us that we made the wrong assignment (everything is ok, although to us it may not be) or that we did string things with number and vice-versa when we really shouldn't or another uncontrolled attempts at processing data. The recent achievements in programming languages like C# or Java or any of the other (interpreted) languages of recent times (people seem to invent new "better" languages on an almost daily basis) have largely passed by unnoticed.
Variables that you create during a script run and do not remove before the end of that same script run cannot be removed anymore by assigning Null() during the next script run. It will look like they were created in the Variable Overview (although nothing of the sort was ever done) and they'll carry an empty string value, although Null() was last assigned to them. Inconsistent? Well...
With respect to your prolonged question (it took me 15 mins. to decypher the description and I'm still not sure that I fully understand what you're after, sorry about that), I think you are actually looking for an AltXX() function that works on any non-null value instead of just numbers. Sort of Default Value for assignments. If you post this as an Idea, you may get it in some future release. I'll immediately vote for it because that would remove a long-time gripe of mine with the current set of functions.
AFAIK there is no Garbage Collection being done in QlikView Script, except for the removal of all Mapping Tables at the very end of a script run. So yes you have to tidy up your environment yourself. But I always found that to be good programming practice. Am I wrong?