Making Your Code Better

Exclusive offer: get 50% off this eBook here
ReSharper Essentials

ReSharper Essentials — Save 50%

Make your Microsoft Visual Studio work smarter with ReSharper with this book and ebook

$17.99    $9.00
by Łukasz Gąsior | February 2014 | Open Source

This article by Łukasz Gąsior, the author of the book ReSharper Essentials, will focus on one of the most notable ReSharper features—code analysis.

In this article, we will cover the following topics:

  • Code quality analysis
  • Eliminating errors and code smells

(For more resources related to this topic, see here.)

Code quality analysis

The fact that you can compile your code does not mean your code is good. It does not even mean it will work. There are many things that can easily break your code. A good example is an unhandled NullReferenceException. You will be able to compile your code, you will be able to run your application, but there will be a problem.

ReSharper v8 comes with more than 1400 code analysis rules and more than 700 quick fixes, which allow you to fix detected problems. What is really cool is that ReSharper provides you with code inspection rules for all supported languages. This means that ReSharper not only improves your C# or VB.NET code, but also HTML, JavaScript, CSS, XAML, XML, ASP.NET, ASP.NET MVC, and TypeScript.

Apart from finding possible errors, code quality analysis rules can also improve the readability of your code. ReSharper can detect code, which is unused and mark it as grayed, prompts you that maybe you should use auto properties or objects and collection initializers, or use the var keyword instead of an explicit type name.

ReSharper provides you with five severity levels for rules and allows you to configure them according to your preference. Code inspection rules can be configured in the ReSharper's Options window. A sample view of code inspection rules with the list of available severity levels is shown in the following screenshot:

Background analysis

One of the best features in terms of code quality in ReSharper is Background analysis. This means that all the rules are checked as you are writing your code. You do not need to compile your project to see the results of the analysis. ReSharper will display appropriate messages in real time.

Solution-wide inspections

By default, the described rules are checked locally, which means that they should be checked in the current class. Because of this, ReSharper can mark some code as unused if it is used only locally; for example, there can be any unused private method or some part of code inside your method.

These two cases are shown in the following screenshot:

Additionally, for local analysis, ReSharper can check some rules in your entire project. To do this, you need to enable Solution-wide inspections. The easiest way to enable Solution-wide inspections is to double-click the circle icon in the bottom-right corner of Visual Studio, as seen in the following screenshot:

With enabled Solution-wide inspections, ReSharper can mark the public methods or returned values that are unused.

Please note that running Solution-wide inspections can hit Visual Studio’s performance in big projects. In such cases, it is better to disable this feature.

Disabling code inspections

With ReSharper v8, you can easily mark some part of your code as code that should not be checked by ReSharper.

You can do this by adding the following comments:

// ReSharper disable all // [your code] // ReSharper restore all

All code between these two comments will be skipped by ReSharper in code inspections. Of course, instead of the all word, you can use the name of any ReSharper rule such as UseObjectOrCollectionInitializer.

You can also disable ReSharper analysis for a single line with the following comment:

// ReSharper disable once UseObjectOrCollectionInitializer

ReSharper can generate these comments for you. If ReSharper highlights some issue, then just press Alt + Enter and select Options for “YOUR_RULE“ inspection, as shown in the following screenshot:

Code Issues

You can also an ad-hoc run code analysis. An ad-hoc analysis can be run on the solution or project level.

To run ad-hoc analysis, just navigate to RESHARPER | Inspect | Code Issues in Solution or RESHARPER | Inspect | Code Issues in Current Project from the Visual Studio toolbar.

This will display a dialog box that shows us the progress of analysis and will finally display the results in the Inspection Results window. You can filter and group the displayed issues as and when you need to. You can also quickly go to a place where the issue occurs just by double-clicking on it.

A sample report is shown in the following screenshot:

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 let you select the best one for you.

Fix in scope

Quick fixes described above 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 some 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:

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:

You can see two things here:

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

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

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

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 proper severity for your pattern, as shown in the following screenshot:

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

Code Cleanup

ReSharper also allows you to fix more than one issue in one run. Navigate to RESHARPER | Tools | Cleanup Code… from 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:

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.

Summary

Code quality analysis is a very powerful feature in ReSharper. As we have described in this article, ReSharper not only prompts you when something is wrong or can be written better, but also allows you to quickly fix these issues.

If you do not agree with all rules provided by ReSharper, you can easily configure them to meet your needs.

There are many rules that will open your eyes and show you that you can write better code. With ReSharper, writing better, cleaner code is as easy as just pressing Alt + Enter.

Resources for Article:


Further resources on this subject:


ReSharper Essentials Make your Microsoft Visual Studio work smarter with ReSharper with this book and ebook
Published: February 2014
eBook Price: $17.99
Book Price: $29.99
See more
Select your format and quantity:

About the Author :


Łukasz Gąsior

Łukasz Gąsior is a programming enthusiast with several years of experience. A big fan of ReSharper and jQuery, he is primarily involved in projects related to web applications (ASP.NET and ASP.NET MVC). He enjoys trying his hand at different technologies, such as creating Android applications, just for the fun of it. He strongly believes that JavaScript can be mastered just like any other language.

If you would like to get in touch, he tweets at @lukaszgasior and blogs at http://gasior.net.pl and http://codingtv.pl (both blogs are in Polish).

Books From Packt


Instant StyleCop Code Analysis How-to
Instant StyleCop Code Analysis How-to

Data Manipulation with R
Data Manipulation with R

Reporting with Visual Studio and Crystal Reports
Reporting with Visual Studio and Crystal Reports

Visual Studio 2012 Cookbook
Visual Studio 2012 Cookbook

Software Testing using Visual Studio 2012
Software Testing using Visual Studio 2012

Visual Studio 2012 and .NET 4.5 Expert Development Cookbook
Visual Studio 2012 and .NET 4.5 Expert Development Cookbook

C++ Application Development with Code::Blocks
C++ Application Development with Code::Blocks

LiveCode Mobile Development Beginner's Guide
LiveCode Mobile Development Beginner's Guide


No votes yet

Post new comment

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
p
2
c
5
t
Q
Enter the code without spaces and pay attention to upper/lower case.
Code Download and Errata
Packt Anytime, Anywhere
Register Books
Print Upgrades
eBook Downloads
Video Support
Contact Us
Awards Voting Nominations Previous Winners
Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
Resources
Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software