Design-table-driven approach reduces the cyclomatic complexity of the code

Design-table-driven approach reduces the cyclomatic complexity of the code

1. The design-table-driven method reduces the cyclomatic complexity of the code

Let's look at a problem first

设计一个根据不同scheme判断应用内打开还是外部打开的程序。

/**
 * 优化一段代码,降低代码的圈复杂度
 *
 * scheme        打开
 * weixin://    true
 * mqq://       true
 * baidu://     true
 * https://     false
 *
 */

Naturally, we can think of such a method

    /**
     * 传统的方式实现scheme判定
     * @param scheme
     * @param yes
     * @return
     */
    public static boolean shouldOpenSchemeBySystem(String scheme,boolean yes){
    
    
        if(!isCorrectFormat(scheme)){
    
    
            return false;
        }
        String schemePrefix=scheme.substring(0,scheme.indexOf("://"));
        if(schemePrefix.equals("weixin://")){
    
    
            return false;
        }else if(schemePrefix.equals("mqq://")){
    
    
            return false;
        }else if(schemePrefix.equals("baidu://")){
    
    
            return false;
        }else if(schemePrefix.equals("https://")){
    
    
            return true;
        }else {
    
    
            //other schemes ...etc
        }
        return false;
    }

But this code is a bit verbose. If there are dozens of schemes, don’t we have to write dozens of if else?
That will greatly increase the difficulty of code maintenance. Each if else contributes a cyclomatic complexity to the code , We must use some way to optimize the above code and reduce cyclomatic complexity.

In order to achieve the above purpose, it is recommended to use the table-driven method to optimize, not only can reduce the time complexity to O(1), but also reduce the cyclomatic complexity by 1.
At the same time, for the maintainability of the code, we can save the mapping relationship in the configuration file in advance, write it as a json-type key-value pair, analyze it with Gson, etc., or dynamically deliver a piece of json (that is, configured by the server).

    private static Map<String,Boolean> schemeMap;
    /**
     * 表驱动的方式实现scheme判定
     * @param scheme
     * @return
     */
    public static boolean shouldOpenSchemeBySystem(String scheme){
    
    
        if(!isCorrectFormat(scheme)){
    
    
            return false;
        }
        Map<String,Boolean> map=loadSchemeOption();
        String schemePrefix=scheme.substring(0,scheme.indexOf("://"));
        if(!map.containsKey(schemePrefix)){
    
    
            return false;
        }
        return map.get(schemePrefix);
    }

    public static Map<String,Boolean> loadSchemeOption(){
    
    
        if(schemeMap==null){
    
    
            //load properties from file or network,json format is perfect.
            schemeMap=new HashMap<>();
        }

        return schemeMap;
    }
    public static boolean isCorrectFormat(String scheme){
    
    
        return scheme!=null&&scheme.contains("://");
    }

Guess you like

Origin blog.csdn.net/qq_23594799/article/details/108027989