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?
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.
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…
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.