Declaring Variables

J

Joe

Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?

Joe
 
J

JimC

Joe said:
Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?

I think legibility is enhanced by introducing them where they're used.
"Collection" fetishists might not be able to deal with the stress -- they
experience what a first-time visitor to Kinko's experiences when his
large document is divided up by the experienced staff behind the
counter -- but software isn't written to aggrandize the neurotic
personality.

An argument for lumping everything in front might be that if your
software is going to write beyond its stack, as an infinitely looping
recursive call will inevitably do, you're slightly better off finding out
about it sooner than later.

JimC
www.cross-comp.com
www.cross-comp.com/pages/software
 
D

David Harmon

Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?

Declare variables close to point of first use.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[27.6] Should I declare locals in the middle of a function or at the
top?" It is always good to check the FAQ before posting. You can get
the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
J

Joe

Thanks for that. Will do next time.
Joe.

David Harmon said:
Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?

Declare variables close to point of first use.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[27.6] Should I declare locals in the middle of a function or at the
top?" It is always good to check the FAQ before posting. You can get
the FAQ at:
http://www.parashift.com/c++-faq-lite/
 
A

Andrey Tarasevich

Joe said:
Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?
...

Declare them as you need them.

I heard only one argument for declaring all variables at the very
beginning of the function: that that way the variable doesn't go out of
scope and its last value can be examined in the debugger much longer.
But I don't think that this is enough to outweight the relatively long
list of problems inherent to this style of variable declaration.
 
O

Old Wolf

Is it generally considered "better" (i.e. clearer) to declare all variables
An argument for lumping everything in front might be that if your
software is going to write beyond its stack, as an infinitely looping
recursive call will inevitably do, you're slightly better off finding out
about it sooner than later.

On all stack-using implementations I know about, the stack pointer
advances at the start of the function, as if all the variables
had been declared at the start of the function.

Anoter argument for lumping in front is that it makes it easier to
find what variables are in scope at any point, if you are dealing
with large functions. Also, it reduces the likelihood of
introducing problems with variables of the same name in a
narrower scope.
 
S

Stewart Gordon

While it was 2/10/04 7:33 PM throughout the UK, Joe sprinkled little
black dots on a white screen, and they fell thus:
Is it generally considered "better" (i.e. clearer) to declare all variables
at the start of a function or as you need them within it?

I personally tend to put them at the beginning for the most part - this
means that they're not going to get lost as they're scattered about my
function bodies.

But if I'm defining a function that can be logically divided into
phases, then if a phase introduces new variables I'd tend to declare
them at the beginning of the phase.

Of course, if you've got object initialisations depending on values
calculated within the function, that's another matter....

Stewart.
 

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,756
Messages
2,569,540
Members
45,025
Latest member
KetoRushACVFitness

Latest Threads

Top