COBOL Migration to Java or C#/.NET

Migrating COBOL to new languages such as Java or C#/.Net promises the following nice properties:

  • removing the stranglehold of the legacy hardware and the cost of the lease/licensing costs,
  • making the application more maintainable, and
  • providing the organization with access to more easily found software engineers.

The first concern is, What will translated code look like? Weak translators convert code blindly line-by-line, with virtually no understanding of how the context of the code should shape its translation. Such translated code preserves the properties of the original COBOL, sometime the point of even preserving the syntax. Even if such code works, it not be maintainable and will ultimately cost the organization sorely. (You can see here the results of weak translator, not from SD, to compare to the results you see on this page).

Semantic Designs translators parse the original code with tools that have the same understanding as the original compilers; (for example, see our COBOL Front End). SD tools know the types of variables, where and how they are used ("the context"), and can thus produce extremely good translations. Because SD also customizes the tools to the specific needs of the customer, the code reflects those needs properly.

Semantic Designs has extended its COBOL tanslator to handle HP NonStop COBOL and SCOBOL, in addition to other COBOL dialects (including IBM Enterprise, Fujitsu NetCOBOL, and MicroFocus Visual COBOL, etc.)

A COBOL program translation

Here we show you a small sample of COBOL translated by SD's tools to C# and another to Java. We present them in this order to make a point about code translation that depends on language features.

A simple COBOL program

We have chosen to provide a small COBOL program to prevent the reader from being overwhelmed with an 8000 line example.

	IDENTIFICATION DIVISION.
	PROGRAM-ID. ORDER-DISCOUNT.

	DATA DIVISION.
	WORKING-STORAGE SECTION.
	01 COMPANY.
	   05 COMPANY-NAME	  PIC X(60)    VALUE "Semantic Designs".
	   05 COMPANY-ADDRESS.
	      10 STREET		  PIC X(80)    VALUE "8101 Asmara Dr.".
	      10 CITY.
		 15 CITY-NAME	  PIC X(40)	   VALUE "Austin".
		 15 FILLER	  PIC XX	   VALUE ", ".
		 15 CITY-STATE	  PIC XX	   VALUE "TX".
		 15 ZIP.
		    20 ZIP-5	  PIC 9(5)	   VALUE 78750.
	01 LINE-ITEM.
	   05 ITEM		  PIC X(20)	   VALUE "Blue widget".
	   05 QUANTITY		  PIC 999	   VALUE 217.
	   05 PRICE		  PIC 9999V99	   VALUE 24.95.

	77 TOTAL-AMOUNT		  PIC 999999V99.
	77 DISCOUNT-THRESHOLD	  PIC 999999V99	   VALUE 1111.11.
	77 DISCOUNT-PERCENT	  PIC 99	   VALUE 20.
	77 DISCOUNT-AMOUNT	  PIC 99999999V99.
	77 TOTAL-FOR-OUTPUT	  PIC $$$$$$9.99.

	PROCEDURE DIVISION.
	PERFORM-TASK.
	  PERFORM COMPUTE-TOTAL.
	  PERFORM DISPLAY-TOTAL.
	  STOP RUN.

	COMPUTE-TOTAL.
	  MULTIPLY QUANTITY BY PRICE GIVING TOTAL-AMOUNT.
	  IF TOTAL-AMOUNT > DISCOUNT-THRESHOLD
	    MULTIPLY TOTAL-AMOUNT BY DISCOUNT-PERCENT
				  GIVING DISCOUNT-AMOUNT
	    DIVIDE 100 INTO DISCOUNT-AMOUNT
	    SUBTRACT DISCOUNT-AMOUNT FROM TOTAL-AMOUNT.

	DISPLAY-TOTAL.
	  DISPLAY COMPANY-NAME.
	  MOVE TOTAL-AMOUNT TO TOTAL-FOR-OUTPUT.
	  DISPLAY "Total: ", TOTAL-FOR-OUTPUT.

COBOL program automatically translated to C#

Here we present the translation of the simple COBOL program above to C#.

One should note that the translator has chosen data types compatible with their declaration in the original program, but with a different representation better suited for C#. Note the different choice of C# integer types based on the sizes of the values being stored. In particular, note the choice of the C# built-in decimal data type, used where decimal data with fractional parts is necessary.

Note that paragraphs have been converted to methods, which provides a clean way to translate PERFORM A THRU B.

This output is reproduced exactly as the translator generated it. The translator produces nicely formatted code as a standard by-product.

using System;
using System.Diagnostics;
using SemanticDesigns;
using static SemanticDesigns.SDCSC;

public class OrderDiscount
{
    public readonly Library.OrderDiscount.CompanyTType Company = new Library.OrderDiscount.CompanyTType();

    public readonly Library.OrderDiscount.LineItemTType LineItem = new Library.OrderDiscount.LineItemTType();

    public decimal TotalAmount;

    public decimal DiscountThreshold = (decimal)1111.11M;

    public sbyte DiscountPercent = (sbyte)20;

    public decimal DiscountAmount;

    public decimal TotalForOutput;

    private static OrderDiscount _self = null;

    public static void Main()
    {
        if (_self == null)
        {
            _self = new OrderDiscount();
        }
        _self._Main();
    }

    private void _Main()
    {
        // label: LblPerformTask
        FnComputeTotal();
        FnDisplayTotal();
        System.Environment.Exit(0);
    }

    private void FnComputeTotal()
    {
        // label: LblComputeTotal
        TotalAmount = LineItem.Price * LineItem.Quantity;
        if (TotalAmount > DiscountThreshold)
        {
            DiscountAmount = DiscountPercent * TotalAmount;
            DiscountAmount = Data.Truncate(DiscountAmount / 100, 2);
            TotalAmount = System.Math.Abs(TotalAmount - DiscountAmount);
        }
    }

    private void FnDisplayTotal()
    {
        // label: LblDisplayTotal
        Console.WriteLine(Company.CompanyName.PadRight(60));
        TotalForOutput = TotalAmount;
        Console.Write("Total: ");
        Console.WriteLine(TotalForOutput.Format("$(6)9.9(2)"));
    }

}

So one can get good code from a translator, if one has the right foundation technology such as SD's DMS Software Reengineering Tookit®. As a practical matter, you can't get good code from an off the shelf translator, because every application system has unique properties: languages, OS features, scripting languages, screens, databases, and a correspondingly unique target as decided by the client. So it takes some effort to configure DMS for the client's particular source and target software configurations.

COBOL program automatically translated to Java

Here we present the translation of the same simple COBOL program above to Java.

One should note that the translator has chosen data types compatible with their declaration in the original program. In contrast with C#, Java does not have native decimal types, so the BigDecimal library class has been chosen to represent decimal numbers which can have fractional parts. The translation is adjusted to use appropriate method calls on the BigDecimal class instead of native Java operators +, -, *, / which do not work on classes. We remark that a different custom translation could scale decimal numbers by 100 or 10000, allowing use of Java-native + and - operators instead of method calls. Which of these choices are made depend on what the client thinks in more important to long term maintainablity: representation of decimal numbers as 'decimals' in the BigDecimal class with somewhat less readable expressions for decimal arithmetic, or a slightly non-intuitive representation of decimal numbers with readable expressions.

As with the C# version, note that paragraphs have been converted to methods, which provides a clean way to translate PERFORM A THRU B.

This output is reproduced exactly as the translator generated it.

package Application;

import com.semanticdesigns.cobol2java.* ;

public class OrderDiscount {
  public final Library.OrderDiscount.CompanyTType company = new Library.OrderDiscount.CompanyTType();

  public final Library.OrderDiscount.LineItemTType lineItem = new Library.OrderDiscount.LineItemTType();

  public java.math.BigDecimal totalAmount = java.math.BigDecimal.ZERO;

  public java.math.BigDecimal discountThreshold = new java.math.BigDecimal("1111.11");

  public byte discountPercent = (byte) 20;

  public java.math.BigDecimal discountAmount = java.math.BigDecimal.ZERO;

  public java.math.BigDecimal totalForOutput = java.math.BigDecimal.ZERO;

  private static OrderDiscount _self = null;

  public static void main(String args[]) {
    if (_self == null) {
      _self = new OrderDiscount();
    }
    Data.setCommandLineArguments(args);
    _self._main();
  }

  private void _main() {
    // label: lblPerformTask
    fnComputeTotal();
    fnDisplayTotal();
    System.exit(0);
  }

  private void fnComputeTotal() {
    // label: lblComputeTotal
    totalAmount = lineItem.price.multiply(java.math.BigDecimal.valueOf(lineItem.quantity), java.math.MathContext.DECIMAL128);
    if (totalAmount.compareTo(discountThreshold) > 0) {
      discountAmount = java.math.BigDecimal.valueOf(discountPercent).multiply(totalAmount, java.math.MathContext.DECIMAL128);
      discountAmount = Data.truncate(discountAmount.divide(java.math.BigDecimal.valueOf(100), java.math.MathContext.DECIMAL128), 2);
      totalAmount = totalAmount.subtract(discountAmount).abs();
    }
  }

  private void fnDisplayTotal() {
    // label: lblDisplayTotal
    System.out.println(Data.format(company.companyName, "X(60)"));
    totalForOutput = totalAmount;
    System.out.print("Total: ");
    System.out.println(Data.format(totalForOutput, "$(6)9.9(2)"));
  }
}

Configuring a custom migration tool to match client needs

Semantic Designs can provide custom configuration of migration tools and migration support to your organization, providing high quality, maintainable code translations.

For more information: info@semanticdesigns.com    Follow us at Twitter: @SemanticDesigns

COBOL
Migration