Using ReSharper (R#) at Typemock R&D

As you can guess, our R&D team believes in craftsmanship and works to pursue it accordingly. Craftsmanship means, besides unit testing, many things. Among these things we find code quality. In order to achieve high code quality two things are required:

  1. Awareness – the knowledge and understanding of how good code looks like and how to take existing code and improve it.
  2. Ability – making existing code better requires effort. If the effort is too high, developers may find themselves unable to put it in time.

I assume that most of us already have awareness to code quality. But in any moment each of us needs to decide whether it is worthy to pay the price to improve the code or continue to the next thing (which could be a new feature, bug fix or new unit test). The greatest advantage that JetBrains ReSharper provides is that it makes the improving process much cheaper. How it is done? By simplifying both of the improvement steps:

  1. Understanding the code – ReSharper provides very quick and simple navigation along with immediate usage information.
  2. Changing the code – ReSharper provides extensive refactoring options. Most of the known refactoring techniques are built-in.

Understanding the code

Understanding code is the first step of code quality improvement. What exactly does it mean? Is it just reading a method code and guessing what it does? Not exactly. When one look at the code, the first thing he or she wants to do is understand what it does – it should be enough to take a quick look at the relevant unit test. But, in some scenarios you’d like to get quick information about the context. There are two interesting navigation features we find to be very useful:

Find usages

The first context in which we’d like to find usages is local. Using the quick highlight we can get a sense element usage in the current context. For example, if we’re looking to find for how extensive is the usage of an argument in a method is, it can be done in no time:

image

The second one is search with in-place results. This one is very useful for navigation to usages of method with non-extensive usage:

image

The last one is wide search in tool window. This type of search stores the results and allows navigation to the methods one-by-one. This is very useful for methods with extensive usage where it is likely to visit more than one caller:

image

These navigation feature offer great advantage over the built-in search. The search is much faster, enables quick navigation out of the search results while providing reach context information.

Navigate in hierarchy

The next useful navigation features are “go to base” and “go to derived”. When working with polymorphic code this is very powerful. The navigation is based on smart analysis meaning it finds the methods based on type hierarchy instead of members names, this is a great advantage over the navigation possibilities built-in Visual-Studio. This is an example for finding method implementations:

image

This is extremely useful when working with IOC container or extensive interfaces usage. In cases where we’re interested in the implementation this is very helpful, this way we can jump to the code directly from method usage in our code:

image

Changing the code

In order to simplify code changes ReSharper provides two things, the first is powerful refactoring “recipes” and the second are visual tips that brings to awareness possible refactorings.

Powerful refactoring

The main advantage of the powerful refactoring is the fact it lowers the barrier to start a change in a significant way.

Extract method

The most commonly known refactor is “Extract Method”, the thing that makes it powerful is the fact it is based on extensive context information. ReSharper makes this refactor much more effective and safe by analyzing where the extracted code is use. For example if the code block being extracted contains exit points it’ll warn the developer. Another example is the smart placement of parameters modifiers, in case where two variables are assigned inside the block and later user, the new method will automatically define them are ‘out’/’ref’.

To factory method

This refactor is straight forward, it takes a constructor and “hide” it by making it accessible to the class itself only. All the existing  calls to the constructor are changed with the call to the new factory method. This refactor is very useful when we need to inject to a constructor some global dependency while avoid the constructor knowing the dependency accessor. For example:

image

And in a single click we get:

image

This kind of refactor can take a lot of effort in case the class is extensively instantiated.

Conclusion

In order to produce good software several things are required, among those code lies code quality. Quality code means that it is more readable and maintainable, so it’ll reduce the cost of the development in the long run (like unit tests). In every point of the development cycle there’s a tension between the short-term goals, like moving to the next feature, and the long-term goals like reducing the price of future bug fixes. Reducing the immediate price we need to pay reduces the tension between the short and long-term, thus allowing continuous code quality improvement. Since ReSharper is a tool that does exactly that, we believe it is a must have tool for every developer.

TOP