473,706 Members | 2,043 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1591

"Joe" <an**@anon.co m> wrote in message
news:Rl******** ************@ne wsfep2-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.co m> 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.we st.earthlink.ne t...
On Tue, 10 Feb 2004 19:33:23 -0000 in comp.lang.c++, "Joe"
<an**@anon.co m> 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
6333
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 (user-defined types). The point is that for one of them i can't define (hard coded) a fixed length because the application is reading data from another file (In that file they have the same lenght). Is it possible to use the lenght i obtain from reading that other file to declare my variable or...
2
2139
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 allocating memory for a new variable more efficient than repeatedly referencing the member in a loop? Maybe using a string isn't the best example, but hopefully you get the idea! * example (referencing member):
2
54063
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; DECLARE end_date date; SET start_date = '2005-01-01'
1
2429
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 variable set to the name of the control to tell the program which control to requery. This makes it easy to requery controls on a different form. However, up until today, I was dimming a new local variable for each control I wanted to deal with ( E.G. Dim cbxctl as control) and then setting that...
5
2228
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
3513
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. For example Public Test1 as String I'll get en error saying to change Public to Dim. When I do, it will say that Test1 is an unused local variable.
3
2265
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 WebForm2.aspx. For declaring the property oRpte() on WebForm1.aspx, I use "Public Property" and I declare variable _oRpte as Friend Shared. That's my problem. If I don't declare _oRpte as Friend Shared, I can't use WebForm1.oRpte() on other webpage. If I declare _oRpte as Friend Shared, I can use...
2
2057
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 folders, but web pages would be called across the 4 projects. For example, web page Page1 in Project1 will redirect to web page Page2 in Project2 which will further redirect to Page3 in project3 and Page4 in Project4 and finally back to Page1. In each web page, some session variables, such as...
8
7519
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 and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
6
2986
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 < 10; i++) {
0
8783
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8697
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9157
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7912
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5939
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4454
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4710
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2503
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.