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. Prometheus监控神器-Rules篇

    本章主要对如何使用Prometheus与Alertmanager组件集成配置,以及对警报规则 Rules 的俩种类型及其模板内容进行讲解. 与Alertmanager集成 Prometheus把产生的 ...

  2. cudnn加速计算

    cudnn加速运算 torch.backends.cudnn.enabled = True torch.backends.cudnn.benchmark = True 第一句话是说,使用的是非确定性算 ...

  3. C#设计模式之21-策略模式

    策略模式(Stragety Pattern) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/427 访问. 策略模式属于 ...

  4. C#算法设计查找篇之01-顺序查找

    顺序查找(Sequential Search) 该文章的最新版本已迁移至个人博客[比特飞],单击链接 https://www.byteflying.com/archives/697 访问. 顺序查找也 ...

  5. 简谈BFS

    BFS的最好理解就是“层次遍历”,他是一层层往下走的.时间复杂度同DFS. 重点在于使用队列来缓存要遍历的结点. 给出核心代码(c++):使用STL中的queue,vex[v][vi] is adja ...

  6. 第四周:卷积神经网络 part 3

    第四周:卷积神经网络 part 3 视频学习 语义分割中的自注意力机制和低秩重建 语义分割(Semantic Segmentation) 概念:语义分割是在像素级别上的分类,属于同一类的像素都要被归为 ...

  7. AS 新电脑clone项目报错:Clone failed: Authentication failed for 'https://gitee.com/XXX/Demo.git/'

    在新的电脑上安装Android Studio,并且使用git clone 项目,报以下错误: Clone failed: Authentication failed for 'https://gite ...

  8. linux驱动之模块化编程

    今天刚开始学习linux驱动的编写.在网上开了许多网友的博客,感觉比较好的摘抄下来,以便以后忘记可以随时查看.下面是摘抄文章的地址,非常感谢他们. http://blog.chinaunix.net/ ...

  9. ceph osd跟cpu进行绑定

    通过cgroup将ceph-osd进程与某一个 CPU core 绑定脚本: mkdir -p /sys/fs/cgroup/cpuset/ceph # cup number : ,,, = - ec ...

  10. Shell编程—sed进阶

    1多行命令 sed编辑器包含了三个可用来处理多行文本的特殊命令. N:将数据流中的下一行加进来创建一个多行组来处理. D:删除多行组中的一行. P:打印多行组中的一行. 1.1next命令 1. 单行 ...