Table of Contents

10.14 Code Reusability

When using event handlers and listeners, don't forget the code-centralization principles discussed in Chapter 9. Avoid unnecessary duplication and intermingling of code across movie elements. If you find yourself entering the same code in more than one event handler's body, you should try defining it in a single function and pointing each event handler to that function. Try generalizing your code, pulling it off the object and placing it in a code repository somewhere in your movie; global code used throughout a movie should be attached to the _global object.

In almost all cases, it's a poor idea to hide statements inside an on( ) or onClipEvent( ) handler. Remember that encapsulating your code in a function makes your code reusable and easy to find. This is particularly true of buttons—I rarely place any code on a button in Flash MX, and I rarely use more than a single function-invocation statement in a Flash 5 on( ) handler. For movie clips, you'll need to employ keener judgment, as placing code directly on clips can often be a healthy part of a clean, self-contained code architecture. Experiment with different approaches until you find the right balance for your needs and skill level. Regardless, it always pays to be mindful of redundancy and reusability issues.


Table of Contents