Programming style question (inline variable declarations)

scootermaster

Platinum Member
Nov 29, 2005
2,411
0
0
Simple question:

I usually avoid inline variable declarations, and prefer to stick them at the top of my function. Probably for cleanliness' sake, and probably because I started with C, not C++.

But I notice sometimes I wonder if a variable was initialized, or [egads] what its for, or what type it is and stuff. Now, hopefully a lot of this can be gleaned from context and the variable name (I'm getting better about calling things "arrayIndexCounter" instead of "temp") but in some sense, it makes sense to have variables that are just going to be used "right now" (i.e. as I'm coding) declared right before I'm using them.

So the question is whether or not this is considered bad form (or maybe I'm so out of the loop it's considered bad for not to? Who knows?)
 

Cogman

Lifer
Sep 19, 2000
10,283
135
106
Some languages (such as delphi) force the "every variable at the top of the function" setup. I personally am not a big fan of it. I feel code looks cleaner when variables are initialized as late as possible. That way, you know the basic locality that a variable is being used in.

I don't think it is considered bad form, for small functions it may even be desirable, but for larger functions, I like late variable declarations. Nothing is more off putting then to see a function with 5000 variables declared at the top.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
The only variables I don't declare at the top are variables that don't hold 'important' data. For example say I suddenly needed a counter variable to see how many times I ran a loop.
 

degibson

Golden Member
Mar 21, 2008
1,389
0
0
I'll swim against the current and say that I prefer to declare variables with minimum possible scope, whenever a language allows me to do so.

Minimum scoping reduces the chance that old values will accidentally pollute new computation (e.g., though an omission of a re-initialization), minimizes local stack pressure, and improves data locality.
 

Red Squirrel

No Lifer
May 24, 2003
69,473
13,166
126
www.anyf.ca
I find it depends, sometimes I just need a temp variable to put something in for a single section of the app. i want such variable to have the least scope possible. For example, something like this:

int tmp=0;
while(getdata(tmp))dostuff(tmp);

Where getdata() actually returns true/false but also modifies tmp. It would be silly to declare tmp at the top. Though, if I need to keep reusing it as a temp variable, I could see value in doing that. Just need to be careful.
 

Nothinman

Elite Member
Sep 14, 2001
30,672
0
0
I tend to agree with sourceninja. I think it looks cleaner with as many variables as possible declared at the beginning. However for temp/scratch variables with limited scope I don't see anything wrong with declaring them there.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
In general, keep the scope of a variable name as limited as possible. In cases where the language provides tools for sub-local scoping, use them.
 

sourceninja

Diamond Member
Mar 8, 2005
8,805
65
91
In general, keep the scope of a variable name as limited as possible. In cases where the language provides tools for sub-local scoping, use them.

Yea, I don't mean to say everything is global. Just declared at the top of where it is used (top of the method, top of the function, etc)
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
For C/C++ I declare "important" or often-used variables at the top, but will often place variables used in just one loop right above it, as a kind of global / local separation even though they're technically in the same scope.

The fake-local variables are generally counters or scratch buffers. If it didn't look ugly I should probably wrap the one loop and its variables with { } to really create a local scope.
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
The smaller the scope of a variable, the easier it is to debug, IMO. And having variables declared at the top makes the code look cleaner.

That said, I think it's perfectly all-right to have temporary variables declared in the middle. As long as they are temporary. A rule of thumb, is that if you need to access a variable a page below where it's declared, then you'd better move its declaration to the top.

Back in college, it was an amusing scene figuring out how people's code worked, only to discover a "temporary" variable evolved into a "critical" variable. :D

-chronodekar
 

seemingly random

Diamond Member
Oct 10, 2007
5,277
0
0
I'll swim against the current and say that I prefer to declare variables with minimum possible scope, whenever a language allows me to do so.

Minimum scoping reduces the chance that old values will accidentally pollute new computation (e.g., though an omission of a re-initialization), minimizes local stack pressure, and improves data locality.
This, hands down. A knowledge of assembly language and compiler construction is helpful in understanding.

Cogman, two posts before you, agrees with you. So, it looks like you'll have other salmon to swim with.
 

KIAman

Diamond Member
Mar 7, 2001
3,342
23
81
I notice a trend between database-centric coders and web-centric coders. Those from the database realm seem more likely to declare variables first while the web-centric tend to declare variables within the local scope.

I go both ways and don't see any reason to only go one way or the other. Declaring variables all at once looks cleaner and (along with good commenting) allow a better readability of the code. Late variable declaration (also along with good commenting) makes the code easier to debug.
 
Oct 27, 2007
17,009
1
0
Studies have shown (will provide details if I can be bothered, currently typing from in bed) that declaring variables as close as possible to where the variable will be used leads to more correct code.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Studies have shown (will provide details if I can be bothered, currently typing from in bed) that declaring variables as close as possible to where the variable will be used leads to more correct code.

It's basically just modularity and decoupling, which I think all will agree are proven to be a Good Thing. Programmers sometimes don't think about scoping past the level of a method body, but most languages provide tools for lower scopes now. I can't see why anyone wouldn't use them. If you only need a variable in a particular try block, for example, declaring at method scope would be the equivalent of moving a method scope variable up to class scope. Doesn't make sense unless you really need to share it across the whole scope in which it is declared.
 

chronodekar

Senior member
Nov 2, 2008
721
1
0
Studies have shown (will provide details if I can be bothered, currently typing from in bed) that declaring variables as close as possible to where the variable will be used leads to more correct code.

No arguments here. :thumbsup: The problem is to avoid the temptation to use them in other places too.

-chronodekar