Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Calculate C.R.A.P. Score from Cobertura file? #641

Closed
rikrak opened this issue Jan 5, 2024 · 7 comments · Fixed by #642
Closed

Calculate C.R.A.P. Score from Cobertura file? #641

rikrak opened this issue Jan 5, 2024 · 7 comments · Fixed by #642

Comments

@rikrak
Copy link

rikrak commented Jan 5, 2024

Not so much a bug as a feature request/question.

When processing Cobertura files, would it be possible to also calculate the crap score? Corbertura itself doesn't provide a value for this metric, but it looks possible to calculate it from the Cyclomatic Complexity and Coverage values for each method using:
CRAP(m) = CC(m)^2 * U(m)^3 + CC(m)
Where
CC(m) = Cyclomatic Complexity (e.g. 5)
U(m) = Uncovered percentage (e.g. 30% = 0.3)

I've played about with the code locally and produced a report from cobertura that does this. Just wondering if this is worth working into a PR?

@rikrak
Copy link
Author

rikrak commented Jan 5, 2024

For info, here's a snippet:

    // CoberturaParser.cs

        private static void SetMethodMetrics(CodeFile codeFile, IEnumerable<XElement> methodsOfFile)
        {
            foreach (var method in methodsOfFile)
            {
                string fullName = method.Attribute("name").Value + method.Attribute("signature").Value;
                fullName = ExtractMethodName(fullName, method.Parent.Parent.Attribute("name").Value);

               // ... abridged for clarity

                if (cyclomaticComplexity.HasValue && coveragePercent.HasValue)
                {
                    // https://testing.googleblog.com/2011/02/this-code-is-crap.html
                    // CRAP(m) = CC(m)^2 * U(m)^3 + CC(m)
                    // CC(m) <= Cyclomatic Complexity (e.g. 5)
                    // U(m) <= Uncovered percentage (e.g. 30% = 0.3)
                    var uncoveredPercent = (100f - (double)coveragePercent.Value) / 100.0;
                    var complexity = (double)cyclomaticComplexity.Value;
                    var crapScore = (Math.Pow(complexity, 3.0) * Math.Pow(uncoveredPercent, 2)) + complexity;

                    metrics.Insert(0, Metric.CrapScore((decimal)crapScore));
                }

               // ... abridged for clarity
            }
        }

@danielpalme
Copy link
Owner

@rikrak
That sound great!
If you like, you can create a PR, but I'm also able to use your snippet to add this metric.
Just let me know how you would like to handle it.

@rikrak
Copy link
Author

rikrak commented Jan 8, 2024

I'll put together a PR :-)

@rikrak
Copy link
Author

rikrak commented Jan 8, 2024

I've submitted a PR. I'm quite new to contributing to OS projects, so let me know if the PR needs adjusting in any way :-)

@danielpalme
Copy link
Owner

Thank you!

@danielpalme
Copy link
Owner

Release 5.2.1 is now available containing the new metric!

@rikrak
Copy link
Author

rikrak commented Feb 5, 2024

Terrific, I'll check it out tomorrow :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants