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.

Free DevExpress Products - Get Your Copy Today

The following free DevExpress product offers remain available. Should you have any questions about the free offers below, please submit a ticket via the DevExpress Support Center at your convenience. We'll be happy to follow-up.
No Comments

Please login or register to post comments.