B
Barry Schwarz
snip
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.
Maybe to avoid the overhead of function calls.
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).
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).