Refactoring COBOL with RDz

Off
Strongback Consulting

What is refactoring?

Refactoring has long been a discipline of the agile development world. Wikipedia defines refactoring as:

A disciplined technique for restructuring an existing body of code,  altering its internal structure without changing its external behavior…. [and its] advantages include improved code readability and reduced complexity to improve the maintainability of the source code, as well as a more expressive internal architecture or object model to improve extensibility.” 

Refactoring, is really akin to writing literature. For example, a novel author first stubs out an outline, then writes the paragraphs in a stream of thought format. Later, the author (or an editor), can refactor (edit) the novel to improve the story’s grammar, punctuation, or to improve the delivery of the story line.

Refactoring is a rather new term in the COBOL world. Java, .NET, PHP, and Python guys have been doing this for years. In the COBOL world, there are countless programs that appear to have stopped maturing at the stream of thought level. So, first let’s introduce you to a scenario where refactoring comes into place. Let’s say you just inherited another developer’s spaghetti code. That developer retired 10 years ago, and nobody’s touched the source until now, when some strange production error just came up. Sound familiar? Read on.

So, now you have to manage this code that is about as manageable as a python wrapped around an alligator bathed in Tabasco sauce. With RDz, you’ve got some handy tools available. All of which will help you make your code more manageable, and readable, without hindering the operational functionality of the code.

Extract Paragraph

First, lets say you are reading your code and realize that the source file keeps repeating the same 10 lines of code over and over throughout the program. Its as if s/he got paid based on the lines of code s/he wrote. You, however just need to manage it. So you want to replace these with simple PERFORM statements, taking cues from our object oriented buddies in the distributed realm. To do this, we use the Extract Paragraph technique. Select the lines of code you want to refactor, and then right click and select from the context menu “Refactor – Extract Paragraph“.

In the dialog, enter in the paragraph name and ending paragraph name. You can also add comments to the paragraph, and force it to do a preview.

Remove Noise Words

COBOL allows for verbosity to help readability of the code. This verbosity is called “noise words”. They do not affect the operation of the program and can be removed if desired. In your source, right click and select “Refactor – Remove Noise Words”. You can force a preview to show what the file will look like using the change explorer after the words have been removed.

Rename Variable

Have you ever had to work behind someone who named their variables VAR1, VAR2, VAR3….? Or, have you ever had to go back and change variable names across a system because it conflicted with another applications variables? Well, there is an easy way to do this. Double click on the variable in the editor. Then, right click and select “Rename”. In the interface, type in the new name of the variable. This will rename that variable everywhere that it is referenced in the file, and any copybooks that are referenced from the source member. This is more efficient (and accurate), than using the Find/Replace dialog. This uses the parser to find real variables, not just matching text strings with that variable name.

Source Format

Perhaps the best new tool in the RDz arsenal is the source code format tool. This feature has been available for the Java developers for many years. In fact, I used to take it for granted, until I started working with COBOL. Let’s say you inherit someone else’s spaghetti code. Its about as readable as the micro-font legalese on the back of your TV warranty manual. Here is an example of some ugly, unformatted code.

As you can see, its difficult to determine where a block begins and ends. Now, you could do this in ISPF, but it involves a bunch of tedious block-move line commands.
Open your source in the COBOL editor. No, not the System Z LPEX editor, the COBOL editor. Yes, there are two separate editors in RDz for editing source. One can act like ISPF, the other acts more like other Eclipse based editors. Right click on your source and select “Source – Format“.










Note, the keyboard shortcut (Ctrl+Shift+F). This will format your source code according to the built in rules. Here is what the source looks like after the format. Nice, huh?

Now, this will not always format nicely depending upon what you have formatted. If, for example, you are trying to import code that has line numbers, or COBOL that has source from another vendor (i.e. MicroFocus), you’ll have a bit more work to do to clean it up. To get to those, go to your workbench preferences (Window – Preferences), then drill down to COBOL – Editor – Formatter. You’ll find rules for specifying how you want to handle record descriptions, procedure divisions,custom indentations, line lengths, capitalization, and more.

Summary

So, we’ve shown you how to change your code without changing its functionality. Refactoring can make your code significantly more readable, and more maintainable. This in turn, makes it likely that whoever inherits your code when you retire will have an easier time maintaining it.

Comments are closed.

Strongback Consulting