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条回答
虎瘦雄心在
2楼-- · 2019-01-16 18:48

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.

查看更多
一纸荒年 Trace。
3楼-- · 2019-01-16 18:48

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

查看更多
男人必须洒脱
4楼-- · 2019-01-16 18:51

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.

查看更多
欢心
5楼-- · 2019-01-16 18:57

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(); ) 
查看更多
Fickle 薄情
6楼-- · 2019-01-16 19:03

I've seen people declare at the top and at the bottom of functions. I prefer the top, where I can see them quickly. It's a matter of choice and preference.

查看更多
smile是对你的礼貌
7楼-- · 2019-01-16 19:04

The proper way is to declare variables exactly when they are first used and minimize their scope in order to make the code easier to understand.

Declaring variables at the top of functions is a holdover from C (where it was required), and has absolutely no advantages (variable scope exists only in the source code, in the byte code all local variables exist in sequence on the stack anyway). Just don't do it, ever.

Some people may try to defend the practice by claiming that it is "neater", but any need to "organize" code within a method is usually a strong indication that the method is simply too long.

查看更多
登录 后发表回答