Tuesday, June 19, 2012

VS2010 Code Searching features

Searching and Navigating code

Developers need to be able to easily navigate, search and understand the code-base they are working on.  In usability studies we’ve done, we typically find that developers spend more time reading, reviewing and searching existing code than actually writing new code. 
The VS 2010 code editor adds some nice new features that allow you to more productively search and navigate a code-base, and enable you to more easily understand how code is being used within a solution. 

Searching and Navigating the ASP.NET MVC Source Code

For this blog post I’m going to use the ASP.NET MVC framework code-base (which has many thousand lines of code) to help demonstrate some of the new VS 2010 searching and navigation features.  If you have VS 2010 Beta 2 installed, you can follow along by downloading and opening the ASP.NET MVC framework source code from here.
image
You should find that the performance of the below features is really fast with this project – despite it being many thousands of lines of code in size.  All of the features I’m demonstrating below are also now built-into VS 2010 (and work for all project types and for both VB and C#).

VS 2010 “Navigate To” Support

Being able to quickly find and navigate code is important with both big and small solutions.
Visual Studio 2010 now supports a new (Ctrl+comma) keyboard shortcut (meaning the control key is held down together with the comma key).  When you press the (Ctrl+comma) combination, a new VS 2010 “Navigate To” dialog will appear that allows you to quickly search for types, files, variables and members within your solution – and then open and navigate to them:
image
The “Navigate To” dialog provides an fast incremental search UI – with results immediately populating as soon as you start typing search terms.  For example, type “cont” (without pressing enter) and you’ll see that 176 results immediately show up within the results list as you start to type:
image
Type a few more characters and you’ll see the list automatically filters to just those results that match “controller”:
image
You can use the scroll bar to scroll through the results – or alternatively press the tab key and then use the cursor arrows if you don’t want to take your hands off the keyboard.  You’ll find that the “Navigate To” window lists all types of results that match your search term – including Type names, Method/Property names, Field declarations, and file names:
image
Selecting any of the results in the results list will open the relevant source file within VS 2010 (if it isn’t already open) and take you immediately to the relevant source location (and highlight the relevant name within it):
image
Nice Fuzzy Search Capabilities
The “Navigate To” search box supports some nice “fuzzy search” capabilities that allow you to perform smart filters and searches without having to know exactly the name of the thing you are looking for.  These work well with the incremental/immediate search UI of the dialog – and allow you to refine your searches and get real-time results as you type.
To try this out let’s first search on the word “cache”.  Notice how the search results include not just items that start with the word “cache” – but also display any results that have the word “cache” in it:
image
We can add multiple words to the search textbox to further filter the results.  For example, below I am filtering the list to only include those that have both “cache” and “action” in the name:
image
Types and members within the .NET Framework using a naming design-guideline pattern called “Pascal Casing” – which means that the first letter of each word in a Type or Member name is capitalized.  The “Navigate To” dialog allows you to optionally use this “Pascal Casing” convention to quickly filter types. Just type the uppercase first letter of names in a type/member and it will automatically filter for results that match the uppercase pascal naming convention. 
For example, typing “AMS” will filter to the below results (just those types and members that have words in them that start with A then M then S):
image
The “Navigate To” dialog allows you to quickly filter and locate code with a minimum of keystrokes – and avoid you ever having to use the mouse, open the solution explorer, and click on a file directly.

View Call Hierarchy

Having the ability to quickly search and navigate to code is great.  Being able to also quickly discover how that code is being used is even better.  VS 2010 introduces a new “View Call Hierarchy” feature that allows you to quickly discover where a particular method or property within your code-base is being called from, and allows you to quickly traverse the call tree graph throughout the code-base (without having to run or debug the solution).
To use this feature, simply select a method or property name within your code-base, and then either type the (Ctrl+K,Ctrl+T) keyboard shortcut combination, or right-click and select the “View Call Hierarchy” context menu command:
image
This will bring up a new “Call Hierarchy” tool window that by default shows up under the code editor.  Below you can see how the “Call Hierarchy” window is displaying the two methods within our solution that invoke the ViewPage.RenderView() method we selected above. 
image
We can then optionally drill down hierarchically into the first “RenderViewAndRestoreContentType” method to see who in-turn calls it:
image
For virtual methods/properties you can also use the call hierarchy window to see what types sub-class and override them.
Double clicking any of the members within the “Call Hierarchy” window will open the appropriate source file and take you immediately to that source location:
image
This allows you to quickly navigate throughout a code-base and better understand the relationships between classes and methods as you code.

No comments: