예제 #1
0
        public const int ThresholdCyclomaticComplexity = 20; //higher for C# than Java due to LINQ

        public override ToxicityScore CalculateToxicity(Instance instanceToScore)
        {
            // Class Level Toxicity
            var linesOfCode        = ComputeToxicity(instanceToScore.LinesOfCode, ThresholdLinesOfCode);
            var classCoupling      = ComputeToxicity(instanceToScore.ClassCoupling, ThresholdClassCoupling);
            var depthOfInheritance = ComputeToxicity(instanceToScore.DepthOfInheritance, ThresholdDepthOfInheritance);
            var numberOfMethods    = ComputeToxicity(instanceToScore.Members.Count, ThresholdNumberOfMethods);

            double cyclomaticComplexity = 0;
            double methodLength         = 0;

            // Method Level Toxicity
            foreach (var method in instanceToScore.Members)
            {
                cyclomaticComplexity += ComputeToxicity(method.CylomaticComplexity, ThresholdCyclomaticComplexity);
                methodLength         += ComputeToxicity(method.LinesOfCode, ThresholdMethodLength);
            }

            // Rationalize
            var score = new ToxicityScore
            {
                LinesOfCode          = Rationalize(linesOfCode),
                ClassCoupling        = Rationalize(classCoupling),
                DepthOfInheritance   = Rationalize(depthOfInheritance * DepthOfInheritanceFactor),
                NumberOfMethods      = Rationalize(numberOfMethods),
                MethodLength         = Rationalize(methodLength),
                CyclomaticComplexity = Rationalize(cyclomaticComplexity)
            };

            score.Toxicity = score.LinesOfCode + score.ClassCoupling + score.DepthOfInheritance +
                             score.NumberOfMethods + score.MethodLength + score.CyclomaticComplexity;

            return(score);
        }
예제 #2
0
        public override ToxicityScore CalculateToxicity(Instance instanceToScore)
        {
            // Class Level Toxicity
            var linesOfCode                  = ComputeToxicity(instanceToScore.LinesOfCode, ThresholdLinesOfCode);
            var numberOfMethods              = ComputeToxicity(instanceToScore.Members.Count, ThresholdNumberOfMethods);
            var innerClassAnonymous          = ComputeToxicity(instanceToScore.AnonymousInnerClassLength, ThresholdAnonymousInnerClassLength);
            var classDataAbstractionCoupling = ComputeToxicity(instanceToScore.ClassDataAbstractionCoupling, ThresholdClassDataAbstractionCoupling);
            var classFanOutComplexity        = ComputeToxicity(instanceToScore.ClassFanOutComplexity, ThresholdClassFanOutComplexity);

            // Method Level Toxicity
            double cyclomaticComplexity = 0;
            double methodLength         = 0;
            double missingDefaultCase   = 0;
            double booleanComplexity    = 0;
            double nestedIfDepth        = 0;
            double nestedTryDepth       = 0;
            double parameterNumber      = 0;

            foreach (var method in instanceToScore.Members)
            {
                cyclomaticComplexity += ComputeToxicity(method.CylomaticComplexity, ThresholdCyclomaticComplexity);
                methodLength         += ComputeToxicity(method.LinesOfCode, ThresholdMethodLength);
                missingDefaultCase   += ComputeToxicity(method.MissingDefaultCase, ThresholdDefaultCase);
                booleanComplexity    += ComputeToxicity(method.BooleanExpressionComplexity, ThresholdBooleanComplexity);
                nestedIfDepth        += ComputeToxicity(method.NestedIfDepth, ThresholdNestedIfDepth);
                nestedTryDepth       += ComputeToxicity(method.NestedTryDepth, ThresholdNestedTryDepth);
                parameterNumber      += ComputeToxicity(method.NumberOfParameters, ThresholdParameterNumber);
            }

            // Rationalize
            var score = new ToxicityScore
            {
                // class level
                LinesOfCode     = Rationalize(linesOfCode),
                NumberOfMethods = Rationalize(numberOfMethods),
                AnonInnerLength = Rationalize(innerClassAnonymous),
                ClassDataAbstractionCoupling = Rationalize(classDataAbstractionCoupling),
                ClassFanOutComplexity        = Rationalize(classFanOutComplexity),

                // method level
                MethodLength                = Rationalize(methodLength),
                CyclomaticComplexity        = Rationalize(cyclomaticComplexity),
                MissingDefaultCase          = Rationalize(missingDefaultCase),
                BooleanExpressionComplexity = Rationalize(booleanComplexity),
                NestedIfDepth               = Rationalize(nestedIfDepth),
                NestedTryDepth              = Rationalize(nestedTryDepth),
                NumberOfParameters          = Rationalize(parameterNumber)
            };

            score.Toxicity = score.LinesOfCode + score.NumberOfMethods +
                             score.ClassFanOutComplexity + score.ClassDataAbstractionCoupling +
                             score.AnonInnerLength + score.MethodLength + score.CyclomaticComplexity +
                             score.MissingDefaultCase + score.BooleanExpressionComplexity +
                             score.NestedIfDepth + score.NestedTryDepth + score.NumberOfParameters;

            return(score);
        }
예제 #3
0
        public override ToxicityScore CalculateToxicity(Instance instanceToScore)
        {
            // Class Level Toxicity
            var linesOfCode        = ComputeToxicity(instanceToScore.LinesOfCode, ThresholdLinesOfCode);
            var numberOfMethods    = ComputeToxicity(instanceToScore.Members.Count, ThresholdNumberOfMethods);
            var methodLength       = 0d;
            var numberOfParameters = 0d;
            var nestedIfDepth      = 0d;
            var missingDefaultCase = 0d;
            var noFallThrough      = 0d;

            double cyclomaticComplexity = 0;

            // Method Level Toxicity
            foreach (var method in instanceToScore.Members)
            {
                cyclomaticComplexity += ComputeToxicity(method.CylomaticComplexity, ThresholdCyclomaticComplexity);
                methodLength         += ComputeToxicity(method.LinesOfCode, ThresholdMethodLength);
                numberOfParameters   += ComputeToxicity(method.NumberOfParameters, ThresholdNumberOfParameters);
                nestedIfDepth        += ComputeToxicity(method.NestedIfDepth, ThresholdNestedIfDepth);
                missingDefaultCase   += ComputeToxicity(method.MissingDefaultCase, ThresholdMissingDefaultCase);
                noFallThrough        += ComputeToxicity(method.NoFallthrough, ThresholdNoFallthrough);
            }

            // Rationalize
            var score = new ToxicityScore
            {
                LinesOfCode          = Rationalize(linesOfCode),
                NumberOfMethods      = Rationalize(numberOfMethods),
                MethodLength         = Rationalize(methodLength),
                CyclomaticComplexity = Rationalize(cyclomaticComplexity),
                NumberOfParameters   = Rationalize(numberOfParameters),
                NestedIfDepth        = Rationalize(nestedIfDepth),
                MissingDefaultCase   = Rationalize(missingDefaultCase),
                SwitchNoFallThrough  = Rationalize(noFallThrough)
            };

            score.Toxicity = score.LinesOfCode + score.NumberOfMethods +
                             score.CyclomaticComplexity + score.NumberOfParameters + score.MethodLength +
                             score.NestedIfDepth + score.MissingDefaultCase + score.SwitchNoFallThrough;

            return(score);
        }