Rule for (or against) var

I would like to suppose a new rule for Java Sonar about the “var” keyword.

My first reaction about it was “What the hell? Is this a scripting language now?” And this has not change must, but still a bit.

The problem with usage of var is, that a code reader has a harder time to understand the code, because a variable’s type must be researched by analysing further code. Well, one could argument, that the type does not matter to understand the code. But I think, we all know, that is is untrue. Though there might be situations, where this is indeed true.

I would have preferred the language designers to avoid this step. Though I always thought, it was designed in the wrong direction. But this thought leads me to this rule proposal.

The motivation for the var keyword was certainly to avoid redundant notation of possibly long class names.

MyClassWithLongName x = new MyClassWithLongName();

Unfortunately the possibility to use var for declaration opens the world to bad code like this:

var y = executeSomeMethod();

or:

for ( var z : list ) {}

In both situations you have to further analyze the code to know the type or use the IDE and hover over y resp. z.

I would like to suppose a rule, that generally marks the use of var as error, warning or code smell or what ever, with one exception: constructor calls.

The rule should ignore lines like this:

var x = new MyClassWithLongName();

In this case nothing is left to imagination or analyze.

Maybe accepting constructor calls for var should be a configuration flag.

The rule should have another configuration flag to tell, whether to accept constructor calls with generics like this:

var x2 = new MyGeneraicClass<>( arg0, arg1 );

In this case generics need to be researched again.

What do you think? Is there a chance for a rule like this?

2 Likes

Good morning.

In my opinion its very possible to be done. I did some rules like that before and i could help you with that if you want. Which languague are you using? This rule have to analyze which languague? Javascript? Well, for me its very possible to do, i can imagine the rule code done, reading your request.

But this is the answer for your question : it is possible.

The language is Java.

Grat thing, that you want to help. Unfortunately I cannot currently affort the time to do that myself. At the moment I have to restrict myself with reporing issues and suggestions. Sorry.

1 Like

Hey @mfroehlich,

Thank you for the rule suggestion but we currently have a rule that does the opposite (S6212) and we try not to implement contradicting rules when possible.

@matheus’s suggestion of a custom rule looks like the best way to go if you want to discourage the use of var. It is indeed feasible and if you can find people to help you with this, the tools are available and a part of this community is dedicated to this.

Cheers,

Dorian

1 Like

Well, i cannot do everything to you right now because of my job, but i can help at my free time. I suggest to you to get the custom rules plugin provided by sonar source(also comment by @Dorian_Burihabwa above) and start trying to understand the plugin files and archtecture. I can give you some tips and develop together with you in my free time

Hey, Matheus, thank you so much for your great offer. I cannot say thank you enough. Unfortunately I cannot put any private time into this atm. with two small kids in corona times. So unfortunately a project like this would have to wait.

But…
Dorian, I read the description of rule S6212. And it looks like you’re very well aware of the disadvantages of var. I like the examples, when the rule suggests to use var. Wouldn’t it be consequent to make the rule encourange not to use var fo all the other cases (where it produces less readable code)? I think, this would be a great improvement.

Hello,

You can use my plugin:

1 Like