Eliminating errors and code smells

We think you will agree that the code analysis provided by ReSharper is really cool and helps create better code. What is even cooler is that ReSharper provides you with features that can fix some issues automatically.

Quick fixes

Most errors and issues found by ReSharper can be fixed just by pressing Alt + Enter. This will display a list of the available solutions and lets you select the best one for you.

Fix in scope

The quick fixes we just described allow you to fix the issues in one particular place. However, sometimes there are issues that you would like to fix in every file in your project or solution. A great example is removing unused using statements or the this keyword.

With ReSharper v8, you do not need to fix such issues manually. Instead, you can use a new feature called Fix in scope. You start as usual by pressing Alt + Enter but instead of just selecting a solution, you can select more options by clicking the small arrow on the right from the available options.

A sample usage of the Fix in scope feature is shown in the following screenshot:

Fix in scope

This will allow you to fix the selected issue with just one click!

Structural Search and Replace

Even though ReSharper contains a lot of built-in analysis, it also allows you to create your own analyses. You can create your own patterns that will be used to search some structures in your code. This feature is called Structural Search and Replace (SSR).

To open the Search with Pattern window, navigate to RESHARPER | Find | Search with Pattern…. A sample window is shown in the following screenshot:

Structural Search and Replace

You can see two things here:

  • On the left, there is a place to write your pattern
  • On the right, there is a place to define placeholders

In the preceding example, we were looking for if statements to compare them with a false expression.

You can now simply click on the Find button and ReSharper will display every piece of code that matches this pattern. Of course, you can also save your patterns.

Tip

You can create new search patterns from the code editor. Just select some code, click on the right mouse button, and select Find Similar Code….This will automatically generate the pattern for this code, which you can easily adjust to your needs.

SSR allows you not only to find code based on defined patterns, but also replace it with different code. Click on the Replace button available on the top in the preceding screenshot. This will display a new section on the left called Replace pattern. There, you can write code that will be placed instead of code that matches the defined pattern.

For the pattern shown, you can write the following code:

if (false = $value$)
{
$statement$
}

This will simply change the order of expressions inside the if statement.

The saved patterns can also be presented as Quick fixes. Simply navigate to RESHARPER | Options | Code Inspection | Custom Patterns and set the right severity for your pattern, as shown in the following screenshot:

Structural Search and Replace

This will allow you to define patterns in the code editor, which is shown in the following screenshot:

Structural Search and Replace

Code Cleanup

ReSharper also allows you to fix more than one issue in one run. Navigate to RESHARPER | Tools | Cleanup Code… in the Visual Studio toolbar or just press Ctrl + E, Ctrl + C. This will display the Code Cleanup window, which is shown in the following screenshot:

Code Cleanup

By clicking on the Run button, ReSharper will fix all issues configured in the selected profile. By default, there are two patterns:

  • Full Cleanup
  • Reformat Code

You can add your own pattern by clicking on the Edit Profiles button.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.133.180