views:

247

answers:

8

Hi

I just installed Reshaper 4.5 and it has come up with the following suggestions:

return this.GetRuleViolations().Count() == 0; -- REMOVE this.

new string[] { this.ID.ToString(), this.Registration } -- REMOVE string, MAKE ANONYMOUS TYPE

int i = Method.GetNumber(); -- REPLACE int WITH var

Should I do these?

I think in some cases it is going to make the code less readable but will it improve performance? what are the benefits of making these changes?

Thanks

A: 

First one: Resharper is asking about removing this which is just a style thing to me. Nothing more, keeping it won't harm performance in any way. It is just a matter of readability.

For second and third: Resharper normally prefers using var instead of specific data type, that's why the suggestions. I believe it is a matter of personal choice and provides no extra gain other than readability.

Aamir
A: 

The first seems unclear to me. You usually don't have to prefix this. as long as there are no ambiguities, which I cannot tell from this example. Resharper is probably right. The other two won't improve performance, the compiled result will be the same. It's just a matter of taste and, of course, your coding guidelines.

Malte Clasen
A: 

The first one should be configurable. As far as I remember, you can tell ReSharper whether you want to have "this." in front of only fields, methods, both or none.

Using "var" will not change anything in the generated CIL code, so the performance will stay the same. I haven't used ReSharper for some time and I don't know why it promotes anonymous types so aggressively, but one advantage of "var" is that it's more resistant to change.

Meaning if, instead of calling Method.GetNumber(), you called a wrapper, eg. Filter(Method.GetNumber()) in the same line that returns a Nullable, you won't have to update the variable's type.

Cygon
A: 

I think the first one is for the purpose, if you want to make "GetRuleViolations()" a static method. Then you have not to remove the "this" identifier.

Jehof
+1  A: 

For the 3rd one - the one that annoys me the most. It provides the reader with less information and i think it's just a matter of showing off a newish feature.

I'd say - use var when you know the return type and use the correct object type when you do not like this:

var reader = new XmlReader(....  // Implicit
XmlReader reader = SomeClass.GetReader() // Explicit when you can't be sure
Per Hornshøj-Schierbeck
+7  A: 

1) The explicit this pointer is only necessary when the reference would otherwise be ambiguous. Since GetRuleViolations is defined on the type, you most likely do not need this.

Another point here is that if GetRuleViolations return an IEnumerable of something, you will generally be much better off using Any() instead of Count() == 0 as you risk enumerating the entire sequence.

2) String can be inferred from the initialization.

3) Resharper prefers var over specific types.

Brian Rasmussen
+1 - Nice tip to use Any() thanks
Rigobert Song
+3  A: 

Apart from the obvious benefit of your little square going green, if you are writing code that will be maintained by someone else later, it makes good sense not to use your personal preference in coding syntax. Resharper is becoming useful in formatting code in a way that is recognisable to a very wide audience.

I belong to the school of thought that says it doesn't matter who's way is right. If we all stick to a pattern, we'll all find it easier to read each others code.

So, in my humble opinion, don't change the default resharper settings. Just accept that if you use the defaults, you make life simple for everyone.

grenade
I agree with you on using the tools defaults. Unfortunately from what I've seen, I don't like Resharper's defaults
Alfred Myers
A: 

None of these will have any effect on performance, only on readability.

I find suggestions 1 and 2 to be more readable, and 3 less readable than your original code.

But you don't need to just follow these suggestions if, e.g., you find them less readable or if they violate your company's code style standard. When you put the cursor on the squiggly line, press Alt-Enter to show the list of Contex Actions. One of them will be to to change the severity of the inspection; you can not show it at all or show it as a hint. You can find a complete list of inspections at ReSharper | Options | Code Inspection | Inspection Severity.

Alexey Romanov