Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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;
like image 447
Craig Angus Avatar asked Sep 19 '08 09:09

Craig Angus


People also ask

Can you declare multiple variables on the same line?

int i = 1, j = 1; Declaring each variable on a separate line is the preferred method. However, multiple variables on one line are acceptable when they are trivial temporary variables such as array indices.

Is it possible to declare different type of variable on the same line in Java?

6.1 Number Per LineDo not put different types on the same line.

Can we declare same variable in two times?

It is okay to declare variables with same name in different functions. Show activity on this post. Variables declared inside a function only exist in the scope of that function, so having the same variable name across different functions will not break anything.

Can you declare variables having the same name?

Yes, variables belonging to different function can have same name because their scope is limited to the block in which they are defined. Variables belonging to different functions in a single code can have same name if we declare that variable globally as shown in the following code snippet below .


1 Answers

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.

like image 62
Michel Avatar answered Sep 30 '22 04:09

Michel