Some jobs are too tricky to leave to your computer unsupervised, but RDPi has tools that speed up even the most tedious of programming tasks.
A computer will do exactly what you tell it to do, no matter how much that request might defy common sense. A perfect example of that truism is the ubiquitous and sometimes dangerous "find and replace." You can tell any decent source-editing tool to replace all instances of the field name CUSNAM with CUSNME and it will dutifully do exactly that, and thanks to the cryptic nature of field names in RPG, the chances are that the execution will be flawless. However, use find and replace enough and you will eventually run into a situation where the editor replaces things you really didn't want replaced. This article shows you how to combine a couple of features of Rational Developer for Power Systems Software for IBM i (RDPi) to create a semi-automated find and replace mechanism.
Do What I Mean, Not What I Say!
At the end of the day, programming boils down to telling a computer to execute an intricate series of very specific steps. Having a six-year-old, I can attest to the similarities between a very powerful but very literal computer and a grade-schooler jazzed on strawberry donuts (it's that time of year here in Illinois, but I digress). Attention to the smallest detail is critically important: even if you tell a first-grader to put on clothes and carefully specify both outer clothes and underwear, but forget to precisely identify the order of said apparel, you are liable to end up with a child who either will be sent home to change or will start a new fashion trend for rap singers. The same holds true for a computer—in this case, the find and replace function.
Let me show you an example.
Figure 1: This simple program retrieves the address information from an order. (Click images to enlarge.)
In Figure 1, you see a simple program that takes an order number and then chains out to the customer order history file to get the address information for the customer. It mostly involves a couple of moves, but also a little formatting. There are other ways to do this, such as using qualified data structures and EVAL-CORR, but this example suffices for today's purpose.
The business problem is that the CUORDHST file is being changed. Rather than being the customer order history file, it is going to be a more generic order history file, ORDHIST. In keeping with the file rename, all fields in the file will be renamed, and rather than having the prefix CH, they will instead start with OH. The field CHNAME, for example, will become OHNAME. This seems relatively straightforward, and a simple find and replace of CH to OH ought to work; that is, until you look at line 002000. Your CHAIN operation will be transformed into OHAIN, which won't compile. Interestingly, this isn't the worst problem. Since this won't compile, you'll find the problem right away. Other more subtle errors can be introduced, from changes in text literals to changes in comments. The point is that a fully automatic find and replace function isn't always perfect.
Semi-Auto vs. Fully Automatic
How do we get around the problem? RDPi provides a way of inserting yourself into the flow of find and replace. It consist of two separate techniques that, combined, make for a very productive large-scale replacement mechanism. The first part is fairly common to most text editors: you can do one change at a time.
Figure 2: The Find and Replace dialog provides buttons to change one instance at a time.
Figure 2 shows the Find and Replace dialog. You can see the buttons Next, Previous, and Replace, which allow you to move through the source file one change at a time (as opposed to the Replace All button, which replaces every occurrence of the Find string). This is a somewhat primitive but functional alternative, and it's what most editors provide. However, the problem is that often (in larger programs especially) the lines are some distance apart and it's not always clear which lines you need to change. So you have to go through the tedious task of hitting Next, jumping to the next section of code, determining if this line needs to change, and then moving on to the next occurrence.
Wouldn't it be nice if you could filter the view so that you saw only lines eligible for change? As you might guess, RDPi provides just such a technique. If you click on the All button (not Replace All!), you'll see the results in Figure 3.
Figure 3: Hitting the All button filters the view to show only lines containing the Find text.
This is pretty significant. While the example I showed today is obviously trivial, imagine one of those 27,000-line programs that has been converted twice (first from S/38, then to ILE RPG). You could easily see every line that might need to be changed and then spin through them. This is a standard RDPi filter view, with the plus signs (+) on the left of the source allowing you to expand or contract sections of the code as needed to get context. In fact, the filter is so standard that you don't need to go into the Find and Replace dialog to get it. Figure 4 shows how to execute the same filter using the standard right-click Context menu in the source.
Figure 4: You can use the Context menu to filter by selected text as well.
To filter the source, first select the text you want to filter by. This is really easy for fields and files and the like; you can usually just double-click on the name and RDPi is smart enough to isolate it from its surroundings. But even if you can't—as in this case, where you want to filter the first two characters of a field name—just use the standard keyboard or mouse controls to mark the text you want to filter by. Then, just right-click in the source to bring up the Context menu and choose menu option Selected, sub-menu option Filter Selection. You will see the same filtered view you saw using the All button, although without the Find and Replace dialog.
One last point: You may notice a difference between your screen and the one in the article. In Figure 4, you'll see that the Filter selection sub-option has the text "Alt-F" to the right, which indicates that the command has a shortcut, Alt-F. Yours probably doesn't have that, because I created the shortcut myself. I used the Preferences menu to add my own User Action that binds the Alt-F key to the filter selection action. Now, I can just mark some text and then hit Alt-F to filter the source. That's just a little trick that I'll cover in more detail in the next installment of "Practical RDPi."
LATEST COMMENTS
MC Press Online