13 oktober 2022

Qlik variable scope in subroutines

Deel dit bericht
Qlik variable scope in subroutines - Bitmetric Friday Qlik Test Prep question

Every Friday at Bitmetric we’re posting a new Qlik certification practice question to our LinkedIn company page. Last Friday we asked the following Qlik Data Architect certification practice question about Qlik variable scope in subroutines:

Qlik variable scope in subroutines question of the week.

Many people got the right answer, though not always for the right reason. There’s a few things to unpack here.

The correct answer is A: null

As we’ve seen in the question about passing by reference and by value from two weeks ago, when calling the subroutine on line 8, we supply 2 as the value to pass to the pInput parameter. Or so we think! The result isn’t what we expected though. When we check the variable overview we now suddenly have a variable called 2 with a value of 4:

Qlik Sense variable overview

So what’s happening here?

We’re still passing by reference

When passing an argument to a parameter, we have the following options:

  • Passing a string, for example ‘Hello world‘. This will pass as value.
  • Passing an expression, for example 1+1. We can also pass a fixed value as an expression by enclosing it in parentheses, for example (2). This will pass as value.
  • Passing a variable, for example vNumber. This will pass as reference.

Note that there is no option to pass an integer. It looks as if we’re passing the value 2 on line 7, but in reality we’re still passing a reference. In this case to a variable called 2. This variable does not yet exist, but gets created on line 3. What happens is that the statement gets interpreted as:

Qlik Sense variable LET

So, the result of the multiplication gets set to the variable named 2, not the parameter named pInput. When tracing pInput on line 8 it returns null.

So if we fix the input it would print?

So what if we change line 7 to use (2) instead of 2. Will that make the TRACE statement on line 8 work?

Qlik pass variable as value

No!

We are now passing the value the right way to the subroutine. Alternatively, we could also have used ‘2’ (string) or even 1+1 (also an expression).

On the bright side, the variable called 2 is no longer being created so we’ve improved the situation somewhat. The TRACE statement still returns null though.

Can we even get this to print?

No, passing by value or by reference was a red herring, we can never get it to trace in this setup.

pInput is a parameter of the MultiplyByTwo subroutine. Subroutine parameters are always scoped to the subroutine they belong to. This means that, outside of the subroutine, pInput does not exist. Even if we set the right value, we still won’t be able to reference it on line 8.

How can we pass values back from a subroutine then?

There are multiple ways to return values from a subroutine:

Providing a variable by reference as an argument

As we learned in the question from two weeks ago, whe can provide a variable by reference as an argument. The contents of that variable can be updated by the subroutine and referenced outside of the subroutine:

Qlik pass variable by reference

Creating a variable within the subroutine

While parameters are scoped to the subroutine, any variables that are created within the subroutine will exist in the global scope. In the following example we can reference vResult outside of the subroutine.

Returning values from a Qlik subroutine

It’s also good to note that if you want variables to only exist within the subroutine you should delete them before exiting the subroutine.

Return multiple values with the “stem” technique

When you want to return multiple values from a subroutine, the “stem” technique is a clever way to go about it. We’ll discuss this in another blog post. If you want to learn more sooner, then you may want to join us for the Masters Summit for Qlik, where Rob Wunderlich covers this in his Advanced Scripting training 😉

Inadvertently creating global variables within a subroutine can lead to several challenges, especially in complex Qlik applications. When variables are created and not explicitly deleted before exiting a subroutine, they remain accessible in the global scope, which can cause naming conflicts with variables elsewhere in the script. This might lead to unexpected results, as scripts could unintentionally modify or use these global variables, leading to data integrity issues and debugging difficulties. Therefore, developers must be vigilant about managing variable scope and lifecycle to maintain script clarity and performance.

The “stem” technique for returning multiple values from a subroutine involves using a naming convention that associates multiple related variables with a common prefix, allowing them to be treated as a grouped set of data. For example, if a subroutine calculates several metrics, each metric could be stored in a separate variable with names like Metric.Sales, Metric.Profit, and Metric.Revenue. This method simplifies the process of returning and accessing multiple related values from a subroutine, as they can be easily identified and manipulated based on their common prefix. We’ll go more in-depth on the practical implementation in a future blog post.

Effective management and cleanup of variables within a subroutine to prevent clutter in the global scope can be achieved through disciplined coding practices and the strategic use of Qlik’s scripting capabilities. Developers should adopt a clear naming convention for variables that are meant to be temporary or local to a subroutine, making it easier to identify and manage these variables. One approach is to prefix temporary variables with a specific string, such as “temp_”, which signals that these variables should not persist beyond their immediate use.

Additionally, developers can leverage the LET and SET commands to explicitly delete or nullify these variables at the end of a subroutine. This ensures that temporary variables do not remain in memory and affect the global scope, thereby preventing potential conflicts or memory issues. It’s also advisable to document the purpose and expected lifespan of each variable within the script, aiding in maintenance and readability.

For subroutines that are called frequently or generate a significant number of temporary variables, developers might consider implementing a cleanup routine or a section at the end of the subroutine dedicated to variable management. This routine can systematically remove or reset all temporary variables, ensuring that the global scope remains clean and that the application’s performance is not adversely affected.

Incorporating these practices into the development workflow not only helps in managing the variable scope effectively but also contributes to a more structured, understandable, and maintainable Qlik script. By being mindful of the scope and lifecycle of variables, developers can mitigate risks associated with global variable pollution and ensure that their Qlik applications remain robust and efficient.

That’s it for this week. See you next Friday?

Take your Qlik skills to the next level!

Since 2013, the Masters Summit for Qlik is the premier advanced training for Qlik. Join us in Vienna and take your Qlik skills to the next level.

Join the team!

Do you want to work within a highly-skilled, informal team where craftsmanship, ingenuity, knowledge sharing and personal development are valued and encouraged? Check out our job openings.

Friday Qlik Test Prep Script Solution

Hoe kunnen we je ondersteunen?

Barry beschikt over meer dan 20 jaar ervaring als architect, developer, trainer en auteur op het gebied van Data & Analytics. Hij is bereid om je te helpen met al je vragen.