CodeRush Code Templates overview
CodeRush code templates allow you to generate large code blocks on the fly with just a few keystrokes. The use of code templates dramatically decreases the code writing time, because it’s not necessary to type the entire block of code manually. The Templates library shipped with CodeRush contain lots of code templates for most code blocks and regular coding structures including regions and comments.
There are several basic things to know about templates:
Template expansion
Template expansion is the resulting code or text generated after a particular template is expanded. The expansion can be of any-size (e.g. thousands lines of code) and can be interactive (e.g. you can interactively change specific parts and navigate between them in the expansion).
You can think of a template expansion as a ready code block like a member (e.g. a method or property), a statement (e.g. if, for, foreach, switch, etc) or an expression (e.g. an equality comparison, a value or property assignment). The Templates library contains hundreds of code templates for regular code blocks for all supported programming languages. Of course, you are able to create your own templates with ease.
Due to the context-sensitive nature of templates, a single template may be expanded differently depending on the current environment and conditions (context).
Template name
The abbreviation (name) used to identify the template and its expected expansion. СodeRush has its own templates language that is pretty easy to learn and understand. When you know the templates language and expand templates for code generation, your coding speed is increased significantly. The name of the template can contain one or more characters.
Template key
The keyboard shortcut that is used to expand the template. When you type the template name in the code editor and press the Template key, the template name is replaced with its expansion.
The Space bar is used by CodeRush for the maximum coding speed by default. Some templates may use an alternative key that is a Shift+Space. Both template keys are configurable. You can setup your own template key (like Tab, for example) if any templates interfere with your coding habits.
Code Templates samples
Here are example of the simplest code templates:
m – expands into a method inside a body of a class:
f – expands into a for loop inside a body of a member:
sw – expands into a switch statement inside a body of a member:
Due to the context-sensitive nature of templates, the ‘sw’ template can be expanded differently. For example, if the Clipboard contains a variable name of the Enumeration type (e.g. System.DayOfWeek), you will get the following code generated:
Most of the code templates from the CodeRush Templates Library have short names (m, f, sw) and must be protected from an unexpected expansion with a correct context set. For example, if you prefer to use a single-letter variable name when you declare it and then press the Space bar, the template with the same name will not be expanded:
However, in other situations (with a different context) the template will be expanded as expected:
If you see unexpected template expansions, you might refer to the “Resolving templates conflicts” topic where you can learn how to avoid unexpected template expansions.
Want to learn more?
2. CodeRush Templates language and learning basics
3. CodeRush Dynamic templates and dynamic lists
4. CodeRush Templates options page
5. CodeRush Dynamic Lists options page
6. Resolving code templates conflicts
7. Importing and Exporting CodeRush templates
8. CodeRush Standard Templates Library
9. CodeRush Templates vs Visual Studio snippets
10. Creating CodeRush templates overview
10.a. Creating simple templates step by step
10.b. Creating advanced code templates step by step
11. Creating and managing templates programmatically
You might also be interested in the following videos about CodeRush templates from DevExpress TV Channel:
CodeRush – Cool Templates
CodeRush Template Deep Dive Webinar with Mark Miller and Rory Becker
CodeRush – Creating a Custom Template
CodeRush – Merging Templates
CodeRush – The Template Mnemonic language
Advanced TextCommands in CodeRush