28. June 2023
Ah, the famous 80% code coverage - the golden number that many developers I've encountered seem to believe has magical powers to keep all the coding gremlins away. It means that 80% of the new code should be put to the test, as if saying, “Prove your worth, code!” This bar is high, no doubt, and is set to make sure that most of our codebase is given a good check.
However, let’s not get too starry-eyed about this 80%. Sometimes, depending on the type of project or part of the code, it’s like trying to fit a square peg in a round hole. It just doesn’t work! In such cases, it’s smarter to focus on creating solid, quality tests rather than running a race to hit that 80%.
Now, when deciding how much testing is just right, we need to think about complexity and risk. Imagine them as partners in crime – when complexity goes up, risk is right there with it. That means more chances of things going wrong. To set the right target for code coverage, you have to keep an eye on how complicated the code is. The SonarQube Coverage Overview report is like a helpful friend in this mission. It’s easy on the eyes and shows the connection between code coverage and complexity.
Here’s the quick tour of what the report shows:
- Bottom-right corner: This is where complex but well-tested code hangs out. Think of it like a tough puzzle that’s been solved.
- Bottom-left corner: The dream zone! Code here is simple and has been tested a lot. It’s where you’d want most of your code to be.
- Top-right corner: Red alert area! Code here is like a tangled mess and hasn’t been tested enough. It’s risky and needs attention ASAP.
- Top-left corner: This area has simple code, but it hasn’t been tested much. It’s not ideal, but not a total disaster.
So, in summary, this report is like a handy map for your code. It helps you see what needs your attention and testing effort. This way, you can make sure your software is in good shape. But always remember, 80% is a guiding light, not a magic spell!