Value tracking updates in ReSharper 2018.1 EAP

Every now and then, we need to find out where a value is coming from or where it is going to. Luckily, ReSharper has built-in value tracking for quite some time already and allows us to inspect value origin and inspect value destination. In the latest ReSharper 2018.1 EAP build, we are adding support for ref-returning methods, pattern matching and out-variable declarations.

Value tracking in action

Inspecting value origins and destinations can be handy in a range of situations, for instance when checking for null-reference exceptions (NRE) or other unexpected behavior. Often, debugging is used over value tracking to solve the issue – which is of course totally appropriate. Therefore, let us focus on an example that only value tracking provides excellent help for – introducing default values to cleanup our codebase.

Given a parameter with low variance (like a bool parameter), we can much easier decide what default value to chose. We can use Inspect this… | Value Origin  (Ctrl+Shift+Alt+A) on the parameter usage to find out which value is being passed most often. We can even make use of bulk quick-fixes, to cleanup related issues in the whole file, project or solution!

Value tracking in ReSharper - Where does the value come from?

Inspection results are always presented within a tree structure. We can think of it like a recursive Find Usages with the major advantage, that the inspected value is always highlighted. The results are searchable, however, it is important to note, that only expanded items will be searched.

Find usages of a value in our codebase with ReSharper

In this case we were able to verify that the value Hadi is actually used in the property Fullname.

New C# language features

C# 7.0 introduced pattern matching and out variable declarations. We’ve updated value tracking to support them. Both features greatly reduce the amount of language ceremony, including type checks, safe casts, null-checks and variable declarations. Our method IsEven supports int as well as string and is a good example for such boilerplate code. Let’s try value tracking on this again:

Support for new CSharp language features in value tracking

Also ref-returning methods are now supported in ReSharper’s value tracking. Let’s suppose we have a struct Player, which exposes a field Position. When trying to determine the closest Player related to a certain Point, we can use ref-returning methods to improve our application performance. This example also illustrates, how multiple value origins will be presented in the tree:

Support for new CSharp language features in value tracking

Whenever we call GetClosestPlayer, the runtime can now pass the Player by reference, no longer having to copy the whole data. This can yield a reasonable performance boost, depending on the amount of data and method calls. However, we should always keep in mind, that passing by reference will allow the method to mutate the object.

Download ReSharper 2018.1 EAP now! We’d love to hear your feedback!

This entry was posted in How-To's and tagged , , , , , . Bookmark the permalink.

2 Responses to Value tracking updates in ReSharper 2018.1 EAP

  1. Ulrich says:

    Yeah! Love this feature!

Leave a Reply

Your email address will not be published. Required fields are marked *