Bot

run a code quality check over any C# application

Here two code quality checks are performed:

one for naming conventions and another for excessive complexity.

The IsPascalCase method checks if a given name follows PascalCase naming convention. You can modify this method to implement your own naming convention rules if needed.

The CalculateCyclomaticComplexity method calculates the cyclomatic complexity of a given syntax node. In this example, it counts various control flow statements such as 'if', 'while', 'do', and 'for', code sample below:

using System;

using System.Linq;

using Microsoft.CodeAnalysis;

using Microsoft.CodeAnalysis.CSharp;

using Microsoft.CodeAnalysis.CSharp.Syntax;


namespace CodeQualityChecker

{

    class Program

    {

        static void Main(string[] args)

        {

            // Your existing code for getting the syntax tree and semantic model


            Console.WriteLine("Code quality check results:");


            var classes = root.DescendantNodes().OfType<ClassDeclarationSyntax>();

            foreach (var cls in classes)

            {

                var symbol = semanticModel.GetDeclaredSymbol(cls);


                // Check for naming conventions

                if (!IsPascalCase(symbol.Name))

                {

                    Console.WriteLine($"Class '{symbol.Name}' does not follow PascalCase naming convention.");

                }


                // Check for excessive complexity

                var complexity = CalculateCyclomaticComplexity(cls);

                if (complexity > 10)

                {

                    Console.WriteLine($"Class '{symbol.Name}' has a cyclomatic complexity of {complexity}. Consider refactoring for better maintainability.");

                }

            }


            var methods = root.DescendantNodes().OfType<MethodDeclarationSyntax>();

            foreach (var method in methods)

            {

                var symbol = semanticModel.GetDeclaredSymbol(method);


                // Check for naming conventions

                if (!IsPascalCase(symbol.Name))

                {

                    Console.WriteLine($"Method '{symbol.Name}' does not follow PascalCase naming convention.");

                }


                // Check for excessive complexity

                var complexity = CalculateCyclomaticComplexity(method);

                if (complexity > 5)

                {

                    Console.WriteLine($"Method '{symbol.Name}' has a cyclomatic complexity of {complexity}. Consider refactoring for better maintainability.");

                }

            }


            // Additional checks for namespaces and interfaces


            Console.WriteLine("Code quality check completed.");

        }


        static bool IsPascalCase(string name)

        {

            return !string.IsNullOrEmpty(name) && char.IsUpper(name[0]);

        }


        static int CalculateCyclomaticComplexity(SyntaxNode node)

        {

            var complexity = 1;

            complexity += node.DescendantNodes().OfType<IfStatementSyntax>().Count();

            complexity += node.DescendantNodes().OfType<WhileStatementSyntax>().Count();

            complexity += node.DescendantNodes().OfType<DoStatementSyntax>().Count();

            complexity += node.DescendantNodes().OfType<ForStatementSyntax>().Count();

            // Add more control flow statements as needed


            return complexity;

        }

    }

}


Comments