This blog outlines the new refactoring tools added to Visual COBOL and Enterprise Developer 6.0. These new capabilities allow COBOL developers to “slice out” code based on different criteria. Conceptually, refactoring helps break up complex programs to smaller pieces automatically. These smaller pieces can be used for testing, exposed as web services, or just kept as is for easier maintenance of code.
In my previous refactoring blog I explained the value of a refactoring approach versus a more draconian, risky, rewrite approach.
Tackle the impossible. A customer story.
Let’s start with a story. Once upon a time, a customer was struggling to assess their COBOL system. Using our technology, they identified a single “main” program in their application, which was 150k lines, needing 13 developers working on it full-time. You can imagine the code was not getting any cleaner over time. Quality and throughput were suffering.
They expressed a wish to disentangle that program into more discrete functions. Their code was spaghetti, and not easy to unravel. In their words, this was an impossible task.
So was born our mission to use our analysis technology to not only understand the complexities of the application, but to help simplify it through a process often called refactoring, or code slicing.
An innovative solution for the application developer
Leveraging our flagship Visual COBOL and Enterprise Developer IDEs, in both Eclipse and Visual Studio and our code analysis products , we have created a unique capability to “slice out” pieces of logic.
With these new refactoring capabilities, developers can create new programs from existing code based on different criteria. These new programs are completely independent and can be used and re-used in different contexts. For example, they can be used for unit test creation, exposed as a web service, invoked from Java or .NET, or just used to simplify the original application.
Slicing a section or a paragraph
This is the most straightforward use case. Say you have a big program that handles inventory reports generation. In that program there is a paragraph which calculates how many items are left given a barcode number so it can be written in the report. If you want to reuse this logic in a different program, or to expose it as a service for a mobile app, a simple Copy & Paste won’t do the trick as in order for the code to work, you’ll also need the used data items, file descriptions, called paragraphs if any etc.
Code slicing a section will do all that for you automatically, creating a new program with all the needed DATA DIVISION items including LINKAGE SECTION to pass in needed parameters and for populating the result.
You also have an option to change the code of the original program to use the newly created program instead of the original code.
Code slicing a computation
Now this is getting interesting. Let’s say that embedded in your 1,000s of lines of code, there is a specific computation, an interest rate discount based on some parameters for example.
This computation is needed as a service, or it has been proven faulty, and you want to add some unit tests to make sure it does not break in the future.
Using the computation code slicing, you can point at a data item, somewhere in the PROCEDURE DIVISION, and you will be able to slice out a completely new program that calculates the same value. The new program will comprise the original code, stripped of anything that is not affecting the requested computation.
Code slicing based on a condition
The last algorithm lets you create a new program by virtually applying a value to the code.
So, for example, if we apply the value NC to TXN-SUPP-CODE
This code snippet:
If you are asking yourself, what happened to the ADD 1 TO NC-TOT line? It was optimized, as it is not used in the new program. Our refactoring logic takes care of this.
The same logic will examine the entire program and optimize the code to reflect the execution under the defined condition, removing redundant data items, paragraphs, and sections.
Use cases for this facility are many, from specializing a program for handling a specific state code or a payment method, all the way to create a simple cut down of a program for documenting the logic or researching a production issue.
Seeing is believing
To see Micro Focus’ new code slicing capabilities action, watch our refactoring video here and stay tuned for additional technical blogs about this.