Variables


Use variables with care. Improper use can affect code readability and performance.

This section describes:



Global Variables

Avoid global variables whenever possible. Use static class members instead.



Static Variables

Use static variables with care. They can be problematic when developing code that must run under multiple threads.

Developers often use static variables in functions that return a value or object that is generated or computed dynamically. The easiest solution is to keep a static variable in which the computed value is placed, and return a pointer to the static variable. The problem is that if multiple threads attempt to execute the function at the same time, one of them will overwrite the results for the other.

The best solution in this case is to design the function so that the caller passes in the value or object to be filled out. This is a common place to use references.



Local Variables

When using a C++ compiler, local variables can be declared anywhere within a file or body of a function. Declare local variables as late in the code as appropriate. Place one blank line after each group of variable declarations, and place one blank line before each group except when it immediately follows an opening brace.

For example:


// In this function, 'iter' and 'shape' are declared inside the if block, and
// therefore are only constructed if the if clause is used.
myFunc(const oaNet      &net,
        const oaTerm    &term)
{
    oaString    netName;
    oaString    termName;

    net.getName(netName);
    term.getName(termName);

    if (netName == termName) {
        oaNetShapeIter  iter;
        oaShape         shape;

        while (iter.getNext(shape)) {
            . 
            .
            .
        }
    }
}

Define local variables using the most limited scope possible. The previous example could have declared iter and shape just above the if statement. However, because these variables are only used inside the if block, they should be placed inside the block, limiting their scope and ensuring they are only constructed when the if clause is true.

Declare only one local variable per line. This adds clarity to variable definitions and encourages variable construction or initialization at the same time as the declaration.

For example, the following is incorrect:

	// Don't do this.
	oaUInt4 termIndex, netIndex, shapeIndex;

	termIndex = term.index;
	netIndex = termTbl::getNet(termIndex);
	shapeIndex = netTbl::getFirstShape(netIndex);

Instead, put each variable on its own line and do as much initialization or construction as possible:

	// This is much cleaner, and sometimes more efficient.
	oaUInt4 termIndex       = term.index;
	oaUInt4 netIndex        = termTbl::getNet(termIndex);
	oaUInt4 shapeIndex      = netTbl::getFirstShape(netIndex);

This example also illustrates another guideline. If you initialize several related variables together, align the assignments using indentation unless this makes the code more difficult to read.

Local variables can be declared within the expression of a while loop and within the initializer of a for loop.  Use a space (not a tab) to separate the type and the name of the variable.

For example:



while (oaProp *prop = iter.getNext()) { 
    prop->destroy();  
} 

for (oaUInt4 i = 0; i < numUsed; i++) {  
    if (!isDeleted(i)) { 
        hashTbl->add(i);  
    } 
}  

#define Statements

The #define statement is a pre-processor instruction. The compiler simply inserts the specified value into the code whereever it sees the defined value.

For example, if you use the following statement, whereever myInitVar appears, the compiler inserts the value 20:

#define myInitVar       20

Most debuggers cannot properly handle this situation; if you ask the debugger for the value of myInitVar, the debugger doesn't know. It doesn't even know it is a variable.

Some compilers duplicate storage for the variable if a #define statement is used. Also, there is no typing associated with a #define variable.

Instead of using #define, define a const variable. For example:

const oaUInt4   myInitVar = 20;




Return to top of page