Tuesday, September 26, 2006

 

Visual Studio 2005 IDE Productivity Tips and Tricks

If you use the Visual Studio IDE frequently there are a number of shortcuts that can make you more productive. None of these tips are new, but there might be one or two shortcuts you were not aware of. The majority of these tips come directly from the Tech-Ed 2005 and 2006 presentations given by Anson Horton and Karen Liu respectively. I’ve condensed the gist of these 1 hour sessions into what you see here. [Note: these are the C# key bindings; other languages may differ]. One thing certainly worth remembering is that adding the Shift key to most key combinations causes the opposite sense of whatever the action is.

These useful shortcuts come under the following headings:

  • Understanding code
  • Navigating code
  • Modifying code
  • Debugging code

Understanding Code: Exploring the .NET framework’s Class Relationships

The dynamic class diagram is a great way of exploring class relationships in ­­the .NET framework. Now I’m sure most people will already be using the class diagram for their own class hierarchies (Add a class diagram, go to class view [Ctrl-W, Ctrl-C], and drag a class to the diagram), but did you know that you can also use a class diagram as a great way of getting familiar with the .NET framework in VS2005? In class view, open the “Project References” tree and drag a namespace to the class diagram (e.g. System.IO). Also, don’t forget you can “Show Derived classes” by right-clicking on a class.


Navigating Code

You are probably aware of the right-click ‘Go to Definition” navigation (with your cursor placed in a symbol). It is also bound to F12. What you might not be aware of is its counterpart, Shift-F12 which shows all references to the symbol defined at the current cursor location.

F12 - Go to symbol definition
Shift-F12 - Show all references to a symbol

The F8 key is a great shortcut key to remember. It can cycle through all references found with F12, and in addition it can cycle through any ‘List’ output window, such as the task list, error list, etc.

F8 - Cycle through list of items in the currently active output window
Shift-F8 - Same as F8 but in the opposite direction

The next two key ‘chords’ are very useful when you are dealing with unfamiliar code, as they provide a high level view of a class’s methods:

Ctrl-M, Ctrl-O - Collapses all methods to outline view
Ctrl-M, Ctrl-M - Collapses/expands the method the cursor is currently in.

Most people are aware of the “IE style navigation” using Ctrl-minus and Ctrl-Shift-minus to move backwards and forwards through visited code:

Ctrl-‘-‘ - Move backwards to previous cursor location
Ctrl-Shift-‘-‘ - Move forwards to last cursor location

But the following two are less well known (and in many instances, more useful). If you are navigating a sequence of method calls, and you move around in the surrounding code, the IE style navigation will cycle through every location the cursor was placed. The following two commands move up and down the “Definition Navigation Stack”, so just those methods that you delved into using F12:

Ctrl-Shift-8 - Move up the definition navigation stack (stack pop)
Ctrl-Shift-7 - Move down the definition navigation stack (stack push)

Another less known shortcut is Ctrl-I which activates incremental search. Open a source file, hit Ctrl-I and start typing the first few letters of your search text. Visual Studio will go to the first occurrence and continuing searching as you enter more characters. Hit Ctrl-I to go to the next occurrence (and of course, Ctrl-Shift-I to go to the previous). Pressing Escape will turn off incremental search.

Ctrl-Shift-I - Incremental search
Ctrl-Shift-F - Find in all files (more useful than Ctrl-F)
Ctrl-H - Replace

Another tip involves finding files. You can download a free add-in called VS File Finder from the following link: http://www.zero-one-zero.com/vs. It’s easy to use and good for navigating in large solutions. File Finder shows up as a new window after you install it from the .msi file. I docked mine with the output and list panels at the bottom of the IDE window. Any text you enter in the textbox will filter the list of files displayed by partial file name matches. [From Visual Studio Hacks, by James Avery.]

For some reason, I always seem to forget these two shortcuts:

F7 - Switch to code view
Shift-F7 - Switch to design view


Modifying Code

Ctrl-K, Ctrl-C - Comment out currently selected code
Ctrl-K, Ctrl-U - Uncomment currently selected code
Ctrl-K, Ctrl-F - Auto format selected code

FxCop is a Static Analysis tool that can not only help you come to terms with an unfamiliar code base (see previous post) but also to improve existing code. FxCop runs over the IL generated from your source code and performs a set of rule based comparisons on it. There are 2 rule sets that can help with refactoring: naming and maintainability. One of the rules that can generate warnings relates to something called ‘cyclomatic complexity’. Cyclomatic complexity is just the number of branches in a method. The higher the value, the greater the likelihood that a method could be reduced by having sections of code extracted into new methods (refactored).

Visual Studio 2005 contains a number of refactoring commands to help you reorganise code:

Ctrl-R, Ctrl-M - Extract method
Ctrl-R, Ctrl-E - Encapsulate field
Ctrl-R, Ctrl-I - Extract Interface
F2 - Rename

[Ctrl-R is the first ‘note’ in most refactoring key combinations, and there are other refactoring commands in addition to those I've listed above]


Debugging Code

These are essential debugging shortcuts:

F10 – Step over
Ctrl-F10 – Run to cursor
F11 – Step into
Shift-F11 – Step out
F9 – Toggle a breakpoint
F5 – Run with debugging
Shift-F5 – Stop debugging
Control-F5 – Run without debugging

Attributes can make your debugging more effective by declaratively altering the behaviour of the debugger:

[DebuggerStepThrough()] is used to decorate a method or property to prevent the debugger stepping into it whilst debugging.

[DebuggerDisplay(“{propertyname}”)] allows you to define the value which is displayed by default in debugger visualisers.

[DebuggerBrowsable(DebuggerBrowsableState.Never)] can be used to prevent the display of data, such as private fields, in visualisers.

In order to use the debugger attributes you will need to import the System.Diagnostics namespace. You can find out more about debugger attributes here: Enhancing Debugging with the Debugger Display Attributes.

One final debugging tip: you can hold down control key when drilling into a visualiser to make it transparent while you inspect the code beneath it.



    

Powered by Blogger