Guidelines for Posting Variable Ads in Java

It seems that there are two valid variable variable locations for Java variables, each of which has a different raison d'รชtre.

From the Sun conventions, we can see:

Place ads only at the beginning of blocks. (A block is any code surrounded by curly braces "{" and "}".) Do not wait to declare variables before their first use; it can confuse a careless programmer and inhibits code portability within an area.

However, in the highly acclaimed Code Complete, several other authors advocate keeping the scope of the variable to a minimum. It basically waits for variable declarations before their first use.

These two approaches are clearly contradictory, although I can see their point of view.

Why should I follow? Is there a consensus on this?

+7
source share
5 answers

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

  • Vertical locality
  • tool tip

Vertical locality simplifies the discussion of pieces of code. The less scanning the reader needs to do, the easier it is to understand what the code does and what side effects it

Reducing the scope also allows better tool tips for automated tools such as refactoring. The closer they are connected to each other, the more obvious they are.

This suggests that if the method is long enough to involve the above points, most likely this method is already too long and should be reorganized in any case.

+13
source

It basically waits for variable declarations before their first use.

This is actually not the case, and the two styles do not contradict each other. Limiting the scope of a variable means that this variable does not, in fact, exist outside this scope. For example.

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

In this case, a is the scope limited by the for block, and this is what refers to the code.

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

+1
source

My personal opinion is that in any case, everything is in order. I think that as long as the variable names are sufficiently descriptive, that doesn't really matter. Again, this is just my opinion. I had to go through a lot of code that was not written by me, and the biggest disappointment with the variables that I came across is that their names are not very descriptive. Most IDEs will be able to "move on to definition", so it doesn't matter where you declare them.

0
source

These two statements should not be contradictory. The scope is determined by curly braces, so if you start a new set of brackets closer to where you use them, this is consistent with the Sun coding convention. Of course, if you just randomly insert area constraints, it is a problem to read the code stream.

However, it is very important for me 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 in the long run, and nesting the declarations at the beginning of the class clearly indicates what significant state the class has.

0
source

I also recommend Joshua Bloch's "Effective Java" for many reasons why variables should be declared where they are first used.

0
source

All Articles