Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

Given these alternatives:

int x, y, z;


int x;
int y;
int z;

Is there any real reason to choose one over the other?

I don't believe one way is better than the other. I would prefer the latter if the types differ at all though. –  Ryan Gates Jan 31 '13 at 18:18
Please note that this is apparently somewhat language and type dependant - see this question's comments/answers. –  Clockwork-Muse Jan 31 '13 at 19:48
comments disabled on deleted / locked posts

locked by Jamal Dec 20 '13 at 1:41

This question exists because it has historical significance, but it is not considered a good, on-topic question for this site, so please do not use it as evidence that you can ask similar questions here. This question and its answers are frozen and cannot be changed. More info: help center.

5 Answers

up vote 3 down vote accepted

I'll be the outlier and take a firm stand on the subject.

The second way is better. It's more readable, easier to maintain/refactor, and avoids potentially confusing statements like int x, y=0; which might lead a novice developer to assume that both x and y have been initialized to 0 (or for an even worse example, int* x, y=0).

So if you're deciding on a set of standards to adopt as official code-style guidelines, always choose the second option. One declaration per line is better in nearly all cases. The exceptions to this are uncommon cases where you need 100 variables (and can't put them in a struct or Object) and must declare them all at the start of a block/method/whatever. But there's little sense in choosing standards based upon the uncommon cases.

That said, if you're maintaining existing code, it's almost always best to just follow whatever conventions are already in use. Mixing two styles in the same source file does not often produce readable results. Readability and consistency are important, and unfortunately that means if someone has implemented code using sub-optimal coding style sometimes the best option is to just continue using that style.

add comment

Do it like specified in your code style guide. From my experience, there is no final argument for any version.

I would prefer the second way to use a new line for every variable, because it is less error prone and could be more readable for more complex types and modifiers.

But someone can easily disagree pointing to some code/language which forces you to use a lot of variables and to declare them at the beginning of the program/source. And to have 200 lines, each for one variable, is not so nice.

+1 for consistency. But remember that if you have too many declarations, it's a sign you need to refactor, not a sign you need to fit more on a single line. –  codesparkle Jan 31 '13 at 19:39
@codesparkle Yes, in general and theoretically, I agree. But the real world is not always nice to us. In some programming languages and some old code, you can only hardly avoid it. –  tb- Jan 31 '13 at 19:47
+1. There is a small corner case (in C/C++) where the first version can generate objects of the incorrect (unexpected) type. int* x,y,z; Only x is an int* while y and z are int. Which may (or may not) be what you expect. Even if you understand the subtle difference in syntax a less experienced subsequent maintainer may miss it. As a result style guides for these languages usually indicate to use the second version so that this corner case is avoided. –  Loki Astari Jan 31 '13 at 20:43
The second way is also better for diff tools. Further, if you're declaring 200 lines worth of variables anywhere, you probably have bigger problems than variable declaration syntax. –  Yuushi Feb 1 '13 at 1:51
add comment

I'd put the variable declarations to separate lines. From Code Complete, 2nd Edition, p759:

With statements on their own lines, the code reads from top to bottom, instead of top to bottom and left to right. When you’re looking for a specific line of code, your eye should be able to follow the left margin of the code. It shouldn’t have to dip into each and every line just because a single line might contain two statements.

+1 for the reference. Best book ever. –  trideceth12 Feb 1 '13 at 7:36
add comment

Generally, there is no difference between these two ways. I prefer second way because of better readability, although, it happens sometimes to use first one (when having short names variables used as counters, or in some other similar way). In practice, it depends on your own preferences or, if you are working in a team, it's best if all members of the team are using same way of declaration.

add comment

I don't think there's a clear advantage of one approach over the other. There might be an argument for the first approach if x, y and z are closely related and the second if not, but it's not a strong argument.

add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.