<tfoot id='ZuwQM'></tfoot>

<small id='ZuwQM'></small><noframes id='ZuwQM'>

      <bdo id='ZuwQM'></bdo><ul id='ZuwQM'></ul>

  1. <legend id='ZuwQM'><style id='ZuwQM'><dir id='ZuwQM'><q id='ZuwQM'></q></dir></style></legend>
    1. <i id='ZuwQM'><tr id='ZuwQM'><dt id='ZuwQM'><q id='ZuwQM'><span id='ZuwQM'><b id='ZuwQM'><form id='ZuwQM'><ins id='ZuwQM'></ins><ul id='ZuwQM'></ul><sub id='ZuwQM'></sub></form><legend id='ZuwQM'></legend><bdo id='ZuwQM'><pre id='ZuwQM'><center id='ZuwQM'></center></pre></bdo></b><th id='ZuwQM'></th></span></q></dt></tr></i><div id='ZuwQM'><tfoot id='ZuwQM'></tfoot><dl id='ZuwQM'><fieldset id='ZuwQM'></fieldset></dl></div>

      降低 Switch 语句的圈复杂度 - Sonar

      Reduce Cyclomatic Complexity of Switch Statement - Sonar(降低 Switch 语句的圈复杂度 - Sonar)
      <legend id='I7xhs'><style id='I7xhs'><dir id='I7xhs'><q id='I7xhs'></q></dir></style></legend>
        <tfoot id='I7xhs'></tfoot>

      • <i id='I7xhs'><tr id='I7xhs'><dt id='I7xhs'><q id='I7xhs'><span id='I7xhs'><b id='I7xhs'><form id='I7xhs'><ins id='I7xhs'></ins><ul id='I7xhs'></ul><sub id='I7xhs'></sub></form><legend id='I7xhs'></legend><bdo id='I7xhs'><pre id='I7xhs'><center id='I7xhs'></center></pre></bdo></b><th id='I7xhs'></th></span></q></dt></tr></i><div id='I7xhs'><tfoot id='I7xhs'></tfoot><dl id='I7xhs'><fieldset id='I7xhs'></fieldset></dl></div>

        <small id='I7xhs'></small><noframes id='I7xhs'>

          <tbody id='I7xhs'></tbody>

                <bdo id='I7xhs'></bdo><ul id='I7xhs'></ul>

              • 本文介绍了降低 Switch 语句的圈复杂度 - Sonar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我想降低我的开关盒的圈复杂度我的代码是:

                I want to reduce cyclomatic complexity of my switch case my code is :

                public String getCalenderName() {
                        switch (type) {
                    case COUNTRY:
                        return country == null ? name : country.getName() + HOLIDAY_CALENDAR;
                    case CCP:
                        return ccp == null ? name : ccp.getName() + " CCP" + HOLIDAY_CALENDAR;
                    case EXCHANGE:
                        return exchange == null ? name : exchange.getName() + HOLIDAY_CALENDAR;
                    case TENANT:
                        return tenant == null ? name : tenant.getName() + HOLIDAY_CALENDAR;
                    default:
                        return name;
                    }
                }
                

                此代码块复杂度为 16,希望将其降低到 10.country、ccp、exchange 和tenant 是我的不同对象.根据类型,我将调用它们各自的方法.

                This code blocks complexity is 16 and want to reduce it to 10. country, ccp, exchange and tenant are my diffrent objects. Based on type I will call their respective method.

                推荐答案

                我相信这是一个 Sonar 警告.我认为 Sonar 警告不是必须做的规则,而只是指南.您的代码块是 READABLEMAINTAINABLE 原样.已经很简单了,如果你真的想改,可以试试下面这两种方法,看看复杂度会不会变低:

                I believe it is a Sonar warning. I think Sonar warnings are not must-do-rules, but just guides. Your code block is READABLE and MAINTAINABLE as it is. It is already simple, but if you really want to change it you can try those two approaches below, and see if complexity becomes lower:

                注意:我现在没有编译器,所以可能会出现错误,提前抱歉.

                Note: I don't have compiler with me now so there can be errors, sorry about that in advance.

                第一种方法:

                Map<String, String> multipliers = new HashMap<String, Float>();
                    map.put("country", country);
                    map.put("exchange", exchange);
                    map.put("ccp", ccp);
                    map.put("tenant", tenant);
                

                那么我们就可以使用地图来抓取正确的元素了

                Then we can just use the map to grab the right element

                    return map.get(type) == null ? name : map.get(type).getName() + HOLIDAY_CALENDAR;
                

                第二种方法:

                你所有的对象都有相同的方法,所以你可以在其中添加一个带有 getName() 方法的 Interface 并更改你的方法签名,例如:

                All your objects have same method, so you can add an Interface with getName() method in it and change your method signature like:

                getCalendarName(YourInterface yourObject){
                    return yourObject == null ? name : yourObject.getName() + HOLIDAY_CALENDAR;
                }
                

                这篇关于降低 Switch 语句的圈复杂度 - Sonar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Slf4j LoggerFactory.getLogger and sonarqube(Slf4j LoggerFactory.getLogger 和 sonarqube)
                Security - Array is stored directly(安全性 - 数组直接存储)
                SonarQube quot;Class Not Foundquot; during Main AST Scan(SonarQube“找不到类在主 AST 扫描期间)
                Integrate Spock#39;s test with Sonar(将 Spock 的测试与声纳集成)
                How do I make Hudson/Jenkins fail if Sonar thresholds are breached?(如果违反声纳阈值,我如何让 Hudson/Jenkins 失败?)
                automatically add curly brackets to all if/else/for/while etc. in a java code-base(自动将大括号添加到 java 代码库中的所有 if/else/for/while 等)

                <small id='8LABv'></small><noframes id='8LABv'>

                • <bdo id='8LABv'></bdo><ul id='8LABv'></ul>

                    • <i id='8LABv'><tr id='8LABv'><dt id='8LABv'><q id='8LABv'><span id='8LABv'><b id='8LABv'><form id='8LABv'><ins id='8LABv'></ins><ul id='8LABv'></ul><sub id='8LABv'></sub></form><legend id='8LABv'></legend><bdo id='8LABv'><pre id='8LABv'><center id='8LABv'></center></pre></bdo></b><th id='8LABv'></th></span></q></dt></tr></i><div id='8LABv'><tfoot id='8LABv'></tfoot><dl id='8LABv'><fieldset id='8LABv'></fieldset></dl></div>
                          <legend id='8LABv'><style id='8LABv'><dir id='8LABv'><q id='8LABv'></q></dir></style></legend>
                          <tfoot id='8LABv'></tfoot>

                            <tbody id='8LABv'></tbody>