CodeRush Xpress

Consume-First Declaration

 

CodeRush Xpress includes powerful features to intelligently declare classes, members, fields, local variables and so much more.

Consume-first Declaration

The consume-first declaration features of CodeRush Xpress 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 (the consumption code). Just move the caret to the reference that needs to be declared and press the CodeRush key (Ctrl+` by default). CodeRush Xpress will do the rest, declaring the missing member, type, enum, or variable.

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

Declaring Members

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

  • Declare Constructor
  • Declare Method
  • Declare Property
  • Declare Property (auto-implemented)
  • Declare Property (with backing field)
  • Declare Getter
  • Declare Setter
  • Declare Event Handler

Declaring Types

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:

Declare Types from Reference

At this point you can press the CodeRush key (Ctrl+`) to declare a new local variable as we've seen before. You can also place the caret on the "FileLogger" text and press the CodeRush key to declare a class or struct.

Here are the consume-first type declarations you get with CodeRush Xpress:

  • Declare Class
  • Declare Delegate
  • Declare Enum
  • Declare Enum Element
  • Declare Interface
  • Declare Struct

Declaring Variables

As we've 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.

Declare Variables from Usage

Here are the consume-first variable declarations you get with CodeRush Xpress:

  • Declare Field
  • Declare Local
  • Declare Local (implicit)

Create Method Contract

Often inside methods, before you work with the parameters, you need to verify that those parameters are valid. Developers typically select one of three mechanisms to ensure the data coming in is correct:

  • Throw an exception
  • Call Debug.AssertXxxx()
  • Exit the method

Regardless of which one of these you prefer, CodeRush Xpress makes it easy to build this contract code. Just move the caret to the start of the method, like this:

Create Method Contract

Then press the CodeRush key (Ctrl+`). The Create Method Contract menu will appear, allowing you to select the ideal mechanism for validating any parameters that are without validation code.

Create Method Contract - Result

The preview hint shows you exactly what you're going to get. Create Method Contract makes the process of ensuring parameters are valid fast and easy.

Duplicate Line

Duplicate line lets you create a new line of code based on an existing line of code. You can duplicate variable declarations, constants, and method calls. The shortcut is Shift+Enter. For example, if the caret is on a declaration like this in C#:

Duplicate Line

Pressing Shift+Enter will create a duplicate declaration that looks like this:

Duplicate Line - Result

Now it's simply a matter of typing in the new field name. Note that the Camel Case features , shown above, may be useful here if you want to change only a portion of the name (for example, changing _StartTime to _EndTime would be easy with Camel Case Nav and Camel Case Select).

More from DevExpress
Live Chat
Have a pre-sales question?
Need assistance with your evaluation?
We are here to help.
Chat is one of the many ways you can contact members of the DevExpress Team. We are available Monday-Friday between 8:30am and 5:00pm Pacific Time.
If you need additional product information, require pre-sales assistance, or want help with your order, write to us at info@devexpress.com or call us at
+1 (818) 844-3383.