views:

778

answers:

15

Why is it bad practice to declare variables on one line?

e.g.

private String var1, var2, var3

instead of:

private String var1;
private String var2;
private String var3;
+4  A: 

Because in some languages, var2 and var3 in your example would not be strings, they would be variants (untyped).

Ed Guiness
+13  A: 

In my opinion, the main goal of having each variable on a separate line would be to facilitate the job of Version Control tools.

If several variables are on the same line you risk having conflicts for unrelated modifications by different developers.

Michel
A: 

Agree with edg, and also because it is more readable and easy for maintenance to have each variable on separate line. You immediately see the type, scope and other modifiers and when you change a modifier it applies only to the variable you want - that avoids errors.

m_pGladiator
+4  A: 

With separate lines, you have the opportunity to add a comment on each line describing the use of the variable (if it isn't clear from its name).

Greg Hewgill
Although wouldn't it be better to rename the variable in this case?
Tim Whitcomb
+2  A: 

Here's my reasons:

  • Readability, easier to spot if you know there's only one on each line
  • Version control, less intra-line changes, more single-line additions, changes, or deletions, easier to merge from one branch to another
Lasse V. Karlsen
+9  A: 

In C++ :

int * i, j;

i is of type int *, j is of type int. The distinction is too easily missed.

Besides having them on one line each makes it easier to add some comments later

David Pierre
+3  A: 

I think that there are various reasons, but they all boil down to that the first is just less readable and more prone to failure because a single line is doing more than one thing.

And all that for no real gain, and don't you tell me you find two lines of saved space is a real gain.

It's a similar thing to what happens when you have

if ((foo = some_function()) == 0) {
    //do something
}

Of course this example is much worse than yours.

Vinko Vrsalovic
+2  A: 

To be honest I am not against it. I think that its perfectly feasible to group similar variables on the same line e.g.

float fMin, fMax;

however I steer clear when the variables are unrelated e.g.

int iBalance, iColor;

A: 
  1. to be more apparent to you when using Version Control tools (covered by Michel)
  2. to be more readable to you when you have the simplest overflow/underflow or compile error and your eyes failed to point out the obvious
  3. to defend the opposite (i.e. multi-variable single-line declaration) has less pros ("code textual vertical visibility" being a singleton)
jpinto3912
+2  A: 

In C/C++, you also have the problem that the * used to indicate a pointer type only applies to the directly following identifier. So a rather common mistake of inexperienced developers is to write

int* var1, var2, var3;

and expecting all three variables to be of type 'int pointer', whereas for the compiler this reads as

int* var1;
int var2;
int var3;

making only var1 a pointer.

hheimbuerger
A: 

What about the case such as:

public static final int NORTH = 0,
                        EAST = 1;
                        SOUTH = 2;
                        WEST = 3;

Is that considered bad practice as well? I would consider that okay as it counters some of the points previously made:

  • they would all definitely be the same type (in my statically typed Java-world)
  • comments can be added for each
  • if you have to change the type for one, you probably have to do it for all, and all four can be done in one change

So in an (albeit smelly code) example, is there reasons you wouldn't do that?

Grundlefleck
A: 

It is bad practice mostly when you can and want to initialize variables on the deceleration. An example where this might not be so bad is:

string a,b;
if (Foo())
{
  a = "Something";
  b = "Something else";
}
else
{
  a = "Some other thing";
  b = "Out of examples";
}
ripper234
+3  A: 

Why is that bad practice? I don't think it is, as long as your code is still readable.

//not much use
int i, j, k;

//better
int counter, 
    childCounter, 
    percentComplete;
Keith
Your "better" style is what I generally use in languages that require variable declaration. It's a bit more DRY while still maintaining readability.
Xiong Chiamiov
A: 

Generally it is, for the version control and commenting reasons discussed by others, and I'd apply that in 95% of all cases. however there are circumstances where it does make sense, for example if I'm coding graphics and I want a couple of variables to represent texture coordinates (always referenced by convention as s and t) then the declaring them as

int s, t; // texture coordinates

IMHO enhances code readability both by shortening the code and by making it explicit that these two variables belong together (of course some would argue for using a single point class variable in this case).

Cruachan
+3  A: 

Relevance.

Just because two variables are of type String does not mean they are closely related to each other.

If the two (or more) variables are closely related by function, rather then variable type, then maybe they could be declared together. i.e. only if it makes sense for a reader of your program to see the two variables together should they actually be placed together

Vihung