3 Code Metrics Every Developer Should Know
Tags: refactoring
Published: 2021-03-28 16:44:51

## Here are 3 high-quality code metrics.


[Živković Miloš](https://zivce.medium.com/)



We need code metrics, to pinpoint smelly code. We all know bad code when we see one. Even so, sometimes it slips through.

Bad code resides in big codebases where developers come and go.

The leaving developer doesn’t care, does a bad job, and leaves. You join the team and encounter a code mess.

You need valuable code metrics to combat bad code.

Even when you see team members writing bad code, you need to point it out. It will, sooner or later, end up on your plate.

# 1\. What is code churning?

> Churn is a measure of how often a file changes. Files that change more have higher churn. — Sandi Metz


Complex code with most changes lives in the top right corner. This is your`Util`class. This is your`Manager`class. This is where your core logic resides, so no one wants to touch it.

Either way, you make changes, causing bugs and tech debt along the way.

Michael Feathers [talks about this code metric](https://www.stickyminds.com/article/getting-empirical-about-refactoring). He even developed [software](http://www.campwoodsw.com/sourcemonitor.html)to find these churned files. These are the best candidates for refactoring.

When can we use this metric? Use it in your technical grooming sessions. Pinpoint which most churned class, evaluate tech debt, and start refactoring.

Most software quality measuring is static. Sonarqube, lines of code, and other code quality metrics. Code churn metric adds another dimension with version control.

Code churning is done in big classes. We get pulled into it, as Sandi says, it has its own gravitational force.

Why add a small helper class, when you add logic in a 500 line class? We are all guilty of this behavior. The need to iterate faster leads to more code churn.

More on code churning implementation in the article below.

[Why I Made My Own Code Quality Tool, Attractor](https://betterprogramming.pub/why-i-made-my-own-code-quality-tool-c44b40ceaafd)

# 2\. What is the ABC metric?

The first mention of this metric was in [an article](https://www.softwarerenovation.com/ABCMetric.pdf)by Jerry Fitzpatrick. This is an extension of cyclomatic complexity.

Not only Conditions make up this metric, but also Assignments and Branches. Hence the name Assignments, Branches, Conditions metric.

> ABC is a measure of complexity. — Sandi Metz

ABC metric tells more about the code complexity. If the score is high, your code is complex.

For the sake of this article, I’ll post the formula. For those math nerds, reading this here is the formula. There are few tools that calculate this metric, and you can find them in the wilderness.

|ABC| = sqrt((A*A)+(B*B)+(C*C)) - calculating ABC score

> I am a Java developer. How can I count the As, Bs, and Cs in Java?

![](https://miro.medium.com/max/419/1*hXSpK6RCiqm8bOCAA59F7A.png)

[https://www.win.tue.nl/\~wstomv/edu/2ip30/references/ABCmetric.pdf](https://www.win.tue.nl/~wstomv/edu/2ip30/references/ABCmetric.pdf)—[Pg. 8 in PDF](https://www.win.tue.nl/~wstomv/edu/2ip30/references/ABCmetric.pdf)

Why should we care about this metric? What are the benefits of it?

Humans make mistakes, metrics don’t._You can’t manage what you can’t measure._Use metrics to improve. Metrics won’t lie.

# 3\. What is cyclomatic complexity

> An algorithm that counts the number of unique execution paths through a body of source code. — [Thomas J. McCabe](http://www.literateprogramming.com/mccabe.pdf)

You’ve seen code with a lot of if-else blocks. Example of bad cyclomatic complexity, or high score of cyclomatic complexity.

Writing OO programs leads to reduced cyclomatic complexity. Extract conditional logic into separate classes. Then inject it into your class.

I wrote more on how to reduce this metric [here](https://levelup.gitconnected.com/3-object-oriented-tips-sandi-metz-uses-for-better-software-design-1c5393c7698d). Adhering to OO principles vastly reduces this metric.

When you face code with a cyclomatic complexity score of 10, you should refactor.

McCabe states developers who knew structured programming, wrote optimal code. Even before he invented the metric. I wrote about developing structured programs [here](https://levelup.gitconnected.com/how-to-develop-better-program-structure-3a57f48bf971).

Code with high cyclomatic complexity is hard to test. By definition, tests should cover all execution paths. Think of code with a cyclomatic complexity of 50, you’d need 50 test cases.

Sandi Metz points the reverse way of using this metric. Use it to know if you’ve written enough tests. For example, you have a code with a complexity score of 40 but only 20 tests. You need more tests to cover all paths.

# Conclusion

Code metrics are used to navigate us through the project. They tell us where the stinky code part is living. Where we need to clean up.

No one says you need them. For your hobby project, local gym, or MVP they might not be of benefit.

Things get interesting if the project succeeds. Codebase grows bigger, and then they are essential for the life of your project.

I use metrics, at least the cyclomatic complexity, to break up my code. This leads to testable code and reasonable code chunks.

You should take away at least one metric, and implement it today.

# Resources

[99 Bottles Sample PHP - Sandi Metz](https://sandimetz.com/99bottles-sample-php)

[Getting Empirical about Refactoring](https://www.stickyminds.com/article/getting-empirical-about-refactoring)

[Breaking Up the Behemoth - Sandi Metz](https://sandimetz.com/blog/2017/9/13/breaking-up-the-behemoth)

[Code Churn — A Magical Metric for Software Quality — DZone Performance](https://dzone.com/articles/code-churn-a-magical-metric-for-software-quality)


Photo by[ThisIsEngineering](https://www.pexels.com/@thisisengineering?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)from[Pexels](https://www.pexels.com/photo/female-engineer-with-equipment-3862631/?utm_content=attributionCopyText&utm_medium=referral&utm_source=pexels)