R# suggests using implicitly typed vars

Hello.

Why does R# suggests using implicit typed vars all over the palce? Is there any good justification for this?

Thanks.

0
20 comments

As for me, it improves code readability and reduce code duplication

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Luis Abreu" <labreu@gmail.com> wrote in message
news:26279681.1203520748354.JavaMail.itn@is.intellij.net...

Hello.

>

Why does R# suggests using implicit typed vars all over the palce? Is
there any good justification for this?

>

Thanks.



0

Well, I don't think I agree with that. I do see that in some places it is
better to use VAR (Linq queries) but in other cases I would say it is more
readable to use exact types. For example see the attached code. This is in
my eyes readable code. If I look at the use of my class I see the following:

IClass instance = MyFactory.Construct();
Console.Write(instance.GetMessage());

I know exactly what it is I'm dealing with. Now let's take a look what
ReSharper is suggesting:

var instance = MyFactory.Construct();
Console.Write(instance.GetMessage());

Now can you please elaborate on the readability of this code snippet? If I
see this code I would HAVE TO go inside the Construct method to find out
what the return type is.

I would STRONGLY recommend not to suggest implicitly typed vars by default.
If somebody wants that it should be possible to turn it on but not by
default.

What is your opinion about that?

Regards,
David Pokluda.

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fphj9o$ejq$1@is.intellij.net...

As for me, it improves code readability and reduce code duplication

>

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Luis Abreu" <labreu@gmail.com> wrote in message
news:26279681.1203520748354.JavaMail.itn@is.intellij.net...

>> Hello.
>>
>> Why does R# suggests using implicit typed vars all over the palce? Is
>> there any good justification for this?
>>
>> Thanks.
>



Attachment(s):
MyClass.cs
0

I agree. Another example that makes code less readable:

class Class
{
string myConstant = "abc";

void Method()
{
var a = new[] { myConstant };
}
}

R# would suggest changing string[] to var, but usually there should be some
indicator of the type: either
string[] a = new[] ;
or
var a = new string[] ;

Either one is enough, both at the same time are not needed.

Thanks,
--Yuri

"David Pokluda" <info@online.pokluda.cz> wrote in message
news:fphrsa$9ro$1@is.intellij.net...

Well, I don't think I agree with that. I do see that in some places it is
better to use VAR (Linq queries) but in other cases I would say it is more
readable to use exact types. For example see the attached code. This is in
my eyes readable code. If I look at the use of my class I see the
following:

>

IClass instance = MyFactory.Construct();
Console.Write(instance.GetMessage());

>

I know exactly what it is I'm dealing with. Now let's take a look what
ReSharper is suggesting:

>

var instance = MyFactory.Construct();
Console.Write(instance.GetMessage());

>

Now can you please elaborate on the readability of this code snippet? If I
see this code I would HAVE TO go inside the Construct method to find out
what the return type is.

>

I would STRONGLY recommend not to suggest implicitly typed vars by
default.
If somebody wants that it should be possible to turn it on but not by
default.

>

What is your opinion about that?

>

Regards,
David Pokluda.

>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fphj9o$ejq$1@is.intellij.net...

>> As for me, it improves code readability and reduce code duplication
>>
>> --
>> Eugene Pasynkov
>> Developer
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> "Luis Abreu" <labreu@gmail.com> wrote in message
>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net...
>>> Hello.
>>>
>>> Why does R# suggests using implicit typed vars all over the palce? Is
>>> there any good justification for this?
>>>
>>> Thanks.
>>
>>



0

Hello David,

I would just rename instance to classInstance (or whatever I maps to) and this would solve the problem. No? Sincerely, Ilya Ryzhenkov JetBrains, Inc http://www.jetbrains.com "Develop with pleasure!" DP> Well, I don't think I agree with that. I do see that in some places DP> it is better to use VAR (Linq queries) but in other cases I would DP> say it is more readable to use exact types. For example see the DP> attached code. This is in my eyes readable code. If I look at the DP> use of my class I see the following: DP> DP> IClass instance = MyFactory.Construct(); DP> Console.Write(instance.GetMessage()); DP> I know exactly what it is I'm dealing with. Now let's take a look DP> what ReSharper is suggesting: DP> DP> var instance = MyFactory.Construct(); DP> Console.Write(instance.GetMessage()); DP> Now can you please elaborate on the readability of this code DP> snippet? If I see this code I would HAVE TO go inside the Construct DP> method to find out what the return type is. DP> DP> I would STRONGLY recommend not to suggest implicitly typed vars by DP> default. If somebody wants that it should be possible to turn it on DP> but not by default. DP> DP> What is your opinion about that? DP> DP> Regards, DP> David Pokluda. DP> "Eugene Pasynkov (JetBrains)" wrote DP> in message news:fphj9o$ejq$1@is.intellij.net... DP> >> As for me, it improves code readability and reduce code duplication >> >> -- >> Eugene Pasynkov >> Developer >> JetBrains, Inc >> http://www.jetbrains.com >> "Develop with pleasure!" >> "Luis Abreu" wrote in message >> news:26279681.1203520748354.JavaMail.itn@is.intellij.net... >>> Hello. >>> >>> Why does R# suggests using implicit typed vars all over the palce? >>> Is there any good justification for this? >>> >>> Thanks. >>>]]>


0

Hello Yuri,

I would rename myConstant to explicitly state "what is it". It is identifier?
Member name? Resource name?
Give it meaningful name, and "var problems" will vanish.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


Y> I agree. Another example that makes code less readable:
Y>
Y> class Class
Y> {
Y> string myConstant = "abc";
Y> void Method()
Y> {
Y> var a = new[] { myConstant };
Y> }
Y> }
Y> R# would suggest changing string[] to var, but usually there should
Y> be some
Y> indicator of the type: either
Y> string[] a = new[] ;
Y> or
Y> var a = new string[] ;
Y> Either one is enough, both at the same time are not needed.
Y>
Y> Thanks,
Y> --Yuri
Y> "David Pokluda" <info@online.pokluda.cz> wrote in message
Y> news:fphrsa$9ro$1@is.intellij.net...
Y>
>> Well, I don't think I agree with that. I do see that in some places
>> it is better to use VAR (Linq queries) but in other cases I would say
>> it is more readable to use exact types. For example see the attached
>> code. This is in my eyes readable code. If I look at the use of my
>> class I see the following:
>>
>> IClass instance = MyFactory.Construct();
>> Console.Write(instance.GetMessage());
>> I know exactly what it is I'm dealing with. Now let's take a look
>> what ReSharper is suggesting:
>>
>> var instance = MyFactory.Construct();
>> Console.Write(instance.GetMessage());
>> Now can you please elaborate on the readability of this code snippet?
>> If I see this code I would HAVE TO go inside the Construct method to
>> find out what the return type is.
>>
>> I would STRONGLY recommend not to suggest implicitly typed vars by
>> default.
>> If somebody wants that it should be possible to turn it on but not by
>> default.
>> What is your opinion about that?
>>
>> Regards,
>> David Pokluda.
>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote
>> in message news:fphj9o$ejq$1@is.intellij.net...
>>
>>> As for me, it improves code readability and reduce code duplication
>>>
>>> --
>>> Eugene Pasynkov
>>> Developer
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>> "Luis Abreu" <labreu@gmail.com> wrote in message
>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net...
>>>> Hello.
>>>>
>>>> Why does R# suggests using implicit typed vars all over the palce?
>>>> Is there any good justification for this?
>>>>
>>>> Thanks.
>>>>


0

Well, it would help. At the same time you can also suggest to rename the
method to ConstructClassObject but I don't think that it is not the main
reason of this thread.

I think that VARs are there so that Linq works. I think that trying to
suggest/use to use VARs everywhere is wrong. We should use it where it make
sense and use explicit types otherwise. I personally hope that it will be
possible to turn it (the suggestions) off because it is confusing and it
hurts the readability.

David.

"Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
news:76a2bd0b150d178ca4236d00a4f3b@news.intellij.net...

Hello David,

>

I would just rename instance to classInstance (or whatever I<Name> maps
to) and this would solve the problem. No?

>

Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>

DP> Well, I don't think I agree with that. I do see that in some places
DP> it is better to use VAR (Linq queries) but in other cases I would
DP> say it is more readable to use exact types. For example see the
DP> attached code. This is in my eyes readable code. If I look at the
DP> use of my class I see the following:
DP> DP> IClass instance = MyFactory.Construct();
DP> Console.Write(instance.GetMessage());
DP> I know exactly what it is I'm dealing with. Now let's take a look
DP> what ReSharper is suggesting:
DP> DP> var instance = MyFactory.Construct();
DP> Console.Write(instance.GetMessage());
DP> Now can you please elaborate on the readability of this code
DP> snippet? If I see this code I would HAVE TO go inside the Construct
DP> method to find out what the return type is.
DP> DP> I would STRONGLY recommend not to suggest implicitly typed vars by
DP> default. If somebody wants that it should be possible to turn it on
DP> but not by default.
DP> DP> What is your opinion about that?
DP> DP> Regards,
DP> David Pokluda.
DP> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote
DP> in message news:fphj9o$ejq$1@is.intellij.net...
DP>

>>> As for me, it improves code readability and reduce code duplication
>>>
>>> --
>>> Eugene Pasynkov
>>> Developer
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>> "Luis Abreu" <labreu@gmail.com> wrote in message
>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net...
>>>> Hello.
>>>>
>>>> Why does R# suggests using implicit typed vars all over the palce?
>>>> Is there any good justification for this?
>>>>
>>>> Thanks.
>>>>
>


0

Hello David,

I very well understand you, because that was exactly my concernes when I
first started to use vars. I have different feeling now, but it doesn't matter
here :)
So we prepared two analysis options, which you can configure in Options /
Code Inspection / Inspection Severity
One does pretty conservative analysis, and the other suggests vars whenever
possible. You can set it at your preffered style.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


DP> Well, it would help. At the same time you can also suggest to rename
DP> the method to ConstructClassObject but I don't think that it is not
DP> the main reason of this thread.
DP>
DP> I think that VARs are there so that Linq works. I think that trying
DP> to suggest/use to use VARs everywhere is wrong. We should use it
DP> where it make sense and use explicit types otherwise. I personally
DP> hope that it will be possible to turn it (the suggestions) off
DP> because it is confusing and it hurts the readability.
DP>
DP> David.
DP>
DP> "Ilya Ryzhenkov" wrote in message DP> news:76a2bd0b150d178ca4236d00a4f3b@news.intellij.net... DP> >> Hello David, >> >> I would just rename instance to classInstance (or whatever I >> maps to) and this would solve the problem. No? >> >> Sincerely, >> Ilya Ryzhenkov >> JetBrains, Inc >> http://www.jetbrains.com >> "Develop with pleasure!" >> DP> Well, I don't think I agree with that. I do see that in some >> places >> DP> it is better to use VAR (Linq queries) but in other cases I would >> DP> say it is more readable to use exact types. For example see the >> DP> attached code. This is in my eyes readable code. If I look at the >> DP> use of my class I see the following: >> DP> DP> IClass instance = MyFactory.Construct(); >> DP> Console.Write(instance.GetMessage()); >> DP> I know exactly what it is I'm dealing with. Now let's take a look >> DP> what ReSharper is suggesting: >> DP> DP> var instance = MyFactory.Construct(); >> DP> Console.Write(instance.GetMessage()); >> DP> Now can you please elaborate on the readability of this code >> DP> snippet? If I see this code I would HAVE TO go inside the >> Construct >> DP> method to find out what the return type is. >> DP> DP> I would STRONGLY recommend not to suggest implicitly typed >> vars by >> DP> default. If somebody wants that it should be possible to turn it >> on >> DP> but not by default. >> DP> DP> What is your opinion about that? >> DP> DP> Regards, >> DP> David Pokluda. >> DP> "Eugene Pasynkov (JetBrains)" >> wrote >> DP> in message news:fphj9o$ejq$1@is.intellij.net... >> DP> >>>> As for me, it improves code readability and reduce code duplication >>>> >>>> -- >>>> Eugene Pasynkov >>>> Developer >>>> JetBrains, Inc >>>> http://www.jetbrains.com >>>> "Develop with pleasure!" >>>> "Luis Abreu" wrote in message >>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net... >>>>> Hello. >>>>> >>>>> Why does R# suggests using implicit typed vars all over the palce? >>>>> Is there any good justification for this? >>>>> >>>>> Thanks. >>>>>]]>


0

Hi Ilya,

I was very glad to see the addition of code analysis "hints" in addition to
"suggestions". I think many would agree that the severity for using
implicitly typed vars should be "hint" by default, and not "suggestion".

Cheers,
Mike

"Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
news:76a2bd0b150d968ca424339fbae4d@news.intellij.net...

Hello David,

>

I very well understand you, because that was exactly my concernes when I
first started to use vars. I have different feeling now, but it doesn't
matter here :)
So we prepared two analysis options, which you can configure in Options /
Code Inspection / Inspection Severity
One does pretty conservative analysis, and the other suggests vars
whenever possible. You can set it at your preffered style.

>

Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>

DP> Well, it would help. At the same time you can also suggest to rename
DP> the method to ConstructClassObject but I don't think that it is not
DP> the main reason of this thread.
DP> DP> I think that VARs are there so that Linq works. I think that
trying
DP> to suggest/use to use VARs everywhere is wrong. We should use it
DP> where it make sense and use explicit types otherwise. I personally
DP> hope that it will be possible to turn it (the suggestions) off
DP> because it is confusing and it hurts the readability.
DP> DP> David.
DP> DP> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
DP> news:76a2bd0b150d178ca4236d00a4f3b@news.intellij.net...
DP>

>>> Hello David,
>>>
>>> I would just rename instance to classInstance (or whatever I >>> maps to) and this would solve the problem. No? >>> >>> Sincerely, >>> Ilya Ryzhenkov >>> JetBrains, Inc >>> http://www.jetbrains.com >>> "Develop with pleasure!" >>> DP> Well, I don't think I agree with that. I do see that in some >>> places >>> DP> it is better to use VAR (Linq queries) but in other cases I would >>> DP> say it is more readable to use exact types. For example see the >>> DP> attached code. This is in my eyes readable code. If I look at the >>> DP> use of my class I see the following: >>> DP> DP> IClass instance = MyFactory.Construct(); >>> DP> Console.Write(instance.GetMessage()); >>> DP> I know exactly what it is I'm dealing with. Now let's take a look >>> DP> what ReSharper is suggesting: >>> DP> DP> var instance = MyFactory.Construct(); >>> DP> Console.Write(instance.GetMessage()); >>> DP> Now can you please elaborate on the readability of this code >>> DP> snippet? If I see this code I would HAVE TO go inside the >>> Construct >>> DP> method to find out what the return type is. >>> DP> DP> I would STRONGLY recommend not to suggest implicitly typed >>> vars by >>> DP> default. If somebody wants that it should be possible to turn it >>> on >>> DP> but not by default. >>> DP> DP> What is your opinion about that? >>> DP> DP> Regards, >>> DP> David Pokluda. >>> DP> "Eugene Pasynkov (JetBrains)" >>> wrote >>> DP> in message news:fphj9o$ejq$1@is.intellij.net... >>> DP> >>>>> As for me, it improves code readability and reduce code duplication >>>>> >>>>> -- >>>>> Eugene Pasynkov >>>>> Developer >>>>> JetBrains, Inc >>>>> http://www.jetbrains.com >>>>> "Develop with pleasure!" >>>>> "Luis Abreu" wrote in message >>>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net... >>>>>> Hello. >>>>>> >>>>>> Why does R# suggests using implicit typed vars all over the palce? >>>>>> Is there any good justification for this? >>>>>> >>>>>> Thanks. >>>>>> >]]>


0

As you may notice, there are 2 kinds of "replace-to-var" suggestions. The
first is when the type is evident from the right part, and the second where
it doesn't.

For example, compare these 2 lines:

Dictionary<MyClass1,MyClass2> dictionary = new
Dictionary<MyClass1,MyClass2>();

and

var dictionary = new Dictionary<MyClass1,MyClass2>();

--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"David Pokluda" <info@online.pokluda.cz> wrote in message
news:fphrsa$9ro$1@is.intellij.net...

Well, I don't think I agree with that. I do see that in some places it is
better to use VAR (Linq queries) but in other cases I would say it is more
readable to use exact types. For example see the attached code. This is in
my eyes readable code. If I look at the use of my class I see the
following:

>

IClass instance = MyFactory.Construct();
Console.Write(instance.GetMessage());

>

I know exactly what it is I'm dealing with. Now let's take a look what
ReSharper is suggesting:

>

var instance = MyFactory.Construct();
Console.Write(instance.GetMessage());

>

Now can you please elaborate on the readability of this code snippet? If I
see this code I would HAVE TO go inside the Construct method to find out
what the return type is.

>

I would STRONGLY recommend not to suggest implicitly typed vars by
default.
If somebody wants that it should be possible to turn it on but not by
default.

>

What is your opinion about that?

>

Regards,
David Pokluda.

>

"Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote in
message news:fphj9o$ejq$1@is.intellij.net...

>> As for me, it improves code readability and reduce code duplication
>>
>> --
>> Eugene Pasynkov
>> Developer
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> "Luis Abreu" <labreu@gmail.com> wrote in message
>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net...
>>> Hello.
>>>
>>> Why does R# suggests using implicit typed vars all over the palce? Is
>>> there any good justification for this?
>>>
>>> Thanks.
>>
>>



0

Hi Eugene,

I did notice there are 2 types of var replace sugestions, the first one from your example, that you called "Use 'var' keyword when initializer explicitly declares type" , and that one I agree 100% with you, the code is easier to read with the var and we have adapted to our coding standards.

Everything else you've called "Use 'var' keyword when possible", and you can use it on foreach or from method calls like:
var x = CalculateX();

those ones make the code less legible. But the fact that R# 4 has several "severity warnings" that are user configurable makes this a non issue.

My suggestion for the defaults is to make the "Use 'var' keyword when initializer explicitly declares type" as hint, and the "Use 'var' keyword when possible" to "do not show", but maybe that will make the second one less discoverable?

0

I agree with both Mike & Joao's posts: converting to var when there is no
explicit type given on the right side should be a hint, not recommendation.

"Joao Paulo Carreiro" <no_replay@jetbrains.com> wrote in message
news:22467193.1203591093917.JavaMail.itn@is.intellij.net...

Hi Eugene,

>

I did notice there are 2 types of var replace sugestions, the first one
from your example, that you called "Use 'var' keyword when initializer
explicitly declares type" , and that one I agree 100% with you, the code
is easier to read with the var and we have adapted to our coding
standards.

>

Everything else you've called "Use 'var' keyword when possible", and you
can use it on foreach or from method calls like:
var x = CalculateX();

>

those ones make the code less legible. But the fact that R# 4 has several
"severity warnings" that are user configurable makes this a non issue.

>

My suggestion for the defaults is to make the "Use 'var' keyword when
initializer explicitly declares type" as hint, and the "Use 'var' keyword
when possible" to "do not show", but maybe that will make the second one
less discoverable?



0

Hello Yuri,

Exactly, and this is the default. What's wrong?

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


Y> I agree with both Mike & Joao's posts: converting to var when there
Y> is no explicit type given on the right side should be a hint, not
Y> recommendation.
Y>
Y> "Joao Paulo Carreiro" <no_replay@jetbrains.com> wrote in message
Y> news:22467193.1203591093917.JavaMail.itn@is.intellij.net...
Y>
>> Hi Eugene,
>>
>> I did notice there are 2 types of var replace sugestions, the first
>> one from your example, that you called "Use 'var' keyword when
>> initializer explicitly declares type" , and that one I agree 100%
>> with you, the code is easier to read with the var and we have adapted
>> to our coding standards.
>>
>> Everything else you've called "Use 'var' keyword when possible", and
>> you
>> can use it on foreach or from method calls like:
>> var x = CalculateX();
>> those ones make the code less legible. But the fact that R# 4 has
>> several "severity warnings" that are user configurable makes this a
>> non issue.
>>
>> My suggestion for the defaults is to make the "Use 'var' keyword when
>> initializer explicitly declares type" as hint, and the "Use 'var'
>> keyword when possible" to "do not show", but maybe that will make the
>> second one less discoverable?
>>


0

Hello Joao,

Indeed, this will make it non-discoverable. I think we will leave default
as is - suggestion for more conservative analysis and hint for all other.
You can change severity right from QuickFix, so everyone can configure to
their taste instantly.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


JC> Hi Eugene,
JC>
JC> I did notice there are 2 types of var replace sugestions, the first
JC> one from your example, that you called "Use 'var' keyword when
JC> initializer explicitly declares type" , and that one I agree 100%
JC> with you, the code is easier to read with the var and we have
JC> adapted to our coding standards.
JC>
JC> Everything else you've called "Use 'var' keyword when possible", and
JC> you can use it on foreach or from method calls like:
JC>
JC> var x = CalculateX();
JC>
JC> those ones make the code less legible. But the fact that R# 4 has
JC> several "severity warnings" that are user configurable makes this a
JC> non issue.
JC>
JC> My suggestion for the defaults is to make the "Use 'var' keyword
JC> when initializer explicitly declares type" as hint, and the "Use
JC> 'var' keyword when possible" to "do not show", but maybe that will
JC> make the second one less discoverable?
JC>


0

Hello Mike,

When there is explicit type on the right side - it is suggestion by default.
When there is no explicit type on the right side - it is hint by default.

So, what's being discussed, I wonder? :)

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


MS> Hi Ilya,
MS>
MS> I was very glad to see the addition of code analysis "hints" in
MS> addition to "suggestions". I think many would agree that the
MS> severity for using implicitly typed vars should be "hint" by
MS> default, and not "suggestion".
MS>
MS> Cheers,
MS> Mike
MS> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
MS> news:76a2bd0b150d968ca424339fbae4d@news.intellij.net...
MS>
>> Hello David,
>>
>> I very well understand you, because that was exactly my concernes
>> when I
>> first started to use vars. I have different feeling now, but it
>> doesn't
>> matter here :)
>> So we prepared two analysis options, which you can configure in
>> Options /
>> Code Inspection / Inspection Severity
>> One does pretty conservative analysis, and the other suggests vars
>> whenever possible. You can set it at your preffered style.
>> Sincerely,
>> Ilya Ryzhenkov
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> DP> Well, it would help. At the same time you can also suggest to
>> rename
>> DP> the method to ConstructClassObject but I don't think that it is
>> not
>> DP> the main reason of this thread.
>> DP> DP> I think that VARs are there so that Linq works. I think that
>> trying
>> DP> to suggest/use to use VARs everywhere is wrong. We should use it
>> DP> where it make sense and use explicit types otherwise. I
>> personally
>> DP> hope that it will be possible to turn it (the suggestions) off
>> DP> because it is confusing and it hurts the readability.
>> DP> DP> David.
>> DP> DP> "Ilya Ryzhenkov" wrote in message >> DP> news:76a2bd0b150d178ca4236d00a4f3b@news.intellij.net... >> DP> >>>> Hello David, >>>> >>>> I would just rename instance to classInstance (or whatever I >>>> maps to) and this would solve the problem. No? >>>> >>>> Sincerely, >>>> Ilya Ryzhenkov >>>> JetBrains, Inc >>>> http://www.jetbrains.com >>>> "Develop with pleasure!" >>>> DP> Well, I don't think I agree with that. I do see that in some >>>> places >>>> DP> it is better to use VAR (Linq queries) but in other cases I >>>> would >>>> DP> say it is more readable to use exact types. For example see the >>>> DP> attached code. This is in my eyes readable code. If I look at >>>> the >>>> DP> use of my class I see the following: >>>> DP> DP> IClass instance = MyFactory.Construct(); >>>> DP> Console.Write(instance.GetMessage()); >>>> DP> I know exactly what it is I'm dealing with. Now let's take a >>>> look >>>> DP> what ReSharper is suggesting: >>>> DP> DP> var instance = MyFactory.Construct(); >>>> DP> Console.Write(instance.GetMessage()); >>>> DP> Now can you please elaborate on the readability of this code >>>> DP> snippet? If I see this code I would HAVE TO go inside the >>>> Construct >>>> DP> method to find out what the return type is. >>>> DP> DP> I would STRONGLY recommend not to suggest implicitly typed >>>> vars by >>>> DP> default. If somebody wants that it should be possible to turn >>>> it >>>> on >>>> DP> but not by default. >>>> DP> DP> What is your opinion about that? >>>> DP> DP> Regards, >>>> DP> David Pokluda. >>>> DP> "Eugene Pasynkov (JetBrains)" >>>> wrote >>>> DP> in message news:fphj9o$ejq$1@is.intellij.net... >>>> DP> >>>>>> As for me, it improves code readability and reduce code >>>>>> duplication >>>>>> >>>>>> -- >>>>>> Eugene Pasynkov >>>>>> Developer >>>>>> JetBrains, Inc >>>>>> http://www.jetbrains.com >>>>>> "Develop with pleasure!" >>>>>> "Luis Abreu" wrote in message >>>>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net... >>>>>>> Hello. >>>>>>> >>>>>>> Why does R# suggests using implicit typed vars all over the >>>>>>> palce? Is there any good justification for this? >>>>>>> >>>>>>> Thanks. >>>>>>>]]>


0
Avatar
Permanently deleted user

Instead of forcing the user to encode the type in the name - which is equally verbose, why not provide a popup on 'var' with the inferred type, make a popup on variable reference display the inferred type as well (might have been already done, but to make it sure) and provide a code assist action to convert back to typeful declaration?

0

This is fine for writing new code, but what exactly is the behavior for
existing code?

I'd hate to load up an old file and have Resharper insist that I could
replace a type name with 'var' in 400 different places... which I won't do,
because I'm maintaining existing code.


"Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
news:76a2bd0b150d188ca4236f5347d45@news.intellij.net...

Hello Yuri,

>

I would rename myConstant to explicitly state "what is it". It is
identifier? Member name? Resource name?
Give it meaningful name, and "var problems" will vanish.

>

Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>

Y> I agree. Another example that makes code less readable:
Y> Y> class Class
Y> {
Y> string myConstant = "abc";
Y> void Method()
Y> {
Y> var a = new[] { myConstant };
Y> }
Y> }
Y> R# would suggest changing string[] to var, but usually there should
Y> be some
Y> indicator of the type: either
Y> string[] a = new[] ;
Y> or
Y> var a = new string[] ;
Y> Either one is enough, both at the same time are not needed.
Y> Y> Thanks,
Y> --Yuri
Y> "David Pokluda" <info@online.pokluda.cz> wrote in message
Y> news:fphrsa$9ro$1@is.intellij.net...
Y>

>>> Well, I don't think I agree with that. I do see that in some places
>>> it is better to use VAR (Linq queries) but in other cases I would say
>>> it is more readable to use exact types. For example see the attached
>>> code. This is in my eyes readable code. If I look at the use of my
>>> class I see the following:
>>>
>>> IClass instance = MyFactory.Construct();
>>> Console.Write(instance.GetMessage());
>>> I know exactly what it is I'm dealing with. Now let's take a look
>>> what ReSharper is suggesting:
>>>
>>> var instance = MyFactory.Construct();
>>> Console.Write(instance.GetMessage());
>>> Now can you please elaborate on the readability of this code snippet?
>>> If I see this code I would HAVE TO go inside the Construct method to
>>> find out what the return type is.
>>>
>>> I would STRONGLY recommend not to suggest implicitly typed vars by
>>> default.
>>> If somebody wants that it should be possible to turn it on but not by
>>> default.
>>> What is your opinion about that?
>>>
>>> Regards,
>>> David Pokluda.
>>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote
>>> in message news:fphj9o$ejq$1@is.intellij.net...
>>>
>>>> As for me, it improves code readability and reduce code duplication
>>>>
>>>> --
>>>> Eugene Pasynkov
>>>> Developer
>>>> JetBrains, Inc
>>>> http://www.jetbrains.com
>>>> "Develop with pleasure!"
>>>> "Luis Abreu" <labreu@gmail.com> wrote in message
>>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net...
>>>>> Hello.
>>>>>
>>>>> Why does R# suggests using implicit typed vars all over the palce?
>>>>> Is there any good justification for this?
>>>>>
>>>>> Thanks.
>>>>>
>


0

Makes sense.

BTW, I really love the quickfix configure. Keep up the good work :)

0

Hello Paul,

You just don't apply quick-fix! Or is it something like a second nature to
quick-fix all things around? :))

Actually, you can instantly change severity to hint from a quick-fix menu.
Also, this doesn't change green status of file, or something like this.
Otherwise, how ReSharper can guess if you are maintaining legacy code or
writing brand new code?

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


PB> This is fine for writing new code, but what exactly is the behavior
PB> for existing code?
PB>
PB> I'd hate to load up an old file and have Resharper insist that I
PB> could replace a type name with 'var' in 400 different places...
PB> which I won't do, because I'm maintaining existing code.
PB>
PB> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
PB> news:76a2bd0b150d188ca4236f5347d45@news.intellij.net...
PB>
>> Hello Yuri,
>>
>> I would rename myConstant to explicitly state "what is it". It is
>> identifier? Member name? Resource name?
>> Give it meaningful name, and "var problems" will vanish.
>> Sincerely,
>> Ilya Ryzhenkov
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> Y> I agree. Another example that makes code less readable:
>> Y> Y> class Class
>> Y> {
>> Y> string myConstant = "abc";
>> Y> void Method()
>> Y> {
>> Y> var a = new[] { myConstant };
>> Y> }
>> Y> }
>> Y> R# would suggest changing string[] to var, but usually there
>> should
>> Y> be some
>> Y> indicator of the type: either
>> Y> string[] a = new[] ;
>> Y> or
>> Y> var a = new string[] ;
>> Y> Either one is enough, both at the same time are not needed.
>> Y> Y> Thanks,
>> Y> --Yuri
>> Y> "David Pokluda" <info@online.pokluda.cz> wrote in message
>> Y> news:fphrsa$9ro$1@is.intellij.net...
>> Y>
>>>> Well, I don't think I agree with that. I do see that in some places
>>>> it is better to use VAR (Linq queries) but in other cases I would
>>>> say it is more readable to use exact types. For example see the
>>>> attached code. This is in my eyes readable code. If I look at the
>>>> use of my class I see the following:
>>>>
>>>> IClass instance = MyFactory.Construct();
>>>> Console.Write(instance.GetMessage());
>>>> I know exactly what it is I'm dealing with. Now let's take a look
>>>> what ReSharper is suggesting:
>>>> var instance = MyFactory.Construct();
>>>> Console.Write(instance.GetMessage());
>>>> Now can you please elaborate on the readability of this code
>>>> snippet?
>>>> If I see this code I would HAVE TO go inside the Construct method
>>>> to
>>>> find out what the return type is.
>>>> I would STRONGLY recommend not to suggest implicitly typed vars by
>>>> default.
>>>> If somebody wants that it should be possible to turn it on but not
>>>> by
>>>> default.
>>>> What is your opinion about that?
>>>> Regards,
>>>> David Pokluda.
>>>> "Eugene Pasynkov (JetBrains)" <Eugene.Pasynkov@jetbrains.com> wrote
>>>> in message news:fphj9o$ejq$1@is.intellij.net...
>>>>> As for me, it improves code readability and reduce code
>>>>> duplication
>>>>>
>>>>> --
>>>>> Eugene Pasynkov
>>>>> Developer
>>>>> JetBrains, Inc
>>>>> http://www.jetbrains.com
>>>>> "Develop with pleasure!"
>>>>> "Luis Abreu" <labreu@gmail.com> wrote in message
>>>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net...
>>>>>> Hello.
>>>>>>
>>>>>> Why does R# suggests using implicit typed vars all over the
>>>>>> palce? Is there any good justification for this?
>>>>>>
>>>>>> Thanks.
>>>>>>


0

Hi Ilya,

I personally changed both code inspection severities for using 'var' to
"Show as hint" because of the sheer abundance of variable declarations in
existing code. I find it distracting to see many green indicators in the
text editor margin representing suggestions for using 'var'. While running
code cleanup could automatically take care of these, we would prefer not to
introduce so many new file differences into source control history for what
is purely a cosmetic change. Also, "old habits die hard," as they say, and
many developers would prefer to continue using explicit type declarations,
particularly where primitive and string types are concerned. Consider the
readability of "int x = 4" vs. "var x = 4", or even better, "decimal x = 4m"
vs. "var x = 4m". In such cases, I find explicit typing more readable.
While implicit typing is certainly useful for saving keystrokes,
particularly where long type names or nested generic types are concerned, I
contend that it is a purely cosmetic change, and thus better suited to be
shown as a 'hint' rather than a 'suggestion'.

Best regards,
Mike Strobel

"Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
news:76a2bd0b150eef8ca42d539286127@news.intellij.net...

Hello Mike,

>

When there is explicit type on the right side - it is suggestion by
default.
When there is no explicit type on the right side - it is hint by default.

>

So, what's being discussed, I wonder? :)

>

Sincerely,
Ilya Ryzhenkov

>

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>
>

MS> Hi Ilya,
MS> MS> I was very glad to see the addition of code analysis "hints" in
MS> addition to "suggestions". I think many would agree that the
MS> severity for using implicitly typed vars should be "hint" by
MS> default, and not "suggestion".
MS> MS> Cheers,
MS> Mike
MS> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
MS> news:76a2bd0b150d968ca424339fbae4d@news.intellij.net...
MS>

>>> Hello David,
>>>
>>> I very well understand you, because that was exactly my concernes
>>> when I
>>> first started to use vars. I have different feeling now, but it
>>> doesn't
>>> matter here :)
>>> So we prepared two analysis options, which you can configure in
>>> Options /
>>> Code Inspection / Inspection Severity
>>> One does pretty conservative analysis, and the other suggests vars
>>> whenever possible. You can set it at your preffered style.
>>> Sincerely,
>>> Ilya Ryzhenkov
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>> DP> Well, it would help. At the same time you can also suggest to
>>> rename
>>> DP> the method to ConstructClassObject but I don't think that it is
>>> not
>>> DP> the main reason of this thread.
>>> DP> DP> I think that VARs are there so that Linq works. I think that
>>> trying
>>> DP> to suggest/use to use VARs everywhere is wrong. We should use it
>>> DP> where it make sense and use explicit types otherwise. I
>>> personally
>>> DP> hope that it will be possible to turn it (the suggestions) off
>>> DP> because it is confusing and it hurts the readability.
>>> DP> DP> David.
>>> DP> DP> "Ilya Ryzhenkov" wrote in message >>> DP> news:76a2bd0b150d178ca4236d00a4f3b@news.intellij.net... >>> DP> >>>>> Hello David, >>>>> >>>>> I would just rename instance to classInstance (or whatever I >>>>> maps to) and this would solve the problem. No? >>>>> >>>>> Sincerely, >>>>> Ilya Ryzhenkov >>>>> JetBrains, Inc >>>>> http://www.jetbrains.com >>>>> "Develop with pleasure!" >>>>> DP> Well, I don't think I agree with that. I do see that in some >>>>> places >>>>> DP> it is better to use VAR (Linq queries) but in other cases I >>>>> would >>>>> DP> say it is more readable to use exact types. For example see the >>>>> DP> attached code. This is in my eyes readable code. If I look at >>>>> the >>>>> DP> use of my class I see the following: >>>>> DP> DP> IClass instance = MyFactory.Construct(); >>>>> DP> Console.Write(instance.GetMessage()); >>>>> DP> I know exactly what it is I'm dealing with. Now let's take a >>>>> look >>>>> DP> what ReSharper is suggesting: >>>>> DP> DP> var instance = MyFactory.Construct(); >>>>> DP> Console.Write(instance.GetMessage()); >>>>> DP> Now can you please elaborate on the readability of this code >>>>> DP> snippet? If I see this code I would HAVE TO go inside the >>>>> Construct >>>>> DP> method to find out what the return type is. >>>>> DP> DP> I would STRONGLY recommend not to suggest implicitly typed >>>>> vars by >>>>> DP> default. If somebody wants that it should be possible to turn >>>>> it >>>>> on >>>>> DP> but not by default. >>>>> DP> DP> What is your opinion about that? >>>>> DP> DP> Regards, >>>>> DP> David Pokluda. >>>>> DP> "Eugene Pasynkov (JetBrains)" >>>>> wrote >>>>> DP> in message news:fphj9o$ejq$1@is.intellij.net... >>>>> DP> >>>>>>> As for me, it improves code readability and reduce code >>>>>>> duplication >>>>>>> >>>>>>> -- >>>>>>> Eugene Pasynkov >>>>>>> Developer >>>>>>> JetBrains, Inc >>>>>>> http://www.jetbrains.com >>>>>>> "Develop with pleasure!" >>>>>>> "Luis Abreu" wrote in message >>>>>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net... >>>>>>>> Hello. >>>>>>>> >>>>>>>> Why does R# suggests using implicit typed vars all over the >>>>>>>> palce? Is there any good justification for this? >>>>>>>> >>>>>>>> Thanks. >>>>>>>> >]]>


0

Hello Mike,

I very well understand you. No, really! Some time ago, when we've got var
working well in ReSharper, I had the very same concernes as you currently
have. I also thought that explicit type specification is better than var.
Today, I use vars everywhere, and adopted somewhat different naming style
(not Hungarian Notation) for my local variables.

That said, we are not going to change defaults, and if you don't like them,
you are welcome to configure it to your taste.

Sincerely,
Ilya Ryzhenkov

JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"


MS> Hi Ilya,
MS>
MS> I personally changed both code inspection severities for using 'var'
MS> to "Show as hint" because of the sheer abundance of variable
MS> declarations in existing code. I find it distracting to see many
MS> green indicators in the text editor margin representing suggestions
MS> for using 'var'. While running code cleanup could automatically
MS> take care of these, we would prefer not to introduce so many new
MS> file differences into source control history for what is purely a
MS> cosmetic change. Also, "old habits die hard," as they say, and many
MS> developers would prefer to continue using explicit type
MS> declarations, particularly where primitive and string types are
MS> concerned. Consider the readability of "int x = 4" vs. "var x = 4",
MS> or even better, "decimal x = 4m" vs. "var x = 4m". In such cases, I
MS> find explicit typing more readable. While implicit typing is
MS> certainly useful for saving keystrokes, particularly where long type
MS> names or nested generic types are concerned, I contend that it is a
MS> purely cosmetic change, and thus better suited to be shown as a
MS> 'hint' rather than a 'suggestion'.
MS>
MS> Best regards,
MS> Mike Strobel
MS> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
MS> news:76a2bd0b150eef8ca42d539286127@news.intellij.net...
MS>
>> Hello Mike,
>>
>> When there is explicit type on the right side - it is suggestion by
>> default.
>> When there is no explicit type on the right side - it is hint by
>> default.
>> So, what's being discussed, I wonder? :)
>>
>> Sincerely,
>> Ilya Ryzhenkov
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>> MS> Hi Ilya,
>> MS> MS> I was very glad to see the addition of code analysis "hints"
>> in
>> MS> addition to "suggestions". I think many would agree that the
>> MS> severity for using implicitly typed vars should be "hint" by
>> MS> default, and not "suggestion".
>> MS> MS> Cheers,
>> MS> Mike
>> MS> "Ilya Ryzhenkov" <orangy@jetbrains.com> wrote in message
>> MS> news:76a2bd0b150d968ca424339fbae4d@news.intellij.net...
>> MS>
>>>> Hello David,
>>>>
>>>> I very well understand you, because that was exactly my concernes
>>>> when I
>>>> first started to use vars. I have different feeling now, but it
>>>> doesn't
>>>> matter here :)
>>>> So we prepared two analysis options, which you can configure in
>>>> Options /
>>>> Code Inspection / Inspection Severity
>>>> One does pretty conservative analysis, and the other suggests vars
>>>> whenever possible. You can set it at your preffered style.
>>>> Sincerely,
>>>> Ilya Ryzhenkov
>>>> JetBrains, Inc
>>>> http://www.jetbrains.com
>>>> "Develop with pleasure!"
>>>> DP> Well, it would help. At the same time you can also suggest to
>>>> rename
>>>> DP> the method to ConstructClassObject but I don't think that it is
>>>> not
>>>> DP> the main reason of this thread.
>>>> DP> DP> I think that VARs are there so that Linq works. I think
>>>> that
>>>> trying
>>>> DP> to suggest/use to use VARs everywhere is wrong. We should use
>>>> it
>>>> DP> where it make sense and use explicit types otherwise. I
>>>> personally
>>>> DP> hope that it will be possible to turn it (the suggestions) off
>>>> DP> because it is confusing and it hurts the readability.
>>>> DP> DP> David.
>>>> DP> DP> "Ilya Ryzhenkov" wrote in message >>>> DP> news:76a2bd0b150d178ca4236d00a4f3b@news.intellij.net... >>>> DP> >>>>>> Hello David, >>>>>> >>>>>> I would just rename instance to classInstance (or whatever >>>>>> I maps to) and this would solve the problem. No? >>>>>> >>>>>> Sincerely, >>>>>> Ilya Ryzhenkov >>>>>> JetBrains, Inc >>>>>> http://www.jetbrains.com >>>>>> "Develop with pleasure!" >>>>>> DP> Well, I don't think I agree with that. I do see that in some >>>>>> places >>>>>> DP> it is better to use VAR (Linq queries) but in other cases I >>>>>> would >>>>>> DP> say it is more readable to use exact types. For example see >>>>>> the >>>>>> DP> attached code. This is in my eyes readable code. If I look at >>>>>> the >>>>>> DP> use of my class I see the following: >>>>>> DP> DP> IClass instance = MyFactory.Construct(); >>>>>> DP> Console.Write(instance.GetMessage()); >>>>>> DP> I know exactly what it is I'm dealing with. Now let's take a >>>>>> look >>>>>> DP> what ReSharper is suggesting: >>>>>> DP> DP> var instance = MyFactory.Construct(); >>>>>> DP> Console.Write(instance.GetMessage()); >>>>>> DP> Now can you please elaborate on the readability of this code >>>>>> DP> snippet? If I see this code I would HAVE TO go inside the >>>>>> Construct >>>>>> DP> method to find out what the return type is. >>>>>> DP> DP> I would STRONGLY recommend not to suggest implicitly >>>>>> typed >>>>>> vars by >>>>>> DP> default. If somebody wants that it should be possible to turn >>>>>> it >>>>>> on >>>>>> DP> but not by default. >>>>>> DP> DP> What is your opinion about that? >>>>>> DP> DP> Regards, >>>>>> DP> David Pokluda. >>>>>> DP> "Eugene Pasynkov (JetBrains)" >>>>>> wrote >>>>>> DP> in message news:fphj9o$ejq$1@is.intellij.net... >>>>>> DP> >>>>>>>> As for me, it improves code readability and reduce code >>>>>>>> duplication >>>>>>>> >>>>>>>> -- >>>>>>>> Eugene Pasynkov >>>>>>>> Developer >>>>>>>> JetBrains, Inc >>>>>>>> http://www.jetbrains.com >>>>>>>> "Develop with pleasure!" >>>>>>>> "Luis Abreu" wrote in message >>>>>>>> news:26279681.1203520748354.JavaMail.itn@is.intellij.net... >>>>>>>>> Hello. >>>>>>>>> >>>>>>>>> Why does R# suggests using implicit typed vars all over the >>>>>>>>> palce? Is there any good justification for this? >>>>>>>>> >>>>>>>>> Thanks. >>>>>>>>>]]>


0

Please sign in to leave a comment.