0

Which is the better way of declaring variables?

1.

    int i,j,k;  


2.

    int i;
    int j;
    int k;

Can anybody explain which is the better way and why?

Tony
  • 9,672
  • 3
  • 47
  • 75
Bond
  • 3
  • 1
  • Not that it matters in this case but it's nice to tag your question with the language you are using. – Tony Mar 04 '11 at 08:20
  • You havent specified language. For C/C++ i recommend the explicit declarations to void any bad typedefs/defines. I also find the explicit form alot more clear and understandable when it comes to pointers. – stefan Mar 04 '11 at 08:21

5 Answers5

3

This is ultimately a matter of personal taste. It doesn't matter to the compiler or your program either way.

If you're working on a team with other programmers, the important thing is that you follow their established standards. If you're maintaining a base of existing code, follow the style already established in the source. Otherwise, you're free to make your own decisions about how to format your code.


Personally, I prefer the second style. It makes it much clearer to me what the types are of each variable. Additionally, if you're working in C or C++ and declaring pointers, it's important to keep in mind that

int* i, j, k;

will only declare i as a pointer to an int (see this question for more discussion). Using the second declaration style makes it completely unambiguous, which is always better for long-term maintainability. The amount you're saving by squashing all variable declarations to one line doesn't seem worth it to me.

Community
  • 1
  • 1
Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • I've always disliked having the asteriks next to the _type_ rather than the _identifier_, for the exact reason you stated. The asteriks has closer affinity to the identifier than the type. Yet it is the proper convention. Any idea why? – Assaf Levy Mar 04 '11 at 08:29
  • @ALevy: There isn't really a "proper" convention; that's also a matter of personal style. C programmers tend to prefer the asterisk next to the identifier, C++ programmers tend to prefer the asterisk next to the type. I presume this has something to do with their background in object-oriented programming, where the type is fundamentally more important. A pointer to an `int` is a *completely* different type than an `int`. I've given up arguing about which style is "better", and instead just recommend that all pointer variables be declared on their own line. – Cody Gray - on strike Mar 04 '11 at 08:32
1

It's a matter of taste, there is no difference as far as the compiler is concerned

Tony
  • 9,672
  • 3
  • 47
  • 75
0

the first might help in readability and easy spotting of variables in declaration especially if you are not using any IDE and the code is long in single file

sashank
  • 1,531
  • 2
  • 13
  • 26
0

You need to define "better". When it comes to the program's efficiency, there's no difference.

If you're refering to styling, then I believe it's most readable and convenient to allow every variable a line for itself:
int i; // Holds the value of...
int j; // Iterator for...
int k; // Dummy for function()...

That also gives you the option for nice descriptive comments per each. Naturally, it boils down to your (and your team's as Cody stated) taste and conventions.

Assaf Levy
  • 1,312
  • 1
  • 12
  • 20
0

Depends on what level of readability, understandability and comprehensiveness you want to keep in your code. First type qualifies well in terms of understandability whereas second fits better in terms of readability and comprehensiveness.

Also as mentioned by @Code Gray, it can be confusing to use first syntax in some languages if it isn't used carefully.

Usually first type is always used in loops but in declaration sections, I prefer second one.

In the end, it is your choice and style that you want to adopt.

nitesh
  • 91
  • 1
  • 7