Flash programming tip: global vs static functions

! Warning: this post hasn't been updated in over three years and so may contain out of date information.

A fairly common feature of most object-orientated (OO) languages is the concept of globally accessible functions, which are wrapped up inside classes. In order to make them globally accessible, they’ll be defined as static methods, or whatever the syntactic equivalent is in the particular OO language in question.

In AS3, such a set methods might look something like the following:

One of the problems with defining functions like this is that the syntax for accessing them is very cumbersome:

Each time we access a method in MyStringUtils, the class must be specified. This increases typing (and thus the chances of errors), makes the code look clunky and thus hinders readability. This is such a well recognised problem that Java, for example, introduced the concept of “static imports” with Java 5. This feature enables a static method – rather than its class – to be referenced in the import and thus used within the code without needing to specify the class.

ActionScript has a different – and in my opinion at least, much better – solution. Rather than requiring such global functionality to be wrapped up inside a class, AS3 allows those functions to be declared outside of any class. For example we could replace the above class with three individual source files, each of which defines a function:

initialiseIfNull.as

isEmptyString.as

stripWhitespace.as

As a result of defining the functions this way, we can greatly improve the readability of the code:

Readability is not the only benefit though. There are other benefits:

Smaller SWFs
As each function is self-contained, only those functions used end up in the SWF, rather than the whole class in the case of static methods.

Better functional behaviour
One of the biggest weaknesses of OO is persistent state. It is all too easy to have many static methods in a class that share one or more static variables. Calling one method can then affect how subsequent calls to other methods behave. This problem with OO is one of the strong points of functional principles as it is based on self-contained functions that do not therefore persist state. Likewise with AS3, by breaking the functions out into individual files, it becomes much more difficult to have shared state between them.

Conclusion
In order to write good code, globally accessible functions should be used with care. I follow a couple of rules for what is permissible in a globally accessible function, that both aids testing and makes tracking program flow easier:

  • Parameters must be treated as immutable. The function must not modify the contents of parameters if those changes will persist after the function ends. So, by example, assigning a new value to a string parameter is OK, but adding an element to an array parameter is not.
  • The function must have no reliance on state and must have no side effects. The only values it may reference are those of the parameters and the only value they may change is the return value.

Global functions do not help with the former, but they help hugely with the second point.

I’d not necessarily suggest switching all static methods to global functions, and I’d recommend reading a piece by Zwetan Kjukov on the subject with an interesting discussion in the comments. I do believe though that global variables can play an important role in helping create clean, well structured, code.

3 thoughts on “Flash programming tip: global vs static functions

  1. It is unfortunate matter of life, that you cannot use custom namespaces with global functions.

    http://pastebin.com/aTYpnXxr – this declaration of custom namespace with global function returns 1116: A user-defined namespace attribute can only be used at the top level of a class definition stripWhitespace.as

  2. @John Lindquist,
    great link. Thanks.

    @JabbyPanda,
    this is true. Global functions, like interfaces and classes, have to use either the public or internal namespaces.

    However, they can by specified via their fully qualified package name. So if you have two functions called foo, one on utils.strings and one in utils.numbers, they can be referenced as utils.strings.foo() and utils.numbers.foo() in the same piece of code.

Comments are closed.