Table of Contents

9.13 Centralizing Code

Probably the single most important feature of functions is their reusability—we can create reusable segments of code that can be executed from anywhere in a movie. The mark of good code is that it is modularized and centralized. Modules can be replaced without affecting other modules or calls to the updated module. Furthermore, the code is in one central place that makes it easy to find and maintain. Importantly, there aren't multiple scripts in which identical or nearly identical code is repeated. Instead, function calls are used to perform operations that are centralized in a library of generalized functions. Code modularity and centralization make it easier to reuse all or part of the code in other projects. Because functions can be executed from other movie clips, they can be stored in a central location, easing code maintenance and upgrades.

Suppose, for example, that we have three buttons in three different movie clips, all with the same purpose. Instead of triplicating our code by adding it to each button, we can put the code in a function on the main timeline and invoke it from each button as needed. This saves time, reduces the potential for errors, and makes it easy to update the behavior of all three buttons at once. Because the code is centralized, testing is more reliable, and troubleshooting is much easier. If one button works and a second doesn't, chances are that the problem is in the second button's function invocation and not in the centralized behavior.


Table of Contents