Initializing Variables

Variables store information in memory that is used by the program. Variables can store strings of text and numbers.

When you declare a variable, you should also initialize it. Two types of variable initialization exist: explicit and implicit. Variables are explicitly initialized if they are assigned a value in the declaration statement. Implicit initialization occurs when variables are assigned a value during processing.

This information covers standards for declaring and initializing variables in business functions and includes an example of standard formats.

Use these guidelines when declaring and initializing variables:

  • Declare variables using this format:

    datatype variable name = initial value; /* descriptive comment*/
    
  • Declare all variables used within business functions and internal functions at the beginning of the function. Although C allows you to declare variables within compound statement blocks, this standard requires all variables used within a function to be declared at the beginning of the function block.

  • Declare only one variable per line, even if multiple variables of the same type exist. Indent each line three spaces and left align the data type of each declaration with all other variable declarations. Align the first character of each variable name (variable name in the preceding format example) with variable names in all other declarations.

  • Use the naming conventions set forth in this guide. When initializing variables, the initial value is optional depending on the data type of the variable. Generally, all variables should be explicitly initialized in their declaration.

  • The descriptive comment is optional. In most cases, variable names are descriptive enough to indicate the use of the variable. However, provide a comment if further description is appropriate or if an initial value is unusual.

  • Left align all comments.

  • Data structures should be initialized to zero using the memset function immediately after the declaration section.

  • Some Application Program Interfaces (APIs), such as the JDB ODBC API, provide initialization routines. In this case, the variables intended for use with the API should be initialized with the API routines.

  • Always initialize pointers to NULL and include an appropriate type call at the declaration line.

  • Initialize all variables, except data structures, in the declaration.

  • Initialize all declared data structures, MATH_NUMERIC, and JDEDATE to NULL.

  • Ensure that the byte size of the variable matches the size of the data structure you want to store.