Click or drag to resize

Metrics

Examples of calculating metrics for a code object tree.

Calculating metrics
C#
Solution solution = Solution.Load("Nova.Examples.sln");
if (solution != null)
{
    // Basic metrics can be calculated for any individual code object by calling ".CalculateMetrics()" on it.

    // Here's an example of some metrics for an entire Solution:
    Metrics metrics = solution.CalculateMetrics();
    Log.WriteLine(string.Format("Solution '{0}': {1:N0} projects; {2:N0} files; {3:N0} lines; {4:N0} SLOC; {5:N0} types; {6:N0} code objects; {7:N2} code objects per SLOC",
        solution.Name, metrics.Projects, metrics.Files, metrics.Lines, metrics.SLOC, metrics.Types, metrics.CodeObjects, metrics.CodeObjectsPerSLOC));

    Project project = solution.FindProject("Nova.Examples");
    if (project != null)
    {
        CodeUnit codeUnit = project.FindCodeUnit("Program.cs");
        if (codeUnit != null)
        {
            // Here's an example of some metrics for an individual source file:
            metrics = codeUnit.CalculateMetrics();
            Log.WriteLine(string.Format("CodeUnit '{0}': {1:N0} lines; {2:N0} SLOC; {3:N0} types; {4:N0} code objects; {5:N2} code objects per SLOC",
                codeUnit.Name, metrics.Lines, metrics.SLOC, metrics.Types, metrics.CodeObjects, metrics.CodeObjectsPerSLOC));

            var programClass = project.Find("Nova.Examples.Program") as ClassDecl;
            if (programClass != null)
            {
                var thisMethod = programClass.FindFirst<MethodDecl>("CalculateMetrics");
                if (thisMethod != null)
                {
                    // Here's an example of some metrics for a method:
                    metrics = thisMethod.CalculateMetrics();
                    Log.WriteLine(string.Format("Method '{0}': {1:N0} lines; {2:N0} SLOC; {3:N0} code objects; {4:N2} code objects per SLOC",
                        thisMethod.Name, metrics.Lines, metrics.SLOC, metrics.CodeObjects, metrics.CodeObjectsPerSLOC));
                }
            }
        }
    }
}