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

Planning & Scoping Your Audit
15 January 2018

Planning & Scoping Your Audit

With Data Visualization

Data visualization is changing the way auditors are communicating with their audiences. Easy planning, find risk areas and prioritize your scope of work.

Read More

Infusing IDEA into Your Organization
16 November 2017

Infusing IDEA into Your Organization

Building a successful and sustainable data analytics program requires a mix of people, processes and products...it all starts with collaboration.

Read More

Using Nested @Functions()
16 November 2017

Using Nested @Functions()

IDEA Tech Tip

Combining @functions in creative ways can open a wealth of new capabilities that you may not have known your IDEA software is capable of.

Read More

IDEA 10.3 Now Available
15 November 2017

IDEA 10.3 Now Available

CaseWare Analytics Launches Update

Newest release of CaseWare IDEA introduces key features and enhancements to help auditors, accounts and other finance professionals improve their audits.

Read More

Innovation Starts with IDEA
14 November 2017

Innovation Starts with IDEA

Wrap Up - Part 1

More than 100 IDEA users gathered in Houston on November 1-2 to learn new strategies for implementing and growing their use of data analytics.

Read More

Expert Data Importing Services Available
17 October 2017

Expert Data Importing Services Available

Our goal is to eliminate the inherent time and frustration of importing challenging files so professionals can focus on more value-add tasks.

Read More

Fraud: An Ounce of Prevention...
17 October 2017

Fraud: An Ounce of Prevention...

When it comes to fraud, an ounce of prevention is worth a pound of cure. The full impact of fraud can be devastating, well beyond the financial loss.

Read More

GreenSky Loan Servicing Company Reduces Monthly Statement Review from Days to Minutes with IDEA
17 October 2017

GreenSky Loan Servicing Company Reduces Monthly Statement Review from Days to Minutes with IDEA

Auditors are able to access and analyze raw data with IDEA, which has significantly reduced their need to wait on IT for reports.

Read More

Take a Lean Six Sigma Approach for Audits
19 September 2017

Take a Lean Six Sigma Approach for Audits

Henry Ford once said that if he had asked people what they wanted, they would have said faster horses. Constant innovation keeps businesses thriving.

Read More

Agile Analysis Techniques for the GL
19 September 2017

Agile Analysis Techniques for the GL

Shortcuts to find anomalies and outliers in the general ledger - speed up your analysis with the Financial App

Read More

RSS