ReSharper Series - Part 5: Generating Code

by Rasmus Kromann-Larsen March 04, 2009 21:55

Welcome to the 5th part of my ReSharper Series - in this post we are going to take a look at how you can easily generate a lot of the fluff code that surrounds your real functionality.

While C# is a great language, there is a quite of bit of the code that you write every day that feels somewhat crufty. It is these standard things that we do a thousand times, like writing constructors, properties, backing fields to said properties (better with automatic properties - but you still often need a backing field). Luckily, ReSharper can help ease your pain - or at least let you focus on writing that core functionality - and not worry about the cost of adding another class (in terms of typing).

Generating Class Members

We have already the power of Alt+Enter in one of the earlier posts - and this a tool that most people I have seen use for creating new code.  It can do stuff like implementing missing methods and assigning creating backing fields for you from constructor arguments. If we use it on our balance argument that is unused (gray), ReSharper offers the following options:

image

However, we might want more - we had to write the constructor ourselves - and we have to create the property afterwards. Let's introduce another shortcut - Alt+Ins. While Alt+Enter is a general purpose tool, Alt+Ins is focused on generating code. Use it anywhere in a class and you will be presented with the following options:

image

Generating a constructor will bring up a dialog allowing you to select which members / properties you want to initialize from the constructor. Read-only properties / Properties will give quick options to create read-only properties from whatever backing fields you already have in your class. Implementing missing members will create any members not yet implemented from interfaces or abstract base classes. Equality members will implement Equals and the equality operators - GetHashCode included, based on the properties / fields you choose. Formatting members will introduce a ToString method that contains the values of whatever properties / fields you want.

The last one I didn't mention is the one I think is mostly underused - it is the Delegating members option. It is often useful in object oriented design to encapsulate another class and provide delegated methods for a number of the contained type's methods. The perfect example is when implementing the decorator pattern - this pattern, by design, requires you to delegate all the methods the contained type. This is a pain to do by hand. However, let's try using the delegating members option in my Account class, after adding a List<int> - like so:

image

This brings up the following dialog:

image

So basically, ReSharper lets me pick and choose any or all methods / properties that I want to directly delegate. For this class, maybe I need the Add and indexer methods, so I check them off and hit finish:

image

The code for delegating members is silly simple - and this is EXACTLY why we want to generate it in the first place. Generating delegating members let me focus on my intent of which options I want to expose from my class instead of the laborious task of typing out the code.

Generating Files

As a final note in this post, I would like to introduce another usage of Alt+Ins which I have grown quite fond of - in the solution explorer, pressing the shortcut will allow you to add new files in a more lightweight way than the usual add file dialog:

image

Later, when we look at live templates, we will also see how to add your own file templates to this menu.

kick it on DotNetKicks.com

Tags: ,

ReSharper

ReSharper Series - Part -2: Introduction & Motivation

by Rasmus Kromann-Larsen January 10, 2009 12:44
image

This is the first and -2. part of my ReSharper series. -2 because it there's still a few parts until I hit real ReSharper content. The subject today will be a quick introduction to ReSharper and the motivation for using it - so why should we?

What is ReSharper?

ReSharper is a Visual Studio add-in made by JetBrains. In their words, ReSharper is:

The most intelligent add-in to Visual Studio.

What it really amounts to is a lot of smart static analysis of your code, much like Visual Studio does already to provide you with IntelliSense and red squigglies when you make mistakes. What they do with this information is to provide context-based suggestions and generally reduce the number of keystrokes required to produce the most common code scenarios.

In addition to this, the static analysis information can be used to provide a structured way to refactor your code without resorting to error-prone search and replace techniques. Since ReSharper "knows" the structure of your code, it can provide much better support for stuff like renaming classes, extracting and moving functionality.

Strengths

From my point of view and experience the main strength of ReSharper is it's context-awareness. This is what gives the intelligent and intuitive feel when using it. Since ReSharper provides a lot of functionality, filtering it by what is available (or reasonable) at this given point in the code is really valuable.

Besides context-awareness, I really love the navigation options provided by ReSharper. It allows me to navigate along different axis depending on what I'm looking for. Providing easy options for navigating according to usages (where is this method called from), inheritance trees (superclasses/interfaces/subclasses) or completely different files, depending on which aspects of the code I am exploring at the given moment.

Third is code-generation and refactoring. This is often mentioned as the main strength of ReSharper - this is mostly convenience, reducing the amount of manual work required to do refactoring and doing (mostly small-scale) code generation used to create the usual cruft code for classes, properties and such.

Weaknesses

Seems odd to talk about weaknesses in this motivation post, but this isn't really weaknesses in ReSharper per se, but more things I still feel I'm missing. I actually feel that ReSharper is doing the job it is supposed to do quite well.

At the moment, ReSharper is the only real productivity add-in I use in Visual Studio - but I feel I'm missing features for basic text editor stuff, quick navigation in the current file (ReSharper provides some, but it can be heavy sometimes). It seems that the vim add-in ViEmu might be some of what I'm looking for. Maybe after I'm done with this series.

kick it on DotNetKicks.com

Tags: ,

ReSharper

Powered by BlogEngine.NET 1.6.1.0
Theme by Mads Kristensen | Modified by Mooglegiant | Adjusted by Rasmus Kromann-Larsen

About Me

I am a danish .NET developer blogging about the technical side of my life, mostly .NET stuff, but also fundamental topics like design patterns, principles and productivity boosters.

In addition, I am a core group member of Aarhus .NET User Group.