MainframeMaster

COBOL Tutorial

COBOL ATTRIBUTE - Quick Reference

Progress0 of 0 lessons

Overview

ATTRIBUTE in COBOL refers to properties or characteristics of data items, files, or other program elements. It describes various aspects such as data type, format, behavior, or other specifications that define how data should be handled or processed, providing better data organization and control.

Purpose and Usage

  • Data property definition
  • File characteristic specification
  • Object property definition
  • Data validation rules
  • Format and behavior control

Syntax

ATTRIBUTE specifications follow specific syntax patterns in COBOL:

Basic Attribute Syntax

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
* Basic attribute specifications 01 customer-data. 05 customer-id PIC X(10) ATTRIBUTE REQUIRED. 05 customer-name PIC X(50) ATTRIBUTE NOT NULL. 05 customer-email PIC X(100) ATTRIBUTE VALIDATE. * Attribute with validation 01 product-record. 05 product-id PIC X(8) ATTRIBUTE UNIQUE. 05 product-name PIC X(40) ATTRIBUTE REQUIRED. 05 product-price PIC 9(6)V99 ATTRIBUTE POSITIVE. * File attributes FD customer-file ATTRIBUTE ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS customer-id.

ATTRIBUTE specifications define properties of data items and files.

Object-Oriented Attributes

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
* Object-oriented attributes CLASS-ID. CustomerClass. OBJECT SECTION. CLASS CustomerClass. 01 customer-id PIC X(10) ATTRIBUTE PRIVATE. 01 customer-name PIC X(50) ATTRIBUTE PUBLIC. 01 customer-email PIC X(100) ATTRIBUTE PROTECTED. * Method attributes METHOD-ID. getCustomerInfo ATTRIBUTE PUBLIC. DATA DIVISION. LINKAGE SECTION. 01 return-info PIC X(100). PROCEDURE DIVISION RETURNING return-info. * Method implementation MOVE customer-name TO return-info. EXIT METHOD. END METHOD getCustomerInfo. END CLASS CustomerClass.

Practical Examples

Here are some practical uses of ATTRIBUTE in COBOL:

Data Validation Attributes

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
* Data validation with attributes DATA DIVISION. WORKING-STORAGE SECTION. 01 employee-record. 05 employee-id PIC X(8) ATTRIBUTE REQUIRED. 05 employee-name PIC X(50) ATTRIBUTE NOT NULL. 05 employee-salary PIC 9(7)V99 ATTRIBUTE POSITIVE. 05 employee-email PIC X(100) ATTRIBUTE VALIDATE. 05 employee-status PIC X(1) ATTRIBUTE VALUES "A", "I", "T". PROCEDURE DIVISION. VALIDATE-EMPLOYEE-DATA. * Check required attributes IF employee-id = SPACES DISPLAY "Employee ID is required" PERFORM error-handling END-IF. IF employee-name = SPACES DISPLAY "Employee name is required" PERFORM error-handling END-IF. * Check positive salary IF employee-salary <= 0 DISPLAY "Salary must be positive" PERFORM error-handling END-IF. * Check valid status IF employee-status NOT = "A" AND employee-status NOT = "I" AND employee-status NOT = "T" DISPLAY "Invalid employee status" PERFORM error-handling END-IF. DISPLAY "Employee data validation successful". ERROR-HANDLING. DISPLAY "Data validation failed". * Additional error handling logic.

Data validation using ATTRIBUTE specifications.

File Organization Attributes

cobol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
* File organization with attributes ENVIRONMENT DIVISION. INPUT-OUTPUT SECTION. FILE-CONTROL. SELECT customer-file ASSIGN TO "CUSTOMER.DAT" ORGANIZATION IS INDEXED ACCESS MODE IS RANDOM RECORD KEY IS customer-id ALTERNATE RECORD KEY IS customer-name FILE STATUS IS customer-file-status. DATA DIVISION. FILE SECTION. FD customer-file ATTRIBUTE ORGANIZATION IS INDEXED. 01 customer-record. 05 customer-id PIC X(10) ATTRIBUTE KEY. 05 customer-name PIC X(50) ATTRIBUTE ALTERNATE-KEY. 05 customer-email PIC X(100) ATTRIBUTE UNIQUE. 05 customer-phone PIC X(15) ATTRIBUTE OPTIONAL. PROCEDURE DIVISION. PROCESS-CUSTOMER-FILE. * Open indexed file OPEN I-O customer-file. * Read customer by ID (primary key) MOVE "CUST001" TO customer-id. READ customer-file INVALID KEY DISPLAY "Customer not found" NOT INVALID KEY DISPLAY "Customer: " customer-name END-READ. * Read customer by name (alternate key) MOVE "John Doe" TO customer-name. READ customer-file INVALID KEY DISPLAY "Customer not found by name" NOT INVALID KEY DISPLAY "Customer ID: " customer-id END-READ. CLOSE customer-file. STOP RUN.

File organization using ATTRIBUTE specifications.

Best Practices

  • Use ATTRIBUTE specifications to define clear data properties and constraints.
  • Specify validation attributes for data integrity.
  • Use appropriate file organization attributes for efficient data access.
  • Document attribute specifications for maintenance.
  • Consider performance implications of attribute specifications.
  • Use consistent attribute naming conventions.

Common Pitfalls

  • Not specifying appropriate validation attributes.
  • Using conflicting attribute specifications.
  • Not considering performance impact of attribute specifications.
  • Failing to document attribute meanings and purposes.
  • Over-specifying attributes that are not needed.

Test Your Knowledge

1. What is ATTRIBUTE in COBOL?

  • A data type
  • A property or characteristic of data
  • A file operation
  • A program structure

2. What does ATTRIBUTE typically describe?

  • Only numeric properties
  • Only file properties
  • Properties of data items, files, or objects
  • Only program properties

3. Where are ATTRIBUTE specifications typically used?

  • Only in the PROCEDURE DIVISION
  • In data declarations and file specifications
  • Only in the ENVIRONMENT DIVISION
  • Only in the WORKING-STORAGE SECTION

4. What is a common use of ATTRIBUTE?

  • Arithmetic operations
  • Defining data characteristics and properties
  • File operations
  • Program flow control

5. How does ATTRIBUTE help in COBOL programming?

  • It improves performance
  • It provides better data organization and control
  • It reduces memory usage
  • It simplifies syntax

Frequently Asked Questions