VS 2010, Generate method from usage.

Published on Jun 03, 2009

One of the main reasons I got ReSharper originally was for it’s ability to do micro code generation. This is a functionality that I found very useful when doing test first development.

You can write your test first that will access an object that it doesn’t exist yet and them consume some methods on that object. Them you just go Alt+Enter and ReSharper will generate the stubs for the class, the methods and the properties.

VS 2010 incorporate this functionality as well. In ReSharper you can write the initialization of a new class that it have not been create yet and you will see the blue underscore below it. Hit Ctrl+. (Control+dot) and a drop down menu will ask you to generate the class.

All this is good and works very similar to ReSharper, but not so powerful. If you are initializing the class with some parameters a constructor will be created for you. The name of the parameters will be inferred (if at all possible) from the name of the variable that you are using in your code. For example in this code the MyNewClass didn’t exist before hand.

After writing the code I when Ctrl+dot and create the class (it was created in a new file by default, nice) and the methods.

Notice the name of the parameters, in those ones where I used variables the parameters have proper names, the other parameters names are kind of ugly and I hope they fix that (p, p_2?). My suggestion will be to use the type plus some contextual information or at least the type plus the word parameter and them a number if you need it. In the case of strings the whole string or the first word if it’s a phrase. All in all it’s a nice feature to come with the IDE.