Increase productivity with ReSharper templates

posted @ Thursday, December 07, 2006 12:00 AM

 

(At the end of this article you'll find a set of links to download my current set of ReSharper templates)

Being a lifehacker and automation junkie, I'm always looking for ways to be more productive in everything I do, especially in how I develop software.  Whether it's automating my build and test process, or utilizing existing tools like NHibernate to reduce the tediousness of writing the same ol' ADO.NET code over and over, or keeping my hand off of the mouse, productivity is something I take pretty seriously.  After all, at the end of the day, we are supposed to actually deliver a high-quality piece of software in a timely fashion.

Well for the past year the #1 tool that has made me more productive in my development is ReSharper.  This is a tool that truly understands C#, and how developers write code.  I can't imagine developing without it anymore.  There are way too many features to talk about here, so I'll just defer that to their features page.  But what I want to focus on in this post is its templating features. 

Liven up your development

Live Templates are a great way to become more productive through micro-code generation.  You know, all those code statements that you seem to write over and over, perhaps in a certain way that you personally like.  Think of how many times we create a new List<T>, or throw a NotImplementedException, or write Assert statements, etc.  Well you can very easily encapsulate these kinds of statements in live templates along with placeholders for just-in-time customizations when you actually use the template in code.

Here's an example of how to create a simple template that can speed up writing your tests:

ReSharperLiveTemplates1

When you want to assert equality in one of your tests, just simply type aae, hit tab and bam.  Then just type the name of the expected value, hit tab, type value of actual value... you get the point.  Another thing I like about this is that it actually helps me write better tests by always providing a message with my assertions.  Back when I would actually type Assert.AreEqual, I would sometimes forget to include a message with my assertion, which, IMHO, is a very good habit to form.

Surround yourself with speed

Surround With Templates are great for code organization and just keeps the "flow" of writing code moving.  Just simply highlight the code you want to surround (using Ctrl-W is a great way) and then just hit Ctrl-Alt-J (that's the default anyway) and choose your template.  I find myself using the #region template more than anything to keep my code nice and organized.  But I'm just starting to tap into the power of these, and am actually finding use for them in other areas besides C# code.

For example, take a look at how you could create a couple templates to help with web development:

ReSharperSurroundWithTemplate1

ReSharperSurroundWithTemplate2

Notice the $SELECTION$ place holder which of course is what you want to surround and the $END$ placeholder is where the cursor will end up once the selection has been surrounded.  I can see huge potential for these is various types of files that we work with and I definitely plan on creating more as I need them.

File away those common code files

File Templates enable you to re-use those common code files your always creating, like of course classes and interfaces, but I also like them for test fixtures, build scripts, etc. 

Here's an example of one of my test fixture file templates:

ReSharperFileTemplate1

Wanna download my current set of ReSharper templates?

So far I've created quite a few templates for my personal development and I thought I'd share them with the community.  Some of them reflect the tools I'm currently use, like my set of NMock2 expectation and stub templates or my template for creating a private static logging variable for log4net.  And others are just some of the common code statements I'm always writing, like creating an anonymous delegate or just creating a static readonly string.

Here's a quick reference for some of my live templates:

NMock2 Templates

em - Method Expectation

ema - Method Expectation with Arguments

emr - Method Expectation with Return Value

emar - Method Expectation with Arguments and Return Value

egp - Property Getter Expectation

esp - Property Setter Expectation

sgp - Property Getter Stub

Test Templates

ntest - NUnit Test Method

vstest - VSTS Test Method

aae - Equality Assertion

ain - IsNull Assertion

ann - IsNotNull Assertion

ait - IsTrue Assertion

aif - IsFalse Assertion

Miscellaneous

anon - Anonymous Delegate

log - log4net private static variable used for logging in the current class

glist - Generic IList<T> variable

psrs - Public static readonly string

thrn - Throw a new NotImplementedException

 So finally, here's the download links:

Of course I'm always adding new templates, but the links above include my current set as of right now.  If there is interest, I'll keep these updated as I add new templates to my library.

Also, check out a few other bloggers who are offering up ReSharper templates that look very useful.

Anyone else have any cool ReSharper templates they care to share?

Comments
joeyDotNet - 8/1/2007 4:26 PM
# re: Increase productivity with ReSharper templates
Hmm, haven't noticed anything like that. You just mean the formatting doesn't look correct when the template is expanded?
Post Comment






Please add 6 and 5 and type the answer here: