Recipe 1.14. Generalizing a Method to Enhance Reusability


Problem

You want to perform slight variations of an action without having to duplicate multiple lines of code to accommodate minor differences.

Solution

Add parameters to your method to make it flexible enough to perform slightly different actions when invoked rather than performing exactly the same action or producing the same result each time.

Define the parameters that account for the variability in what you want the method to do:

private function average (a:Number, b:Number, c:Number):void {   trace("The average is " + (c + b + c)/3); }

If you don't know the exact number of parameters the method will receive, use the built-in arguments array to handle a variable number of parameters.

Discussion

A method that doesn't accept parameters generally does exactly the same result each time it is invoked. However, you will often need to perform almost exactly the same actions as an existing method, but with minor variations. Duplicating the entire method and then making minor changes to the second version is a bad idea in most cases. Usually, it makes your code harder to maintain and understand. More importantly, you'll usually find that you need not only two variations but many variations of the method. It can be unnecessarily difficult to maintain five or six variations of what should ideally be wrapped into a single method. The trick is to create a single method that can accept different values to operate on.

For example, let's say you have an average( ) method for averaging a set of numbers. Instead of having it always average the same two numbers, you want to specify arbitrary values to be averaged each time it is invoked. This can be accomplished with parameters.

The most common way to work with parameters is to list them within the parentheses in the method declaration. The parameter names should be separated by commas, and when you invoke the method you should pass it a comma-delimited list of arguments that corresponds to the parameters it expects.

The terms "parameters" and "arguments" are often used interchangeably to refer to the variables defined in the method declaration or the values that are passed to a method when it is invoked.


The following is a simple example of a method declaration using parameters:

// Define the function such that it expects two parameters: a and b. private function average(a:Number, b:Number):Number {   return (a + b)/2; }

Now here's a method invocation in which arguments are passed during the method call:

// When you invoke the function, pass it two arguments, such as  // 5 and 11, which correspond to the a and b parameters.  var averageValue:Number = average(5, 11);

In most situations it is best to declare the parameters that the method should expect. However, there are some scenarios in which the number of parameters is unknown. For example, if you want the average( ) method to average any number of values, you can use the built-in arguments array that is available within any function's body. All the parameters that are passed to a function are automatically placed into that function's arguments array.

// There is no need to specify the parameters to accept when using the  // arguments array. private function average(  ):Number {   var sum:Number = 0;   // Loop through each of the elements of the arguments array, and    // add that value to sum.   for (var i:int = 0; i < arguments.length; i++) {     sum += arguments[i];   }   // Then divide by the total number of arguments   return sum/arguments.length; } // You can invoke average(  ) with any number of parameters.  var average:Number = average (1, 2, 5, 10, 8, 20);

Technically, arguments is an object with additional properties beyond that of a basic array. However, while arguments is a special kind of array, you can still work with it in the same ways that you would a regular array.





ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net