DEV Community

Cover image for 3 Code Metrics Every Developer Should Know
Milos Zivkovic
Milos Zivkovic

Posted on

3 Code Metrics Every Developer Should Know

Here are 3 high-quality code metrics.

Živković Miloš

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 yourUtilclass. This is yourManagerclass. 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. He even developed softwareto 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

2. What is the ABC metric?

The first mention of this metric was in an articleby 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://www.win.tue.nl/~wstomv/edu/2ip30/references/ABCmetric.pdfPg. 8 in 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

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. 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.

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

Getting Empirical about Refactoring

Breaking Up the Behemoth - Sandi Metz

Code Churn — A Magical Metric for Software Quality — DZone Performance

Photo byThisIsEngineeringfromPexels

Top comments (0)