-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[java] Local Variables At Top #2850
Comments
This rule will also end up enforcing Only one return and possibly violating Premature declaration. What do you think? |
I do not like Putting that aside, how would As for |
And then there's this Checkstyle rule: Personally, I think this new rule is far too fastidious. Let's see what the others have to say, too. |
I disagree with |
This new rule + |
I don't like the rule either. I don't like using |
I'm not uncomfortable with the |
I still do not see how Consider this piece of code.
The same code would have to look like this if
|
I remember why |
Hmm... I disable the |
Then you have to specify which rules are exclusive of |
The second code sample proves my point. To not violate your new rule and |
I'm not debating the byte code. I agree prematurely initializing a variable can have a -ve performance impact. |
https://stackoverflow.com/questions/36707/should-a-function-have-only-one-return-statement |
Proposed Rule Name: LocalVariablesAtTop
Proposed Category: Code Style
Description:
In C++, one could declare the local variables at the top of the method. However, the compiler has to call the constructor at the point of declaration and then throw away this work when the local variable is initialized. This drove C++ programmers to declare the local variable at the point of initialization.
In Java, this does not apply. The bytecode does nothing with the local variable until its first initialization. There is no need to declare local variables at the point of initialization. Yet, some Java programmers do this as a carry over from C++.
Please create a rule that encourages programmers to declare local variables at the top of the method. This will improve code readability. Instead of having to visually hunt for the variable's type, one can simply glance at the top of the method.
Some local variables cannot be declared at the top of the method. Let's say the local variable is assigned in a loop body and then used in a lambda. The local variable must be effectively final and hence must be declared in the loop body. In such a case, please make the rule require programmers to declare the local variable at the top of the loop body.
Code Sample:
Here is some code that is harder to read because the variable declarations happen at the point of initialization. If I am looking at the line
second.eat()
, I have to look up through many lines of code to find out the type ofsecond
.Here is the "correct" code. If I am looking at the line
second.eat()
, I simply glance up at the first few lines of code to find out the type ofsecond
. My code reading speed is greatly improved.Here is an example of a local variable that must be declared in a loop body.
index
must be declared in the loop body and not at the top of the method because it must be effectively final in order to be used in the lambda.Possible Properties:
I cannot think of any. Can you?
The text was updated successfully, but these errors were encountered: