Silverlight Feeds - All your Silverlight feeds in one place.

Sponsors

Sunday, October 03, 2010

5 Easy Tips to Reduce and Refactor Your Code with Resharper

by John Papa via JohnPapa.net on 10/3/2010 12:04:00 AM

I’ve talked a lot about Resharper features such as the Find Usages, live templates, and the instant code analysis. But I have not talked about some of the features that are not as prominent but I find especially helpful. So here are a few very simple tips for how to clean your code up using Resharper.

Tip 1) Removing redundant “this” qualifier

Sometimes I add the this statement when I am writing code. It is a habit I got into a long while ago and have had a hard time breaking. I prefer to not have “this.” leading all of my class level properties and methods. Resharper makes this easy to fix. Just remember this sequence:

  1. F12
  2. ALT + ENTER
  3. ENTER

F12 brings you to the next Resharper warning (or Error). In this case, you will see the “this.” statement (grayed out because it is redundant)

image

ALT-ENTER displays the Resharper hint window (or you can click the red light bulb)

image

ENTER removes the redundant qualifier.

image

Tip 2) Removing Unreachable Code

This same process works for removing unreachable code. 

  1. F12
  2. ALT + ENTER
  3. ENTER

So this code is the before:

image

And this code is after:

image

Tip 3) Removing Redundant Type Arguments

Sometimes you have methods that accept a type using Generics. And sometimes specifying the type is redundant. Resharper let’s you find these, by, guess what, using:

  1. F12
  2. ALT + ENTER
  3. ENTER

So this …

image

becomes this

image

Tip 4) Redundant Using Directives

I like to clean up my Using statements at the top of my class files. It seems some files, like when you create a new Page, have tons of extra “Using” statements. Yes, maybe I’ll need them, but even I do, I’ll let Resharper or Visual Studio’s tools tell me. So let’s get rid of them.

First, I hit the CTRL + HOME keys to go to the top of the page.

Then comes the common sequence:

  1. F12
  2. ALT + ENTER
  3. ENTER

So this …

image

becomes this:

image

I love it!

Tip 5) Property Accessors

When I create public properties sometimes I forget to make the set accessor private. Do I need to? No, but why expose something that does not need to be exposed. Resharper makes this easy again:

  1. F12
  2. ALT + ENTER
  3. ENTER

So this

image

becomes this

image

 

 

There is so much you can do with Resharper but hopefully this shows you how easy it is to tackle some small refactoring jobs to reduce some code in your project.

email it!bookmark it!digg it!

Original Post: 5 Easy Tips to Reduce and Refactor Your Code with Resharper

Subscribe

New Feed

Product Spotlight

Recently Updated Sources

Legal Note

The content of the postings is owned by the respective author. Silverlight Feeds is not responsible for the contents of the postings. This site is automatically generated and cannot be reviewed for abusive content. If you find abusive content on Silverlight Feeds, please contact us. Designated trademarks and brands are the property of their respective owners. All rights reserved.

Advertise with us