Showing posts with label vs.net. Show all posts
Showing posts with label vs.net. Show all posts

Wednesday, 17 October 2007

Getting to grips with ReSharper

On the couple of occasions I have talked people into trying JetBrains ReSharper (R#), the first thing they seem to notice is that it can optimise your using statements, and that it puts are bar down the right hand side of your editor showing you all the mistakes you have made in bright red and yellow stripes. There is obviously a lot more to ReSharper than that, and I've noticed people seem to get as much out of it as they put into it. Here are some of the features I found useful when I first started getting to know ReSharper 3.0.

Navigation

ReSharper introduces many nice ways of finding your way around your code. The File Structure View provides a list of all the members in your current class. Double clicking on a member jumps to that point in the code. You can also rearrange members and manage code #region blocks.

The mechanics of the File Structure View are fantastic, but to me the biggest advantage is the additional awareness it provides. At a glance you can take in all the members in the current file, which means you always have a good awareness of the context in which you are working, such as the class' current responsibilities.

I dock my File Structure View window to the right of VS and find it invaluable. You can access this window via the ReSharper >> Windows menu, or Ctrl + Alt + F.

Screen shot of File Structure View

ReSharper also provides a number of shortcuts to jump to a file, type, or symbol (Ctrl + T, Ctrl + Shft + T and Shft + Alt + T respectively). This lets you start typing part of the file/type/symbol you are looking for, and pops up an auto-complete box that allows you to quickly jump to the item you are after. Generally much faster than using Solution Explorer.

Other navigation features include an improved (over the standard VS) "Find Usages" feature, and a "Navigate from here..." menu that lets you jump to base, type declaration, function exits and more.

Code Generation

Mashing Alt + Insert opens a code generation menu, from which you can access wizards to generate properties with backing fields, constructors, interface implementations, and even delegations to class members. When I write "wizards", don't go picturing Clippy or some equivalent monstrosity -- these are actually helpful! For example, the constructor generator lets you select the fields/properties you wish to initialise in the constructor's parameters and then dumps the constructor into your class. The wizards are generally very keyboard friendly as well, so you don't have to madly chase your rodent around trying to click obscure buttons or links.

Generate constructor screenshot

Templates

The templates in ReSharper are incredible. There are a couple of templates build in (try typing "foreach<TAB>" into a method that takes any IEnumerable like an array as a parameter. No really, go try it! I'll wait...), but the real power comes from creating your own Live or Surround With template.

The templates support parameter substitution (i.e. you put a token in your template), and through the template builder UI you can specify exactly how these parameters are populated at run time. For example, you can tell ReSharper to guess the substitution based on the current type, current parameters, or even transform an existing name (such as using the camel-cased version of a Pascal-cased property name).

Edit template example

The educated guesses ReSharper uses are generally spot on, and are used in a number of places throughout the product, such as some of the Quick Fixes.

You use a template by typing part of the name into VS, and selecting the template from the autocomplete menu that pops up.

Refactoring

The standard Visual Studio refactorings are awful. On every PC I have used it has been almost entirely unusable. Trying to rename a private class member tended to cause a complete recompilation of the entire solution and basically take an eternity. ReSharper's refactorings work in milliseconds, and provide a whole lot more than VS.

The standard ReSharper 3.0 keymap (not the IntelliJ/ReSharper 2.x one) lets you mash Ctrl + Shft + R to open a context-sensitive refactoring menu. For example, if you highlight a passage of code and hit Ctrl + Shft + R, you can choose Extract Method to move that code to its own method. ReSharper will either instantly apply your refactoring, or open one of its helpful wizards if it needs a bit more data from you to continue.

There are also several chords you can press as short cuts to various refactorings, like Ctrl + R, Ctrl + V to apply the Introduce Variable refactoring. The refactoring features of ReSharper are extremely powerful, but in my experience require the most amount of work to learn how to use effectively. Every day I seem to find a new way to use a ReSharper refactoring to make coding easier.

The screen shots below show the "Refactor This" menu that appears after a value is selected and Ctrl + Shft + R is pressed. The second shot shows the Introduce Variable wizard, and the third shows the result.

Refactor This context menu

Introduce variable refactoring example

Result of introduce variable refactoring

Intellisense improvements

ReSharper, depending on which options you have specified (ReSharper menu >> Options), augments the standard Intellisense offered by Visual Studio. One option I turn on is to show more info about members, such as the return types. It is a good idea to play around with this to best suit the way you work.

The main thing ReSharper offers in this area is smart autocomplete and type autocomplete. Instead of the default Intellisense triggers such as Ctrl + Space, you can press Ctrl + Shft + Space or Shft + Alt + Space to get ReSharper to intelligently restrict the autocompletion options base on your current context. For example, if you are initialising a variable from a method call and and trigger a type-autocomplete, ReSharper will only show you a list of methods that return a compatible type.

Quick fixes

This is the best feature in my book. Whenever ReSharper finds an error, a suggestion, or recognises a number of common actions in the current context, it will flash up a light bulb and underline the relevant code. The colour of the light bulb and underline will depend on what type of quick fix ReSharper has identified (error, suggestion etc).

When you see Quick fix icon, press Alt + Enter to open the Quick Fix context menu, which will solve all your problems for you.

An example that is really useful when doing TDD is when you write some test code first that does not yet exist. The missing method will be written in red, and the quick fix icon will appear. Pressing Alt + Enter will display the menu shown below:

Quick fix menu to create a method

If you select "Create method...", a method stub will be generated, and you can TAB through the fields in the template ReSharper uses and fill in the details. The screen shot below shows ReSharper's suggestions for the parameter name, or you can specify a completely different name.

Quick fix process suggests a method name

Another TAB press and you can replace the throw new NotImplementedException(); line with your required implementation:

After quick fix method creation

But wait, there's more!

There is a Unit Test Runner, an option to cycle through your copy/paste clipboard (Ctrl + Shft + V), more code colouring options (such as greying out dead/unused code), automatic xml-doc generation for parameters, automatic importing of relevant namespaces, dead code removal and more!

Can I have my commission now?

There is so much to ReSharper, and you really will get as much out of it as you put into it. Once you become familiar with it you will find your code almost writing itself. I really recommend exploring the options it offers (ReSharper menu >> Options), as well as looking through and trying all the features. If you want an idea of exactly what can be accomplished, have a look at this video from Ilya Ryzhenkov, .NET Tools Product Manager at JetBrains.

There are only a couple of downsides to ReSharper. One is that you need a decent PC to run it on anything but a trivially sized project -- it is a real memory hog, despite improvements being made over v2. Secondly, it is quite easy to become dependent on ReSharper, and feel lost when you go back into normal VS-land. Sometimes I feel like I need to jump back into emacs to remember how to do stuff old-skool :) The other problem with the product is that it leaves you hankering for JetBrains to develop a Visual Studio replacement, say, Intelli.Net :)

Hope this post helps give your ReSharpering a jumpstart. :)

Friday, 12 October 2007

Link: Speed up Visual Studio 2005

Couple of good tips on speeding up your IDE:

.NET Tip of The Day: Speed up Visual Studio 2005

Bookmarking here for next time I need to setup VS 2005 (and some of this will probably go well in VS 2008 too).

Tuesday, 4 September 2007

Opening a VS item's directory in Explorer

I use TortoiseSVN as my SVN client, but it is not integrated into Visual Studio like AnkhSVN and others. This generally suits the way I like to work, but occasionally it would be nice to right click on a new class and pick "Add to SVN", or have an easy way of doing an SVN move. I have recently started using a simple way of accessing these features, without installing a VS-integrated client, by providing a shortcut to opening a Visual Studio item's folder in Windows Explorer where we can use all our standard TortoiseSVN functionality.

UPDATE 2007-09-24: The functionality provided by the following steps is very similar to right-clicking on tab of an open file, and selecting "Open containing folder", which I admittedly only just noticed*. I have left this example online for two reasons. First, the method below offers a bit more functionality, as it also works for files selected in the Solution Explorer. Secondly, the general approach described is a really useful technique for easily extending VS. For example, I remember seeing a screencast of configuring Subsonic's SubCommander as an external tool in VS to generate your DAL and script your database with a single button click.

From Visual Studio 2005 (similar steps should work for 2008/Orcas too):

  1. Go to the Tools menu and select External Tools...
  2. Click on the Add button
  3. Set the executable to your Windows Explorer path (C:\WINDOWS\Explorer.exe on my PC).
  4. Set the arguments and initial directory to "$(ItemDir)". Not sure if the quotes are needed incase you have spaces in your paths, but they don't seem to hurt.
  5. Give it a title, such as Open in E&xplorer. The & just designates the menu accessor key, which is x in this case. -- you can leave it out if you like.
  6. Note which number your external tool is in the list -- in the screenshot mine is #7 (1-based counting here). This is only for if you want to create a menu button for this external tool.
  7. Click OK

Screenshot of External Tools dialog.

You can now select an item in the solution explorer, or just use the file you are currently editing, and go to Tools --> Open in Explorer to open the file's folder. Now you are back in Tortoise country, and can add the file to SVN.

You can make this even easier by setting up a menu button for the external tool:

  1. Right-click on one of your menu bars and select Customise (ok, it says Customize, but I can correct the spelling can't I? :) )
  2. From the Commands tab, select the Tools category, and then External Command {xx}, where {xx} is the number you carefully noted in step 6 of creating the external tool.
  3. Drag and drop the command on to the menu bar of your choice.
  4. Customise the button's appearance as required. I right clicked on the View --> Solution Explorer menu item and selected Copy Button Image, then right-clicked on my button and used Paste Button Image to get a purdy icon. I also hid the text description so it takes less room on my menu bar.
  5. Click Close on the customisation dialog.

You can now select a file, click the icon, and voila, instant Explorer. Ok, so this sounds largely pointless, but it does really help me to quickly switch between Visual Studio and TortoiseSVN.

* There seems to be quite a lot of cool crouching-tiger-hidden-features in VS 2005 (i.e. features that are not very discoverable). Some of the things I love about Resharper are present (mostly in simplified form) in standard VS 2005, but they are easier to discover (and generally easier to use and work better IMHO, YMMV) in Resharper.

Thursday, 12 July 2007

Data Access Guidance Package - DAL generation

David Hayden has posted about a Guidance Package that does data access layer generation. It's not SubSonic, but still interesting:

Data Access Guidance Package on CodePlex - Code Generating a Data Access Layer

Tuesday, 10 July 2007

XSD for .ACTIONS file Intellisense

John Holliday has a nice article on Custom Workflow Activities to SharePoint Designer 2007, which also includes a link to a WSSACTIONS.xsd file that enables Intellisense for .ACTIONS files in VS 2005. John's example is writing the value of a parameter to the event log.

Wednesday, 21 March 2007

NHibernate Plugin for Visual Studio 2005

Graphical designer for VS.NET 2005 that generates .hbm.xml files for NHibernate.

Wednesday, 24 January 2007

AnkhSVN 1.0 released

AnkhSVN is an add-in for VS.NET that lets you work with Subversion (SVN). This release is built against the latest SVN major version, 1.4.

Arild has announced the release in his blog.

Tuesday, 16 January 2007

VS.NET fonts and colours

MS has a font called Consolas designed for use with programming IDEs such as VS.NET. Dave Reed recommends white text on a black background and has posted his VS.NET colour settings. I found this easier on my eyes than the default scheme once I got used to it.

UPDATE: Ayende has posted his settings too.

UPDATE: Scott Hanselman recommends the Inconsolata font, which is also nice.

Wednesday, 13 December 2006

Resharper 2.5 released

JetBrains have released Resharper 2.5, the latest version of their refactoring-and-more addin for VS.NET. The previous version slowed VS.NET 2005 on my lowly laptop to an unusable crawl. This new version is meant to have significant performance increases, so on it goes!

Microsoft's site for official VS.NET extensions (released and CTP)

Microsoft's Developer Solutions websites has links to downloads for .NET 3.0, WWF, WCF and WPF extensions for VS.NET, as well as to the latest Microsoft ASP.NET AJAX (aka ATLAS) versions.