Visual Studio Code Search

Introduction

Developers spend a significant amount of time searching in their code. Hopefully Visual Studio embeds a large set of code search tools.

NDepend proposes a complementary set of search tools to Visual Studio users. The NDepend's search experience supports numerous criteria including searching by: Name, Size, Complexity, Coupling, Popularity, Coverage, Visibility, Mutability, Purity, Change and Coverage of Changes.

Let's start with Search by Name:

Search by Name

The two shortcuts Alt+M and Alt+C can be used to search Methods or Classes by name with the NDepend Visual Studio extension.

Suppose that you want to search classes related to Client in your code. The first step is searching for types whose names include the sub-string "Client":

ndepend search methods by name with code query generation

Several things to notice here:

  • The Client keyword is highlighted in types result. This helps to spot more quickly the searched code element(s).
  • Types can be grouped by parent assembly and namespace. This make the search much more efficient because often we have an idea of where the element searched is declared.
  • The search is based on a CQLinq Query generated from the search user inputs. A button Edit Query let's edit the query generated.
    from t in Types where
       
    t.SimpleNameLike(@"Client\i")
    select t
  • Third-party types containing the word Client are matched like HttpClient. This is because the tick-box Third-Party is checked.

Performance of Search

The NDepend search is fast: for example searching all elements related to XmlDocument in the .NET Core 3 code base (15.000+ classes, 145.000+ methods) is immediate.


Multi Regex Support

Let’s refine our Client search criteria with the extra string Http. This code query is generated. It relies on the NameLike() method .

from t in Types where
   
t.SimpleNameLike(@"Client") &&
   
t.SimpleNameLike(@"Http")
select t
NDepend Search With Regular Expression Regex

The NameLike() method can be parameterized with any regular expression. For example you can suffix your regex with ^, which means begin with:

NDepend Search With Regular Expression Regex begin With

Search on Full Name

Also, you can extend the search to the full name. In other words, you can match any type whose name, type or namespace contains "Http". In the screenshot below the class SessionExtensions is matched because its parent namespace Nop.Core.Http.Extensions contains Http.

ndepend search types by full name

Refining Search by Name with CQLinq

Since the search is based on code querying, by clicking the Edit Query button the CQLinq query generated gets edited. You can then refine it at whim, like for example, to match application static classes with "Http" in the name.

ndepend search methods by name with code query refined

Search by other Criteria than Name

It is possible to search for methods, fields, types, namespaces and assemblies or any of those through many other criteria than name. No matter the criteria chosen, it is based on code query generation.

ndepend search code element through various criteria

Search by Size

It is possible to search for methods, types, namespaces and assemblies according to their size. The size if measured with the code metric Lines of Code. If this metric is not available (because PDB files were not available at analysis time) the metric number of IL instructions is chosen.

ndepend search methods by size with code query generation

Search by Complexity

It is possible to search for methods, types, namespaces and assemblies according to their complexity. The size if measured with the metric source file Cyclomatic Complexity. If this metric is not available (because PDB files were not available at analysis time) the metric IL Cyclomatic Complexity is chosen.

ndepend search methods by complexity with code query generation

Search Coupling in Code

It is possible to search for methods, types, namespaces and assemblies according to their coupling. For example for a type its coupling is made of:

  • The set of types used.
  • The set of types using it.

Typically high number of users indicates a popular code element. A high number of elements used indicates a god code element, meaning a code element that has intimate knowledge of a large portion of the code base. Hence, a too high number of elements used for a type or a method should be considered as a code smell.

In the screenshot below we are searching for methods calling more than 10 methods. The code query generated is:

from m in Application.Methods 
where m.NbMethodsCalled >= 10 
orderby m.NbMethodsCalled descending 
select new { 
   
m,
   
m.MethodsCalled,
   
m.MethodsCallingMe
}
ndepend explore methods coupling with code query generation

Search by Popularity

It is possible to search for types and methods according to their popularity. The popularity of a code elements is computed according to the Ranking metric (the same metric originally used by Google to compute page rank).

ndepend search classes by popularity with code query generation

Search by Visibility

It is useful to list methods, fields or types according to their visibility level: private, protected, internal, internal protected, public. For example this way you can explore your public API.

As shown in the screenshot below, it is also possible to ask for code elements that could have a more restrictive level of visibility. For example, if a method is tagged as CouldBePrivate by NDepend, it means that it is not declared as private but, in the context of the application it is only used inside its declaring type.

ndepend search classes by visibility with code query generation

Search by Immutability / Purity

It is possible to search for immutable types and fields, and pure methods. More about this topic can be found in the article Immutable types: understand their benefits and use them.

ndepend search methods by purity with code query generation

Search by Code Coverage Ratio by Tests

It is possible to search for methods, types, namespaces and assemblies according the ratio of their coverage by tests. Code coverage data are extracted from results produced by dedicated third party tools as explained in this documentation: Importing Code Coverage Results in the NDepend Code Model.

ndepend search classes by code coverage ratio with code query generation

Search in the Code Delta since Baseline

It is possible to search within the code delta since the baseline. This possibility is described in the section Advanced Code Diff with NDepend.

These conventions are used in the search changes result:

  • Underlined font for elements changed since baseline.
  • Bold font for elements added since the baseline
  • Bold strike font for elements removed since the baseline
ndepend search within the code delta since the baseline with code query generation

Search by Code Coverage by Tests in the Code Delta since Baseline

It is possible to search for code elements refactored or new since the baseline, that are not properly covered by tests. This possibility is especially useful for team who care for code coverage. Here the following code query is generated:

from t in Application.Types 
where (t.WasAdded() || t.CodeWasChanged()) && 
       
t.PercentageCoverage < 100 
orderby t.PercentageCoverage descending, 
        
t.NbLinesOfCodeCovered, 
        
t.NbLinesOfCodeNotCovered, 
        
t.NbLinesOfCode 
select new { t, t.PercentageCoverage, t.NbLinesOfCodeCovered,               
             
t.NbLinesOfCodeNotCovered, t.NbLinesOfCode }
ndepend search code coverage ratio within the code delta since the baseline with code query generation