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. java_String类、StringBuilder类、Arrays类、Math类的使用

    String类 java.lang.String 类代表字符串.Java程序中所有的字符串文字(例如 “abc” )都可以被看作是实现此类的实例 构造方法 java.lang.String :此类不需 ...

  2. shell脚本的常用执行方式

    1.sh+脚本的相对路径 [jinghang@hadoop101 datas]$ sh helloworld.sh helloworld sh+脚本的绝对路径 [jinghang@hadoop101 ...

  3. 【JavaScript】windows.open用法详解

    windows.open("URL","窗口名称","窗口外观设定");的用法详解 function onNewWindows(redire ...

  4. 释放DT时代释放金融数据价值,驱动金融商业裂变

    摘要:客户微细分模型上线华为云ModelArts,看如何以AI科技挖掘金融数据价值. 当前信息化浪潮席卷全球,新一轮的科技革命和产业革命推动金融行业发展到全新阶段.人工智能2.0时代,智慧金融方兴未艾 ...

  5. 《闲扯Redis十》Redis 跳跃表的结构实现

    一.前言 Redis 提供了5种数据类型:String(字符串).Hash(哈希).List(列表).Set(集合).Zset(有序集合),理解每种数据类型的特点对于redis的开发和运维非常重要. ...

  6. golang 总结库

    前言 这个是用来进行总结学习的,相当于自学笔记 记录的东西,是随时更新的, 有些东西,可能就是记录下,并不一定代表他一定能解决问题 不要做纯粹的文字的搬运工,要多做灵感整理 我看文章会看好多,所以常常 ...

  7. Hibernate搭建案例步骤:

    5.1引入依赖: <!--hibernate core--> <dependency> <groupId>org.hibernate</groupId> ...

  8. 计算机网络要点---TCP

    计算机网络要点---TCP 浏览器在通过域名通过dns服务器找到你的服务器外网ip,将http请求发送到你的服务器,在tcp3次握手之后(http下面是tcp/ip),通过tcp协议开始传输数据,你的 ...

  9. Android Studio上传项目到GitHub出错

    上传代码到Github出错: 一.github push文件过大(超过50M会有警告,超出100M就会被限制) error: GH001: Large files detected. this exc ...

  10. 微信小程序之蓝牙广播信息

    期初第一次做蓝牙开锁的时候遇到的最尖锐的问题就是ios设备如何对获取的广播信息进行读取,大概用了4中方式,都无法解决,最后不得不求助官方人员.给了一个方法,大家可以参考.在此附图: 由于mac地址是6 ...