All Sites & Public Resources...
Products & Services
Data Science Platform
Personal Analytics for Facebook
Products for Education
Wolfram|Alpha for Mobile
Course Assistant Apps
Paid Project Support
All Products & Services »
Revolutionary knowledge-based programming language.
Central infrastructure for Wolfram's cloud products & services.
Technology-enabling science of the computational universe.
Computable Document Format
Computation-powered interactive documents.
Software engine implementing the Wolfram Language.
Wolfram Natural Language Understanding System
Knowledge-based broadly deployed natural language.
Wolfram Data Framework
Semantic framework for real-world data.
Wolfram Universal Deployment System
Instant deployment across cloud, desktop, mobile, and more.
Curated computable knowledge powering Wolfram|Alpha.
All Technologies »
Aerospace & Defense
All Solutions for Education
Web & Software
Authoring & Publishing
Finance, Statistics & Business Analysis
Financial Risk Management
Internet of Things
All Solutions »
Support & Learning
Find an Answer
Ask for Help
Post a Question
Videos & Screencasts
Conferences & Seminars
All Support & Learning »
Work with Us
A New Kind of Science
Wolfram + Raspberry Pi
Wolfram Innovator Awards
All Company »
Mark as an Answer
Need some advice on how to avoid global vars
I'm designing an app in MA to solve a fairly complicated problem.
It'll have a large data as input, in the process it'll generate quite a bit of intermediate data, at the end there will be a solution set of data and finally a report will have to be generated.
I've coded most of it already in a single unit and it is quickly becoming unmanageable and extremely hard to debug and maintain.
Because of the complexity and to ease the design I've decided to split it all up in several packages. The problem however is that I'm not sure how to best deal with the global vars. Here's a taste of what I have so far:
inputData3: a large list
inputData4: a large list too
a bunch of small parameters that may or may not be set by the user, like par1->whatever1, par2->whatever2....
I'm thinking of having the following packages:
inputPackages: routines to populate the various input lists from different file formats etc
dataPackage: data handling routines, once the input lists are generated - like add element, remove element, find element etc
dataCrunch: core package to solve the problem, generates solutionList, interList1, interList2.....+others
supportPackage: varous more generic data crunching routines that can live in a separate unit
reportsPackage: routines to generate final user friendly reports from the solution data
plotPackage: routines to generate fancy plots
Now - obviously most of the units will need access to the original input data lists as well as the intermediate lists.
Any ideas on how to organize this in order to minimize the use of global vars?
Can global vars be avoided altogether in this case and if yes - how?
Thanks in advance!
Global variables are one of the first things you learn to reduce when learning good programming practices. They cause a number of problems. They can make debugging harder because any section of the program's behavior may depend on the global value. It is also hard to tell what part of the program might make any use of the variable. Any variable's scope should be confined to the context in which it is used and not be accesible by parts of the program that shouldn't be using it.
Can global variables be avoided altogether? Yes, essentially. There is a programming paradigms called functional programing where global variables can't change value once they are defined. Learning different programming paradigms like this is helpful in learning good coding style.
I am not sure how helpful I can really be in describing the best practices for organizing code here. Learning how to structure code takes a while and requires a fair amount of reading and practice with things like design patterns and reading real world code.
From experience talking to people new to programming with Mathematica and other similar languages, my best advice is to write as much of your code as possible using stateless, pure functions (https://en.wikipedia.org/wiki/Pure_function). That is, to write functions which take input and then produce an output which only depends on that input. These functions should not use any global variable. If they do use a global variable, make that global variable an argument to the function instead. Your program then becomes the process of taking the input and running it through these pure functions to produce the result you want to see.
Be respectful. Review our
to understand your role and responsibilities.
Data Science Platform
Volume & Site Licensing
Wolfram Language Documentation
Connected Devices Project
Wolfram Data Drop
Wolfram + Raspberry Pi
© 2015 Wolfram. All rights reserved.
Learn how »