Generate From Usage in Visual Studio 2010

The new Visual Studio 2010 IDE now support a bunch of new code generation features under the umbrella name of Generate From Usage. It is good to see that Microsoft is taking steps in the direction of including very useful features like this in the IDE for all developers to use.

Note: This is a matter of carefully selecting which raisins to pick from the cake of very many features like this that third party IDE refactoring tool vendors have been supporting for a long time. I see nothing wrong with this. It is a symbiotic relationship Microsoft has with their refactoring tool vendors. It must be very hard for a company like JetBrains (with their ever popular refactoring add in for Visual Studio; ReSharper) to keep one step ahead of Microsoft's own IDE enhancements while keeping even steps with the development of the IDE itself. Right now the 4.5 version of ReSharper does not support VS 2010 and I am bracing for a new period of using the Early Adoption Program to bring the pre-releases of ReSharper with VS 2010 support into my VS 2010 IDE. Usually their pre-releases work fairly but there is always some friction. * Sigh *

The Generate From Usage feature enables you to use classes and members before you define them. You can generate a stub for any class, constructor, method, property, field, or enum that you want to use but have not yet defined. You can generate new types and members without leaving your current location in code. This minimizes interruption to your workflow. It is also quite useful – at least in theory – in Test Driven Develoment (TDD); TDD Support with the Generate From Usage Feature.

generate from usage in VS 2010

Depending on the context, those options can include the following:

  • Generate property stub
  • Generate method stub
  • Generate class
  • Generate other (for a class, struct, interface, or enum)

The disadvantage, when generating a class is, that your new class will appear in the same namespace as the present in a new file next to the one you are currently in. This can result in you having to move the file and rename a namespace in which case not much is won compared to doing it the old fashioned way.

generate from usage 2

generate from usage 3

There is support for static members:

generate from usage 4

The Class we had generated now looks like this:

generate from usage 7

Here’s a feature I really like! You can naturally generate new stubs in your current class as well:

generate from usage 5 

The only issue is that if you select a field the generated field is PUBLIC. Since we all know Public Fields suck(!) this was a big surprise for me.

generate from usage 6

Please Microsoft fix this! A field is NOT public by default! Or at least it should not be for any developer!

When you select generate other… you get a nice dialog that let’s you select some specific settings:

generate from usage 10

Finally let’s do something a little more exotic to show off some more feature richness:

generate from usage 8

Becomes:

generate from usage 9

The names of the parameters are copied from the variable names in the source. The constant 42 does not have a name and get’s an automatic name.

All in all this feature clearly shows there is progress in the refactoring team on the Visual Studio department.

Cheers,

M.

Technorati Tags: ,
This work is licensed under a Creative Commons license.

posted @ Tuesday, June 16, 2009 2:40 PM

Print

Comments on this entry:

# Generate From Usage in Visual Studio 2010

Left by Dag König at 6/18/2009 7:49 AM
Gravatar
Av Magnus Mårtensson, Dotway The new Visual Studio 2010 IDE now support a bunch of new code generation

Your comment:



 (will not be displayed)


 
 
 
Please add 3 and 7 and type the answer here:
 

Live Comment Preview: