When coding, it is often useful to use an identifier before it has been declared. This is a common situation for Test Driven Development. CodeRush by DevExpress provides a powerful tool,
which enables you to quickly declare types, members, variables on basis of their references.
The automatic declaration features of CodeRush are a quick way to generate the code you need. It starts with a call or a reference to something that
doesn't exist yet. Just move the caret to the reference that needs to be declared, press the CodeRush key (Ctrl+` by default) and choose the appropriate
code provider from the CodeRush context menu. CodeRush will do the rest, declaring the missing member, type, enum, variable etc. You can also access the
declaration features via the Code item of Visual Studio context menu.
Consume-first declaration is useful because it gets you thinking in terms of developers who would consume the code you write. This can improve the quality of the code and make it easier for developers
to work with your code. Consume-first declaration is also almost always an essential component of Test Driven Development (TDD).
However one of the most significant benefits of consume-first declaration is the efficiency gain -- you can quickly craft the code you need significantly faster than doing the same by hand.
To illustrate, let's look at a practical example. Often developers will use Intellisense to discover the return type of an unfamiliar method call or property, by entering that expression right inside the code, like this:
Typically what happens next is the developer's eyes move to the front of the tool tip and note the type (circled in red above). Then the developer accepts the suggestion and moves the caret to the beginning of the line
to enter a variable declaration of the correct type (e.g., a variable of type "Module" in this example).
Overall there's a great deal of caret movement and typing involved just to declare a variable. To save a little time, some developers avoid specifying the type to implicitly declare variables
(using the var keyword in C#, for example), thus avoiding the discovery and text entry costs associated with explicit declaration.
Fortunately, CodeRush makes it easy to explicitly declare variables in a way that exploits the power of Visual Studio's Intellisense and is likely to be highly compatible with how you already work:
Create the expression fragment on an empty line using Intellisense or simply typing the expression by hand.
If you're working in C#, there is no need to end the expression with a semi-colon.
With the caret at the end of the line, press the CodeRush key (Ctrl+`) and select "Declare Local" from the menu. CodeRush will generate the local variable declaration and select the variable so you
can give it a meaningful name.
So the steps behind exploiting consume-first features are:
- Write the call or reference to the member, type or variable that doesn't exist, OR enter an expression on an empty line (as in the example above).
- Place the caret on the part that doesn't exist.
- Press the CodeRush key (Ctrl+`). If a menu appears select the item you want to declare. For methods and properties, you may also see a horizontal red line appear
(called the Target Picker) that allows you to select the insertion point for that member. Just use the up and down arrow keys to select the ideal location and press Enter to insert the declaration.
- Sometimes CodeRush will shift the view or take you to another file, to show you the code just inserted. This allows you to rename or edit the newly-declared code. When you're satisfied
with the declaration you can press Escape to collect the marker and return to where you started.
More details on specific consume-first declaration features follow:
Want to add a member to a type? Just write the code as you would like it to appear. If it's a method, pass in the parameters you need (tip: if you're passing in parameters that are undeclared,
declare those first before declaring the method). Place the caret on the member and press the CodeRush key.
Here are the consume-first member declarations you get with CodeRush:
- Declare Constructor
- Declare Event
- Declare Event Handler
- Declare Getter
- Declare Initialized Properties
- Declare Initialized Property
- Declare Method
- Declare Method (abstract)
- Declare Properties
- Declare Property
- Declare Property (auto-implemented)
- Declare Property (with backing store)
- Declare Setter
To declare a new type, add a reference to it. Usually when you want a new type, you also want a constructor for it. So it's usually most efficient to enter the constructor call like on an empty line:
At this point you can press the CodeRush key (Ctrl+`) to declare a new local variable as we've seen before, or to declare class or struct.
Here are the consume-first type declarations you get with CodeRush:
- Declare Attribute
- Declare Class
- Declare Class With Properties
- Declare Delegate
- Declare Enum
- Declare Interface
- Declare Struct
CodeRush deeply scans surrounding source code for hints about the type you want to declare and intelligently declares members for all found references. Watch the video below for an example.
As we have already seen, declaring variables to represent an expression on a line is easy. Another way to declare variables is to simply place the caret on a reference to the variable name in an assignment,
as in the "_StartTime" text in the assignment below:
Press the CodeRush key (Ctrl+`) and select the declaration you'd like. A preview hint will let you see the changes to the code before you commit.
Here are the consume-first variable declarations you get with CodeRush:
- Declare Enum Element
- Declare Field
- Declare Field (read-only)
- Declare Initialized Field
- Declare Initialized Fields
- Declare Local
- Declare Local (implicit)