In COBOL, REDEFINES is one of the frequently being used phrase. It has many advantages if you use it properly.
COBOL REDEFINES clause you can use at two levels:
- At group level (01 level)
- At field level
The REDEFINES clause allows you to define the same data field or record in more than one way, as shown in this example:
03 MY-DATE PIC 9(08).
03 MY-DATE-R REDEFINES MY-DATE.
05 MY-YEAR PIC 9(04).
05 MY-MONTH PIC 9(02).
05 MY-DAY PIC 9(02).
In this example, MY-DATE and MY-DATE-R refer to the same data but are described differently.
5 Top Rules to use REDEFINES in COBOL
- MY-DATE is numeric. MY-DATE-R is a group item, so it is inherently alphanumeric, generally equivalent to X(8), except that if you move MY-DATE-R to an edited field, the result will be as if the receiving field were alphanumeric and not edited.
- This capability is very useful, particularly when handling numeric fields or fields that may or may not be numeric. In many systems, referencing a numeric field that contains non-numeric characters can cause a program abort. Having an alphanumeric definition of that field enables you to examine it before using it and to deal with it if it is not numeric.
- When you redefine a 01 record with another 01, either record can be larger, smaller, or the same size as the other. But when you redefine field-1 by field-2 at a higher-level number, field-1 must not be smaller than field-2.
- Tip Indenting the PICTURE of the redefining field and its subordinate entries visually separates the redefinition from the field it is redefining. This is useful when trying to determine the record size or position of fields within the record.
- You can redefine a field any number of times, but each REDEFINES must immediately follow the field being redefined or a previous redefinition of the field. You can also redefine a field that is subordinate to a redefinition.
Here is an example: 03 MY-DATE PIC 9(08). 03 MY-DATE-R REDEFINES MY-DATE. 05 MY-YEAR PIC 9(04). O5 MY-CCYY REDEFINES MY-YEAR. 07 MY-CC PIC 9(02). 07 MY-YY PIC 9(02). 05 MY-MONTH PIC 9(02). 05 MY-DAY PIC 9(02).
As with group-level items, redefining fields can be a source of errors if you are not careful when constructing your definitions.