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. js 事件对象相关笔记

    事件对象     event就是一个事件对象 写到我们的监听函数的括号里面 当形参来看     事件对象只有有了事件才存在,他是系统给我们自动创建的 不需要我们传递参数     事件对象是我们事件的一 ...

  2. 12、Java 正则表达式

    简介 用来描述或者匹配一系列符合某个语句规则的字符串 正则表达式定义了字符串的模式. 正则表达式可以用来搜索.编辑或处理文本. 正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别. 一.正则 ...

  3. sourcetree关于注册的问题

    当前只有Win的版本,Mac自行百度(笑) 很多人用git命令行不熟练,那么可以尝试使用sourcetree进行操作. 然鹅~~sourcetree又一个比较严肃的问题就是,很多人不会跳过注册或者操作 ...

  4. Spring Boot 2.x基础教程:使用集中式缓存Redis

    之前我们介绍了两种进程内缓存的用法,包括Spring Boot默认使用的ConcurrentMap缓存以及缓存框架EhCache.虽然EhCache已经能够适用很多应用场景,但是由于EhCache是进 ...

  5. myBatis源码解析-类型转换篇(5)

    前言 开始分析Type包前,说明下使用场景.数据构建语句使用PreparedStatement,需要输入的是jdbc类型,但我们一般写的是java类型.同理,数据库结果集返回的是jdbc类型,而我们需 ...

  6. [源码解析] 当 Java Stream 遇见 Flink

    [源码解析] 当 Java Stream 遇见 Flink 目录 [源码解析] 当 Java Stream 遇见 Flink 0x00 摘要 0x01 领域 1.1 Flink 1.2 Java St ...

  7. DFS【搜索1】

    DFS模板 void dfs(int depth)//depth表示当前的层数(或深度) { if(depth>n)//到达叶子节点,该路已走到尽头 return; for(int i=;i&l ...

  8. 共享&img (给作者自己

    --------------------------------------------------------- ------------------------------------------ ...

  9. Cobalt Strike简单使用

    ---恢复内容开始--- 一.介绍: 后渗透测试工具,基于Java开发,适用于团队间协同作战,简称“CS”. CS分为客户端和服务端,一般情况下我们称服务端为团队服务器,该工具具有社工功能(社会工程学 ...

  10. IDEA中列编辑

    快捷键 :Alt+Shift+insert,也可以按住Alt+Shift时,点击要编辑部分