Quantcast
Channel: Sampath Lokuge Tech Blog
Viewing all articles
Browse latest Browse all 107

NDepend - Static Analysis Tool for .NET Managed Code

$
0
0
Articles Of The Day


Introduction

  • This Article shows how to use NDepend With your ASP.net MVC Application.

What is NDepend ?
  • NDepend is a static analysis tool for .NET managed code.
  • This tool supports a large number of code metrics, allows for visualization of dependencies using directed graphs and dependency matrix.
  • This tool also performs code base snapshots comparison, and validation of architectural and quality rules.
  • User-defined rules can be written using LINQ queries. This possibility is named CQLinq.
  • This tool also comes with a large number of predefined CQLinq code rules.
  • Code rules can be checked automatically in Visual Studio or during continuous integration.

Top 13 NDepend Features
  1. Code Rule and Code Query - More than 150 default code rules to check against best practices.
  2. Trend Monitoring - Get Trend Charts about pretty much any code metrics to master the evolution of your application.
  3. Harness Test Coverage Data - Import test coverage data, and make the most of it in the NDepend feature-rich context.
  4. Code Quality, 82 Code Metrics - Lines of Code, Cyclomatic Complexity, Coupling, Nesting Depth, Rank, NDepend supports them all.
  5. Explore Existing Architecture - Explore how the code is actually structured and shed light on architectural flaws kept secret.
  6. Detect Dependency Cycles - Get rid of dependency cycles between components to achieve higher code maintainability.
  7. Compare Builds and Code Diff - Compare two versions of a code base and browse code diff and changes in any way you can think of.
  8. Enforce Immutability and Purity - NDepend helps maintaining clean side-effects free code to achieve clean concurrent programming.
  9. Complexity and Diagrams - Spot too complex code at a glance thanks to unique diagramming capabilities in the .NET world.
  10. Continuous Integration Reporting - Integrate NDepend analysis into your build process and get highly customized and detailed reports to see progression and prevent code quality degradation.
  11. Warnings on Build Process Health - Be warned of potential build process problems before they end up causing friction and pain.
  12. NDepend.API and Power Tools - Write your own static analyzer based on NDepend.API, or tweak existing open-sources Power Tools.
  13. Real-World Proof - Full Visual Studio integration, .NET Reflector support, super fast and lightweight analysis, NDepend is conceived for real-world programmers.

How to Install NDepend ?

Step 1 :
  • Download the NDepend installation zip file.
  • To purchase : Buy
  • 14-day Free Trial : Free Trial

Step 2 :
  • Close Visual Studio.
  • To install NDepend, just unzip the files in a private application folder on your machine. 
  • Don't unzip files in '%ProgramFiles%\NDepend'. This will provoke problems because of Windows protection.
  • If you are running NDepend Professional Version, copy your license file in the same folder where you unzipped NDepend files.

Step 3 : 
  • Click NDepend.Install.VisualStudioAddin.exe

NDepend - Static Analysis Tool for .NET Managed Code


 
Step 4 :
  • Select your Visual Studio version and then click 'Install'.

NDepend - Static Analysis Tool for .NET Managed Code


 Step 5 :
  • Open Visual Studio.
  • You can see the 'NDEPEND' menu item on your Visual Studio main menu as shown below.


    NDepend - Static Analysis Tool for .NET Managed Code


    That's it.You're done.



    How to Attach new NDepend Project ?

    Step 1 :
    • Open Visual Studio after having installed the NDepend addin
    • Open your solution containing the code to analyze.

    Step 2 :
    • Click'NDEPEND' menu and then select and click 'Attach new NDepend Project to current VS Solution'. 

    NDepend - Static Analysis Tool for .NET Managed Code

    Step 3 :
    • The dialog opened has been populated with .NET assemblies of the Visual Studio solution.
    • Here you can add more .NET assemblies compiled from another solutions.
    • If you want,you can filter out the assemblies as shown below. 

    NDepend - Static Analysis Tool for .NET Managed Code




























    Step 4 :
    • For the simplicity of this analysis,I have selected only one assembly. 
    • Click'Analyze a single .NET Assembly' button.The NDepend analysis start.
    • It will just take a few seconds to analyze your code base for the first time.

    NDepend - Static Analysis Tool for .NET Managed Code




























    Step 5 :
    • After that it pops up the below kind of dialog box.
    • From that you can select 'View NDepend Dashboard' option.
    • You can use Dashboard as an entry point to get started with NDepend.

    NDepend - Static Analysis Tool for .NET Managed Code



























    Let's Explore some Features of NDepend


    NDepend Dashboard
    • A dashboard that shows at a glance the state of your code base and will soon become your entry point to many features.

    NDepend - Static Analysis Tool for .NET Managed Code




















    Focusing on Rules Violations 
    • When you click the 'Critical Rules Violated'link from the above Dashboard,It shows the screen as below.
    • It's listed all Rules Violated and categorize according to the rule types.It's really awesome.


      NDepend - Static Analysis Tool for .NET Managed Code






















      • If you need to analyze further about the Rules Violations,just click the item which you want from the right side panel.
      • Matter of a second it gives all the details of that rule violation with the recommendations where how to fix that issue and more.It's simply amazing.
      • As an example,You can see that 'ValidationCreateCustomerPetsHistory' method is having 39 parameters on it.Phew ...  :(

      NDepend - Static Analysis Tool for .NET Managed Code





































      Nice minute Video


      What is CQLinq ?
      • Code Query LINQ, is a feature proposed by the tool NDepend since the version 4, to query .NET code through LINQ queries.
      • A CQLinq query is a LINQ query that relies on types defined in the namespace NDepend.CodeModel of the NDepend.API.
      • A CQLinq query can be edited live in the NDepend UI (standalone or in Visual Studio).
      • The query is executed automatically as soon as it compiles.

      The syntax is as simple as:

      from m in Methods where m.NbLinesOfCode > 30 select m

      • The above query will only match large methods that are having more than 30 lines of code.
      • If you need to customize your analysis by using CQLinq,just read this CQLinq Syntax.


      Visualizing Code Metrics with Treemap
      • In the Metric View, the code base is represented through a Treemap.
      • Treemapping is a method for displaying tree-structured data by using nested rectangles.
      • The tree structure used in NDepend Treemap is the usual code hierarchy.

      It's like this :  
      • .NET assemblies contain namespaces.
      • Namespaces contain types.
      • Types contains methods and fields.

      For Which Scenarios Treemap can be Applied ?
      • Too Big, Too Complex
      • Flaws Localization
      • Code Coverage Density
      • Top-Down Code Exploration
      • Code Structure Observations

      Let's Explore one scenario with more details.

      Too Big, Too Complex
      • NDepend provides many code metrics to spot too big and too complex code.
      • Method with too many parameters, too many variables, too many lines of code, too high cyclomatic complexity etc...

      Step 1 : 
      • Click 'NDEPEND' and select 'Metric'.Then choose and click 'Code Metric View' sub menu as shown below.
      NDepend - Static Analysis Tool for .NET Managed Code



      Step 2 :
      • On the screenshot below, the Treemap Level is set to Type and the Metric is number of lines of code (LOC).
      • Large rectangles represent large types of the code base.
      • Hovering a rectangle with the mouse indicates the Type metric values, here the type number of lines of code.
      • You can see that code Treemapping helps not only spotting too large and too complex code elements, but also, it helps comparing their respective size and complexity.It's simply awesome. 
      • You can read more about Code Metrics with Treemap here.

      NDepend - Static Analysis Tool for .NET Managed Code




































      Do you need to Dive Deep ends of  NDepend ?


      Conclusion

      • You saw that the awesomeness of the NDepend tool. It's really great tool to do the Static Analysis of .NET Managed Code.
      • I have never used NDepend before do this article. But I learned lots about this tool within a few hours of hands on practices with the project, which I'm working on. And of course I have seen lots of issues and rule violations which are existing with the project. I'll definitely inform this to my project manager and ask for time for doing the re-factoring phase. Thanks for NDepend. :)
      • You too can learn NDepend very quickly. Because NDepend's home page provides all necessary easy to learn nice videos and well written documentation. Other than that NDepend itself has a very nice Context sensitive help. Which is really awesome.
      • I would like to recommend NDepend for the .NET Senior developers as well as, of course, for the Tech Leads and Architects.
      • I must put a special thanks for Creator of this tool 'Patrick Smacchia'. He is the person who gave me this opportunity to evaluate NDepend with Free License to the product. Thanks again for you. :)


        I hope this helps to You.Comments and feedback greatly appreciated.

        If you feel it was a good article,Give me a +1.Thank You.




        You Might Also Like

        Viewing all articles
        Browse latest Browse all 107

        Trending Articles