<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

      时间:2024-05-09
      <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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:Ncss 类型计数违规 下一篇:Sonar 4.2 在同一个项目中分析 Java 和 JavaScript

                相关文章

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

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

                1. <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>