IntelliJ IDEA在JAVA中使用+ =时强调变量(IntelliJ IDEA underlines variables when using += in JAVA)

编程入门 行业动态 更新时间:2024-10-20 08:36:40
IntelliJ IDEA在JAVA中使用+ =时强调变量(IntelliJ IDEA underlines variables when using += in JAVA)

由于新的更新(2018.2)IntelliJ IDEA强调变量,当它们被“不必要地”重新分配时 - 但是,这包括每次使用“+ =”。 这是IDE中的错误还是我弄错了? 请以此基本方法为例。 (它只是添加一个数字列表。)

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

最满意答案

这是IntelliJ IDEA 2018.2的新功能 :

为重新分配的局部变量和重新分配的参数加下划线

IntelliJ IDEA现在默认为重新分配的局部变量和重新分配的参数加下划线。 支持此功能的所有语言的属性(现在包括Java和Groovy)可以在Preferences / Settings中更改 编辑| 配色方案| 语言默认值| 标识符| 重新分配

强调

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

更多推荐

本文发布于:2023-08-07 01:53:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1457865.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:变量   IDEA   IntelliJ   JAVA   variables

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!