what's meaning of this ?

B

Barry Schwarz

snip
I'm still surprised to find variables that are only used in a restricted
scope (say a loop) declared at the top of a function in C89 code. I
could never understand why people do this. What makes the situation

Maybe to avoid the overhead of repeatedly creating and destroying the
variable as the block is entered and exited.

Maybe so that when someone looking to understand the code sees a new
object name, the definition of that object can be found at the top of
the function without having to scroll back looking to see if it is
defined at the start of some intermediate block.

Maybe due to coding standards that a company tries to keep common
across multiple languages.
worse is this often happens in ridiculously long functions. I guess

Maybe to avoid the overhead of function calls.
these are both signs of poor coding practice.

As a general rule, I would probably agree. God knows I have inherited
some awful crap. But always allow for reasonable exceptions. Even in
a recent product, some of the code may have been lifted (I think code
reuse is still one of the "in" buzzwords) from something much older
(if it ain't broke don't fix it).
 
J

James Kuyper

Barry said:
snip


Maybe to avoid the overhead of repeatedly creating and destroying the
variable as the block is entered and exited.

That comes under the heading of optimizations. There's no inherent
reason why there has to be any overhead. I'd expect a decently
optimizing compiler to generate the same code whether the variable was
defined inside or outside the loop, so long as the only actual usages of
it occurred inside the loop. The advantage of defining it inside the
loop is that there's no danger of accidentally referring to it outside
the loop.
Maybe so that when someone looking to understand the code sees a new
object name, the definition of that object can be found at the top of
the function without having to scroll back looking to see if it is
defined at the start of some intermediate block.

Personally, I find the shorter scroll to be more of an advantage. In
particular, if I find a variable defined in a particular block, I know
that I don't have to worry about whether it's used in any exterior block
in order to figure out how it's being used.
 
A

Andrew Poelstra

Get a laptop.

That won't help him if his company network won't allow his laptop to
connect, and he needs access to a source-control system on that network.
 
I

Ian Collins

Andrew said:
That won't help him if his company network won't allow his laptop to
connect, and he needs access to a source-control system on that network.
Get a laptop without windows. My clients (a couple of whom are very
anal about security) are happy to let me connect once I've assured them
my laptop is windows free.
 
F

Flash Gordon

Ian Collins wrote, On 16/08/08 21:44:
Get a laptop without windows.

Doesn't always help.
My clients (a couple of whom are very
anal about security) are happy to let me connect once I've assured them
my laptop is windows free.

Some of my customers have a blanket ban on "foreign" equipment of any
kind being connected, even if it is not running Windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top