home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Book HomeActionScript: The Definitive GuideSearch this book

10.15. Code Reusability

When using button events and movie clip events, don't forget the code-centralization principles we learned in Chapter 9, "Functions". Always try to prevent 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, it may not be wise to attach that code directly to the object. Try generalizing your code, pulling it off the object and placing it in a code repository somewhere in your movie; often the best place is the main timeline.

In many cases, it's a poor idea to hide statements inside a button or clip handler. Remember that encapsulating your code in a function and calling that function from your handler makes your code reusable and easy to find. This is particularly true of buttons -- I rarely place anything more than a function-invocation statement directly on a button. 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.

For an example of the difference between attaching code to buttons versus calling functions from buttons, see Section 9.12, "Centralizing Code" in Chapter 9, "Functions".



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.