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 (633)/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
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

Jeremy Clopton Joins Integration Partnership Program to Help Professionals State & Sustain Their Use of Data Analytics
18 April 2018

Jeremy Clopton Joins Integration Partnership Program to Help Professionals State & Sustain Their Use of Data Analytics

Data analytics & fraud detection expert Jeremy Clopton joins the Audimation Integration Partnership program to help organizations leverage data analytics.

Read More

Think Like a Thief to Catch One
11 April 2018

Think Like a Thief to Catch One

Apply Logic to Detect Peculiar Behavior

When working with rows & columns of data to spot suspicious behaviors, fraud & errors can look very similar to one another. Tips on sniffing out fraud.

Read More

5 Elements of a Successful Data Analytics Program
12 March 2018

5 Elements of a Successful Data Analytics Program

Building a successful, sustainable data analytics program takes effort…and a few other elements.

Read More

The Power of Field Statistics
7 March 2018

The Power of Field Statistics

IDEA Tech Tip

IDEA’s Field Statistics can save you a wealth of time. It should be the first thing you go to after importing a file. Why?

Read More

Using Data Analytics to Prepare for Fieldwork
20 February 2018

Using Data Analytics to Prepare for Fieldwork

Automating High-Risk Transactions with IDEA

See how a global corporation used IDEA to develop an automated process to prepare auditors for on-site work.

Read More

People. Processes. Tools.
5 February 2018

People. Processes. Tools.

Successful Integration of Data Analytics

Expert IDEA users share their experiences and advice for building and sustaining a successful data analytics program.

Read More

Planning & Scoping Your Audit with Data Visualization
1 February 2018

Planning & Scoping Your Audit with Data Visualization

Data-Driven Insights Save Hours of Effort

Here are some ways IDEA can help you plan and scope an audit using the data visualization features within IDEA.

Read More

Classroom Collaboration at University of Missouri - St. Louis
30 January 2018

Classroom Collaboration at University of Missouri - St. Louis

Student Learn to Develop Analytic Tests

Masters students attending the University of Missouri - St. Louis share their results using data analytics to assess risk, test 100% of the data population.

Read More

Do One Thing Every Day that Scares You
30 January 2018

Do One Thing Every Day that Scares You

Virginia Tech Students Gain Confidence Using Data Analytics

Learn how Associate Professor of Practice Nadia Rogers, CPA, introduced students to IDEA, including types of audit tests that can be performed within the software and various documentation aspects of an audit engagement.

Read More

New Features in IDEA 10.3
29 January 2018

New Features in IDEA 10.3

IDEA now runs faster and offers more flexibility than ever before

The latest version of IDEA introduced a host of new features and improvements.  IDEA now runs faster and offers more flexibility than ever before.

Read More

5 Ways Data Analytics Saves Time
26 January 2018

5 Ways Data Analytics Saves Time

Turn Hours of Work into Minutes

5 time-saving ways data analytics can help you shrink your audit time, while improving the quality of your work.

Read More

Tired of Wrangling Cumbersome Client Data?
24 January 2018

Tired of Wrangling Cumbersome Client Data?

10 Ways Our Importing Services Make it Easy

10 reasons to try our data importing services, which save you time, effort and undue frustration. 

Read More

A Journey in Using Data Analytics
24 January 2018

A Journey in Using Data Analytics

Testimonial from Brian Cullum

Read how Brian Cullum used IDEA to help identify a multi-million-dollar fraud based on the overstatement of the borrower’s lending base.

Read More

RSS