Reassigned local variable java что такое
Перейти к содержимому

Reassigned local variable java что такое

  • автор:

Why are method parameters reassigned to local variables?

While looking through the Java API source code I often see method parameters reassigned to local variables. Why is this ever done?

This is in java.util.HashMap

Solution

This is rule of thread safety/better performance. values in HashMap is volatile. If you are assigning variable to local variable it becomes local stack variable which is automatically thread safe. And more, modifying local stack variable doesn’t force ‘happens-before’ so there is no synchronization penalty when using it(as opposed to volatile when each read/write will cost you acquiring/releasing a lock)

OTHER TIPS

I’d have to look at some real examples, but the only reason I can think to do this is if the original value needs to be preserved for some computation at the end of the method. In this case, declaring one of the "variables" final would make this clear.

IntelliJ IDEA underlines variables when using += in JAVA

If you know what is the side effect in programming then it will be easy for you. To protect your variable from the side effect, the IDE shows the underline as a warning to you.

Hope this screenshot would help.

enter image description here

It’s a new feature of IntelliJ IDEA 2018.2:

Underlining reassigned local variables and reassigned parameters

IntelliJ IDEA now underlines reassigned local variables and reassigned parameters, by default. The attributes for all the languages supporting this feature, which for now include Java and Groovy, can be changed in Preferences/Settings | Editor | Color Scheme | Language Defaults | Identifiers | Reassigned.

underline

Why it may be useful?

If the variable/parameter is underlined, you know that you can’t use it in lambda/anonymous class directly.

When reading a very long method code, if the parameter is not underlined, you know for sure that its value is not reassigned anywhere in this method and it contains exactly the same value that was passed to this method at any point.

Some code guidelines are against reassigned variables and you may want to avoid them where possible to keep the code clean and make it easier to read/debug.

Nowadays many developers prefer to avoid mutable state, and reassign variables only in rare cases when it is really necessary. We don’t want to manually enforce immutability, we suppose that everything is immutable by default and want to bring additional attention to the cases when something is not. If you use final to mark non-mutable variables it means that you need to write more code for regular cases and less code in exceptional cases. (BTW in modern languages declaring immutable variables doesn’t require writing additional code, but unfortunately not in Java).

Brian Goetz, Java Language Architect, also likes the way IntelliJ IDEA highlights reassigned variables (see his tweet).

IntelliJ IDEA underlines variables when using += in JAVA

IntelliJ IDEA now underlines reassigned local variables and reassigned parameters, by default. The attributes for all the languages supporting this feature, which for now include Java and Groovy, can be changed in Preferences/Settings | Editor | Color Scheme | Language Defaults | Identifiers | Reassigned.

underline

Why it may be useful?

If the variable/parameter is underlined, you know that you can’t use it in lambda/anonymous class directly.

When reading a very long method code, if the parameter is not underlined, you know for sure that its value is not reassigned anywhere in this method and it contains exactly the same value that was passed to this method at any point.

Some code guidelines are against reassigned variables and you may want to avoid them where possible to keep the code clean and make it easier to read/debug.

Nowadays many developers prefer to avoid mutable state, and reassign variables only in rare cases when it is really necessary. We don’t want to manually enforce immutability, we suppose that everything is immutable by default and want to bring additional attention to the cases when something is not. If you use final to mark non-mutable variables it means that you need to write more code for regular cases and less code in exceptional cases. (BTW in modern languages declaring immutable variables doesn’t require writing additional code, but unfortunately not in Java).

Brian Goetz, Java Language Architect, also likes the way IntelliJ IDEA highlights reassigned variables (see his tweet).

Solution 2 — Java

Hope this screenshot would help.

enter image description here

Solution 3 — Java

If you know what is the side effect in programming then it will be easy for you. To protect your variable from the side effect, the IDE shows the underline as a warning to you.

Why Reassigned Local Variables Can Lead to Bugs and How to Debug Them

Reassigned local variables can be a common source of bugs in software development. A local variable is a variable that is only accessible within a specific block of code, such as a function or method. These variables can be reassigned, meaning their value can be changed multiple times within the same block of code. This can lead to unexpected behavior and errors in the program.

What Causes Bugs from Reassigned Local Variables?

The main issue with reassigned local variables is that they can be implicitly or explicitly accessed by other parts of the code. This means that if the variable’s value is changed at one location, it can affect the behavior of the program in another location. For example, if a variable is used to store the value of a calculation in one function, and then reassigned with a different value in a later function, it may cause unexpected results when used in the original function or elsewhere in the code.

Another issue is that reassigned local variables can be difficult to debug, especially in large and complex code bases. The changing values can make it challenging to trace the flow of a program and identify the root cause of an error.

Tips for Debugging Reassigned Local Variables

Debugging reassigned local variables requires a systematic approach. Here are some tips to help you identify and fix these bugs:

Identify problematic variables: Start by identifying which variables are being reassigned and where in the code this is happening. Use debugging tools like breakpoints, watches or logging to track the value of the variable as it changes.

Visualize the flow of the program: Use flowcharts or diagrams to visualize how the variable is being used and where it may be causing issues. This can help you see the big picture and identify possible sources of bugs more easily.

Check variable scope: Make sure that the variable is only being accessed within its intended scope. If it’s being used outside of its intended scope, it may be causing unexpected behavior.

Add more descriptive comments: The more descriptive comments you add to your code, the easier it will be to debug issues like reassigned local variables. Comments can help future developers understand variable usage and catch potential issues.

Use unit testing: Unit tests can help identify issues with reassigned local variables by testing specific pieces of code in isolation. This can help you catch issues before deploying code changes into production.

By following these tips, you can reduce the likelihood of bugs caused by reassigned local variables, and make debugging easier in the event that they do occur.

Conclusion

Reassigned local variables can cause unexpected behavior and errors in a program. Identifying these issues and debugging them can be challenging, but a systematic approach that uses debugging tools, diagrams, and testing can make it more manageable. With careful attention to variable usage and best practices for debugging, you can avoid and address these bugs in your code.

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *