Global Variables in Swift

Stop! Stop! Stop! WRONG. BAD.

Photo by Fernando @cferdo on Unsplash

Prerequisites:

  • Coding in Swift Playgrounds (guide HERE) or create a single view application in Xcode (guide HERE)

The example

There is a counter variable in your app (perhaps in your AppDelegate) which can then be updated from anywhere in your App (perhaps when a user presses a button).

The case for: Globals are a fast, easy to use technique

I would like to present a case for using global functions and variables. I’ll pick some reasons for their use, and explain this below:

Make small programs easy!

When we are making small demo programs, we might want to get coding and get the solution out in the world as fast as possible. We don’t want to get bogged down in arguments about architecture, rather we just want to get the code moving and make it work!

Somethings are available everywhere

You might want to set up a constant with a URL, and this is available in many parts of the progam. It doesn’t change so what is the harm?

Wrapping globals as Singletons isn’t the way to go

If you are spending time and effort in wrapping a global as a Singleton…what’s the point. Using a different tool and calling it something else to cover bad design of an App really doesn’t help anybody. If you’re using a global, use a global and be proud of what you’ve done!

Globals can save keystrokes

In order to type faster you can use Globals, because you can use them anywhere an use really small names.

The thought problem

To avoid having to think about where data should go, you can simply use a global. No thought required. Job done. Simples!

The case against: Globals are evil

The problem is that accessing a variable from anywhere in your application generates problems. Some cherry-picked examples of this are shown below

Thread safety

What if one counter is updated from multiple positions in your application of course this example is rather basic, more likely you would make requests from different threads which would reach the variable at (possibly) the same time.

Coupling and Testing

Since we just have a simple variable (as suggested in App Delegate) it is difficult to claim that this is tightly coupled with anything (although global variables and functions often are).

Even for small programs, it’s worth doing things right

Although it may seem persuasive to claim that it doesn’t matter whether we use globals for small programs, it is often those small programs that are the basis of larger Apps and programs. They can be worked upon and built as the very foundation of our work.

Alternatives

Passing data along view controllers

You might choose to pass data from one view controller to another as an alternative to a global variable. Such a thing is called delegation, and does present the opportunity to use data where we need it.

The Singleton option

Essentially a class is wrapped around the global variable, and is known as a design pattern.

Class SingletonClass {
static let sharedInstance = SingletonClass
private init() {}
}

Conclusion

Stop and think about what you’re doing. Should you really be using a global variable for your particular use? You’re the only one who can decide — when programming you need to consider your current context and produce your software to the best of your ability. In other words, the choice is yours.

The Twitter contact:

Any questions? You can get in touch with me HERE

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store