7

There seems to be two accepted variable declaration placements for Java variables, each with different raison d'être.

From the Sun's code conventions we can see:

Put declarations only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope.

However in the highly praised "Code Complete" and some other authors advocate for reducing the scope of a variable to a minimum. That is basically waiting to declare variables until their first use.

These two approaches are clearly contradictory although I can see the point for both of them.

Which should I follow? Is there any consensus on the matter?

skaffman
  • 398,947
  • 96
  • 818
  • 769
DPM
  • 1,960
  • 3
  • 26
  • 49
  • 1
    To prevent this from being "too subjective", I'd focus on the second question: Is there consensus? From what I've seen, there does not seem to be, and it's really up to you and the people you program with. – Owen Nov 16 '11 at 00:03
  • 3
    "Don't wait to declare variables until their first use; it can confuse the unwary programmer and hamper code portability within the scope." is very ironic -- declaring all your variables in one place actually makes the code *less* portable. It makes it more difficult to move chunks of code whole-cloth (perhaps to a new method) since the logic and declarations are not encapsulated. – Kirk Woll Nov 16 '11 at 00:06
  • See also answers here: http://www.mzan.com/article/8144890-variable-declaration-placement-guidelines-in-java.shtml – Grigory Kislin Jun 16 '16 at 20:20

5 Answers5

15

Variables should be declared as close to their use as possible, for two reasons:

  • Vertical locality
  • Tool hinting

Vertical locality makes it easier to reason about chunks of code. The less scanning a reader has to do the easier it is to understand what code does, and what side-effects it

Reducing variable scope also allows better tool hinting for automated tools, like refactoring. The closer together related things are the more obvious they are.

That said: if a method is long enough that the above points come in to play, it's likely the method is already too long, and should be refactored anyway.

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
  • 1
    +1 for "... if a method is long enough that the above points come in to play, it's likely the method is already too long..." – Justin Ohms Mar 04 '13 at 03:16
1

That is basically waiting to declare variables until their first use.

This is actually not true, and these two styes are not conflicting. Limiting the scope of the variable means that that variable, in fact, does not exist outside of that scope. E.g.

for(int i=0; i<10;i++){
   int a = 5;
   doSomething(a);
}

In this case, a is scope limited to the for block and this is what Code complete is referencing.

In any case I agree with sun, that variables within a scope (class, method, if block, etc.) should be declared at the beginning.

jpredham
  • 2,149
  • 1
  • 23
  • 37
  • If you want to minimise the scope of a variable in a method, in general, you won't declare the variable at the very beginning. For that reason I really do see both guidelines as conflicting. – DPM Nov 16 '11 at 00:15
0

My personal opinion is that either way is fine. I think as long as the variable names are descriptive enough, it doesn't really matter. Again, this is only my opinion. I've had to go through lots of code which wasn't written by me, and the biggest frustration with variables I've faced, is that their names are not very descriptive. Most IDEs will have the option of 'go to definition' so it doesn't really matter where you declare them.

PTBG
  • 585
  • 2
  • 20
  • 46
  • Sounds sensible. I forgot to mention, I would, in any case, add a caveat: for code clarity and to avoid confusion, maintain always the same approach and be coherent throughout your code, not deciding randomly where to place your variables. In this way you'd lose the advantages of each. – DPM Nov 16 '11 at 00:10
  • Being forced to "jump to definition" is a cognitive break, and increases the workload of someone trying to reason about the code. I agree consistency is important, but maintaining the ability to reason without undue jumping around is critical. – Dave Newton Nov 16 '11 at 00:53
0

Those two statements do not need to be contradictory. Scope is decided by the curly brackets, so if you start a new set of brackets closer to where you use them, that is consistent with the Sun coding convention. Of course if you are just arbitrarily inserting scope limitations, that is a problem for reading the flow of the code.

However, I find it very important to declare fields at the top of the class, especially mutable fields. Understanding the state of an object can be the hardest part of a class over the long term, and putting the declarations at the beginning of the class clearly states what significant state the class holds.

Yishai
  • 90,445
  • 31
  • 189
  • 263
0

I also recommend "Effective Java" by Joshua Bloch for more reasons why one should declare variables where they are first used.

Puce
  • 37,247
  • 13
  • 80
  • 152