zoukankan      html  css  js  c++  java
  • The relationship between Sonarcube coverage and code branch

    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) : {}.
    {}", 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) : {}.
    {}", 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) : {}.
    {}", 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?

  • 相关阅读:
    [Windows] 一些简单的CMD命令
    开发过程中用到的触发器
    MyEclipse8.5配置struts等框架
    Java编程中中文乱码问题的研究及解决方案
    开源的SSH框架优缺点分析
    java 合并排序算法、冒泡排序算法、选择排序算法、插入排序算法、快速排序
    html,CSS文字大小单位px、em、pt的关系换算
    HTML常用标签参考学习
    匹配中文字符的正则表达式
    Oracle 取上周一到周末的sql
  • 原文地址:https://www.cnblogs.com/heyang78/p/12329791.html
Copyright © 2011-2022 走看看