C# Corner

ReSharper Tips for Quicker Code Delivery

JetBrains's .NET plug-in will make quick work of code inspections and refactoring. Here are a few tips to work with it that will have you wondering what to do with all your newfound free time.

In this column, I will cover some useful tips and tricks that I commonly use in ReSharper, the JetBrains .NET plug-in, to be more productive developing code. By using these tips you will be able to more quickly and efficiently deliver code to your client, which leaves you with more time to enjoy time with family and friends.

My most commonly used Resharper feature is its constructor parameter generator. This is useful if you are using the inversion of control principal and are using constructor injection. To use this feature, simply create a private member variable in a class:

private IPersonService _personService;

Then click on the light bulb to the right of the field in the editor or press your Resharper shortcut key. I use Alt+Enter as seen in Figure 1.

Resharper Helper Toggle

[Click on image for larger view.]
Figure 1. Resharper Helper Toggle Caption

Then select the Initialize field from constructor parameter option as seen in Figure 2.

Initialize Field from Constructor Parameter

[Click on image for larger view.]
Figure 2. Initialize Field from Constructor Parameter Caption

The class constructor will now be modified to take in an IPersonService parameter named personService:

private IPersonService _personService;

 public Program(IPersonService personService)
 {
     _personService = personService;
 }

The second trick is to extract an interface from an existing class and have it automatically added to a new file. To do this, right-click on the class declaration and then choose Extract Interface as seen in Figure 3.

Extract Interface

[Click on image for larger view.]
Figure 3. Extract Interface Caption

You will then be prompted to select the properties and methods to include in the interface as seen in Figure 4.

Adding Properties and Methods

[Click on image for larger view.]
Figure 4. Adding Properties and Methods Caption

You will see that a new interface file named IPersonService.cs has been added to the project:

namespace ResharperTricks
{
    public interface IPersonService
    {
        Person GetById(long Id);
    }
}

The third trick is automatically go to the implementation of a method from its interface declaration. Right-click on the interface method and then choose the Go to Implementation option as seen in Figure 5.

Go to Implementation

[Click on image for larger view.]
Figure 5. Go to Implementation Caption

This feature really should be built into Visual Studio.

The fourth trick is to generate a tertiary statement from an if-else block. This is a nice feature to clean up your code. I recommend using it if your if-else block is simple and you're doing a single variable assignment based on the condition. For example you can take the following if statement:

string status = string.Empty;
 int amount = 100;
 if (amount > 0)
 {
     status = "A";
 }
 else
 {
     status = "D";
 }

then use Resharper to convert it into a ?: statement as seen in Figure 6.

Convert to '?:' Operator (Tertiary Expression)

[Click on image for larger view.]
Figure 6. Convert to '?:' Operator (Tertiary Expression) Caption

The result is a single tertiary statement:

string status = string.Empty;
int amount = 100;
status = amount > 0 ? "A" : "D";

The fifth trick is to invert an if statement or block. This feature is very useful if you have an if condition that has a lengthy code block. By inverting the if condition you can often cut the if down to a single line statement with the rest of the code residing outside of the if block. For example take the if statement in the GetTotal method:

private static decimal GetTotal(string status, int amount)
 {
     decimal total = amount;
     if (status == "A")
     {
         decimal tax = amount * .10M;
         total += amount + tax;
         status = "P";
     }
     return total;
 }

Then use Resharper as seen in Figure 7 to invert the if block.

Invert If Statement

[Click on image for larger view.]
Figure 7. Invert If Statement Caption

The result of the inversion is that the if statement checks for a non "A" status and returns the total as seen here:

private static decimal GetTotal(string status, int amount)
{
    decimal total = amount;
    if (status != "A") return total;
    decimal tax = amount * .10M;
    total += amount + tax;
    status = "P";
    return total;
}

As you can see these five simple code-generation tricks in ReSharper can help boost your code quality and rate of production.

Do you have any Resharper tips that you use often that I missed? Also if you would like to see coverage of other code generation or refactoring tools let me know in the comments.

About the Author

Eric Vogel is a Senior Software Developer for Red Cedar Solutions Group in Okemos, Michigan. He is the president of the Greater Lansing User Group for .NET. Eric enjoys learning about software architecture and craftsmanship, and is always looking for ways to create more robust and testable applications. Contact him at [email protected].

comments powered by Disqus

Featured

Subscribe on YouTube