Coding expressions

When coding expressions, consider the following recommendations:
  • When components of an expression are duplicate expressions, code them either at the left end of the expression or within parentheses, as shown in the following example.
    a = b*(x*y*z);            /* Duplicates recognized */
    c = x*y*z*d;
    e = f + (x + y);
    g = x + y + h;
    
    a = b*x*y*z;              /* No duplicates recognized */
    c = x*y*z*d;
    e = f + x + y;
    g = x + y + h;

    The compiler can recognize x*y*z and x + y as duplicate expressions when they are coded in parentheses or coded at the left end of the expression.

    It is the best practice to avoid using pointers as much as possible within high-usage or other performance-critical code.
    Note: The compiler might not be able to optimize duplicate expressions if either of the following are true:
    • The address of any of the variables is already taken
    • Pointers are involved in the computation
  • When components of an expression in a loop are constant, code the constant expressions either at the left end of the expression or within parentheses.
    The following example shows the difference in evaluation when c, d, and e are constant and v, w, and x are variable.
    v*w*x*(c*d*e);       /* Constant expressions recognized */
    c + d + e + v + w + x;
    
    v*w*x*c*d*e;         /* Constant expressions not recognized */
    v + w + x + c + d + e;