Organize Your ActionScript:Six Strategies for Healthier Flash Code
As Flash applications move from media to mainstream, it's no longer enough to write code that just works. More prosaic concerns such as readability and maintenance become increasingly important as Flash developers begin to target full-size, mission-critical applications.
by Eric Jansson
Aug 13, 2003
Page 1 of 3
e are witnessing increasingly ambitious projects software in Flash. A quick look through the Macromedia Showcase for Flash proves that Flash is no longer just for animations or for dazzling wandering Web visitors. Rather, Flash has matured and is now being used for a host of front-end tasks, such as chat and IM clients, interactive training applications, wireless and PDA applications, and interfaces to commercial applications. Where HTML falters, Flash takes over.
Along with Flash's newfound importance as a development technology comes a responsibility to maintain Flash-based applications, some which may be mission-critical. Unfortunately, it is considerably more difficult to write clean and maintainable ActionScript than it is to write code in other programming languages. ActionScript quickly turns into spaghetti code that only the original author understands; therefore, trying to understand a complex Flash movie written by another programmer can be a daunting task. Why does ActionScript get out of hand so quickly? Here are some of the most important reasons:
Code gets "lost." ActionScript can be attached to so many different places in Flash movies (buttons, frames, movie clips) that it often gets lost in some dark, forgotten corner of the movie.
Editing tools are poor. Flash's built-in ActionScript editor is poor. It has no syntax highlighting, poor searching features, and a single window for code editing, among other things. As a result, ActionScript code is more difficult to read, follow, andmost importantlyrefactor.
Object-oriented programming is not widely used. OO is not yet a common standard for Flash authoring. As a result, longer ActionScripts tend to become a maze of functions where the main logic is difficult to follow.
ActionScript Movie Clips are new to most programmers. Movie Clips are analogous to variables and objects from other programming languages, but not exactly like them. Most developers aren't sure how to handle them, and so tend to do so inconsistently.
Six Simple Strategies
In the rest of this article, I'll present six simple strategies that can help you write readable and maintainable ActionScript that will keep future programmers from groaning over changes or new feature requests to the Flash applications you're building today.