ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

The relationship between Sonarcube coverage and co

2020-02-23 12:00:34  阅读:250  来源: 互联网

标签:code co Sonarcube private coverage executionCount logger public


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?

标签:code,co,Sonarcube,private,coverage,executionCount,logger,public
来源: https://blog.51cto.com/7726611/2473027

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有