472,353 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Declaring Variables

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
Jul 22 '05 #1
6 1496

"Joe" <an**@anon.com> wrote in message
news:Rl********************@newsfep2-win.server.ntli.net...
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


Jul 22 '05 #2
On Tue, 10 Feb 2004 19:33:23 -0000 in comp.lang.c++, "Joe"
<an**@anon.com> was alleged to have written:
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/

Jul 22 '05 #3
Joe
Thanks for that. Will do next time.
Joe.

"David Harmon" <so****@netcom.com> wrote in message
news:40***************@news.west.earthlink.net...
On Tue, 10 Feb 2004 19:33:23 -0000 in comp.lang.c++, "Joe"
<an**@anon.com> was alleged to have written:
Is it generally considered "better" (i.e. clearer) to declare all variablesat 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/

Jul 22 '05 #4
Joe wrote:
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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #5
> > 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?


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.
Jul 22 '05 #6
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.

--
My e-mail is valid but not my primary mailbox, aside from its being the
unfortunate victim of intensive mail-bombing at the moment. Please keep
replies on the 'group where everyone may benefit.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Stefanie | last post by:
Hi, I have a question about declaring variables for writing data to a file for random access. I have two string variables declared in a module...
2
by: Oliver Corona | last post by:
I am wondering if anyone has any insights on the performance benefit (or detriment) of declaring local variables instead of referencing members. Is...
2
by: ross.oneill | last post by:
Hi, I am having trouble with a simple task of declaring a variable. Is this possible? Here is what I want to do. DECLARE start_date date;...
1
by: ColinWard | last post by:
Hi guys. I have a question about declaring variables. I do a lot of re-querying of controls in my database and I use the Set statement with a...
5
by: param | last post by:
Declaring struct as static is creating problem with newer version of CC compiler 5.7 in solaris. e.g. static struct new_str { int a; int b;...
6
by: Mark A. Sam | last post by:
Hello, I am using Visual Web Developer 2005 Express. I want to declare a varible, using Visual Basic as the language and can't get anywhere. ...
3
by: jbeteta | last post by:
Hello, I have a problem declaring variables. I need to create an object oRpte as ReportClass on WebForm1.aspx and be able to use its value on...
2
by: TARUN | last post by:
I have a question about declaring the Global.ascx: In VS.NET I create a Solution, and there are 4 projects under it. They are put under 4...
8
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring...
6
by: =?Utf-8?B?QUw=?= | last post by:
Hi I usually stick to the convention of not declaring variables in my bodies of "loops" (including foreach) ie int x; for (int i = 0; i...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.