Search code examples
refactoringresharperndependautomated-refactoringcode-metrics

Is it possible to use NDepend to find class partitioning in an OOP language?


Fort first let me better explain the context and the problem.

Context

We have a big class with dozen of methods; it violates many Software Engineering principles and this is clearly visible through a code metrics measure tool. See poor cohesion, too many method, and so on.

enter image description here

The problem When we try to split the class into smaller classes the problem arise with the instance methods. They need to access one or more field / property from the class and it may be useful to put all together the methods which access a specific field / property. The issue is clearly visible when trying to move a bunch of methods to a new class with Resharper → Refactor → Extract Class.enter image description here

Is it possible to partition the methods and fields which are connected together (have an high cohesion to use code metric terminology)?


Solution

  • Certainly we can do something interesting here, but this requires a partition algorithm and I am not sure which one to choose.

    The query could look like:

    // Replace "NHibernate.Cfg.Configuration" with your "Namespace.TypeName"
    let type = Application.Types.WithFullName("NHibernate.Cfg.Configuration").Single()
    let dicoFields  =  type.Fields
       .ToDictionary(f => f, f => f.MethodsUsingMe.Where(m => m.ParentType == f.ParentType))
    let dicoMethods = type.Methods
       .ToDictionary(m => m, m => m.FieldsUsed.Where(f => f.ParentType == m.ParentType))
    
    // The partition algorithm on both dicos here
    
    from pair in dicoFields 
    orderby   pair.Value.Count() descending
    select new { pair.Key, pair.Value } 
    
    //from pair in dicoMethods 
    //orderby   pair.Value.Count() descending
    //select new { pair.Key, pair.Value} 
    

    Does this help moving forward?

    NDepend field method partition algorithm