A review of the example above for anyone who wonders why it works and why one should not code like this.
Second line contains the following code:
DESTROYER = gg.makeRequest("link").content
There are 3 main steps in this code:
1. "makeRequest" function from "gg" table is called with the corresponding argument.
2. Result returned by the function is indexed with the key "content".
3. Value from the step 2 is saved in "DESTROYER" variable.
Then, on the third line there is the following code:
if not DESTROYER then
which checks the condition and executes the code after "then" keyword, if the condition is true. Otherwise, it executes the code after "else" keyword.
To understand, why "one should not code like this", let's review the documentation for "makeRequest" function at GG help.
In this case we are interested in the description of the value returned by the function:
Now, let's get back to the second line of the example.
When function succeeds, the code from the second line behaves as intended. Table returned by the function is indexed with the key "content" and the result of this operation is stored in the variable.
And what happens, when there is an error? String returned by the function is indexed with the key "content" and the value is stored in the variable. By default, there is no meaning of indexing string with a key and luckily for the example, the value returned by such operation is nil. But this behavior is not obvious and not obvious behavior may often lead to errors.
Then how can the example be modified to avoid non-obvious behavior?
The result of the call to "makeRequest" function should be saved to a variable. Then the type of the variable should be checked. If it is "table", the function has succeeded, the variable can be indexed with the key "content" and the result of this operation can be stored in another variable. And if it is "string", then an error occurred, the contents of the variable can be printed to log (via "print" function) and/or shown to the user (via "alert" function).
Example to illustrate the answer above:
local functionResult = gg.makeRequest('link')
if type(functionResult) == 'table' then
content = functionResult.content
elseif type(functionResult) == 'string' then
-- print/alert error description
-- other actions
Such modification to the code explicitly shows which actions will be performed and when (function succeeds / function fails) getting rid from non-obvious behavior.