Showcase and discover digital art at yex

Follow Design Stacks

Subscribe to our free newsletter to get all our latest tutorials and articles delivered directly to your inbox!

Reusing code

Reusing code

Smart developers minimize the amount of code they need to write by reusing existing code—whether written by you, your organization, or somebody else altogether.

Try third-party libraries and components

Many useful ActionScript libraries and components are available on the Internet. RbkCustom uses third-party libraries to assist with cryptography, drawing, data formatting, and XML parsing. Readily available libraries and components can also assist with a multitude of other tasks, including data loading, data compression, debugging, server communication, testing, and much more.

Carefully read the license for any third-party software before you use it to be sure the license is compatible with your intended usage. Even free open-source software sometimes carries distribution restrictions, especially for commercial usage.

If you can legally use the code you have found, you should critically evaluate its fitness. You can gain valuable insight into the software by browsing online forums, especially support forums. In our own case, we scan the source code to assure ourselves of its quality.

Although the Internet is loaded with reusable ActionScript code and components, finding the right nugget at the right time can be a challenge. If a simple Google search does not succeed, you can try my two favorite backup resources. The Flashcoders mailing list generates a mountain of ActionScript e-mail. Be sure to set up an automated filter for all that incoming Flashcoders e-mail, and learn how to search the contents of those e-mails efficiently. KartOO is a graphical search engine that suggests and navigates to related keywords and efficiently shows inline document summaries.

Encourage internal code reuse

There are two aspects to effectively reusing code you have written. The code must be configurable enough to be used in a variety of contexts. Hard-coded values should be replaced by parameterized settings. Thorough testing and documentation are mandatory.

The developer of new code also needs to know that existing code is available for reuse. There needs to be some type of repository of code. The larger the organization, the more formal and better organized this repository needs to be. In addition to finding available code, the interested developer needs to be able to learn how to use the code from the documentation.

Fluid has written a command pattern implementation that we have reused within various applications. This implementation is included with this article as a downloadable code sample. The core reusable code supports asynchronous and composite commands as well as simple synchronous commands. This set of classes and interfaces allows us easily to add command implementations to various applications in a simple, consistent, and extensible way.

To implement a synchronous command in an application, for example, we create a class that implements the ICommand interface by defining an execute() function that performs the specific task.

To implement an asynchronous command, we create a class that extends the AbstractAsynchronousCommand class and implements the IConcreteCommand interface. The IConceteCommand interface defines the doExecute() function that the execute() function implementation calls in the AbstractAsynchronousCommand class. Our new class implements doExecute() to perform the necessary asynchronous task, at the conclusion of which it calls the notifyComplete() function.

You can assemble both synchronous and asynchronous commands together into groups of composite commands that run as a unit in sequential order. This ability to aggregate groups of commands allows us to flexibly reuse commands themselves, as well as the command pattern framework.