Refactoring COBOL and PL/I programs

Modernize your applications by breaking them into modular, reusable services while maintaining the same functionality. This section guides you through that transformation process using structured refactoring workflows designed specifically for COBOL and PL/I programs.

Modernizing legacy applications is essential for organizations seeking to improve agility, scalability, and long-term maintainability. One effective approach to achieving this is by breaking down monolithic codebases into modular, reusable services—while preserving the original functionality and business logic. By breaking large applications into smaller, service-oriented components, you can streamline development efforts, reduce complexity, and enable faster adaptation to changing business needs. These modular services can be independently developed, tested, and deployed, making it easier to introduce enhancements without impacting the entire system.

Prerequisites

  • A connection to Z Understand is required for extracting relevant sections of code.
  • Ensure the application project in Z Understand is built and ready.
  • Verify that the application source code is available in the local workspace.
  • Verify that you are working in Z Code mode.

Initiating the refactoring workflow

Open the workspace where the COBOL files and copybooks, or PL/I files and their corresponding PL/I include files are saved. Use one of the following approaches to initiate a refactoring workflow:

  • On the Bob chat window, click on the Start Workflow icon Play button), select the Refactor COBOL or PL/I program workflow and click Start. Bob initiates the workflow in the chat window and prompts you to select the COBOL or PL/I to refactor.
  • Use /refactor filePath (required) refactoringGoal (required) command in the Bob chat interface. For example, /refactor @/LGAPDB01.pli motor policy update logic.
  • Alternatively, you can provide a text command, such as: Refactor my COBOL program. Here is my source file: <@COBOL file>.
Note: After you initiate a refactoring workflow, at several stages, Bob prompts you to approve tools usage. You can either approve all tools for the task at a time by selecting Approve <tool type> tools for this task, or approve the tools after evaluating at each stage by selecting Approve once from the drop-down list.

Preparing the file for refactoring

If you selected the Refactor COBOL or PL/I program workflow, Bob prompts you to select a COBOL or PL/I program file to extract logic from and refactor.

  1. Use the Browse file option to browse and select the COBOL or PL/I file in the Program file box.

  2. Click Continue with selection.
  3. Bob confirms the file selection.
  4. Use the chat interface to enter the logic that you want to extract from the selected program.
  5. Based on your input, Bob proceeds to call the Refactor tool and initiates the refactoring process.

Suggested refactoring goals

Bob reads the selected program, identifies and lists the logical areas that could be good candidates for extraction into reusable services. Use the chat box to specify your refactoring goal.

Selecting a refactoring method

Bob prompts you to select one of the following refactoring methods:

Simple refactoring
This method refactors the program in a single step to deliver immediate results. It is ideal for smaller, less complex programs with clear refactoring goals. The tool analyzes the code and directly generates the refactored output.
Extract relevant sections of code

A two-step approach designed for more complex programs (automatically used for large or intricate codebases):

  • Step 1 - Identify candidates: The tool analyzes the program and identifies specific code blocks suitable for extraction into a new service. You can review these suggestions and choose which ones to proceed with.
  • Step 2 - Generate service: After confirming your selections, the workflow uses these inputs to generate the extracted service into a Markdown file. The Markdown file is then used in the Generate refactored service program workflow to produce the final refactored program.
Note:

The Extract relevant sections of code method requires the Z Understand server to be configured in settings. For large, complex programs, the Extract relevant sections of code method is automatically selected without prompting.

Simple refactoring

The Simple refactoring process consists of 4 steps:

Detect and analyze copybooks

Bob analyzes the selected refactoring goal and detects any referenced include files. If include files are available, then the following options are listed:

  • Continue: Read application copybooks listed above.
  • Skip: Analyze using program structure only.
Present extraction candidates

Bob then identifies extraction candidates aligned with the refactoring goal and presents the top three candidates, ranked by importance. The highest-ranked candidate is marked as recommended, as it represents the most complete and production-ready logic, supported by descriptive reasoning.

You must select one candidate for extraction. Bob performs a detailed analysis and generates an Extraction Summary, which includes:

  • Service name
  • Entry point
  • Extraction type
  • Core logic
  • Infrastructure information
  • Data flow
  • Dependencies
  • Return codes
Present extraction summary

After the analysis is complete, Bob indicates readiness to generate artifacts, a COBOL or PL/I service file, and an extraction report. When prompted, approve all requests to create files and directories.

Generate service code

Bob prompts you to specify whether you want to generate a service from the extracted code. Select one of the following options:

  • Generate - Create <file name> service?: Select to proceed with code extraction to a file. Use the Generate Refactored Service Program workflow to create a service from the generated COBOL or PL/I code extraction file.
  • Cancel - Do not generate: Select to halt the refactoring workflow.

Extract relevant sections of code

Using this feature, you can extract specific code elements from your mainframe COBOL or PL/I programs and move them into a new service. This is a structured method to modernize and modularize legacy applications by extracting logically cohesive code segments into reusable services. It focuses on identifying key logic segments (findings), isolating them by extracting relevant sections of code in the program, and refactoring them into service-oriented components that can be regenerated and documented automatically.

Note: The Extract relevant sections of code option is available only when the Z Understand server URL is configured.

Key characteristics of extracting relevant sections of code:

  • Creates -service.md file containing the extracted code.
  • Helps break down monolithic programs into smaller, more maintainable services.
  • Only conditional statements, paragraphs, and procedures can be added to existing services.
  • Part of the modernization workflow for transforming mainframe applications.

How to extract relevant sections of code

  1. Bob analyzes the file and identifies all candidates for service code extraction based on your refactoring goal.

  2. Click on a extraction candidate to select it. Bob displays a contextual action menu with the following options:

    Extract to new service: Select this option to create a new service container and add the extracted code to this container. Bob continues to prompt you to select more extraction candidates from the identified list.

    Extract to existing service: Select this option to add the extracted logic to an existing service.

    1. A dialog appears prompting you to select a service to add the code to. Use the Select existing service drop-down menu to select an existing service.
    2. After you select a service, Bob prompts you to select one of the following extraction options:
      • Forward: Traces call chains forward from the selected paragraph.
      • Forward through: Traces call chains forward from the selected paragraph to a specified ending paragraph. Bob prompts you to choose the ending paragraph.

      Select an appropriate option and confirm. Bob begins the extraction process.

    3. Continue selecting additional findings or code elements. Once done, select the I'm done extracting code option to proceed to the next step in the workflow.
  3. Bob prompts you to specify whether you want to generate the service program. Select one of the following options:
    • Yes, generate the service program: Select to generate the service program.
    • No, refactor more code blocks: Select to stop the workflow. You can refactor more code from other programs.
  4. Depending on whether you selected the Extract to new service or the Extract to existing service, Bob either create a service file with the name <service-name>-service.md file with the extracted code, or updates an existing service file in the workspace.
  5. Bob proceeds to initiate the Generate Refactored Service Program workflow and uses the Markdown file as input.

    For more information about this workflow, see Generating refactored service programs