Understanding Variable Naming Conventions

Welcome! In this lesson, we'll shift gears from arithmetic to another crucial aspect of COBOL programming: variable naming conventions. While arithmetic operations are vital for calculations, understanding how to name your variables properly will make your code more readable and maintainable. This ensures that others (and you in the future) can easily understand what your code is doing.

What You'll Learn

In this lesson, you'll learn about:

  1. Importance of Descriptive Names: Why using meaningful variable names is essential.
  2. Consistency in Naming: How consistency in variable naming improves readability.
  3. Practical Examples: Illustrations with COBOL code to reinforce these best practices.

First, why is this important? Using clear and consistent variable names helps anyone reading your code understand its intent. This is especially critical in business applications, where clarity can prevent errors and improve collaboration among developers.

Let's look at some COBOL examples to illustrate good and bad naming practices:

IDENTIFICATION DIVISION.
PROGRAM-ID. SimpleDemo.

DATA DIVISION.
WORKING-STORAGE SECTION.

*> Bad Examples:
*> Non-descriptive names
01 z PIC 9(5).

*> Inconsistent naming
01 CUSTOMER_ID PIC 9(5).
01 customerName PIC A(20).

*> Best Practices:
01 CUSTOMER-ID         PIC 9(5).
01 CUSTOMER-FIRST-NAME PIC A(20).
01 CUSTOMER-LAST-NAME  PIC A(20).

PROCEDURE DIVISION.
    MOVE 12345 TO CUSTOMER-ID.
    MOVE "John" TO CUSTOMER-FIRST-NAME.
    MOVE "Doe" TO CUSTOMER-LAST-NAME.

    DISPLAY "Customer ID: " CUSTOMER-ID.
    DISPLAY "Customer Name: " CUSTOMER-FIRST-NAME " " CUSTOMER-LAST-NAME.
    STOP RUN.

Historical Note: COBOL originated in the 1960s and 70s, when keypunch machines only allowed programmers to write in uppercase letters. As a result, early COBOL code and variable names were always in uppercase. Nowadays, most modern COBOL compilers can interpret lowercase variable names as well. In this path, as we did in other units, we will often use lowercase for readability, but you may still see uppercase in many real-world COBOL programs.

Breaking Down the Code
  1. Bad Practices:

    • Using non-descriptive names like z makes it hard to understand what the variable represents.
    • Inconsistent naming conventions can lead to confusion. Notice the mix of lowercase and uppercase in the variable names.
    • Examples:
      01 CUSTOMER_ID PIC 9(5).
      01 customerName PIC A(20).
  2. Good Practices:

    • Use descriptive and consistent names for better readability and maintainability.
    • Examples:
      01 CUSTOMER-ID         PIC 9(5).
      01 CUSTOMER-FIRST-NAME PIC A(20).
      01 CUSTOMER-LAST-NAME  PIC A(20).

In this example, properly named and formatted variables immediately convey their purpose, making the code easier to read and understand.

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal