Amethyst Help

Back to -Products -Amethyst IDE -Amethyst Help

Refactoring ActionScript

Amethyst Professional refactoring options

Amethyst Professional provides a number of refactoring options to automate the restructuring of source code.

Refactoring may usually be invoked in four ways:

1) By selecting an item from the Refactor menu
2) Using a refactoring keyboard shortcut (these are shown in the ‘Refactor’ menu)
3) By right-clicking an item in the source code and selecting ‘Refactor’ from the popup menu.
4) By changing source code and clicking the ‘SmartTag’ to start refactoring

These are the refactoring options available in Amethyst Professional:

- Rename
- Extract Method
- Encapsulate Field
- Extract Interface
- Promote Local Variable to Parameter
- Remove Parameters
- Reorder Parameters
- Move To Package

Note that the selection point in the code editor must be in a block of code which is available for refactoring. For example, if the selection point is inside a method with no parameters, an error message will be shown if you select the ‘reorder parameters’ refactoring option.

Rename

You may rename variables, methods, classes and parameters. You cannot rename standard types such as String or int.

The Rename dialog contains these items:

- New Name: Enter the new name for the selected identifier here
- Class: This shows the class of variable or the class in which the item is scoped.
- Preview reference changes: Select this to preview items that will be renamed. If not selected, renaming will be done without previewing.

Preview Changes

The Preview changes dialog appears if the ‘preview reference changes’ was selected for refactoring. This displays references to the item being renamed. These references may be in several classes and code files. You may optionally omit an entire code file or specific references by checking or unchecking the top-level branches or individual sub-branches in this dialog. Any unchecked items will be omitted from refactoring. Checked items will be refactored. Click ‘Apply’ to complete refactoring.

Extract Method

This allows you to select some lines of code fro one method and move them into their own method. When the code relies on arguments that were sent to the source method, matching arguments will be added to the new method. It is the responsibility of the programmer to ensure that the marked code will be syntactically and functionally correct when placed in its own method.

Encapsulate Field

This option will create getter and setter accessor methods for a ‘class-level’ variable. The variable will be made private and the accessor methods will be public.

For example, assume that you have this declaration:

public class MyClass {
  public var _xxx:int;
}

When the variable xxx is encapsulated, the code will be refactored as follows:

public class MyClass {

  private var _xxx:int;

     public function get xxx ():int {
        return _xxx;
  }

  public function set xxx (value:int):void {
        _xxx = value;
  }
}

It is common practice to name private variables with a preceding underscore (e.g. _xxx) in which case the accessors will be called xxx. If the variable has no preceding underscore, the digit 1 will be appended (by default) to the accessor names, e.g. (xxx1). You may edit the default name for the get/set accessors prior to applying refactoring.

- The Encapsulate Field Dialog

This dialog has the following items:

- Field name: (read only) the name of the selected variable
- Property name: the name to be given to the get/set accessors
- Update references...

  • External: References to the variable outside of the current class will be rewritten to use the new get/set accessors. References to the variable within the class will remain unchanged.
  • All: All references to the variable both outside and inside the current class will be rewritten to use the new get/set accessors.

- Preview reference changes: Select this to preview items that will be renamed. If unselected, renaming will be done immediately. See help item: Preview Changes.

Extract Interface

This allows you to create an interface based on the current class. An interface declares methods which may subsequently be written into classes which implement that interface. For example, if you use this feature with a class called GamePlay, an interface called IGamePlay would be created. The interface would include all the method declarations of the GamePlay class but none of their implementation code. The GamePlay class definition would be rewritten to extend the IGamePlay interface. The new Interface is now available to be extended by other classes, each of which is responsible for implementing the methods declared in the interface.

Promote Local variable To Parameter

This allows you to remove a local variable in a method and substitute an argument in the parameter list. Note that this only works for simple types such as int and String and the variable must be initialized at the time of declaration. Any code calling the method will do so using the value previously assigned to the variable. For example, let’s assume you have this code:

public function addNumbers( aNumber:int ):int {
  var anotherNumber:int = 100;
  return aNumber + anotherNumber;
}

public function myFunction( ):void {
  var x:int;
  x = addNumbers( 5 );
}

You highlight anotherNumber in the addNumbers() function and select ‘Promote Local variable To Parameter’. The code will be rewritten as follows:

public function addNumbers(aNumber:int, anotherNumber:int):int {
  return aNumber + anotherNumber;
}

public function myFunction( ):void {
  var x:int;
  x = addNumbers(5, 100);
}

Remove Parameters

This dialog displays the parameters of the selected method. The parameter name, type and default value (if specified in the argument list) are shown. To delete the parameter select it and click the ‘Remove’ button. Use this refactoring with care: matching arguments from any code that calls the refactored method will be removed! Click ‘Preview reference changes’ to preview (and optionally edit) the code that will be rewritten. See help item: Preview Changes.

Reorder Parameters

This dialog displays the parameters of the selected method. The parameter name, type and default value (if specified in the argument list) are shown. To change the order of one or more parameters select a parameter and click the Up and Down arrows to move it to the left or right of the list. The matching arguments in any code that calls the refactored method will be reordered to match. Note that, in accordance with ActionScript syntax, any parameters with default values (e.g. aNumber:int=10 must appear at the end of the parameter list. Click ‘Preview reference changes’ to preview (and optionally edit) the code that will be rewritten. See help item: Preview Changes.

Move To Package

This option may be selected either by right-clicking a file name in the Solution Explorer and selecting it from the menu or by editing the package name of an ActionScript class in the code editor and selecting an option from the SmartTag (the small rectangle that appears beneath the edited package name).

- SmartTag Options

  • Move package from Original Package to New Package : moves class to the renamed package
  • Move to package with preview: moves class to the renamed package but allows you to preview changes before committing them
  • Move to package: Allows you to edit the new package name and optionally preview changes before committing them

- Move Package Dialog This displays:

  • Current package name (read only)
  • New package name: Defaults to the newly edited package name (if there is one) or to the current class name otherwise. You may edit this name.
  • Location of new package: the path to the folder on disk in which the new package will reside
  • Preview reference changes: See help item: Preview Changes.

NOTE: If the new package name is the same as the current package name, the OK button is disabled. The package names must be different. If the directory containing package already exists, the class will be moved into it. If the directory does not exist, it will be created.

Bookmark and Share   Keywords:  Amethyst  help
© SapphireSteel Software 2014