IntelliJ apply inspection fix throughout entire file

雨燕双飞 提交于 2020-01-04 05:45:16

问题


In IntelliJ, I have the inspection that checks for variables that can be made final turned on so that IntelliJ will highlight those variables and let me quickly add the final keyword with Alt + Enter.

When I open up someone else's code who does not typically use the final keyword, I would like a quick way to add final to all variables that can be throughout the entire file, or even the entire project, if possible.

Is there any shortcut or some other way to do this easily in IntelliJ?


回答1:


Thank you to @Mzf for the link!

From that post:

You can achieve this in the current file using the typical Alt + Enter shortcut when hovering over one such variable that can be made final, press , and select "Fix all 'Local variable or parameter can be final' problems in file".

The next item down in that sub-menu is "Run inspection on ...", which will allow you to apply the fix to multiple files in your project.




回答2:


A non-answer; and backed by my opinion, but in the end, also backed by years of experience thinking about code quality topics:

I know, this is style; but still, I think your style isn't helpful. To the contrary. So the answer is: don't do that. Don't put final on just any variable indiscriminately.

The point is: you want your source code to be precise and verbose ... but also "minimalistic". Keep in mind: each and any character that was written ... needs to be read. And "parsed" and "processed" by the human readers brain.

So you want to balance the things you write down. And thing is: most of the time, those final keyword will not add any value to your source code. Zero.

There are only two places where final really makes a difference:

  1. Class fields. Those should be final as default - because that gets you half-way to immutable objects; which is per-se a really good thing. Of course, sometimes you must allow fields to change their value; but as said: that should be the exception.
  2. Variables that go into lambdas, anonymous inner classes, ... in other words: variables that must be protected from changing; so that it becomes "safe" to use them in a "different" context. But guess what: with Java8, the compiler even learned to detect effectively final variables. So you can use them in lambads even without the final keyword. So even the fathers of the Java language suggest to you to avoid using that keyword!

But beyond those two cases; there are zillions of places in your code where having final only contributes to the input that your brain has to digest; without adding value to your program.

Long story short: step back for a second and re-think what you are trying to achieve with this policy.

Finally: and you are sure that you will remember to undo your changes to someone else's code? As you can see: not everybody supports your style; so you should not only ask how to change other code easily; but also how to revert those changes back later on. Assume you have to make a few real changes in that source code; and then commit them to your repository - are you really willing to then go in and undo the final changes you made manually; whilst ensuring that all other, "real" changes are kept.



来源:https://stackoverflow.com/questions/42283791/intellij-apply-inspection-fix-throughout-entire-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!