Better Business Through Data Analysis & Monitoring My Account
Maximizing Your Environment - Global Variables

Maximizing Your Environment - Global Variables

Automation has been a powerful capability of IDEA from the beginning. Its main goal is to ease the auditing process, but its true value can only be fully appreciated when leveraged properly. A solution is only as good as its implementation and in this case proper implementation will not only facilitate the steps of an analysis, but dramatically cut down the time required to perform the analysis; thereby creating opportunities for greater productivity.

For many tasks, IDEA’s Visual Script component will help. It’s a simpler, more direct way of creating scripted tasks that works with symbols rather than code. For more advanced tasks, it simply lacks too many features and full IDEAScript must be used. IDEA now offers two varieties of code (the original VBA and the newly added Python), but in either case the user must be comfortable with the environment to make the most of it. Our objective is to provide topics that enables you to achieve that level of comfortability in usage of your environment, as we all have different legacy systems, with similar areas.

This post focuses on the concept of global variables. Scope is an important programming concept that some people require a hand in grasping. It’s possible “global variables” and “scope” may have had no meaning to you just now. That’s perfectly fine as these guides will cover the topics from the ground up. To begin, consider the following short VBA script:

Sub Main

Dim user As String

Dim department As String

Dim salary As Double

user = InputBox("Enter the current user's name: ")

department = InputBox("Enter " + user + "'s department: ")

 

'Other data gathering logic

'...

'...

'...

If department = "Department 1" Then

salary = 30000

ElseIf department = "Department 2" Then

salary = 40000

End If

'Other processing logic

'...

'...

'...

MsgBox(user + " earns " + Str(salary))

End Sub

An undeniably useless script, yes, but recall this is purely to explain the concept of scope. Given what a full user database would encompass (format checking, writing/reading files, error handling, etc.), it’s easy to imagine the script will become very large very quickly. Even in this short example, the beginnings of clutter can be seen as it’s all packed into a single method. Very quick aside: Those of you already at least somewhat versed in scripting will likely point out that methods can be created to split up key tasks and cut down on clutter. To those users, global variables can cut down on the complexity of method signatures and decrease the Main method size (the entry point of the script), further by removing some (if not all, depending on the project) variables entirely.

A big part of creating (and then later, interpreting) scripts is readability. The hairier a script gets, the easier it is to induce a headache trying to piece it all together. Consider the following revised example:

Dim user As String

Dim department As String

Dim salary As Double

Sub Main

Call GetUserData

Call ProcessUserData

Call PrintUserData

End Sub

Sub GetUserData

user = InputBox("Enter the current user's name: ")

department = InputBox("Enter " + user + "'s department: ")

'Other data gathering logic

'...

'...

'...

End Sub

Sub ProcessUserData

Dim benefits() As String

If department = "Department 1" Then

salary = 30000

ElseIf department = "Department 2" Then

salary = 40000

End If

'Other data gathering logic

'...

'...

'...

End Sub

Sub PrintUserData

MsgBox(user + " earns " + Str(salary))

End Sub

Right away, the Main method is noted to be very clean. It clearly lists the tasks that are being performed and the logic of those tasks is handled individually within their respective methods. This is an example of scope.

Consider scope to be a building in which people work. The tools required to accomplish their tasks will of course be housed in that building, but in scripting it’s important to determine which tools need to be shared among the buildings. In the scope (building) of ProcessUserData, it of course needs access to the user data (tools). It may have a few of its own unique tools, as can be seen by the “benefits” array declaration, but this is because this is a tool that only ProcessUserData will use. No other method needs to even know it exists. That is what determines where variables are laid out. For data that everyone needs to be able to access, it cannot be declared in any one building since it would mean that building owns the data. See how the original variable declarations were moved from Main to the very top of the script? Being at the very top is convention. It makes it easy to find all the variables by keeping them together as the first thing someone sees, but realistically as long as a variable is declared outside any and all methods it is considered to be global; of course meaning it can be globally accessed by all the methods. Notice the methods did not repeat any of the global variable declarations. ProcessUserData could begin using “department” right away because it was within the global scope. Since “benefits” is declared only within ProcessUserData, attempted access from any other method will cause an error because it is not defined in scope.

VBA Quirk: Unless the “Option Explicit” directive is given at the top of the script, scoping will be very lax. Attempted use of a variable that is not in scope will simply create a new variable and scope it to the method it was used in. For example, trying to use “benefits” in Main will not access the “benefits” from ProcessUserData. It will generate a new variable called “benefits” that only works inside of Main. Most programming languages do not have this quirk, so it can be confusing for beginners. Other programming languages will specifically warn you that the variable cannot be found, thereby verifying you’re accessing what you meant to access. Be mindful that unless it is a global variable (properly declared outside all methods), using a variable across methods is not accessing the same block(s) in memory, regardless of whether the compiler flags it as an error or not.

Global variables are handy but they are certainly not a kind of “best practice.” Global variables are merely a technique at your disposal. Being aware of how scoping works means applying the right techniques for the job. Sometimes global variables are best. Sometimes values should be passed in method signatures. Sometimes those passed values should be returned or referenced directly for multiple returns (which carries its own debate in the developer community, but that is outside the scope of this blog post). Complex tasks will likely see data handled in all sorts of ways. There is no need to force a script to conform to any one way of handling data.

Print

Number of views (947)/Comments (0)

Danny Betancourt
Danny Betancourt

Danny Betancourt

Solutions Specialist, Audimation Services

Other posts by Danny Betancourt
Contact author Full biography

Full biography

Danny Betancourt graduated with a bachelor's degree in Computer Information Systems with a concentration in Web Development. His programming education centered mostly on C# but he has experience, both academic and personal, in C++, VB .Net, and Java. He used his educational background to make changes in various places to Audimation Service's website at the start of his career. Specifically within the Solutions Development team, he has focused on making data more presentable to our clients through our Caseware Monitor implementations as well as brought his knowledge of C# to help add products to IDEA's online Marketplace. The Marketplace endeavors entailed taking scripts developed in-house (known originally as Audimated Apps), such as Excel Exporter, and revamping them to integrate conveniently into IDEA’s Smart Analyzer platform. This in itself contains many aspects but one of the most apparent new features to the eye is the inclusion of the improved User Interfaces. Outside of the office, Danny enjoys programming for fun and has a passion for languages that is seen in the apps and programs he develops in his spare time. His second native language is Spanish and he uses his varied background to expand his worldview and casting a wider net onto the learning resources available.

x

Please login or register to post comments.

Name:
Email:
Subject:
Message:
x
Global Chemical Company Automates Risks Identification with CaseWare IDEA
14 September 2018

Global Chemical Company Automates Risks Identification with CaseWare IDEA

Global corporation used IDEA to standardize & develop an automated process to comply with anti-corruption policy.

Read More

Telecommunications Provider Turns Two Days of Work Into 20 Minutes with CaseWare IDEA®
14 September 2018

Telecommunications Provider Turns Two Days of Work Into 20 Minutes with CaseWare IDEA®

Auditors Analyzing Hundreds of Millions of Records to Track Buying and Usage Patterns

One of the world’s largest telecom companies' internal audit team leverages tech to analyze trends, tendencies & patterns within customer & financial data.

Read More

Exploring IDEA's Built-In Power of Python
20 August 2018

Exploring IDEA's Built-In Power of Python

IDEA Tech Tip

With the release of version 10.3, IDEA created a buzz with its implementation of the Python scripting language, thereby taking a huge step towards having a more robust coding environment.

Read More

Sharing IDEA Data Using IDEA's ODBC Driver
18 July 2018

Sharing IDEA Data Using IDEA's ODBC Driver

IDEA Tech Tip

Read More

Oncor Achieves Operational Efficiencies with CaseWare IDEA
25 June 2018

Oncor Achieves Operational Efficiencies with CaseWare IDEA

Oncor's internal audit team needed to acquire years of data from different environments and formats to help improve processes and efficiency levels.

Read More

Analyzing the Past to Predict the Future with Trend Analysis
18 June 2018

Analyzing the Past to Predict the Future with Trend Analysis

In business, predicting the future is never an easy task. But IDEA can help as long as you have the right data, and Trend Analysis can help.

Read More

2018 IDEA® User of Excellence Award Winner Selected
15 June 2018

2018 IDEA® User of Excellence Award Winner Selected

Jennifer Girard, CIDA with American Red Cross Recognized for Exceptional Use of Data Analytics

Jennifer Girard, Sr Auditor with the American Red Cross is the recipient of the 2018 US IDEA UoE Award. She utilized IDEA to assist Finance Department during a natural disaster.

Read More

The Rules of Nature and Audit
6 June 2018

The Rules of Nature and Audit

What’s missing is often as important as what’s present. The rules of nature apply very well to audit if you consider, “Everyone knows it, but do you test for it?”

Read More

Using IDEA to Import QuickBooks General Ledger Data
4 June 2018

Using IDEA to Import QuickBooks General Ledger Data

The General Ledger is typically the starting point to an expanse of detailed information, see a step-by-step guide on how to get a readable version in idea.

Read More

New Features in IDEA 10.3 Part II
10 May 2018

New Features in IDEA 10.3 Part II

IDEA Tech Tip

In a recent article, we introduced the improvements to Stratified Random Samples and Python integration added in IDEA 10.3. But that’s just a small part...

Read More

Cargo Company Swaps Programming-Intensive Data Analytics Software for CaseWare IDEA
10 May 2018

Cargo Company Swaps Programming-Intensive Data Analytics Software for CaseWare IDEA

Auditors Achieve More Success in First Year with IDEA Than 5+ Years with Other Tool

With the entire audit team up and running using data analytics, they are finding new ways to integrate IDEA into other areas of the business and automate repeatable processes.

Read More

Maximizing Your Environment - Global Variables
8 May 2018

Maximizing Your Environment - Global Variables

Consider scope to be a building in which people work. The tools required to accomplish their tasks will of course be housed in that building, but in scripting it’s important to determine which tools need to be shared among the buildings.

Read More

Tips on Acquiring Data
8 May 2018

Tips on Acquiring Data

An IDEA User Shares Frequent Practices

When working with rows and columns of data to spot suspicious behaviors, fraud and errors can look very similar to one another. While Yeriazarian often uses Excel as an interim format for data, he also understands there are several avenues to navigate before getting the right data.

Read More

Identifying Churn in AR
19 April 2018

Identifying Churn in AR

IDEA Tech Tip

Customer churn (customer attrition, turnover, or defection) is when a customer decides to stop engagement with your company. IDEA can help easily identify “churn” in accounts receivable by following these steps...

Read More

RSS