Patrick Smacchia [MVP C#]

Sponsors

The Lounge

Wicked Cool Jobs

News

  • NDepend v3 is fully integrated in Visual Studio, and is now available for download! Software dependencies visualization, 82 .NET software metrics, continuous rule validations, assembly version diff, declarative code queries and more ! http://ndepend.com

Advertisement

Efficiently Searching Code Elements by Name

Normal 0 false false false EN-US X-NONE X-NONE

What the tremendous success of Google told us is that being able to find the right information quickly is essential. Here both Time (spent searching) and Relevancy (of the result found) are the important assets.

 

We, developers, spend a significant amount of time searching particular code elements in our code base. Personally, I would say at least 15% of my time is dedicated searching in a code base I know pretty well. The last version 2.12 of NDepend comes with a Search Panel that supports numerous search criteria, including search by Name, Size, Complexity, Coupling, Popularity, Coverage, Visibility, Mutability, Purity, Change and Coverage of Changes. In my last post, I explained how to use this Search Panel to significantly make Code Reviews easier.

 

Today I’d like to focus on Search by Name, which is certainly the more widely used search criterion. Every developer tools comes with some sort of search by name, Visual Studio, Reflector, Resharper, NCoverExplorer… So let me show what makes the NDepend’s Search by Name unique.

 

Suppose one want to search a click handler method but doesn’t remember where it is. The first step is searching for methods whose names include Click:

 

 

Several things to notice:

  • The Click keyword is highlighted in method result. This helps to spot more quickly the searched code element(s).
  • Method can be grouped by declaring assembly/namespace/type. This make the search much more efficient because in the general case, we have a clue of where the method searched should be declared.
  • Under the hood, the search is based on a CQL Query generated from the user inputs. Here the query is: SELECT METHODS WHERE (NameLike "Click\i") AND !IsInFrameworkAssembly
  • It is fast, very fast: for example matching the 347.037 methods of the .NET Framework v3.5 that contain an ‘e’ in the name is immediate.

 

 

Let’s refine our search criteria. Suppose that the method to find is a click event for a cancel button. Notice now the CQL Query generated that contains 2 NameLike clauses:

 

 

 

More generally the CQL condition NameLike can be parameterized with any regular expression. You suddenly remember that your method should begin with cancel, lower case. Just click the lower case tick box and suffix your regex with ^, which means in regex terms begin with.

 

 

 

The search can also easily include tier code elements. Clicking the Include Tier tickbox has for effect to discard the IsInFrameworkAssembly CQL restrictive condition.

 

 

 

Also, you can extend the search to the full name. In other words, one can match any method whose name, type or namespace contains Click. For example, all methods declared in a class named like ToolClickedEventArg, are now matched.

 

 

 

And because all this is based on the CQL language, you can easily edit the CQL Query generated and refine it at whim, like for example, to match static methods with Click in the name.

 

 

I can't stop using the new Search Panel for various task and I will describe more use-cases in future posts.

 


Posted Mon, Apr 6 2009 6:16 AM by Patrick Smacchia

[Advertisement]

Comments

Jason Haley wrote Interesting Finds: April 5, 2009
on Mon, Apr 6 2009 10:26 AM

Interesting Finds: April 5, 2009

DotNetShoutout wrote Efficiently Searching Code Elements by Name - Patrick Smacchia [MVP C#] - CodeBetter.Com
on Mon, Apr 6 2009 6:56 PM

Thank you for submitting this cool story - Trackback from DotNetShoutout

gOODiDEA.NET wrote Interesting Finds: 2009 04.06~04.09
on Wed, Apr 8 2009 8:34 PM

Web Ext Core 3.0 Beta Released – Download Announcing Velocity CTP3 ! - What's New – Compares with memcached

Craig Dunn wrote re: Efficiently Searching Code Elements by Name
on Mon, Apr 27 2009 8:12 PM

Hey Patrick,

have you seen

www.krugle.com/.../enterprise_features.html

?

Seems like NDepend-like stats/data but across a set of projects/solutions. Raises an interesting set of searches/visualizations (at a meta-level: what *projects* depend on certain Framework [Microsoft or custom] assemblies/classes, for example; and what versions thereof).

NDepend would need to do the parsing of sets of projects; and have some enterprise datastore with versioned historical data about each project (we have about 30...).

Just a thought - might not even make sense...

Patrick Smacchia [MVP C#] wrote NDepend v3 is now 100% integrated in Visual Studio.
on Thu, Jan 28 2010 4:22 AM

First of all, if you are like me, you certainly don’t want another Visual Studio extension that

Add a Comment

(required)  
(optional)
(required)  
Remember Me?
Devlicio.us