In Java, should variables be declared at the top of a function, or as they're needed?

前端 未结 12 1335
猫巷女王i
猫巷女王i 2020-12-02 13:09

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 a

相关标签:
12条回答
  • 2020-12-02 13:47

    From Google Java Style Guide:

    4.8.2.2 Declared when needed

    Local variables are not habitually declared at the start of their containing block or block-like construct. Instead, local variables are declared close to the point they are first used (within reason), to minimize their scope. Local variable declarations typically have initializers, or are initialized immediately after declaration.

    Well, I'd follow what Google does, on a superficial level it might seem that declaring all variables at the top of the method/function would be "neater", it's quite apparent that it'd be beneficial to declare variables as necessary. It's subjective though, whatever feels intuitive to you.

    0 讨论(0)
  • 2020-12-02 13:48

    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.

    0 讨论(0)
  • 2020-12-02 13:52

    Its a matter of readability and personal preference rather than performance. The compiler does not care and will generate the same code anyway.

    0 讨论(0)
  • 2020-12-02 13:56

    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.

    0 讨论(0)
  • 2020-12-02 14:01

    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. Declaring variables closer to the first spot they're used will also naturally narrow their scope.

    0 讨论(0)
  • 2020-12-02 14:01

    If you have a kabillion variables used in various isolated places down inside the body of a function, your function is too big.

    If your function is a comfortably understandable size, there's no difference between "all up front" and "just as needed".

    The only not-up-front variable would be in the body of a for statement.

    for( Iterator i= someObject.iterator(); i.hasNext(); ) 
    
    0 讨论(0)
提交回复
热议问题