RPG and Programming
Archive for the ‘RPG’ Category
Free-form programming – intuitive?
I came across a discussion of freeform RPG and why the author felt it is the way to go rather than the fixed format , old-style. I would like to analyze it in some depth, not to make fun of the author, but rather because his views are representative of those who think the way he does, and they are stated very concisely. (This may take several posts, so bear with me.)
What is his reason number 4 for using RPG freeform?
“Calculation syntax now looks more like the intuitive mathematical-function-like syntax found in most other modern programming languages. No doubt the Factor1-OpCode-Factor2-Result sequence has some magical storied history, but at this point it should be considered sand on a beach that’s been washed away by common sense and better practice. If I can write intuitive code in Java or C or Perl I should be able to do the same thing in RPG.”
We will ignore the condescending reference to magical storied history and sand on the beach for the moment. and concentrate on other things first.
1. Is freeform, whether in RPG or other languages, INTUITIVE?
Well, what is “intuitive”? According to the Webster’s On line Dictionary:” 1 a: known or perceived by intuition : directly apprehended <had an intuitive awareness of his sister’s feelings> b: knowable by intuition <intuitive truths> c: based on or agreeing with intuition <intuitive responses> <makes intuitive sense> d: readily learned or understood <software with an intuitive interface> .
And what is intuition? “1 : quick and ready insight 2 a : immediate apprehension or cognition b : knowledge or conviction gained by intuition c : the power or faculty of attaining to direct knowledge or cognition without evident rational thought and inference.”
The most obvious applicable meaning is “readily learned and understood”. Certainly some of the languages he mentioned might be intuitive for some; but I thought RPG was readily learned; I learned it in just a few informal sessions by reading a textbook when I didn’t know a physical computer from a dustbin and had no concept of computer science and had never seen a computer language. I would be interested in seeing how many people learned C or Perl – or especially Java- by just being given a computer book for the first time and told, “read this”.
About 20 years ago, I bought a C compiler for my microcomputer and a C textbook. I wanted to see what it was all about, and I was willing to spend money to do so. But I got into it and started seeing things like int main(), all the different data types, and < and >and a= (b= (c=25)) and if (age ==40) – just for starters- I looked down from my RPG II heights and said “You’ve gotta be kidding.”
That last reference is especially intriguing. Nicklaus Wirth, creator of Pascal, Modula-2, and Oberon-2, wrote an interesting article about good and bad ideas in computer science. Here’s what he said on that subject:
“A notorious example for a bad idea was the choice of the equal sign to denote assignment. It goes back to Fortran in 1957 and has blindly been copied by armies of language designers. Why is it a bad idea? Because it overthrows a century old tradition to let “=” denote a comparison for equality, a predicate which is either true or false. But Fortran made it to mean assignment, the enforcing of equality. In this case, the operands are on unequal footing: The left operand (a variable) is to be made equal to the right operand (an expression). x = y does not mean the same thing as y = x. Algol corrected this mistake by the simple solution: Let assignment be denoted by “:=”.
“Perhaps this may appear as nitpicking to programmers who got used to the equal sign meaning assignment. But mixing up assignment and comparison is a truly bad idea, because it requires that another symbol be used for what traditionally was expressed by the equal sign. Comparison for equality became denoted by the two characters “==” (first in C). This is a consequence of the ugly kind, and it gave rise to similar bad ideas using “++”, “–“, “&&” etc. ”
So C came up with the bad idea c== 5; when you first come to it, being acquainted only with “=”, you look at “==” and your first thought is, “What on earth is that?” I place in that same category an expression like x+=1. You don’t know C; you know what a “+” is; you know what an “=” is; but what is a “+=”? I’m sorry folks, but that is not intuitive.
You think such stuff is intuitive not because it is truly intuitive; you think it because you have been raised to expect data in the format x=Y+3 in your “Introduction to Programming” or “Introduction to Computer Science” courses. You may have seen something like it in BASIC, and it fits the pattern. I was raised on X ADD 1 Y. I think THAT is intuitive.
Am I wrong? Is not X ADD 1 Y practically the same structure as 1 + 1 = 2, the mathematical structure you learned in first grade, a long time before your Computer Science courses? And, even if you allow the “bad idea” to be used, is not ADD 1 X easier to comprehend than X+=1? Which is REALLY more intuitive?
So the block-structured languages mentioned are not innately “intuitive”; whether they are more intuitive than fixed-format RPG depends entirely on where you are coming from. And whichever direction you come from, can you really say with a straight face that X+=1 would be more readily comprehensible than ADD 1 X for a new programmer?
Writing a Subprocedure
Late last week I prepared to write a subprocedure for a program I am currently working on. This was significant because, though subprocedures are recommended as a highly significant new way of modularizing our programming (subdividing the programs into manageable procedures rather than the monolithic program model of standard RPG), I had never written one myself, though I had maintained a few. Modularization is common to almost all modern computer languages except RPG. That is, until several years ago when subprocedures were added to RPGIV.
I had taken an introductory course in ILE RPG several years ago and learned about using subprocedures; but since that time, I had neither opportunity or (I confess) inclination to use them. I could see no burning reason to use them, for reasons I shall explain later.
But in this particular case, it seemed an opportune time to try it. The subprocedure would be based upon another program already written by someone else. Without going too deeply into the details, the program determined the status of a particular spooled file; on the basis of this determination, the program performed some other tasks and updated some files. I wanted to use the front portion of this program and use the status determined for another purpose and not update those files. To that end, I cloned the program, performed an amputation on the clone; with that and a little bit of plastic surgery, I had the processing in place that I wanted.
Most of the subprocedures that I had read about did not do any file processing; they were straight calculations. This program used several files, but I did not foresee that as a problem, since I understood that files could be part of subprocedures. I had done my research, and I had printed out what I considered a fine tutorial by Jon Paris that I had found on the Internet.
In line with some of the recommendations, I intended to use this as a separate module that would eventually be pulled together with my original program that would use my new module. I put NOMAIN in the Header specification to indicate that this new module was not a callable program by itself and would not use the RPG cycle. The process I was intending to implement needed to run as efficiently as possible, and the module would not need the cycle anyway.
So with high expectations I began to put together my sub-procedure. The tutorial mentioned above shows quite nicely how to do it, so I will not belabor the general process here. I fully expected to have some problems; you have to expect that sort of thing when you’re trying something for the first time.
So, how did it go?
As expected, I had all sorts of problems, mostly centered around where to put the Input specifications in the subprocedure. Finally, this morning, I figured out what I was doing wrong. I got the subprocedure module to compile, and I am now testing the main program. I seem to be getting more than satisfactory performance, which is what I was aiming for, along with some practice of how it was done.
However, while some think that subprocedures in RPG programs are the greatest thing since sliced bread, I simply don’t agree. Even those who like them admit that creating and using a subprocedure is more of a song-and-dance than a dynamic program CALL is. I had to compile the calling portion as a module, the subprocedure as a module, then run CRTPGM to link the two together. Additional code is also needed, in the form of prototypes and prototype interface definitions. I think a number of different factors might determine whether to use subroutines, dynamic CALLs, or prototyped CALLPs or prototyped function-like expressions. The “modern” way to do it may not necessarily be the best way.
I intend to discuss subprocedures in more depth in another post, as well as that hallmark of “modern” RPG, freeform RPG. In the meantime, back at the office, I will make the program work tomorrow.
But They Loved Her
I was once sent to a client to take care of some fairly modest modifications to some RPG programs on their System/34. As I went about making changes on a certain report program, I concerned myself with the requested changes, not paying much attention to how the program performed its main processing. As I submitted the program changes for test, an employee said to me that I should expect it to run for about 2 or 3 hours. Having looked lightly at the processing, and being acquainted with the 34’s processing speed and the size of the file being processed, I smiled inside. “More like 15 or 20 minutes, I think”, I said to myself.
To my surprise, it in fact did take around 2 ½ hours. I was anxious to find out why I was so wrong, and what I found disgusted me, and for the first time made me angry about the work of another programmer.
The file being processed was a detail file. That is, it was part of a file setup common in data processing where the line items in, for example, an invoice, are stored in a separate file from the general information about the invoice.
Let us suppose that the invoice “header” has a key of invoice number – the file is organized to be read in invoice number order. For our example, let us suppose the invoice number is 123456. In later versions of RPG, you could define file keys from fields physically separate from one another in the file; but this was RPGII on the System/34, and the key data had to be all together. In this case the key to the “detail” file was the invoice number followed by a 3-digit sequence number. So the detail keys for invoice 123456 could range from 123456000 through 123456999, though in practice the 000 record was not usually used (since the files were no doubt designed by business people, who would think starting to count with 0 was nonsense ).
Now, the customary way to read these records was to set lower limits (SETLL) using the invoice number and the lowest possible key (123456000), determining the highest possible key (123456999), then READing the file until you either reached end of file or the key of the record read was more than 123456999. Not having looked closely at the code before, I had assumed that was the way it was done. Since you would very seldom find an invoice with 999 line items, this would be the logical way to do it.
But this was not the way the programmer did it. She instead used a separate counter from 1 to 999; for every number from 1 to 999, she moved that number to the end of the key (getting 123456001, 123456002,123456003, etc.) and performed a random-access CHAIN to the file, using the data if the access was successful. So, if there were 4 detail records for the invoice, she would have 4 successful reads and 995 unsuccessful reads, instead of 4 successful to 1 unsuccessful the normal way. The program was spending over 99% of its time accessing the disk looking for records that didn’t exist!
What made the matter worse was that this logic was replicated in quite a few of their other major programs. Now, I’m not normally a person who likes to rock the boat, but this was too much for me. I went to my supervisor there, explained the situation and asked for permission to change these programs to do it the right way. Permission was granted, and I hunted down the other programs and modified the read routine in all of them to do it properly. And yes, they did complete in 15-20 minutes, just as I thought they should.
Unfortunately, this was too little too late. I found other areas where the programs, though giving correct results, were very poorly designed. The proof of the damage this programmer and others had done to this firm was evident in the fact that, at the time I came to that firm, they were in the process of moving their processing to another, non-IBM minicomputer using another dialect of RPG because it was deemed that the System/34 just didn’t cut the mustard. It was too slow.
This may be an extreme example. Normally, I don’t sweat performance too much, though even today I think programs are more bloated than they need to be. (I once compiled an RPGIV program with only 1 line: eval *inlr=*on. Its size was around 80K.) But bad programming can really have a bad effect.
I prefer that programs be clear and understandable, so that they can be more readily maintainable; performance can be somewhat secondary. I think that reducing disk access is the key to most performance issues. I really don’t worry about whether or not one opcode is 50 nanoseconds faster than another. Talk about a few trillion nanoseconds here and there and we’re talking some real time.
But sometimes you do have to stand up for a certain standard of performance. Unfortunately, in that company, quality control came in a little too late. Perhaps it was a human relations issue. While I was at that company, they talked about how much they liked that programmer as a person. She must have been a real charmer. Evidently, they liked her so much that they accepted whatever excuse she gave for a report run that took 2.5 hours to produce a report. Apparently, she had no intellectual curiosity to try to figure out why it ran so long. That, to me, was her cardinal sin- a lack of intellectual curiosity, a lack of desire to do what she could to improve her product. Her report produced correct results, but she could have done it right, she could have done it better.
I find I’m rambling here, in search of a point. Perhaps it’s just that quality is the important thing. Accuracy and clarity in the design. The ability to discern when something just isn’t right and know how to fix it. Be willing, whether a user or a programmer, to speak up whenever you see something that isn’t quite right, and talk to the people who can do something about it – don’t just vent to your fellow programmers or users. If a user, tell the programmer when you think his program isn’t working right, and show him how. If his pride gets hurt, so be it.
And, in harmony with what may be a recurring theme here, don’t worry about doing it the newest way; just do it the best way. Just do it right. That may not be easy; others may think another way of doing something is better (more right). If they are correct, then be willing to change.
And keep your mind open for change that is not the result of someone prodding you, but of you prodding yourself.
Control Breaks and Sanity
Charlie Massoglia, a well-known author in RPG circles, as well as the owner of his own consulting firm and past president of COMMON (a prominent IBM midrange user group) once wrote a series of articles introducing RPGIV to the world. In one of them he said: “Contrary to conventional wisdom, our shop does use control level indicators for reports since there is no easier way to produce subtotals and we have found negligible performance differences between input primary and full procedural files.” He acknowledged that he was likely a minority holder of the opinion when he followed the above remark and prefaced further discussion by saying, “Without getting into a debate about whether or not control level indicators should be used…”
A discussion of control breaks (or level breaks) almost always ends up in controversy. Even though control breaks, as part of the RPG cycle, have been part of the language, and very commonly used from the start, many RPG programmers don’t have a clue as to how the cycle functions, or even any reason why a programmer would even want it. It often goes beyond a intellectual discussion of merits and demerits of automatic level break handling by the cycle. Most RPG authorities would say that the less said about them the better. Bob Cozzi, an RPG authority, probably says that the “modern” RPG programmer does not use the cycle, though I haven’t looked at his classic “The Modern RPG Language” in a while, so I can’t be sure.
The divide seems to be at the System/38. If a programmer got his initial experience using RPGII on the System/3, System/34 or System/36, in my observation they have absolutely no problem understanding the RPG cycle and think handling level breaks using the cycle is the obvious way to go; If they started their RPG experience with RPG with RPGIII on the System/38 or RPG/400 or RPG IV on the AS/400, the cycle is mind-boggling and those who use it are demented.
Forum discussions of the RPG cycle often deteriorate into name-calling, generally at the expense of the old guard, who should just retire so the real programmers can fix what they did. It becomes almost a religious issue. Once I just brought up to someone the subject of level breaks using the cycle, not even arguing in favor of it, and by his tone of voice in reaction you would have thought I was suggesting I wanted forcible carnal knowledge of his sister.
I find that some authors will admit in forum entries that RPG using the cycle makes sense at least in some circumstances, even though their full blown books will consign discussion of it to an appendix entry.
Level breaks, as described above, are linked tightly to the RPG cycle. Until recently, all RPG programs use the RPG cycle automatically; however, most “modern” RPG programs tend to try to make believe that the cycle doesn’t exist. The one who wants to ignore the cycle will use a repeated READ or READE statement to read a file or a subset of it. The one who uses the cycle does not need to do this, assuming his main focus is reading a single file (or multiple files) from start to finish. (This programmer may use READ or CHAIN opcodes to read files subsidiary to the main file being read.).
For this reason, I will reserve a further discussion of level breaks until I discuss the RPG cycle. This will involve an interesting discussion of programmer psychology and why programmers sometimes reject useful techniques for the sake of an ideology.
The Good (Indicators Part 2)
Back around 1980 or so, I read an article or two by Dick Eagleson in an IBM minicomputer trade publication about the (then) relatively new concepts of structured programming. I believe it was before the System/38 came out, so he explained how to perform sequence, selection (IF-THEN-ELSE-END; CASE;and so on), and iteration (DO-ENDDO-DO WHILE, etc.) in the context of pre-System/36 RPGII, where all you had to work with was COMP and GOTO and TAG. (In this article, he also declared his Law of Software Strangeness: Any code you write and don’t look at for six months may as well have been written by someone else when you look at it again.)
In this article, he also declared that just because IBM provided 99 numbered indicators to use, it didn’t mean that you had to see how many you could stuff into any given program! It was not a revelation, because it was so obvious; but the implications were grand. You did not have to use all the indicators that come to mind in your programs. The need for indicator complexity was nonexistent; do it the structured way.
To give an example, suppose you wanted to convey this RPGIV concept:
This is how you could render it in RPGII:
This new way of thinking changed the way I programmed from then on. While I did not swear off indicators as I learned RPG400 and RPGIV, I made sure that my code would be as succinct as possible, using as few indicators as possible. At the same time, I was not afraid of using them or overly concerned about getting rid of them.
Much of the dislike for indicators seems to be based on the fact that they are not self-defining like a field name is (CUSTBAL, for instance meaning Customer Balance). If you set on indicator 89 for an unsuccessful CHAIN(random file access) operation, and you continue on into your calculations without referring to indicator 89 in your calculations, you tend to lose track of the significance of indicator 89. That is true. But immediately after the CHAIN, you DO know the significance of indicator 89. Do whatever you need to do with it-change the course of your processing, put out an error message, or whatever- then FORGET about indicator 89. If you really need to save the result of the CHAIN, set a field to Y or N based on the results (or in RPGIV, save it in a named indicator variable). Go ahead and use 89 somewhere else, if the spirit moves you.
Other examples could be given. Record identification indicators can be defined by the program to identify when a particular file is read. Especially if you are dealing with Input Primary and Input Secondary files (where entire files are read from beginning to end with no need for a READ calculation), these indicators are self-documenting (unless, if you’re foolish, you change their settings in calculations).
Now, do I use an indicator when doing a CHAIN operation? No, I don’t; I use the %FOUND BIF (built-in function) to test whether a chain was successful. But I have no problem with others using CHAIN with an indicator. At a previous employer, they were considering coding standards, and they wanted to mandate the use of BIFs like %FOUND. Another programmer pointed out that when you use %FOUND, when running a program in debug mode, the BIF does not give immediate feedback on the success of the CHAIN; using CHAIN with an indicator gives you immediate feedback. I had never thought of that; and I can see that as a reason why a programmer should indeed be permitted to use an indicator in this situation, if he deems it appropriate.
So I have no problem with indicators as such; only when they are used badly, as I showed in my previous post. And I think that RPG gurus and others who have a hissy fit when indicators are used (in a proper way) should just calm down, relax, get another cup of coffee and find something worth worrying about.
The Bad and the Ugly (Indicators- Part 1)
For the uninitiated, indicators are essentially Boolean variables which, until recently were defined automatically by the RPG compiler. (With RPGIV, you can now define your own named indicators. See this article for details.)
The system defined indicators are named by the digits 01-99, which may be defined by the programmer, L1-L9 to define level breaks(where changes in input data are automatically detected by the program), MR (used when matching file reads with similar keys), halt indicators H1-H9 to halt the program when an error occurs, and overflow indicators OA-OG and OV to flag when a printout is at end of page and ready to go to the next.
The trend today is to totally swear off indicator usage in RPG programs. This is understandable. Misused indicators have been the cause of many programmer headaches or worse. You’ve never lived until you’ve tried to get your head around a 10000 line RPG program that uses all 99 numbered indicators in calculations, and as many as possible in the output. You find yourself frantically turning from line 2000 to line 8763, trying to find out what on earth is the significance of indicator 56, or when it is true that indicators 45, 65, and 67 are on and 33 and 89 are off, with even fields in the output conditioned by up to three indicators.
Or how about these calculations and output?
There’s just too much going on in one line of code.
So when people want to get rid of indicators entirely, you can understand. But do such programs prove the point that indicators must be gotten rid of at all costs?
Well, not necessarily. It depends……
You are currently browsing the archives for the RPG category.
- Brian Kelly (1)
- COBOL (2)
- Gerald Weinberg (1)
- Oberon-2 (5)
- Personal (26)
- Programming (43)
- Python (2)
- RPG (36)
- Control (level) breaks (5)
- RPG Freeform (16)
- RPG/400 (4)
- RPGII (8)
- RPGIII (6)
- RPGIV (21)
- Uncategorized (6)
- Wirth (9)
RPG and Programming is proudly powered by WordPress