Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IntelliJ IDEA underlines variables when using += in JAVA

Since the new update (2018.2) IntelliJ IDEA underlines variables, when they are "unnecessarily" reassigned - this includes, however, each use of "+=".
Is this a mistake in the IDE or am I getting it wrong?
Please see this basic method as an example. (It just adds a list of numbers.)

example image

like image 728
fandango Avatar asked Aug 05 '18 18:08

fandango


People also ask

What does purple underline mean in IntelliJ?

At the second thing the purple underline appears if a variable isn't from inside the lambda to show the user you're not using a local variable.

How do I set variables in IntelliJ?

Add environment variablesFrom the main menu, select Run | Edit Configurations or choose Edit Configurations from the run/debug configurations selector on the toolbar. In the Run/Debug Configurations dialog, select a configuration you want to add the environment variables to.

What is reassigned local variable in Java?

Local variables are created when the method, constructor or block is entered and the variable will be destroyed once it exits the method, constructor, or block. Access modifiers cannot be used for local variables. Local variables are visible only within the declared method, constructor, or block.


1 Answers

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).

like image 182
CrazyCoder Avatar answered Nov 03 '22 01:11

CrazyCoder