Tuesday, August 31, 2021

nit-picking knf question

Where to declare local variables used in a small subordinate scope?
Specifically at the top of a routine or at the beginning of the
context where they're used?
I don't see it in style(9)

i.e.
extern int fum;

int
foo()
{
here 1> int a_local;
        while (condition()) {
               if (another_condition()) {
here 2>   int a_local;
                      a_local = fum;

IIRC #1 is traditional and can be very clear but doesn't limit scope.
Most of the time the clarity & convenience are most important.
#2 has some advantages but not necessarily enough to outweigh #1.

Is the placement defined or is it up to the programmer?

thanks
geoff steckel

No comments:

Post a Comment