Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Variable declaration placement guidelines in Java [closed]

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?

like image 412
DPM Avatar asked Nov 15 '11 23:11

DPM


People also ask

What are the rules of variable declaration in Java?

Rules to Declare a VariableA variable name can consist of Capital letters A-Z, lowercase letters a-z digits 0-9, and two special characters such as _ underscore and $ dollar sign. The first character must not be a digit. Blank spaces cannot be used in variable names. Java keywords cannot be used as variable names.

Where should you declare variables in Java?

Declare variables as close to the first spot that you use them as possible. It's not really anything to do with efficiency, but makes your code much more readable. The closer a variable is declared to where it is used, the less scrolling/searching you have to do when reading the code later.

Where should you put your variable declaration?

A declaration of a variable is where a program says that it needs a variable. For our small programs, place declaration statements between the two braces of the main method. The declaration gives a name and a data type for the variable. It may also ask that a particular value be placed in the variable.


2 Answers

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.

like image 197
Dave Newton Avatar answered Oct 19 '22 01:10

Dave Newton


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.

like image 1
jpredham Avatar answered Oct 18 '22 23:10

jpredham