Extracting Existing Logic and Creating a New Method

When editing a JADE method, you can select logic within a method and create a new method in the currently selected class with that logic.

To extract existing logic and create a new method

  1. In the editor pane, select the logic to be extracted.

  2. Right-click and then select the Extract Method command from the Refactor submenu of the Edit menu. If the start of the selection includes a whole expression, the Refactor Extract Method for Class class-name dialog is then displayed. If it does not include a whole expression, an error message is displayed.

    The Generated Method editor pane contains the source of the new method, consisting of the parameter signature determined by the analysis of the selected logic, the method template expansion if you have a template defined in your text templates preferences, and the selected logic, as shown in the following image.

    The status line initially displays:

    Replace newMethod with your method Name in the method source
  3. The Enter and Esc keys initially control the dialog cancel and commit functions.

    • When the dialog is first displayed, a transparent text box containing the text newMethod overlays the method name in the Generated Method editor pane, and it has focus. This text box, which has a maximum length of 100 characters, must have a lowercase first character.

    • As you change the method name, the text box is updated and resized to fit, and the actual method source, template entry (if present), and method call text in the Generated Method Call editor pane are all adjusted.

    • The method name text is shown in red, which indicates that you can use the Esc and Enter keys to cancel the dialog or to perform the add method action, respectively.

    • You can use the right and down arrow keys, the Tab key, and the mouse to move to the Generated Method editor pane itself.

    • The text box is hidden as soon as it loses focus, the method name is displayed in its normal color in the editor pane, and the Esc and Enter keys no longer cancel or commit the dialog, because they are now editor‑related.

    • You can still change the method name in the editor pane before you click the Add Method button.

    If you change the signature of the method, the text of the call that will replace the original selected text in the source method is not changed. You should also change this text in the editor pane of the Class Browser to meet your requirements.

    If the text prior to the original text that you selected is a replacement statement and you selected one statement only, the return type of the method is set to the replacement type; for example, if the original method has the statement int := (a * b + c); and you select (a * b + c), the result is a method return type of Integer.

  4. In the Schema combo box, select the schema in which the method is to be created. This combo box, in which the current schema is selected by default, lists all of the schemas in which the method could be created.

  5. If you want to ensure that self. prefixes the method call, check the Include ‘self.’ in method call check box. If self. exists in the method call, uncheck this check box to remove self. from the method call (the check box is checked if self. exists in the method call).

    The value of the Include ‘self.’ in method call check box is set automatically when the method name in the generated method is changed, based on whether self. prefixes the method call text.

  6. Click the Add Method button, to add the new method and to replace the selected source with a call to the new method. Alternatively, click the Cancel button to cancel the current action and return focus to the original editor pane in the Class Browser.

JADE detects local variables used only in the selected logic of methods to be extracted. When the refactored method is created, they are defined as local variables in the new method rather than being passed as io parameters in the signature of the created method. (The definition of local variables is not affected in the original method.) If a variable is used in other logic not selected in the original method to be extracted, it is passed as an io parameter to the new method that is created.

When you extract a method for refactoring, a protected method is created if you have selected the Protected access option button in the Methods group box on the Schema sheet of the Preferences dialog. In the editor pane, manually remove the protected option from the signature of the created method if it is not required.

If the calling method text is changed to self.method-name, the change is accepted, provided that the method-name in the call matches the generated method name when you click the Add Method button.

When you click the Add Method button, the new method is added to the currently selected class in the selected schema, using the method name specified in the Generated Method edit pane, and the selected content of the original source method is replaced with the contents of the Generated Method Call editor pane. This last change is not committed, so you can undo it by using the Ctrl+Z shortcut keys.

If the new method compiles successfully, the Refactor Extract Method for Class class-name dialog is closed and focus returns to the Class Browser editor pane of the original method.

If the new method fails to compile:

The following is an example of a code fragment selected in the original source (Generated Method) editor pane.

d := (a * b + c);

The following example is the generated method in which the whole line is selected (where doCalc has been specified as the method name).

doCalc(d: Integer io; a: Integer; b: Integer; c: Integer);
vars
begin
    d := (a * b + c);
end;

The original statement then becomes:

doCalc(d, a, b, c);

If (a * b + c) were selected, the method would be:

doCalc(a: Integer; b: Integer; c: Integer): Integer;
vars
begin
    return (a * b + c);
end;