Refactor ILE RPG code

Refactoring is the process of changing code without changing the function of the code. The purpose of refactoring is to make the code more readable and maintainable.

Rename declarations

Renaming declarations is one form of refactoring. To rename declarations reliably, use the Refactor > Rename action as an alternative to simple find and replace actions that may produce incorrect results.

About this task

To rename a declaration, select a declaration or any reference to it, then right-click and select Refactor > Rename. Follow the steps of the Rename wizard.

For an in-depth example, the following information demonstrates how to use the Refactor > Rename action by taking you step by step through refactoring a source member that can benefit from renaming due to inconsistent spelling of a declaration.

Procedure

  1. Open the source member that you want to refactor. The following example shows a source member with a declaration called name that is selected on line 8. Throughout the source member, this instance of name is spelled with several different casings that would benefit from renaming so that a consistent spelling can by applied. As well, there are additional declarations called name within another dcl-ds and a dcl-proc that will remain unaffected by renaming because they are within different scopes and are not related to the declaration that was selected.
    Before refactoring
  2. Select the declaration to rename or any reference to it and access the Refactor > Rename action. You can select a declaration or reference by clicking anywhere within it. To begin the rename process, right-click and select Refactor > Rename.
    Select declaration and choose Refactor / Rename action

    The Rename dialog opens.

    Rename dialog before change

  3. In the Rename dialog, enter a new name for the declaration in the New name field. In the following example, name and its various case insensitive spellings will be changed to label.
    Choose new name

    At this point you can click OK to complete the process, click Preview to preview the changes that will occur, or select Force preview and then OK to ensure that the changes are previewed. If you select Force preview, this option will become the default for the next time that you use the wizard.

  4. Preview the changes. The initial preview screen displays how many references there are to the declaration that you want to rename. If there are any problems that will be introduced by renaming, these will be displayed, as well. For example, this image shows a straight forward case in which there are 19 references related to the original declaration selected for renaming. This is displayed as an informational message.
    Review changes and informational message

    This image from a preview for a different source member shows that there will be fatal errors. If there will be fatal errors if the refactoring were to be completed, only Back and Cancel buttons are available.

    Errors displayed in preview

  5. Click the Continue button to proceed to the next preview page. Click the source member name to view the changes that will occur for the entire source member. Original Source displays the source before any changes and highlights the instances of the item that you are renaming. Refactored Source gives a preview of the what the source will be after renaming is completed. Note the instances of name on lines 12, 31, and 32; these are not affected by renaming in this case because they are unrelated to the original name declaration that is being renamed. This shows one of the advantages of refactoring compared to simple find and replace actions. A global find and replace would change these additional items because it would not know these instances were unrelated to the original item being changed. In cases like this, find and replace is much more error prone.
    Preview all changes
  6. Expand the list of previewed changes to work with changes individually. Each proposed change will be listed with a check box. Clear the check box if you do not want a specific change to be performed. Original Source displays the source for the specific instance before any changes. Refactored Source gives a preview of the specific instance after renaming is completed.
    Preview specific changes
  7. Click OK to complete renaming and close the wizard.
  8. Review the changes. After the wizard closes, the updated source is displayed in the editor for you to review. Save the new source if you want to accept the changes. If you do not want to accept the changes, close the source without saving or use the Edit > Undo function.
    After refactoring