TCM
TCM

Reputation: 16920

C sharp code cleanup : resharper

I just used Resharper and it made me feel as if i don't know how to code at all in C#; it gave me a lot of suggestions; few of them are:

1) SomeObject o = new SomeObject();

Resharper will convert to :

var o = new SomeObject()

2) this.Loaded += new RoutedEventHandler(MainPage_Loaded);

to

this.Loaded += MainPage_Loaded;

3) convert my variables and putting _ in front of all instance variables.

4) Removing class parent's name. I tested this on Silverlight.

public partial class MainPage : UserControl

to

public partial class MainPage

5) replace instance variable

 this.variable = somevalue

to

 variable = somevalue

Are all of these really necessary? Is it really going to affect the efficiency of my program? I mean what good is it going to do by replacing my class names with var keyword. After all var is also replaced with class name at compile time. Is it doing because it has been programmed to do or do these things really affect in some or another way?

Upvotes: 8

Views: 1475

Answers (4)

jason
jason

Reputation: 241789

Are all of these really necessary?

None are necessary.

Is it really going to affect the efficiency of my program?

It will not impact the compiled code in a way that changes performance or semantics at all.

I mean what good is it going to do by replacing my class names with var keyword. After all var is also replaced with class name at compile time.

More readable code.

Is it doing because it has been programmed to do or do these things really affect in some or another way?

Yes. These are configurable if you don't like them.

1) Is okay.

SomeObject o = new SomeObject();

The first SomeObject is redundant and useless and with var it's no less readable and arguably more readable. Addtionally for complex definitions like

var dictionary = new Dictionary<string, Dictionary<string, List<string>>();

it's way more readable than the alternative. However, this can be overdone. For example

string s = "s";

is preferred over

var s = "s";

as is

var i = 5;

over

int i = 5;

Note that

var i = 2147483648;

is really bad because it's not immediately clear what the type of i is. For non-complex definitions I prefer using explicit typing over implicit typing. Additionally, sometimes you want to say

IFoo foo = new Foo();

to explicitly type foo an IFoo whereas var would type it as a Foo.

2) Less code is in general better code.

3) I hate this suggestion. I hate the leading underscore. I use the usual naming convention for my member variables and I preface them with this for clarity. I would turn this one off.

public SomeObject(string name) {
    this.name = name;
}

is more readable than

public SomeObject(string name) {
    _name = name;
}

in my opinion.

4) Wait, why is it doing this? I'm a little confused by this one. Because it's partial and another part of the class definition has the inheritance? I can't imagine it's doing this in a case where it changes the semantics. I would turn this one off.

5) I don't like this one (see 3.)

Upvotes: 5

Tim Lloyd
Tim Lloyd

Reputation: 38494

This behaviour is all configurable in ReSharper settings. There are a mix of code clean-up rules (e.g. whether to replace usages with var - I don't!), code style rules (e.g. variable naming) and formatting rules (e.g. how to place braces).

I wrote an article that gives an overview of these settings and how they can be used to shape coding standards and then be automatically applied via code cleanup:

http://gojisoft.com/blog/2010/05/10/coding-standards-using-resharper/

At the end of the day a lot of them are concerned with coding style and removing redundant code, they have no affect on the compiled code. You can configure many of them to suit you or your team's coding style.

Upvotes: 8

Padu Merloti
Padu Merloti

Reputation: 3229

They are not necessary.

They follow a coding standard set by resharper. The nice thing is that you can configure the coding standard to your own ways.

The beautiful thing about resharper though is that you end up learning different ways of doing something you always did in a certain manner.

Upvotes: 3

Joe Enos
Joe Enos

Reputation: 40431

All of those changes should have no effect whatsoever on the compiled CIL code - they're just ReSharper's opinion on readability of the C# code itself.

You can adjust all of those settings to suit your or your department's needs. For the most part, it's a matter of personal preference.

Upvotes: 2

Related Questions