Consider the following guidelines:
- If components of an expression are used in other expressions
and they include function calls or there are function calls between
the uses, assign the duplicated values to a local variable.
- Avoid forcing the compiler to convert numbers between integer
and floating-point internal representations. For example:
float array[10];
float x = 1.0;
int i;
for (i = 0; i< 9; i++) { /* No conversions needed */
array[i] = array[i]*x;
x = x + 1.0;
}
for (i = 0; i< 9; i++) { /* Multiple conversions needed */
array[i] = array[i]*i;
}
When you must use mixed-mode arithmetic, code
the integer and floating-point arithmetic in separate computations
whenever possible.
- Do not use global variables as loop indices or bounds.
- Avoid goto statements that jump into the middle
of loops. Such statements inhibit certain optimizations.
- Improve the predictability of your code by making the fall-through
path more probable. Code such as:
if (error) {handle error} else {real code}
should
be written as: if (!error) {real code} else {error}
- If one or two cases of a switch statement are
typically executed much more frequently than other cases, break out
those cases by handling them separately before the switch statement. If possible, replace the switch statement
by checking whether the value is in range to be obtained from an array.
- Use try blocks for exception handling only
when necessary because they can inhibit optimization.
- Keep array index expressions as simple as possible.