Refactoring source code

The z Systems® LPEX Editor and the COBOL Editor automatically provides various refactoring for you to work with source codes in COBOL program files.

Restriction: Refactoring is not available when you edit EBCDIC DBCS code.

Extracting code into a new copybook

You can extract contiguous COBOL statements into a new COBOL copybook.

Before you begin

Supported Editors:
  • COBOL Editor
  • z Systems LPEX Editor
Open a COBOL source file in the editor. The file must contain COBOL code that includes a Procedure Division.
Note: Extracting code into a new copybook is unsupported in files that contain preprocessor directives.

About this task

The action is enabled based on the validity and the completeness of selected text:
Validity
The action is enabled only when text is selected in the Procedure Division of the COBOL source. Furthermore, if you select a COBOL word or variable name, the action is disabled.
Completeness
The selected text contains one or more valid COBOL statements. It also means that if you select a partial statement such as an IF statement or a partial PERFORM statement, the action is disabled until you extend the selection to select the complete IF or PERFORM statement.
Limitations: In the z Systems LPEX editor, this operation cannot be undone.

Procedure

To extract code to a new copybook, complete the following steps:

  1. Select valid text and right-click.
    A menu displays.
  2. Click Refactor > Create Copybook.
    A Create Copybook wizard opens.
  3. In the Create Copybook wizard, perform the following tasks:
    1. Double-click the folder where you want to create the copybook in the Select a folder field.
    2. Select the file or partitioned data set that you want to create the copybook in. Type the new copybook name in the Name field.
    3. Click Preview.
      The preview view consists of two parts:
      • A tree view at the top that contains all the COBOL elements that are affected by the refactoring.
      • A viewer at the bottom that displays a comparison between the original source and the refactored source of the current program, or the source of the newly-created copybook.
    4. If you are in the preview window and want to return to the previous display, click Back.
    5. Click OK.
      The wizard creates the new copybook and exits, returning you to the editor window.

Extracting code into a new program file

You can extract contiguous COBOL statements into a new program file.

Before you begin

Supported Editors:
  • COBOL Editor
  • z Systems LPEX Editor
Open a COBOL file that contains a Procedure Division. You can refer to Refactor Create Program for more information.
Limitations:
  • Extracting code into a new program file is unsupported in files that contain preprocessor directives.
  • Extracting code into a new program file is unsupported for program logic involving an SQL DECLARE statement.
  • Selected REPLACE statements can only be manually transferred.
  • Avoid using Refactor > Create Program under the COBOL 6.2 conditional directives.
  • You need to specify a procedure copybook file if there are referenced paragraphs or procedures to be made "common." If there aren't any commonly referenced paragraphs or procedures from the selection, the option is not available.
  • In the z Systems LPEX editor, this operation cannot be undone.
  • Create Program is disabled in the following conditions:
    • Selection contains OPEN, READ, WRITE, and CLOSE clauses.
    • Selection contains references to file descriptors.
    • Selection contains the COBOL words, such as EXIT PROGRAM, STOP RUN, GOBACK, CANCEL.
    • Selection contains the CICS® words, such as EXEC CICS RETURN.

About this task

The action is enabled based on the validity and the completeness of selected text:
Validity
The action is enabled only when text is selected in the Procedure Division of the COBOL source. Furthermore, if you select a COBOL word or variable name, the action is disabled.
Completeness
The selected text contains one or more valid COBOL statements. It also means that if you select a partial statement such as an IF statement or a partial PERFORM statement, the action is disabled until you extend the selection to select the complete IF or PERFORM statement.
Logic
The selected texts must have a logic with procedure calls. If the extracted code is not consecutive in the current program, an error displays to explain the problem. You can continue and accept the updates. If the extracted code is valid, the COPY insertion is made in the same location in the source code.

Procedure

To extract code into a new program file, do these steps:

  1. Select valid text, right-click, and select Refactor > Create Program.
    The Create Program wizard opens.
  2. Select the options under Communication Type.
    • CALL (Static): the selected code displays with a static program name.
    • CALL (Dynamic): a CALL statement with a variable name displays. If there is no working storage in the main program, an automatically generated variable will display in the Refactored Source view under the Working Storage section of the source program. If the File section exists in the source program, the newly generated Working Storage section will execute commands from the File section. If the File section does not exist, the newly generated Working Storage section will display at the top of the Data Division to confirm the COBOL data division section order.
    • CICS LINK: specify Channel Name. You also need to specify an interface copybook file if you choose to create new data elements, or specify a working storage copybook file if you choose to reuse existing data element structures.
  3. Specify the name of program file and procedure copybook file. Provide the data set information by browsing your system.
  4. Select an option under Interface Copybook File.
    • Reuse existing data element structures: all high-level data structures are used in the call to the new program. This does not create a new interface and is the default option.
    • Create new data elements for program interface: only the required elements are copied into a new interface copybook. Data is populated to and retrieved from this new data structure before and after the call to the new program. To use this option, you must specify the name for the new copybook file, and browse for the data set information. This option is unavailable when the selected variables use usage POINTER, or when the selected variables are in a copybook, contain the OCCURS clause, and the parent table level does not exist.
  5. Specify the name of working storage copybook file. Provide the data set information by browsing your system.
  6. Click Preview to view the settings, and then click Back to return to the wizard.
    The preview view consists of two parts: a tree view at the top that contains all the COBOL elements that are affected by the refactoring and a viewer at the bottom that displays a comparison between the original source and the refactored source of the current program or the source of the newly-created program and copybooks.
  7. Click OK in the preview window to confirm your settings.
    The wizard creates the new COBOL program from the extracted code and exits, returning you to the editor window.

Renaming a data item or paragraph in a COBOL program

You can rename a data item or paragraph and references to that data item or paragraph in a COBOL program.

About this task

Supported Editors:
  • COBOL Editor
  • z Systems LPEX Editor

The Rename operation shows an information screen with the projected warnings and how many data items are going to be changed if the refactor operation is committed. Optionally, another page is shown with a preview of what the source will look like compared to the original source after the refactor operation is accepted.

The preview page shows the Rename function.

Restriction: The following restrictions and limitations apply to the Rename action:
  • The Rename action does not recognize Pseudo SO/SI bytes included in DBCS strings.
  • The Remove noise words and Extract paragraph actions do not work when sequence numbers are present.
  • The Rename action does not work on DBCS variable names.
  • When errors are detected by real-time syntax checking, the Rename action is not available.
Limitations: In the z Systems LPEX editor, this operation cannot be undone.

Procedure

To rename a data item or paragraph in your COBOL file, complete the following steps:

  1. Select a data item or paragraph name, and right-click.
    A menu is displayed.
  2. Click Refactor > Rename.
  3. Type the new name, and perform one of the following actions:
    • Click OK.
    • Click Preview after optionally selecting Force preview. A Found problems window is displayed.
  4. If you clicked Preview in the previous step, click Continue.
    A preview window is displayed.
  5. Click OK.

Extracting a paragraph or section in a COBOL program

You can extract contiguous COBOL statements into a new paragraph or section.

Before you begin

Supported Editors:
  • COBOL Editor
  • z Systems LPEX Editor
Open a COBOL file that includes a Procedure Division.
Note: Extracting a paragraph or section in a COBOL program is unsupported in files that contain preprocessor directives.

About this task

The action is enabled based on the validity and the completeness of selected text:
Validity
The action is enabled only when text is selected in the Procedure Division of the COBOL source. Furthermore, if you select a COBOL word or variable name, the action is disabled.
Completeness
The selected text contains one or more valid COBOL statements. It also means that if you select a partial statement such as an IF statement or a partial PERFORM statement, the action is disabled until you extend the selection to select the complete IF or PERFORM statement.
Logic
The selected texts must have a logic with procedure calls. If the extracted code is not consecutive in the current program, an error displays to explain the problem. You can continue and accept the updates. If the extracted code is valid, the COPY insertion is made in the same location in the source code.
Limitations: In the z Systems LPEX editor, this operation cannot be undone.

Procedure

To extract a paragraph or section, complete the following steps:

  1. Select valid text and right-click.
    A menu is displayed.
  2. Click Refactor > Extract Paragraph or Section.
    An Extract Paragraph or Section wizard is displayed.
  3. In the Extract Paragraph or Section wizard, perform the following tasks:
    1. Complete the New paragraph name field and/or optionally select the Extract into a new section checkbox.

      You can extract the selection into a new paragraph, or into a new paragraph in a new section, or directly into a new section.

    2. If the New paragraph name field is filled in the previous step:
      • Optional: Complete the End paragraph name and Comment fields.
        Note: When the End paragraph name is chosen, PERFORM THRU will be generated in the source program and an informational message displays: The end paragraph name is specified. A "PERFORM THRU" statement will be used.
      • Specify the extraction location: Closest valid location or At the end or before an existing paragraph.
    3. If you selected the Extract into a new section checkbox, specify the name of the new section, and specify the section after which the new section is to be placed.
    4. Click Preview.
      The Extract Paragraph or Section wizard compares the original source with the refactored source in a preview window.
    5. If you are in the preview window and want to return to the previous display, click Back.
    6. Click OK in the preview window to confirm your settings.
      The wizard inserts the extracted paragraph or section into your COBOL file and exits, returning you to the editor window.

Removing specified unnecessary words from a COBOL program

You can remove specified unnecessary words from a COBOL program. The words, IS, THEN, and PROCEED TO are not necessary in the COBOL source. They are considered noise words. Removing these words from the source has no effect on compiling the program.

About this task

Supported Editors:
  • COBOL Editor
  • z Systems LPEX Editor
Limitations: In the z Systems LPEX editor, this operation cannot be undone.

Procedure

  1. Right-click in the editor window to open the menu.
  2. Click Refactor > Remove Noise Words.
  3. Select one or more of the following noise words that you want to remove.
    • IS
    • THEN
    • PROCEED TO
  4. You can select Force preview. If you select this action, you must click Preview before your changes can be saved.
  5. Click Preview to see a summary of the changes that are made by the action you requested. You can return and modify your selection or to continue.
    If you click Continue, the actual changes are shown.
  6. Click OK.

Identifying unused variables and copybooks

An unused variable is one whose data element structure, including parent and subordinate items, is not referred to in the main program. An unused copybook is one that contains unused variables. You can view the unused variables and copybooks and remove them from the program to clean it of superfluous information.

Before you begin

Supported Editors:
  • COBOL Editor
  • z Systems LPEX Editor
You can set the following preferences for theIdentify Unused Variables functions in the Preferences window:
  • To set the appearance and placement of annotations:
    1. Select General > Editors > Annotations.
    2. On the Annotations page, select Highlight Unused Variables and change the settings as needed.
    3. Click OK to save the changes.
  • To identify unused variables or copybooks when you save a program in the editor:
    1. Select COBOL > Editor > Save Actions.
    2. On the Save Actions page, select Identify unused variables and then click OK to save the setting.
Limitations:
  • This function is not supported in files that contain preprocessor directives.
  • This function identifies unused variables with complete data structures only. Unused subordinates of a variable are not shown individually.
  • In the z Systems LPEX editor, this operation cannot be undone.

About this task

For a video demonstration of this function, see Highlight Unused Variables.

Procedure

  1. Open a COBOL source program file that contains a Procedure Division and a Data Division.
    The program file must not contain syntax errors. Otherwise, identification of unused variables and copybooks is unavailable.
  2. Right click in the editor and click Refactor > Identify Unused Variables.
  3. Choose one of these options.
    These menu options are grayed out and no code is highlighted if there are no unused variables or copybooks in the file.
    • Highlight Unused Variables: Searches for unused variables and copybooks in Data Division of the main program. The identified variables and copybooks are highlighted with annotations and markers.
    • Clear Highlighting: Removes annotations and markers from unused variables or copybooks. Other annotations and markers that are unrelated to unused variables and copybooks remain.
    • Remove Unused Variables: Opens a tree view of all unused variables and copybooks in the main program. Each branch represents the main COBOL file and unused copybooks.
      1. To view individual parent data elements that will be removed and relationships among them, expand or collapse the tree.
      2. To view and confirm the removals, click Preview. To return to the first page of the window, click Back.
      3. To remove the unused variables and copybooks from the main program, click OK.