Efficiently Searching Code Elements by Name




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
. In my last post, I explained how to use this Search Panel to significantly make Code Reviews


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
  • 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
. 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




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.


This entry was posted in Uncategorized. Bookmark the permalink. Follow any comments here with the RSS feed for this post.
  • http://conceptdev.blogspot.com Craig Dunn

    Hey Patrick,
    have you seen

    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…