CodeRush Templates 101

What is a CodeRush Template?
A CodeRush template is a sequence of characters typed within the editor, which then expands into something larger when the TemplateExpand shortcut (<space> by default) is invoked.

Perhaps the simplest way to understand a template is to see one in action. 

The following example uses just 3 keys: vs<space>

Before VSMemberBefore…After… VSMemberAfter

As you can see, CodeRush infers the desire to create a variable of type string. In this case the variable is a member variable. Your caret jumps to the lit area so that custom, post expansion editing (in this case the allocation of a proper variable name) can commence.

This context sensitivity of this is demonstrated when we use the exact same keys whilst the caret is positioned inside a method:

BeforeVSLocalBefore…After…VSLocalAfter

CodeRush again expands the template, this time determining from the context of the expansion, that the user required a local variable of type string. Again see that CodeRush has lit the area which needs follow up work.

Of course Templates can be used for much larger and more complex expansions

Consider the need to create a property complete with backing store. Simple: ps<Space>

Before…
PSBefore

…After…

PSAfter

In this case you can see multiple lit areas in the resultant code. These areas are linked to each other with one being preselected ready for it’s content to be overwritten by the user. When the user begins to overtype this text, the characters are replicated in each of the other linked areas.

…and of course read only properties are catered to as well. rs<space>

Before…
RSBefore

…After…

RSAfter

Hints for some of the more common templates are available via the CodeRush Training Window (DevExpress\Options\Tool Windows\CodeRush)

CodeRushTrainingMemberTemplates
CodeRushTrainingStaticTemplates
CodeRushTrainingNestedTypesTemplates

Here you see a range of templates suggested when your caret is within a class. There’s quite a range including Instance Members, Static Members, and Nested Types. The expansions for some of the instance member templates are shown below.

TemplateNewInstanceString

TemplateMethodString

TemplateReadOnlyFieldString

TemplateWriteOnlyPropertyString

TemplateAutoimplementedPropertyString

And just to remind you … All of these are expanded with only 3 keys.

Further, the CodeRush Training Window will update on a per keystroke basis, suggesting various context sensitive options to you

Simply type the suggested key and watch the CodeRush Training window update to suggest further action.

When you press v (expecting perhaps to continue with s and <space> to replicate our first example [above])…

VTemplate

…the CodeRush Training Window updates itself to show you the possible alternatives to s

CodeRushTrainingVariableTypes

Each of these prospective sequences (shown in bold) represent expansions that you might choose to investigate. Most sequences represent different types that CodeRush knows about.

dt = DataTable
d8 = Date
b = boolean
de = decimal

As you can tell there are many types preregistered with CodeRush with sequences to represent them. There are many more than are pictured here, but I didn’t want to bore you.

They are pretty easy to guess as well. For example:

i = integer
sb  = StringBuilder
u = Unsigned Integer

The other templates I have mentioned are also compatible with these type mnemonics as well as many more besides. So you can type vd8<space> and create either a local variable, or a private member variable of type date depending on the location of the caret. Or, dramatically adding an entire 4th key, you can create a Method that returns a DataTable by typing mdt<space>.

If you’d like to add further types to this list, such as you’re own business types, then simply right click their declarations and choose Use Type in Templates

UseTypeInTemplates

At this point you’ll be offered the chance to choose a mnemonic for your type. Something you can type in place of sUseTypeInTemplatesMnemonicChooser

If you choose the suggested msbc (Determined by CodeRush in this case to be a reasonable mnemonic for MySuperBusinessClass), then you will be able to create new methods returning Objects of type MySuperBusinessClass, by typing mmsbc<space>

Summary

Templates allow you to express your intent in as few characters as possible, expanding that content into it’s fullest form, and then allowing you to quickly and easily edit the end result of that expansion. You save time in the creation of the code, without sacrificing the readability of the code in question. The Templates I have shown you here are nice small nuggets which can be used every day, but there are others which while perhaps not used quite as often can still save large amounts of typing.

Just as an example, position your caret inside a namespace or class block and type x<space>.

That’s a bit more impressive eh?

But don’t take my word for it Download, Compare and Decide for yourself.

6 comment(s)
Marc Greiner (DevExpress MVP)
Marc Greiner (DevExpress MVP)

Hi Rory ;

Very nice article, keep them coming...

22 September, 2010
John Roth
John Roth

Great article!

I've been using the DevExpress IDE tools for quite a while and I still learned something new.

Are you planning on doing a post on _writing_ templates in this series?  That's an area where I still feel woefully unskilled!

23 September, 2010
Rory Becker - DevExpress
Rory Becker - DevExpress

@Marc Thanks man. I certainly will do :)

@John That's the plan. I'm going to cover as much as I possibly can. I just have to get some of the more basic stuff out of the way first. :)

Also in the works are posts on plugin creation :)

(Bet ya never saw that coming :P)

23 September, 2010
Rick Fortney
Rick Fortney

I was moved from Delphi to Visual Studio (VB) a couple of years ago, along with the DevExpress Editor and Grid tools. I've had to teach myself VB along the way but find most resources, including the above article are done in C. Is C used that much more than VB? Are there examples/tutorials/references on the DX site that are for VB?

In these first couple of years I've already found out that coding takes hours and weeks longer without the DX tools. Great product, VB is just a shell without the DX tools. And thanks for the CodeRush article.

23 September, 2010
Rory Becker - DevExpress
Rory Becker - DevExpress

LOL.

Hey Rick

I'm pretty sure there's no way to win here. Would you believe I'm actually a VB.Net coder by day myself. I'm only really doing this example in C# for the fun of it. Everything you see here works just as well in VB.Net  (Language Version permitting)

I have another blogpost coming up, in which I explain a plugin I've written.  I wrote the plugin in question in order to show a customer how it was done, and so in that case I also wrote in C# as I believe it was his language of choice :)

I have discovered during this, that I like C# lambda syntax, but dislike it's ternary expression syntax. (I already knew that I hated it's case sensitivity)

I guess there really is no way to please everyone :)

I'm sure I'll likely switch between languages fairly regularly as I continue to blog. If you ever feel the need for some clarification though, feel free to say so either here in the comments, or via email (roryb@DevExpress.com)

I'm honestly not sure of the best way to approach this in blogposts as typically I'm trying to get a maximum amount of information across in a minimum of space.

Any suggestions?

FWIW I'm not sure I've ever found it difficult to find VB.Net samples on the web. If you feel we've missed something feel free to let us know.

Either through support@DevExpress.com or via the Support Center ( http://www.devexpress.com/sc/ )  

23 September, 2010
Greg Kirk
Greg Kirk

This defintely improves code writing in VS. A similar feature has existed inside the Borland IDEs for sometime now, so It's very good of you to bring it to VS through CodeRush Templates.

23 September, 2010

Please login or register to post comments.