On 9/02/2017 11:30 p.m., Bill Woodger wrote:
> Hi Pete,
>
> Can you provide an example of the compound IF thing you are talking about? With realistic names (maybe ask the lady in (the) question to provide a lump of code).
For privacy reasons I wouldn't post any of the code concerned, but I
think I can come up with a realistic simplification of a real-world
commercial process.
Consider the case where it is required to translate numbers printed on a
cheque into words. Pretty easy you'd think... build a list of
numbers/words, parse each digit in the amount and construct your cheque
line...
so, e.g:
10.00 = TEN DOLLARS EXACTLY
21.45 = TWENTY-ONE DOLLARS AND FORTY-FIVE CENTS
Pretty straightforward. You have to juggle the words MILLION, THOUSAND,
HUNDRED, DOLLARS, AND, CENTS, and EXACTLY
7,654,321.99 = SEVEN MILLION SIX HUNDRED AND FIFY-FOUR THOUSAND THREE
HUNDRED AND TWENTY-ONE DOLLARS AND NINETY-NINE CENTS
If you actually sit down and write some code to do this (I haven't time
at the moment but if anybody else posts code here I'll make time...),
you will realise that the trickiest bit is setting the ANDs in the right
place... you start making rules, something like:
(This is a trivial example, but I don't have the time to create a much
more complex one and the rules below could be much more plentiful and
involve more variables if a less experienced programmer were formulating
them.)
1. If there is a positive number to the right of the point, there must
be an AND before that number, but only if there is a positive number to
the left of the point.
2. The word AND must follow the word HUNDRED, but only if there is a
positive number between the digit preceding HUNDRED, and the point.
3. The word AND must follow the word DOLLAR(S), but only if there is a
positive number to the right of the point.
We could code this from the rules above:
IF POINT-RIGHT IS POSITIVE AND POINT-LEFT IS POSITIVE
OR
CURRENT-WORD-HUNDRED AND POINT-LEFT IS POSITIVE
OR
CURRENT-WORD-DOLLAR OR CURRENT-WORD-DOLLARS AND POINT-RIGHT IS POSITIVE
<then string the word AND into the cheque line...>
But something tells us this is not as good as it could be, so convert it
to symbolic logic and mess with it using Boolean Algebra...
(Read ^ as AND and + as OR... I deliberately contrived to keep NOT out
of it for the sake of this simple example...)
1 = PR ^ PL
2 = WH ^ PL
3 = WD ^ PR
Applying simplification by Boolean Algebra:
(PL ^ (PR + WH)) + (PR ^ WD)
So the simplified expression is:
IF (POINT-LEFT IS POSITIVE AND
(POINT-RIGHT IS POSITIVE OR
CURRENT-WORD-HUNDRED) OR
(POINT-RIGHT IS POSITIVE AND
CURRENT-WORD-DOLLAR OR
CURRENT-WORD-DOLLARS))
<then string the word AND into the cheque line...>
I realize it is not a dramatic example but, hopefully, you'll catch my
drift...(The previous 7 tests have been reduced to 6; I had one example
many years ago where I was able to reduce 24 compound conditions down to
8. Results CAN be dramatic.)
>
> I know we did this before, on LinkedIn, but I didn't really get it :-)
I don't remember "doing it on LinkedIN" (although I don't doubt you
because it is something I have a strong interest in) and I'm too tired
at the moment to go and look... :-)
>
> Is there much use in saying how to write pre-COBOL 85 nested IFs?
Unfortunately, the point I was trying to make seems to have been
generally missed. "DON'T USE NESTED IFs UNLESS you are describing a TREE
structure, and then ONLY nest into the POSITIVE branches." It is as
valid and true in 2017 as it was in 1967. Using EVALUATE is NOT
addressing a TREE structure, unless you use EVALUATE TRUE... ALSO TRUE
(which I personally don't.)
Remember, I made the post because I was asked my opinion. I'm not
suggesting what I posted is the ONLY way; all I'm saying is that it
works for me... If other people get some use from it, great; if not
then, I may have wasted some time. At least my opinion is now
documented. :-) The person who asked me felt she got something of value
from the exchange.
Or does END-IF fall under your "too much typing, not good for you,
even if it makes it clearer"?
For me, END-IF is another scope delimiter and, as such, it is priceless.
I ALWAYS use scope delimiters so I ALWAYS use END-IF. One of the first
machines I ever wrote COBOL for was an ICL 1901 which had a barrel
printer and it was notoriously hard to distinguish commas, full stops,
and smudges on the paper. Scope delimiters were a much-needed blessing
and we embraced them.
>
> In general I'm mightily surprised at the idea of someone doing maintenance fiddling with anything not directly within their spec for the change. I've never had a manager happy with "yes, make lots of changes, spend some time on it, extra testing (where "regression tests" are lacking) and then have it do exactly the same thing as it was doing before. I'm happy to sign-off on that".
>
> Understand it, yes. Document it, yes. Change it? No.
In smaller companies the lines are more blurred. The person concerned
with this has a stake in the company. The "spec" is to make it work
slightly differently than it currently does and she has a vested
interest in making it so.
>
> EVALUATE data-name? Maintenance rats-nest. Analyst looking up from document: "Find me all the programs that use the literal one, and confirm whether any of them are related to ageing-method," reads further, "oh, and the letter A if it is related to warehouse stacking," and then "oh, and the letter A if it is related to transport origin".
Any good cross reference/search/editor should kill that, whether the
code is structured or not. I don't see how EVALUATE dataname makes it
any more difficult.
>
> It is ironic that many "structured things" now in COBOL allow garbage that you'd not have got away with in the 80s, but now the defence is simple - "it's OK, I'm using a Structured Construct".
>
Bad code, like the poor, is always with us. When approaching the problem
of skinning cats it is good to have many knives and many ways to do it.
Thanks for your post, Bill.