Local Variables

A few rules for local variables:

One might think that minimizing the number of declared variables may be more efficient; however, the compiler should be able to determine the most appropriate use of registers and memory locations on the stack for these variables: if one variable is

Declare close to the intended use

As a local variable can be declared at any time prior to its first use, it is most appropriate to declare it as close as possible to the first use. Suppose, for example, that a function deals with a number of special cases, first. The special cases may throw exceptions, return default values, etc., and only at the end is the general case dealt with. In this case, all local variables used in the general case should be declared after the special cases; consequently

void function() {
	if ( ... ) {
		...

		throw exception();
	}

	if ( ... ) {
		...

		return;
	}

	Type obj;
	int sum;

	...
}

is preferable to

void function() {
	Type obj;
	int sum;

	if ( ... ) {
		...

		throw exception();
	}

	if ( ... ) {
		...

		return;
	}

	...
}

Always try to initialize variables

If you are not initializing the value of a local variable, you should ask yourself why the variable cannot be initialized. Are you using it for multiple tasks? If so, declare two separate variables closer the region where they are being used. As an example of a use where a local variable may appear to be uninitialized, consider a for-loop where the value of the index is required after the loop completes:

	// Find the appropriate index, and from that location, after which,
	// we will ...

	int i = 0;

	for ( ; i < n; ++i ) {
		// ...
	}

	// use i

Do not unnecessarily share declarations

Do not use the same local variable for different tasks, even if those tasks are similar. In this case, the local varable rv is used to temporarily store a return value.

	for ( int i = 0; i < n; ++i ) {
		...

		Type rv = ...;

		...

		if ( ... ) {
			return rv;
		}
	}

While one could declare the variable outside the loop, this suggests that the variable will be shared between iterations of the loop and may be used after the loop is complete—neither of which are necessarily true.

	Type rv;

	for ( int i = 0; i < n; ++i ) {
		...

		rv = ...;

		...

		if ( ... ) {
			return rv;
		}
	}

As an example of where a return value may be declared outside the loop is the inner product:

	double product = 0.0;

	for ( int i = 0; i < n; ++i ) {
		product += vec1[i] * vec2[i];
	}

	return product;

In this case, the local variable is initialized, shared between the loops, and used after the loop completes.

Minimize the scope

If you want to emphasize that the scope of a variable is restricted, you can always place the code into its own block:

	{
		int tmp = a;
		a = b;
		b = tmp;
	}

This is preferable to having a single variable declared in a procedure that is to be used for swapping:

void function() {
	int tmp;

	...

	tmp = a;
	a = b;
	b = tmp;

	...

	tmp = c;
	c = d;
	d = tmp;

	...
}

The second suggests that the variable tmp may be used outside the simple operation of being a temporary variable for swapping.