Bryan Reynolds

Software, Business, Life . . .

Contact

Bryan Reynolds
  Bryan Reynolds Linked In
E-mail me Send mail

Recent comments

Authors

Disclaimer

The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

© Copyright 2014

NDepend and complex methods.

First I want to say I am no expert on NDepend. I have just begun my journey. If you are unfamiliar with NDepend here is a description straight from there website.

"NDepend is a tool that simplifies managing a complex .NET code base. Architects and developers can analyze code structure, specify design rules, plan massive refactoring, do effective code reviews and master evolution by comparing different versions of the code. "

This is no lie!  For those of you who are starting to analyze your code and are attempting to improve the overall architecture this product is a must have.

 

Getting Started

To get started its pretty straight forward.  You take your compiled assembly's and point NDepend at them.  Here are instructions straight from the quick start tutorial. 

After unzipping the software into a directory of your choosing, there is no installation program, you follow these steps.

  1. Run VisualNDepend.exe.  There is no shortcut you will need to load it from where you unzipped it.
  2. Select the option: Select assemblies to analyze.
  3. Populate the data grid with your assemblies by using the browse button or by using drag&drop.
  4. Click OK. (This will take a few minutes)

 

The result will look something like this.

image

 

Here are a list of some short beginning video tutorials to get you started.  I have to admit a prefer short videos and NDepend does not disappoint.  Everyone's time is extremely valuable and getting to the heart of the matter, matters!

NDepend Basics

Analyzing Assemblies

Building a NDepend Project

 

Complex Methods

It would take 1000 blog posts to go through how you can use this product.  Today I just wanted to show you how I used it to identify a function that was more complex than it needed to be.

Below is a code snippet of a method of a class that manages Excel files.  It is designed to take a regular expression and search all sheets and all cells for a match.  Once the match is found the method return the associated cell.

 

   1: public Cell GetCellByRegEx(string regularExpression)
   2:       {
   3:           Check.Require(this._xlsFile != null);
   4:           Check.Require(!string.IsNullOrEmpty(regularExpression));
   5:  
   6:           var colCount = this._xlsFile.ColCount;
   7:           var rowCount = this._xlsFile.RowCount;
   8:           var sheetcount = this._xlsFile.SheetCount;
   9:  
  10:           for (var s = 1; s < sheetcount; s++)
  11:           {
  12:               this._xlsFile.ActiveSheet = s;
  13:  
  14:               for (var r = 1; r <= rowCount; r++)
  15:               {
  16:                   for (var c = 1; c <= colCount; c++)
  17:                   {
  18:                       var v = this._xlsFile.GetCellValue(r, c);
  19:                       if (v != null)
  20:                       {
  21:                           var regex = new Regex(
  22:                               regularExpression,
  23:                               RegexOptions.IgnoreCase);
  24:                           var matches = regex.Matches(v.ToString());
  25:  
  26:                           if (matches.Count > 0)
  27:                           {
  28:                               var cell = new Cell { Row = r, Column = c, Value = v.ToString() };
  29:  
  30:                               return cell;
  31:                           }
  32:                       }
  33:                   }
  34:               }
  35:           }
  36:  
  37:           return null;
  38:       }
There is a section for code quality query's within NDepend.  It is designed to run through your code and analyze how it is constructed based on query's made by the developers of the product.  These rules of course are based on what they believe is a best practice.  Users of the product will end using there own skill to determine what is right or wrong.  The goods news is that you can change these CQL queries at any time.  Below is a screen capture of what a CQL looks like. 
I have highlighted below a CQL result that describes "Methods too complex".  There are 4 methods in my project that have been deemed too complex.
image
The above method "GetCellByRegEx" has a IL Nesting depth of 5.  When you select a query result below you will see a listing of the methods that make up that result. 
image
On the bottom right hand side of the NDepend screen shows the results of the CQL Code Quality queries run against your code assembly.
image
Shows when you select edit on the "Methods too complex" CQL item.

Solving the Problem

Now that you have identified that you have a method that is too complex what do you do.  Just fix it right!  Easier said than done is most cases.  There is some good news though. Most of the CQL items I have seen provide you with some assistance right in the query edit screen.  Looking closely CQL Query Edit screen you will see a link to help on this subject.

Here is a snippet from NDepend that covers what this query is suppose to track and what they recommend.

IL Nesting Depth: The metric Nesting Depth for a method is the maximum number of encapsulated scopes inside the body of the method. The metric IL Nesting Depth is computed from the IL code. Values computed are very similar to what we would expect by computing them from the C# or VB.NET source code.
When you have a testing condition with N conditions, such as if( i > 9 && i < 12) then it is considered as N scopes because it is possible to decompose such conditions into N atomic conditions.
When a method has a large number of case statements corresponding to a switch, the C# and VB.NET compiler generally produce optimizations while generating the IL. In such case, the IL Nesting Depth corresponding value might be slightly higher to what you would expect.


Recommendations: Methods where ILNestingDepth is higher than 4 are hard to understand and maintain. Methods where ILNestingDepth is higher than 8 are extremely complex and should be split in smaller methods (except if they are automatically generated by a tool).


Related Link::
A simple trick to code better and to increase testability

After following the link and using there simple trick to adjust my code as follows the method was no longer deemed complex.
   1: public Cell GetCellByRegEx(string regularExpression)
   2:       {
   3:           Check.Require(this._xlsFile != null);
   4:           Check.Require(!string.IsNullOrEmpty(regularExpression));
   5:  
   6:           var colCount = this._xlsFile.ColCount;
   7:           var rowCount = this._xlsFile.RowCount;
   8:           var sheetcount = this._xlsFile.SheetCount;
   9:  
  10:           for (var s = 1; s < sheetcount; s++)
  11:           {
  12:               this._xlsFile.ActiveSheet = s;
  13:  
  14:               for (var r = 1; r <= rowCount; r++)
  15:               {
  16:                   for (var c = 1; c <= colCount; c++)
  17:                   {
  18:                       var v = this._xlsFile.GetCellValue(r, c);
  19:                       if (v == null)
  20:                       {
  21:                           continue;
  22:                       }
  23:                       var regex = new Regex(
  24:                           regularExpression,
  25:                           RegexOptions.IgnoreCase);
  26:                       var matches = regex.Matches(v.ToString());
  27:  
  28:                       if (matches.Count <= 0)
  29:                       {
  30:                           continue;
  31:                       }
  32:                       var cell = new Cell { Row = r, Column = c, Value = v.ToString() };
  33:  
  34:                       return cell;
  35:                   }
  36:               }
  37:           }
  38:  
  39:           return null;
  40:       }

Conclusion

I have been developing software for 20+ years and the tools just keep getting better and better.  If you have the time and the inclination to improve your existing architecture you owe it to yourself to take a look at this product.

Digg It!DZone It!StumbleUponTechnoratiRedditDel.icio.usNewsVineFurlBlinkList

Currently rated 2.7 by 6 people

  • Currently 2.666667/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Categories: Agile | C#
Posted by Bryan on Saturday, August 09, 2008 4:29 AM
Permalink | Comments (0) | Post RSSRSS comment feed

Related posts

Comments