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. react 样式冲突解决方案 styled-components

    前置 在 react 中解决组件样式冲突的方案中,如果您喜欢将 css 与 js 分离,可能更习惯于 CSS-Modules:如果习惯了 Vue.js 那样的单文件组件,可能习惯于使用 styled- ...

  2. 又一个小而美的Java Web框架: Solon!

    Solon 是Java世界里一个新的极易上手的Web框架.参考过 Javalin . Spring 等很多现有框架的设计. 取名自海贼王里的角色,说是希能像他一样能打 小.真的是小.最小的运行单位只有 ...

  3. Visual Studio 2017版本15.9现在可用

    本文转自 https://blogs.msdn.microsoft.com/visualstudio/2018/11/19/visual-studio-2017-version-15-9-now-av ...

  4. Newbe.Claptrap 框架如何实现 Claptrap 的多样性?

    Newbe.Claptrap 框架如何实现 Claptrap 的多样性?最近整理了一下项目的术语表.今天就谈谈什么是 Claptrap Design 和 Claptrap Factory. 特别感谢  ...

  5. JavaScript基础-02

    1. 六种数据类型: string字符串:number数值:boolean布尔值:null空值:undefined 未定义:object对象 基本数据类型(值类型): string字符串:number ...

  6. Java实现token的生成与验证-登录功能

    一.token与cookie相比较的优势1.支持跨域访问,将token置于请求头中,而cookie是不支持跨域访问的: 2.无状态化,服务端无需存储token,只需要验证token信息是否正确即可,而 ...

  7. 汇编环境搭建错误VMware Workstation 不可恢复错误: (vcpu-0) vcpu-0:VERIFY vmcore/vmm/main/cpuid.c:386 bugNr=1036521

    一.错误 在使用VMware建立虚拟机时,出现 二.解决 在此界面不要选择MS-DOS

  8. 土地购买 (斜率优化dp)

    土地购买 (斜率优化dp) 题目描述 农夫 \(John\) 准备扩大他的农场,他正在考虑$ N(1 \leqslant N \leqslant 50,000)$ 块长方形的土地. 每块土地的长宽满足 ...

  9. Dataway与SpringBoot集成干掉后台开发

    Dataway与SpringBoot集成干掉后台开发 Dataway让SpringBoot不在需要Controller.Service.DAO.Mapper了. 第一步:引入相关依赖 <depe ...

  10. 地图绘制之basemap第一弹 basemap选择与安装

    作为一个测绘GIS专业的学生,会有很多绘制地图的需求,一般情况使用ArcGIS.QGIS就可以解决,但是在绘制如论文插图时需要使用更加专业可定制化程度更高的工具,专业传统一般使用GMT,几经比较,最终 ...