What is a Cobol 88-type equivalent in another lang

2019-06-24 06:54发布

问题:

I'm learning COBOL now and really liking the 88-type of variables, and I want to know if there are anything like them in another languages (most known languages also, such as C, Objective-C), even using a library.

The only thing I can think being similar is using

#define booleanResult (variableName==95) 

But it isn't possible to set boolenResult to true and make variableName assume 95 as value.

回答1:

05  nicely-named-data             PIC X.
    88  a-meangingful-condition   VALUE "A".
    88  another-meaingingful-condition 
                                  VALUE "A" "B" 
                                     "X" THRU "Z" 
                                      SPACE ZERO.

IF a-meaningful-condition
IF another-meaningful-condition
SET a-meaningful-condition TO TRUE
SET another-meaningful-condition 
                           TO TRUE

The IFs test the value referenced by the data-name (conditional variable) that the 88 (condition name) is associated with, for a single value or one of multiple value, which can included ranges (THRU) and figurative-constants (ZERO, SPACE, LOW-VALUES, etc).

The SET, which in this form is a more recent addition to COBOL from the 1985 Standard, will change the value of the data-name to the first value specified on the 88, such that if you immediately referenced the 88 in a test, the test would be true.

COBOL does not have booleans in the sense of something resolving to 0 or 1, or anything else, being false/true.

Any language which supports Objects could be used to mimic the behaviour. Perhaps you've even done it already without really realising it.

As NealB points out in the comments, functions could be used (or a procedure, or a transfer of control to another module) but the data and references to it would not be together and protected from accidental mischief.

COBOL has great flexibility in defining data-structures. The 88-level is a powerful aid to maintaining and understanding programs, as well as writing them in the first place.

I don't know of another language which has a direct and natural element which is equivalent to this, but then there are lots of languages I don't know.

Again NealB makes an important point in the comments about the use of THRU/THROUGH to specify a range of values.

Care does need to be taken. Although the author may think that the data that they want to select can be represented by the range "010" THRU "090", they may not realise that what the compiler does is to include every single possible value in that range, by generating code for greater than or equal to "010" and less than or equal to "090".

If using THRU, ensure that your data cannot contain anything in the range which is not expected. If you mean "010" "020" "030" ... "090" that is fine, as long as the data is validated at its entry-point, so that it can never include any intervening values.

The classic example is "A" THRU "Z" on the Mainframe. We all know what the author means, but the compiler takes it literally. You cannot use "A" THRU "Z" on its own for validation, because in EBCDIC there are "gaps" between three groups of letters, and using "A" THRU "Z" would treat those gaps as true for a use of the 88.

Where the 88 level in some COBOL compilers does fall down, is in the missing "FALSE".

To re-use from the above example:

    88  a-meaingingful-condition  VALUE "A".
    88  a-meaingingful-condition-NOT 
                                  VALUE "N".

To test the switch/flag, you use the first 88. To turn the flag.switch off, you have to use the second. Not ideal. See one of the links below for an example of FALSE on the 88 definition.

In olden times, flags/switches were set and reset with MOVE statements. As soon as the MOVE is involved, you have the same problem as you have in trying to use functions. There is no bound relationship between the MOVE and the 88-level VALUE.

These days, SET can be used to change the value of a field, to turn a flag/switch on or off.

    05  FILLER PIC X.
        88  a-meaingingful-condition 
                                  VALUE "A".
        88  a-meaingingful-condition-NOT 
                                  VALUE "N".

The field being tested does not even need a name (it can be FILLER or omitted (an implied FILLER)).

Of course, as NealB points out in a comment on one of the links below, someone can still get at the field with a MOVE using reference-modification on a group item. So...

01  FILLER.
    05  FILLER PIC X.
        88  a-meaingingful-condition 
                                  VALUE "A".
        88  a-meaingingful-condition-NOT 
                                  VALUE "N".

Now they can't use reference-modification even, as there is no field to name. The value of the field can only come from a VALUE clause on the definition, or from a SET statement setting one of the 88s to TRUE.

At the stage, the value that a flag/switch has, its actual value, becomes irrelevant.

01  FILLER.
    05  FILLER PIC X(7).
        88  a-meaingingful-condition 
                                  VALUE "APPLE".
        88  a-meaingingful-condition-NOT 
                                  VALUE "BICYCLE".

Because nothing can be used to test against a literal/data-name, and the field cannot be the target of any verb except SET, you no longer have to check that all fields which say they contain N, or Y, or 0, or 1, do so, and they're not the wrong case, and no other values get placed in those fields.

I'm not suggesting the use of APPLE and BICYCLE, just using them to illustrate the point.

An 88 can also have a value expressed in hexadecimal notation, like any alpha-numeric field:

    88  a-meaingingful-condition  VALUE X"25".

An 88 can also be specified on a group item, typically with a figurative-constant as the value:

01  a-group-item.
    88  no-more-data-for-matching VALUE HIGH-VALUES.
    05  major-key                 PIC X(10).
    05  minor-key                 PIC X(5).

In a file-matching process, the keys can be set to high-values at end-of-file, and the use of the keys will still cause the other file(s) to be processed correctly (keys lower than on this file).

Here are links to a number of questions from SO relating directly, or tangentially with important aspects, to 88-levels.

COBOL level 88 data type

Group variable in cobol

In Cobol, to test "null or empty" we use "NOT = SPACE [ AND/OR ] LOW-VALUE" ? Which is it?

Does a prefix of "NO" have any special meaning in a COBOL variable?

COBOL Data Validation for capital letter?



回答2:

My first programming language was Cobol, now I am using c# and here is my solution to Cobol's 88 level:

In Cobol:

01  ws-valid-countries pic xx.
    88  valid-country 'US', 'UK' 'HK'.

move ws-country  to ws-valid-countries

if  valid-country
    perform...

in C#

string[] ValidCountries = {"US","UK","HK"} ;

if ( ValidCountries.Contains(newCountry.Trim().ToUpper())  )
{
   // do something


回答3:

Think of it as a boolean getter (essentially as in your macro) and a setter (forcing the variable to be the corresponding value). Who says COBOL wasn't modern in 1965?



回答4:

As others said, just some object programming. Which is more powerful, but far less elegant. Like :

01  MY-DATASET.
    05  MY-DEPARTEMENT PIC 9(2).
        88  ILE-DE-FRANCE VALUES 75, 77, 78, 91 THRU 95.

Can be roughly translated in old VBA in a class named MyDataset :

Public MyDepartement As Integer
Property Get IleDeFrance() As Boolean
    Dim MyArray() As Variant
    MyArray = Array(75, 77, 78, 91, 92, 93, 94, 95)
    IleDeFrance = UBound(Filter(MyArray, MyDepartement, True)) > -1
End Property

(just tested, it works on VBA-excel2013) And I made the VBA as simple as possible, no clean getter or setter for the departement number, just a public data. As a class is a depot of data plus coded actions against them, you can do more things inside than a simple 88-level(that's probably why this feature didn't make up to more modern languages). But at a the price of complexity & readability.

Less elegant because the array has to be specifically defined, and testing presence in it has to be specified also. While it's inherent to the wonderful 88 level.