However, the key point is that I maintain a lot of code that was not
written according to these guidelines. I've got a fair amount of code
where a single function might contain hundreds and even thousands of
lines.
Yeah, and how much nested scope does that translate to? Remember its
O(*LOG*(#lines of code)).
[...] If I had the resources to do so, I might consider re-writing
those changes. However, in the absence of any known actual bugs in the
code, it's pretty hard to justify applying my scarce resources to
perform aesthetically motivated fix-ups.
Well more importantly what if there is *ONE* bug in there. Would you
rather have your scarce resources scan O(#lines) or O(*LOG*(#lines))
for where declarations are, to see if there is a simple "return
pointer to a local" kind of error?
[...] Since scopes nest in C,
there's no way to be certain unless declarations are restricted not
merely to the top of the scope, but to the top of the function.
This is the very definition of pedantry. The problem of finding the
top of a function is not significantly different from finding the set
of scope beginnings up to the top of the function declaration. The
depth of your scopes is typically O(log(lines in function)).
Nor it it very different from searching for previous definitions of
the identifier by name, which I've found to be a much quicker and more
reliable method.
Really? Personally, I cannot even make that work. VC++'s editor does
not have a backward search, nor does it have Perl compatible RE
search, nor does it have simple semantics for ping-pong buffering
between two different searches. But in C89, its hardly needed as an
eyeball scan is quick and usually sufficient.
[...] I would
argue against the "top of the function" solution, but since you don't
seem to be advocating it, I won't bother.
Since a large fraction of the code I've ever worked on was written by
other people, there's no guarantee that any given variable is declared
in any particular scope, no matter what I might prefer. I have to do
the same kind of backwards search to find the top of the right scope
that I would have to perform to find a declaration that's not at the top
of a scope, so I don't see how it saves me any trouble.
Search backwards through indents or "{"s is the same as scanning line
by
line to you?
Finding the indents or "{"s isn't sufficient; then you must still scan
line by line for the matching declaration; the context switch between
the two search modes costs at least as much as the nominally more
efficient mode saves; that's true whether using my eyes or using a
text editor.
WTF?!?! You do a "context switch" *BETWEEN EACH PAIR OF LINES* in C99
(and C++). You only do so between scope starts, declarations and
statements, in C89. You are not arguing honestly.
That's not a problem with my approach. My approach has never been
"smallest scope, regardless of whether or not it works". I've always
considered that it was implicitly clear that I was referring to the
smallest scope that is consistent with the actual use of the variable.
Yes, I was not accusing you of writing wrong code -- that's precisely
the problem! *Correctness* dictates where you put your declaration,
not some aspiration that your declarations will be close to all its
usages. And that means the above example becomes a very typical
situation.