Java rule to forbid the use of var

Could we have a rule that does the opposite of java:S6212, Local-Variable Type Inference should be used; a rule that instead of supporting the use of var, forbids its use?

The only intent of this Java language modification seems to be to save developers a few keypresses in a few scenarios whilst reducing the clarity of the developer’s intent. A developer may think that his expression returns a value of a particular type when it actually returns some other type leading to subtle bugs later. It is also confusing for developers that have to use both Java and JavaScript because var has different scopes in the two languages.

Hi Stephen,

Thank you for your suggestion, but we don’t feel that var is unpopular enough that there would be enough demand for such a rule. You are, of course, free to define your own custom rule to do this if you feel strongly about this.


Even though you have a rule that forbids the use of the ternary operator that most of the industry came to accept the use of in Java over 20 years ago?

for what it is worth: being part of a large corporation with a central team managing our SonarQube server, we are not ‘free’ to define our own custom rules because of the logistics of installing, managing and maintaining such things in such a large organisation. That is why the template rules and parametrised rules are so useful for us.

1 Like

An article trying to defend var in Java and IMHO showing exactly why it was a bad idea: Local Variable Type Inference: Frequently Asked Questions

You may like my plugin: