5 Easy Tips to Reduce and Refactor Your Code with Resharper

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

  <p>I love it! </p>  <h3><strong>Tip 5) Property Accessors</strong></h3>  <p>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:</p>  <ol>   <li>F12 </li>    <li>ALT + ENTER </li>    <li>ENTER </li> </ol>  <p>So this</p>  <p><a href="/wp-content/uploads/files/media/image/WindowsLiveWriter/25609f6e6ec2_E7DB/image_22.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="/wp-content/uploads/files/media/image/WindowsLiveWriter/25609f6e6ec2_E7DB/image_thumb_10.png" width="483" height="96" /></a> </p>  <p>becomes this</p>  <p><a href="/wp-content/uploads/files/media/image/WindowsLiveWriter/25609f6e6ec2_E7DB/image_24.png"><img style="border-right-width: 0px; display: inline; border-top-width: 0px; border-bottom-width: 0px; border-left-width: 0px" title="image" border="0" alt="image" src="/wp-content/uploads/files/media/image/WindowsLiveWriter/25609f6e6ec2_E7DB/image_thumb_11.png" width="477" height="43" /></a> </p>  <p>&#160;</p>  <p>&#160;</p>  <p>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.</p>
Tweet Post Share Update RSS

Hi, I'm John Papa. I author this blog, create courses for Pluralsight and am a Google Developer Expert and Microsoft Regional Director. I travel speaking at events and train technology thought leaders

Tags:
Silverlight