• 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?

  • 相关阅读:
    【HDU 6096】—String(扫描线+Trie)
    【BZOJ #4231】—回忆树(Kmp+Ac自动机)
    【BZOJ #4231】—回忆树(Kmp+Ac自动机)
    【BZOJ #3942】【Usaco2015 Feb】—Censoring(哈希)
    【BZOJ #3942】【Usaco2015 Feb】—Censoring(哈希)
    【洛谷P2444】【POI2000】—病毒(Ac自动机)
    【洛谷P2444】【POI2000】—病毒(Ac自动机)
    响应式实践
    2016-12-30
    响应式调研资料
  • 原文地址:https://www.cnblogs.com/heyang78/p/12329791.html
Copyright © 2020-2023  润新知