Friday, December 27, 2013

NDepend: Set of Rules to Show how Code Quality Was Improved

As it was mentioned in the previous article it's interesting to know how the code quality has been improved. NDepend includes only queries for quality regressions, this article contains CQLinq queries for quality improvements.

Big types that were totally improved or removed

// Big types that were totally improved or removed
from oldT in codeBase.OlderVersion().Types
let newT = oldT.NewerVersion()
where 
    (
        oldT.NbLinesOfCode > 500
        &&
        (oldT.WasRemoved() || newT.NbLinesOfCode <= 500)
    )
    ||
    (
        oldT.NbILInstructions > 3000
        &&
        (oldT.WasRemoved() || newT.NbILInstructions <= 3000)
    )
orderby oldT.NbLinesOfCode descending,
        oldT.NbILInstructions descending
select new
{
    type = oldT.WasRemoved() ? oldT : newT,
    oldLines = oldT.NbLinesOfCode,
    newLines = oldT.WasRemoved() ? 0 : newT.NbLinesOfCode,
    oldInstructions = oldT.NbILInstructions,
    newInstructions = oldT.WasRemoved() ? 0 : newT.NbILInstructions
}

Types with too many methods that were totally improved or removed

// Types with too many methods that were totally improved or removed
from oldT in codeBase.OlderVersion().Types
let newT = oldT.NewerVersion()
where
    (
        oldT.NbMethods > 20
        &&
        (oldT.WasRemoved() || newT.NbMethods <= 20)
    )
orderby oldT.NbMethods descending
select new
{
    type = oldT.WasRemoved() ? oldT : newT,
    oldMethods = oldT.NbMethods,
    newMethods = oldT.WasRemoved() ? 0 : newT.NbMethods
}

Complex methods that were totally improved or removed

// Complex methods that were totally improved or removed
from oldMethod in codeBase.OlderVersion().Methods
let newMethod = oldMethod.NewerVersion()
where 
    (
        oldMethod.CyclomaticComplexity > 6
        &&
        (oldMethod.WasRemoved() || newMethod.CyclomaticComplexity <= 6)
    )
    ||
    (
        oldMethod.ILCyclomaticComplexity > 10
        &&
        (oldMethod.WasRemoved() || newMethod.ILCyclomaticComplexity <= 10)
    )
    ||
    (
        oldMethod.ILNestingDepth > 5
        &&
        (oldMethod.WasRemoved() || newMethod.ILNestingDepth <= 5)
    )
orderby oldMethod.CyclomaticComplexity descending,
        oldMethod.ILCyclomaticComplexity descending
select new {
    method = oldMethod.WasRemoved() ? oldMethod : newMethod,
    oldCC = oldMethod.CyclomaticComplexity,
    newCC = oldMethod.WasRemoved() ? 0 : newMethod.CyclomaticComplexity,
    oldILCC = oldMethod.ILCyclomaticComplexity,
    newILCC = oldMethod.WasRemoved() ? 0 : newMethod.ILCyclomaticComplexity,
    oldDepth = oldMethod.ILNestingDepth,
    newDepth = oldMethod.WasRemoved() ? 0 : newMethod.ILNestingDepth
}

Large methods that were totally improved or removed

// Large methods that were totally improved or removed
from oldMethod in codeBase.OlderVersion().Methods
let newMethod = oldMethod.NewerVersion()
where 
    (
        oldMethod.NbLinesOfCode > 30
        &&
        (oldMethod.WasRemoved() || newMethod.NbLinesOfCode <= 30)
    )
    ||
    (
        oldMethod.NbILInstructions > 200 
        &&
        (oldMethod.WasRemoved() || newMethod.NbILInstructions <= 200 )
    )
orderby oldMethod.NbLinesOfCode descending, 
        oldMethod.NbILInstructions descending
select new 
{
    method = oldMethod.WasRemoved() ? oldMethod : newMethod,
    oldLines = oldMethod.NbLinesOfCode,
    newLines = oldMethod.WasRemoved() ? 0 : newMethod.NbLinesOfCode,
    oldInstructions = oldMethod.NbILInstructions,
    newInstructions = oldMethod.WasRemoved() ? 0 : newMethod.NbILInstructions
}

Methods with too many parameters that were totally improved or removed

// Methods with too many parameters that were totally improved or removed
from oldMethod in codeBase.OlderVersion().Methods
let newMethod = oldMethod.NewerVersion()
where 
    (
        oldMethod.NbParameters > 8
        &&
        (oldMethod.WasRemoved() || newMethod.NbParameters <= 8)
    )
orderby  oldMethod.NbParameters descending
select new
{
    method = oldMethod.WasRemoved() ? oldMethod : newMethod,
    oldParameters = oldMethod.NbParameters,
    newParameters = oldMethod.WasChanged() ? 0 : newMethod.NbParameters
}

Summary

Feel free to comment if you have any notes or suggestions regarding existing or adding new queries.

No comments:

Post a Comment