Rory Becker - DevExpress CodeRush Blog

September 2010 - Posts

  • 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:


    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>




    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>




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


    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.






    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])…


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


    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


    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>


    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.

  • Why CodeRush Templates?

    Efficient Coding

    There are many interpretations to efficient coding, but I’d like to consider just one. I’d like to consider Efficient coding as the act of expressing your solution as efficiently as possible, without sacrificing maintainability. ie I’d like to think about the process of generating the code itself.

    I think we can all agree that there are patterns within the field of coding, which are proven to yield good results given the right circumstances. Yet how often do you find yourself creating yet another property with backing store? How often do you find yourself writing another exception class? How often do you find yourself repeating the same thing over and over again within the space of even a few minutes? You probably can envisage the code you’ll need from the outset, and yet still you trudge through the motions of recreating that same code again.

    Well fear not … There are several tools to help you create this code a whole lot more efficiently, but not all of them are as optimal as you might believe.

    Out of the box the combination of windows and Visual studio, provides us with the Clipboard, a Clipboard ring, the VS toolbox, and VS Snippets. So lets see how far these take us?

    First up....

    The Clipboard
    Yeah I didn't think that would have you leaping for joy, but you must admit it's an option. (If not always the correct one.)

    We all know the downsides of the Clipboard: It's limited to a single slot of data that shared amongst all applications in the system. With a single key for copying and another for pasting it's also far too simple to use and thus can be easily accidentally used, misused and overused without enough consideration for the any side effects that this may cause.

    As a result of this, mistakes (as well as the occasional set of good code) gets copied all over the place and technical debt can spiral out of control.

    The Clipboard Ring
    I'm not sure if the clipboard ring is a stage above or below the clipboard on the evolutionary scale. Visual Studio allows you to store up to 20 items on the ring, which helps because we can now reuse a greater number of distinct items. Additionally these are not wiped simply because another application chooses to store something on the clipboard.

    Accessing your carefully positioned nuggets of text is not an especially great experience though, as you have to repeatedly press Ctrl+Shift+V until the item you actually wanted, is placed on screen. I'll grant you this isn't exactly rocket science and therefore it's relatively easy to remember, but it's going to get annoying oh so very quickly.

    Next up…

    The VS Toolbox
    Yup that's right, the same toolbox which you're used to dragging controls from prior to placing them on a design surface. It turns out that you can also drag text to and from this rather narrow little storage area. This allows you to store some of your oft’ used and carefully crafted code and text snippets here, ready for use at a moment's notice.

    However the Toolbox suffers from some disadvantages of it's own:

    Vertical: This space is shared with the controls, you're so used to manipulating onto the design surface, and as such , the more control tabs you have, the less space you have for nuggets of text to be placed whilst still being relatively accessible.

    Horizontal: It can be a little tricky to navigate through the code stored in this area in order to locate the item you're after. The nature of the Toolbox's UI is such that it is very narrow and only capable of showing the first few characters of each. This means that if you have a number of nuggets of text that start with the same few characters, locating the one you're after can a be a bit awkward.

    Mouse only: You are limited to accessing this area of the IDE using only the mouse.. That's right you have to move one hand away from your precious keyboard and reach for that oh so familiar pointing device.

    Finally we come to…

    VS Snippets
    Available since around VS2005, snippets are a way of injecting potentially quite large quantities of code into the editor.

    Snippets are a much bigger improvement on the previously mentioned text generation techniques. Introduced in VS2005, some people see them as a poor man’s CodeRush Template.

    Activation is relatively simple: Just type ? and hit <tab> and you are presented with a selection of approx. 8-10 categories of code. Select a category (Arrow keys and enter) from the presented menu, then another (and possibly a third) and you are presented with a list of descriptions representing snippets of code that can be injected into your editor. Navigate through these again and hit enter to select the one you require.

    You'll recall I said this was much better that the previously mentioned techniques. However it's not without it's pitfalls.

    Look back to the description of it’s usage. 3 levels of folder/menu before you get to the option you were looking for is quite steep really. You are forced to stop actually producing code whilst you think about what folder (and possibly sub folder) a given snippet might be stored in.

    Whilst snippets are editable (using a built in editor) and additional snippets may be added, there appears no simple way to customise snippets at the time of expansion beyond directly editing the emitted code after the fact.

    And so to CodeRush

    So why are CodeRush Templates any better? Well I’m glad you asked :)

    • The interface to CodeRush’s templates is keyboard rather than mouse based, which means you don’t need to loose time switching between the 2. All you need do is type the name of the template and hit space. Your code is expanded in place and ready for you to further customize if need be.
    • The mnemonic conventions used in naming the templates that ship with CodeRush are simple to understand and allow you to master dozens of templates simply with just a few keystrokes.
    • The Templates are Context Sensitive. CodeRush doesn’t just know the type of file, you’re editing. It knows the project type, references assemblies, and caret position in the current file. It knows if you’re positioned in a method, class, struct, interface, namespace and even what sort of statement you’re in the middle of writing.
    • The CodeRush Training Window likewise understands your current context, and will constantly update itself to be relevant to your current situation. This helps you get up and running really quickly. you can even bind a key to toggle this help on and off on a whim.
    • The number of Templates provided with CodeRush is vast, with plenty of scope for the user to add additional templates as new opportunities arise, without worry of running out of slots.
    • Dynamic Templates allow a single defined template to serve the needs of dozens of situations.

    With all of this and more in it’s favour, it’s hard to see why you wouldn’t choose to use CodeRush Templates every day.

    CodeRush Templates: Clean UX. Vast library. Context Sensitive Help. Unlimited Potential.

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

    In our next instalment we’ll dive into some specifics, and start to show you around the CodeRush Training Window and the library of Templates that are available out of the box.


Chat is one of the many ways you can contact members of the DevExpress Team.
We are available Monday-Friday between 7:30am and 4:30pm Pacific Time.

If you need additional product information, write to us at or call us at +1 (818) 844-3383


DevExpress engineers feature-complete Presentation Controls, IDE Productivity Tools, Business Application Frameworks, and Reporting Systems for Visual Studio, Delphi, HTML5 or iOS & Android development. Whether using WPF, ASP.NET, WinForms, HTML5 or Windows 10, DevExpress tools help you build and deliver your best in the shortest time possible.

Copyright © 1998-2018 Developer Express Inc.
All trademarks or registered trademarks are property of their respective owners