COBOL Migration to Java or .NET

Migrating COBOL to new languages such as Java or .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. EXAMPLE.

	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 "13171 Pond Springs Rd.".
	      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 78729.
	            20 FILLER PIC X    VALUE "-".
	            20 ZIP-E4 PIC 9(4) VALUE 7102.
	01 LINE-ITEM.
	   05 ITEM PIC X(20)     VALUE "Item Description".
	   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.

	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.
	  DISPLAY "Total: ", TOTAL-AMOUNT.

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.

class Example
{
    public class CompanyType
    {
        public char[] CompanyName = "Semantic Designs".PadRight(60).ToCharArray();

        public class CompanyAddressType
        {
            public char[] Street = "13171 Pond Springs Rd.".PadRight(80).ToCharArray();

            public class CityType
            {
                public char[] CityName = "Austin".PadRight(40).ToCharArray();

                public char[] CityState = "TX".ToCharArray();

                public class ZipType
                {
                    public uint Zip5 = 78729;

                    public ushort ZipE4 = 7102;

                }

                public ZipType Zip = new ZipType();

            }

            public CityType City = new CityType();

        }

        public CompanyAddressType CompanyAddress = new CompanyAddressType();

    }

    public CompanyType Company = new CompanyType();

    public class LineItemType
    {
        public char[] Item = "Item Description".PadRight(20).ToCharArray();

        public ushort Quantity = 217;

        public decimal Price = 24.95M;

    }

    public LineItemType LineItem = new LineItemType();

    public decimal TotalAmount;

    public decimal DiscountThreshold = 1111.11M;

    public byte DiscountPercent = 20;

    public decimal DiscountAmount;

    void PerformTask()
    {
        ComputeTotal();
        DisplayTotal();
        System.Environment.Exit(0);
    }

    void ComputeTotal()
    {
        TotalAmount = LineItem.Quantity * LineItem.Price;
        if (TotalAmount > DiscountThreshold)
        {
            DiscountAmount = TotalAmount * DiscountPercent;
            DiscountAmount /= 100;
            TotalAmount -= DiscountAmount;
        }
    }

    void DisplayTotal()
    {
        System.Console.WriteLine(Company.CompanyName);
        System.Console.Write("Total: ");
        System.Console.Write(TotalAmount);
        System.Console.WriteLine();
    }

    static void Main()
    {
        new Example().PerformTask();
    }

}

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.

import java.lang.String;
import java.math.BigDecimal;
import java.util.Arrays;

class Example {
  public class CompanyType {
    public char[] companyName = String.format("%1$-60s", "Semantic Designs").toCharArray();

    public class CompanyAddressType {
      public char[] street = String.format("%1$-80s", "13171 Pond Springs Rd.").toCharArray();

      public class CityType {
        public char[] cityName = String.format("%1$-40s", "Austin").toCharArray();

        public char[] cityState = "TX".toCharArray();

        public class ZipType {
          public int zip5 = 78729;

          public short zipE4 = 7102;
        }

        public ZipType zip = new ZipType();
      }

      public CityType city = new CityType();
    }

    public CompanyAddressType companyAddress = new CompanyAddressType();
  }

  public CompanyType company = new CompanyType();

  public class LineItemType {
    public char[] item = String.format("%1$-20s", "Item Description").toCharArray();

    public short quantity = 217;

    public BigDecimal price = new BigDecimal(24.95);
  }

  public LineItemType lineItem = new LineItemType();

  public BigDecimal totalAmount;

  public BigDecimal discountThreshold = new BigDecimal(1111.11);

  public byte discountPercent = 20;

  public BigDecimal discountAmount;

  void performTask() {
    computeTotal();
    displayTotal();
    System.exit(0);
  }

  void computeTotal() {
    totalAmount = lineItem.price.multiply(new BigDecimal(lineItem.quantity));
    if (totalAmount.compareTo(discountThreshold) > 0) {
      discountAmount = totalAmount.multiply(new BigDecimal(discountPercent));
      discountAmount.divide(new BigDecimal(100));
      totalAmount.subtract(discountAmount);
    }
  }

  void displayTotal() {
    System.out.println(company.companyName);
    System.out.print("Total: ");
    System.out.print(totalAmount);
    System.out.println();
  }

  public static void main(String[] args) {
    new Example().performTask();
  }
}

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