Once I was asked to enhance the sonarcube coverage of the class:‘jp.co.XXXXp.DltApiHttpRequestRetryHandler’ as below:

public class DltApiHttpRequestRetryHandler implements HttpRequestRetryHandler {
private PropertiesConfiguration config = BusinessConfigUtil.getConfiguration();
private Logger logger = LoggerFactory.getLogger(getClass()); @Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
logger.warn("(Could not execute request. Execution count) : {}.\n{}", executionCount, exception.getMessage());
if (executionCount >= Integer.parseInt(config.getString("dlt.api.request.max.count"))) {
return false;
}
return true;
}
}

It's current coverage is 33.3%, and the target is 85%.

Firstly, I tried to modify as below:(1st Modification)

public class DltApiHttpRequestRetryHandler implements HttpRequestRetryHandler {

    private PropertiesConfiguration config = BusinessConfigUtil.getConfiguration();
private Logger logger = LoggerFactory.getLogger(getClass()); @Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
logger.warn("(Could not execute request. Execution count) : {}.\n{}", executionCount, exception.getMessage()); int maxCount=0;
try {
String strMaxCount=config.getString("dlt.api.request.max.count");
maxCount=Integer.parseInt(strMaxCount);
}catch(NoSuchElementException ex) {
throw new BatchApplicationException(ex.getLocalizedMessage());
}catch(java.lang.NumberFormatException ex) {
throw new BatchApplicationException(ex.getLocalizedMessage());
} return executionCount<maxCount;
} }

And after rebuilding, sonarcube told me the coverage was lowered to 20%!

I realized that more branches will bring lower coverage, on the contrary, less branches will enhance the coverage, that is a effective way!

Next,I simplified code as below:(2nd modification)

public class DltApiHttpRequestRetryHandler implements HttpRequestRetryHandler {
private PropertiesConfiguration config = BusinessConfigUtil.getConfiguration();
private Logger logger = LoggerFactory.getLogger(getClass()); @Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
logger.warn("(Could not execute request. Execution count) : {}.\n{}", executionCount, exception.getMessage()); return executionCount<Integer.parseInt(config.getString("dlt.api.request.max.count"));
}
}

According to expectation, the coverage was enhanced to 42.3%, but NOT as expected,the rate is not 100% or 0%.

In my view, there is no branch in the 2nd modification so that the coverage rate should be 100% or 0% because either it was invoked, or it will never be run.

How was 42.3% calculated? This makes me confused.

Conclusion:

As we all know, the three codes are same indeed, the different sonar-cude coverage rates can't change the reality!

I think sonar-cude is like a black-box, in which there are something we don't know, maybe something in it is unreasonable and unreliable.

Obviously, the 1st code is more readable and robuster, but for it's lower coverage and need more test-cases(Not easy to add, you know,sometimes the branches can'r be covered), the coder will avoid writing like this.

And there are too many functions in one line in 2nd code,it is a bad smell that so many rules told us. But for higher coverage and less test-case, the coder will tend to do so. That will result in violation of proper code style.

Better code style or higher coverage, which one we should choose? In my opinion, I prefer the former.

So I propose the coverage rate should not be the unique evidence to judge a piece of code and be the final target of coding.

Do you agree?

The relationship between Sonarcube coverage and code branch的更多相关文章

  1. 10 Code Coverage Tools for C & C++

    Code coverage is a measure used in software testing that describes the degree to which the source co ...

  2. Gumshoe - Microsoft Code Coverage Test Toolset

    Gumshoe - Microsoft Code Coverage Test Toolset 2014-07-17 What is Gumshoe? How to instrument a binar ...

  3. Code alignment 代码对齐改进(VS2017)

    In mathematics you always keep your equals lined up directly underneath the one above. It keeps it c ...

  4. EntityFramework Code-First 简易教程(二)-------Code First约定

    Code First 约定 在前一篇中,我们已经知道了EF Code-First怎样从模型类(domain classes)中创建数据库表,下面,我们开始学习默认的Code-First约定. 什么是约 ...

  5. PHPUnit 手册

    PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015 ...

  6. PHPUnit 手册(转)

    PHPUnit 手册 PHPUnit 手册 Sebastian Bergmann 版权 © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012, 2013, ...

  7. 读书笔记-Software Testing(By Ron Patton)

    Software Testing Part I:The Big Picture 1.Software Testing Background Bug's formal definition 1.The ...

  8. 安装tensorflow,那叫一个坑啊

    最近,项目团队需要研究并应用AI的技术,在具体的产品实施环节中使用.之前的几个项目,是委托武汉大学给做的,基于keras框架,实现了一些图像识别的项目. 这不,上方希望自己能够掌握一些常用且成熟的AI ...

  9. [转] org.scalatest.FunSuite Scala Examples - Scala FunSuite 测试的例子

    [From]  https://www.programcreek.com/scala/org.scalatest.FunSuite org.scalatest.FunSuite Scala Examp ...

随机推荐

  1. C#LeetCode刷题-记忆化

    记忆化篇 # 题名 刷题 通过率 难度 329 矩阵中的最长递增路径   31.0% 困难

  2. CSS卡片右上角标记样式设计

    template <div class="each-one-in-list"> <div class="show-icon">进行中&l ...

  3. 后端排序时去掉element表格排序的null状态

    经常会遇到远程排序,需要去掉null状态的排序,当设置sortable='custom'时,设置sort-orders为['ascending', 'descending']是不生效的.然后查到了一种 ...

  4. HTML基础-05

    字体 文本颜色:color:red;字体分类: 衬线字体serif --字体宽度各异,有衬线 --Times.Georgia.宋体 无衬线字体sans-serif --字体宽度各异,无衬线 --Hel ...

  5. bluecms v1.6 代码审计

    0x01 使用seay源代码审计系统进行审计 扫描到了很多个可疑漏洞,不过工具都有一定的误报,下面我们就逐个进行验证 0x02 /ad_js.php SQL注入漏洞 查看源码,我们发现程序通过GET方 ...

  6. 洛谷 P6218 [USACO06NOV] Round Numbers S

    洛谷 P6218 [USACO06NOV] Round Numbers S 题目描述 如果一个正整数的二进制表示中,\(0\) 的数目不小于 \(1\) 的数目,那么它就被称为「圆数」. 例如,\(9 ...

  7. go module 获取码云私有仓库代码

    因为码云免费组织有5人限制,其他人想获得代码 只能通过别的方式 go mod 底层使用的git 获取代码, 所以首先解决如何通过git clone代码 思路为通过ssh密钥的方式获取 首先在码云仓库部 ...

  8. 第2章 RDD编程(2.1-2.2)

    第2章 RDD编程 2.1 编程模型 在Spark中,RDD被表示为对象,通过对象上的方法调用来对RDD进行转换.经过一系列的transformations定义RDD之后,就可以调用actions触发 ...

  9. python 用 prettytable 输出漂亮的表格

    原文链接:https://linuxops.org/blog/python/prettytable.html #!/usr/bin/python #**coding:utf-** import sys ...

  10. 如何寻找决策最优解?熵权TOPSIS助你科学决策

    熵权topsis是一种融合了熵值法与TOPSIS法的综合评价方法.熵值法是一种客观赋值法,可以减少主观赋值带来的偏差:而topsis法是一种常见的多目标决策分析方法,适用于多方案.多对象的对比研究,从 ...