In Java, should variables be declared at the top o

2019-01-16 18:45发布

I'm cleaning up Java code for someone who starts their functions by declaring all variables up top, and initializing them to null/0/whatever, as opposed to declaring them as they're needed later on.

What are the specific guidelines for this? Are there optimization reasons for one way or the other, or is one way just good practice? Are there any cases where it's acceptable to deviate from whatever the proper way of doing it is?

12条回答
Lonely孤独者°
2楼-- · 2019-01-16 19:06

I've found that declaring them as-needed results in fewer mistakes than declaring them at the beginning. I've also found that declaring them at the minimum scope possible to also prevent mistakes.

When I looked at the byte-code generated by the location of the declaration few years ago, I found they were more-or-less identical. There were ocassionally differences depending on when they were assigned. Even something like:

for(Object o : list) {
   Object temp = ...;  //was not "redeclared" every loop iteration
}

vs

Object temp;
for(Object o : list) {
   temp = ...; //nearly identical bytecoode, if not exactly identical.
}

Came out more or less identical

查看更多
祖国的老花朵
3楼-- · 2019-01-16 19:06

I think it is actually objectively provable that the declare-at-the-top style is more error-prone.

If you mutate-test code in either style by moving lines around at random (to simulate a merge gone bad or someone unthinkingly cut+pasting), then the declare-at-the-top style has a greater chance of compiling while functionally wrong.

I don't think declare-at-the-top has any corresponding advantage that doesn't come down to personal preference.

So assuming you want to write reliable code, learn to prefer doing just-in-time declaration.

查看更多
家丑人穷心不美
4楼-- · 2019-01-16 19:11

From the Java Code Conventions, Chapter 6 on Declarations:

6.3 Placement

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.

void myMethod() {
    int int1 = 0;         // beginning of method block

    if (condition) {
        int int2 = 0;     // beginning of "if" block
        ...
    }
}

The one exception to the rule is indexes of for loops, which in Java can be declared in the for statement:

for (int i = 0; i < maxLoops; i++) { ... }

Avoid local declarations that hide declarations at higher levels. For example, do not declare the same variable name in an inner block:

int count;
...
myMethod() {
    if (condition) {
        int count = 0;     // AVOID!
        ...
    }
    ...
}
查看更多
你好瞎i
5楼-- · 2019-01-16 19:11

Defining variable in a wider scope than needed hinders understandability quite a bit. Limited scope signals that this variable has meaning for only this small block of code and you can not think about when reading further. This is a pretty important issue because of the tiny short-term working memory that the brain has (it said that on average you can keep track of only 7 things). One less thing to keep track of is significant.

Similarly you really should try to avoid variables in the literal sense. Try to assign all things once, and declare them final so this is known to the reader. Not having to keep track whether something changes or not really cuts the cognitive load.

查看更多
男人必须洒脱
6楼-- · 2019-01-16 19:12

Principle: Place local variable declarations as close to their first use as possible, and NOT simply at the top of a method. Consider this example:

/** Return true iff s is a blah or a blub. */
public boolean checkB(String s) {
    // Return true if s is a blah
    ... code to return true if s is a blah ... 

    // Return true if s is a blub. */
    int helpblub= s.length() + 1;
    ... rest of code to return true is s is a blah.

    return false;
}

Here, local variable helpblub is placed where it is necessary, in the code to test whether s is a blub. It is part of the code that implements "Return true is s is a blub". It makes absolutely no logical sense to put the declaration of helpblub as the first statement of the method. The poor reader would wonder, why is that variable there? What is it for?

查看更多
爷的心禁止访问
7楼-- · 2019-01-16 19:15

I am doing this very same thing at the moment. All of the variables in the code that I am reworking are declared at the top of the function. I've seen as I've been looking through this that several variables are declared but NEVER used or they are declared and operations are being done with them (ie parsing a String and then setting a Calendar object with the date/time values from the string) but then the resulting Calendar object is NEVER used.

I am going through and cleaning these up by taking the declarations from the top and moving them down in the function to a spot closer to where it is used.

查看更多
登录 后发表回答