Best Coding Practices


This section of the coding standards offers guidelines for writing clean and concise OpenAccess code. While these are guidelines, not rules, they are considered best practices for OpenAccess coding and development.

Using for Instead of while Statements

It is common to see a control loop written as follows:

    oaUInt4 i = firstMem;
    while (i != oacNullIndex && (objectTbl[i] != objIndex || typeTbl[i] != objType)) {
        i = nextTbl[i];
    }

Although the while statement obtains the necessary results, it is simpler, cleaner, and more obvious to use a for statement instead. For example:

    for (oaUInt4 i = firstMem; i != oacNullIndex && (objectTbl[i] != objIndex || typeTbl[i] != objType); i = nextTbl[i]) {
        ;
    }

An alternative approach, which can be even more obvious, tests the loop bounds in the for statement and checks the search condition with a separate if statement. For example:

    for (oaUInt4 i = firstMem; i != oacNullIndex; i = nextTbl[i]) {
        if (objectTbl[i] != objIndex || typeTbl[i] != objType) {
            break;
        }
    }

Return to top of page