Unit Tests Correlate With Desirable Codebase Properties
Tags: architecture, dotnet, csharp, unittest
Published: 2020-04-09 14:02:16

---
title: Unit Tests Correlate With Desirable Codebase Properties
published: true
description: I did a series of data-driven studies about codebases.  Here's one where I wrapped a look at what you see in codebases that have unit tests.
tags: #architecture #dotnet #csharp #unittest
canonical_url: https://blog.ndepend.com/unit-tests-desirable-codebase-properties/
---
<em>I originally <a href="https://blog.ndepend.com/unit-tests-desirable-codebase-properties/">posted this on the NDepend blog</a> about two years ago.  <a href="https://www.ndepend.com/">NDepend</a> is a static analysis tool that plugs right into Visual Studio, and it was what I used to gather and record this data.</em>

Today, I give you the third post in a series about how unit tests affect codebases.

<a href="https://blog.ndepend.com/wp-content/uploads/Unit-Tests-Correlate-With-Desirable-Codebase-Properties-01-960x460.png"><img class="alignnone size-full wp-image-1292" src="https://blog.ndepend.com/wp-content/uploads/Unit-Tests-Correlate-With-Desirable-Codebase-Properties-01-960x460.png" alt="" width="614" height="762" /></a>

The <a href="https://blog.ndepend.com/unit-testing-affect-codebases/">first one</a> wound up getting a lot of attention, which was fun.  In it, I presented some analysis I'd done of about 100 codebases.  I had formed hypotheses about how I thought unit tests would affect codebases, and then I tested those hypotheses.

In <a href="https://blog.ndepend.com/unit-test-effect-study-refined/">the second post</a>, I incorporated a lot of the feedback that I had requested in the first post.  Specifically, I partnered with someone to do more rigorous statistical analysis on the raw data that I'd found.  The result was much more clarity about not only the correlations among code properties but also how much confidence we could have in those relationships.  Some had strong relationships while others were likely spurious.

In this post, though, I'm incorporating the single biggest piece of feedback.  I'm analyzing more codebases.

<h2>Analysis of 500 (ish) C# Codebases</h2>

Performing static analysis on and recording information about 500 codebases isn't especially easy.  To facilitate this, I've done significant work automating ingestion of codebases:
<ul>
 	<li>Enabling autonomous batch operation</li>
 	<li>Logging which codebases fail and why</li>
 	<li>Building in redundancy against accidentally analyzing the same codebase twice.</li>
 	<li>Executing not just builds but also NuGet package restores and other build steps.</li>
</ul>
That's been a big help, but there's still the matter of <em>finding </em>these codebases.  To do that, I mined a handful of "awesome codebase" lists, <a href="https://github.com/quozd/awesome-dotnet">like this one</a>.  I pointed the analysis tool at something like 750 codebases, and it naturally filters out any that don't compile or otherwise have trouble in the automated process.

This left me with 503 valid codebases.  That number came down to 495 once adjusted for codebases that, for whatever reason, didn't have any (non-third party) methods or types or that were otherwise somehow trivial.

So the results here are the results of using NDepend for static analysis on 495 C# codebases.

<h2>Stats About the Codebases</h2>

Alright.  So what happened with the analysis?  I'll start with some stats that interested me and hopefully interest you.  I'm looking here to offer some perspective.
<ul>
 	<li>I analyzed a total of 6,273,547 logical lines of code (LLOC).  That's a lot of code!  (Note: codebases have fewer LLOC than editor LOC.  The latter is probably how you're used to reasoning about lines of code.)</li>
 	<li>The mean codebase size was 12,674 and the median size was 3,652, so a handful of relative monsters dragged the mean value pretty high.</li>
 	<li>The maximum codebase size was 451,706 LLOC.</li>
 	<li>The percent of codebases with 50% or more test methods was 3.4%, which is pretty consistent with the 100 codebase analysis.</li>
 	<li>The percent of codebases with 40% or more test methods, however, was 9.1%, which was up from about 7% in the last analysis.  So, it appears that we're getting a little more test-heavy codebase representation here.</li>
</ul>
<h2>Findings From Last Time</h2>

Here's a quick recap of some of the findings from last time around.
<ul>
 	<li><span style="color: #ff9900;">Average method cyclomatic complexity seemed unrelated to prevalence of unit tests.</span></li>
 	<li><span style="color: #ff9900;">Average method nesting depth also seemed unrelated to the prevalence of unit tests.</span></li>
 	<li><span style="color: #ff0000;">More lines of code per method probably correlated with more unit tests, surprisingly.</span></li>
 	<li><span style="color: #339966;">Lines of code per constructor decreased with an increase in unit tests, but the p-value was a little iffy.</span></li>
 	<li><span style="color: #339966;">Parameters per method decreased as unit tests increased and with pretty definitive p-value.</span></li>
 	<li><span style="color: #339966;">Number of overloads per method possibly had a negative relationship with unit test prevalence.</span></li>
 	<li><span style="color: #339966;">More inheritance correlated with fewer unit tests, fairly decisively.</span></li>
 	<li><span style="color: #339966;">Type cohesion correlated strongly with an increase in the unit test percentage.</span></li>
</ul>
I've omitted a few of the things I studied in the previous posts, both for the sake of brevity and in order to focus on what I think of as properties of clean codebases.  Generally speaking, you want code with fewer lines, less complexity, fewer parameters, fewer overloads, and less nesting per method.  In terms of types, you want a flat inheritance hierarchy and more cohesion.

<h2>What a Difference 400 Codebases Makes</h2>

So, let's take a look at what happens now that we substantially increased sample size.  I'll summarize here and add a couple of screenshots below that.
<ul>
 	<li><span style="color: #339966;">Average method cyclomatic complexity has a strong negative correlation with prevalence of unit tests!</span></li>
 	<li><span style="color: #339966;">Average method nesting depth now correlates negatively with more unit tests, though p-value isn't perfect.</span></li>
 	<li><span style="color: #ff9900;">More lines of code per method flattened and saw a p-value spike, changing this to "probably no relationship."</span></li>
 	<li><span style="color: #339966;">Lines of code per constructor decreased even more with more unit tests, and p-value became bulletproof.</span></li>
 	<li><span style="color: #339966;">Parameters per method, like lines of code per constructive, became an even more bulletproof negative correlation.</span></li>
 	<li><span style="color: #ff9900;">Number of overloads per method became flatter and p-value worse, so I'm going to say a relationship here isn't overly likely.</span></li>
 	<li><span style="color: #339966;">More inheritance still correlated with fewer unit tests, but p-value is now non-trivial and the relationship flattened.</span></li>
 	<li><span style="color: #339966;">Type cohesion's relationship didn't change very much at all.</span></li>
</ul>
<h4> Average Cyclomatic Complexity Per Method</h4>
<a href="https://blog.ndepend.com/wp-content/uploads/CyclomaticComplexity.jpg"><img class="alignnone size-full wp-image-1292" src="https://blog.ndepend.com/wp-content/uploads/CyclomaticComplexity.jpg" alt="" width="614" height="762" /></a>
<h4>Average Method Nesting Depth</h4>
<a href="https://blog.ndepend.com/wp-content/uploads/NestingDepth.jpg"><img class="alignnone size-full wp-image-1293" src="https://blog.ndepend.com/wp-content/uploads/NestingDepth.jpg" alt="" width="610" height="613" /></a>
<h4>Lines of Code Per Method</h4>
<a href="https://blog.ndepend.com/wp-content/uploads/LinesOfCodePerMethod.jpg"><img class="alignnone size-full wp-image-1291" src="https://blog.ndepend.com/wp-content/uploads/LinesOfCodePerMethod.jpg" alt="" width="610" height="773" /></a>
<h4>Number of Overloads Per Method</h4>
<a href="https://blog.ndepend.com/wp-content/uploads/Overloads.jpg"><img class="alignnone size-full wp-image-1294" src="https://blog.ndepend.com/wp-content/uploads/Overloads.jpg" alt="" width="612" height="750" /></a>
<h3>Unit Tests and Clean Code</h3>
If I circle back to my original hypotheses,  it seems I'm doing better as I add more codebases to the study.

With 500 codebases in the mix, the results have improved considerably, though I'm not entirely sure why.  Perhaps some outliers skewed the original study a bit more, or perhaps this resulted from the codebase corpus on the whole becoming more "unit-test heavy."  But whatever the reason, five times the sample size is starting to show some pretty definitive results.

The properties that we associate with clean code --- cohesion, minimal complexity, and overall thematic simplicity --- seem to show up more as unit tests show up more.

The only exception that truly surprises me was and remains lines of code per method.  I wonder if this might be the result of a higher prevalence of properties in non-test-heavy codebases or some other common relationship situation.  In any case, though, it's interesting.

But 500 codebases analyzed automatically and results synthesized with statistical modeling software, I feel pretty good about where this study is.  And while it doesn't paint a "unit tests make everything rainbows and unicorns" picture, this study now demonstrates, pretty definitively, that codebases with unit tests also have other desirable properties.
<h2>What's Next?</h2>
I'm going to keep working, in conjunction with the person doing the statistical models, to study more properties of codebases.  And I think, for now, I'm going to wrap this unit test study and move on to other things, satisfied that we've given it a pretty good treatment.

One thing that occurs to me is the somewhat important differences between 100 codebases and 500.  Maybe I should grow the corpus to 1,000 or even 2,500 to make sure I don't see a similar reversal.  But the thing is, that's a <em>lot </em>of codebases, and I've already nearly exhausted the "awesome lists," so I'm worried about diminishing returns.  Rest assured, though --- I'll keep slurping down codebases, and if I find myself with significantly more at some point, we'll redo the analysis.

So what's next?  I've had a few ideas and am brainstorming more of them.
<ul>
 	<li>See what effect having CQRS seems to have on a codebase.</li>
 	<li>Skinny or fat domain objects?  Which is better?</li>
 	<li>How does functional style programming impact codebases?</li>
 	<li>How does a lot of async logic affect codebases?</li>
 	<li>Or what about lots of static methods?</li>
</ul>
These are just some ideas. Weigh in below in the comments with your own, if you'd like.  Hopefully you all find this stuff as interesting as I do!

<em>If you want to take a Freakonomics style look at your codebase by turning code into data, you can <a href="https://www.ndepend.com/downloadcn?utm=blog">take NDepend for a 14 day spin</a>.</em>