Alex Rivera | Logout

Why do you not declare several variables of the same type on the same line?

Asked 2008-09-19T09:06:16.533
18

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;
Edit
Report

3 Answers

5

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

answered 2008-09-19T09:06:59.417
4

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;

answered 2008-09-19T09:15:57.397
3

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
answered 2008-09-19T09:12:19.090

Your Answer