A View Inside My Head

Jason's Random Thoughts of Interest

NAVIGATION - SEARCH

CodeRush: Property Templates

There is A LOT of repetition in coding.  To accomplish certain things, you often need to include boilerplate code that is repeated throughout your program. 

Take Properties, for instance.  Defining Properties on a class, especially a lot of them, can be a very tedious task. 

Traditionally, you have to create a member variable of the class to serve as the Property's backing store, and then create the Property itself (with a getter and setter to interact with that member variable).  Lather, rinse, repeat... ad nauseum.

Traditional Property syntax written in C#

This type of work is where CodeRush excels by providing you with mnemonic-driven code templates.  In other words, you type a few characters (the mnemonic), hit the space bar, and it will expand into a much larger snippet of code for you (saving you scores of keystrokes in the process).

The most basic template to help with writing Properties is: p

Move your cursor to somewhere within your class { } block, where defining a Property is legal syntax, and then type 'p' followed by a space.  This will expand into the following:

CodeRush p template expanded
The cursor will be placed in the first orange box, where you can change the data type from "object" to whatever type your Property needs (i.e., "string").  Notice that the Property's type is linked to the backing store's type, so changing one of them will change the other.

The trick with the orange boxes in CodeRush is that you press Enter when you finish editing their contents.  This will remove the orange coloring from that element, and move the focus to the next orange box within the template.  Keep repeating this until there are no more orange boxes left to edit.

Since we knew this was a string Property, we could have saved even more keystrokes by using a mnemonic that included the type: ps

CodeRush ps template expanded

The only real difference between "p" and "ps" is that the type will already be set to "string" for you in the latter.  There are other mnemonic templates that expand into Property definitions for other types, like "pi" for "int" and "pd8" for "DateTime".  (I'll blog about an awesome discovery feature to help you learn these in my next blog post).

But, wait!  There's more!

If you already have a member variable defined (maybe you used the "vs" mnemonic, which expands into a private string variable), and wish to create a Property in your class that uses this variable as a backing store...  well, CodeRush has a trick to help you accomplish that!

private string _MyCoolString;
Simply copy that line of source code into the copy buffer as you normally would (i.e., select the line, and then hit Ctrl-C).  Now, paste it where you would like the Property to be created (i.e., maybe on the line directly below).  Since it's illegal to define this variable twice, CodeRush does something awesome called a Intelligent Paste.  In this case, based on the contents of the copy buffer, it expands a Property template that uses your variable as the backing store!

SmartPaste with property expanded

Similarly, you can also use the "Encapsulate Field" refactoring instead of the Intelligent Paste.  Move your cursor to the line declaring the variable to use as a backing store, and then hit your Refactor Key.  Use the Down Arrow key to move through the list of available refactorings, and press Enter when "Encapsulate Field" is highlighted.

Encapculate Field refactoring
Next, CodeRush will prompt you where you would like to insert the code.  Use the Up and Down arrows to move through valid places where the new code can be inserted.  Press Enter to select the location designated by the Target Picker (red arrow).

Insertion Point
The result will be a new property with a getter and setter that uses your variable as a backing store.

New property inserted

And, finally, as C# evolved, Microsoft recognized that a lot of Properties that were being created were very simple in nature, requiring no logic in the getters and setters other than reading and writing to their backing store.  A new Property syntax was added that did not require a backing store to be explicitly defined (or rather, the compiler actually creates one for you behind the scenes).  This new syntax is referred to as an Auto-Implemented Property.

Similar to the "ps" template, you can use "as" to create an Auto-Implemented Property of type "string". 

CodeRush "as" template expanded
Note: "a" by itself will not expand into an Auto-Implemented Property of type "object" similar to what "p" by itself does.

What? You didn't know about Auto-Implemented Properties, or you want to convert all of your old-style Properties to the new Auto-Implemented Properties?  There happens to be a refactoring for that in CodeRush!

Move your cursor to the Property definition that you would like to convert, and then hit your Refactor Key.  Use the Down Arrow to select "Convert to Auto-implemented Property", and observe the changes that CodeRush will be making to your code (the changes will be painted on top of the code that will be changed).  Press the Enter key, and voila!  No more backing store!

Convert to Auto-implemented Property

(credit goes to Dennis Burton for telling me about this refactoring... which goes to prove that there are way more features in this tool than you'll likely ever master)

Thanks to the magic of CodeRush, you now have many different ways to quickly create properties in your code, saving hundreds (if not thousands) of keystrokes in the process. 

Happy Coding!