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

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

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

        <legend id='freuH'><style id='freuH'><dir id='freuH'><q id='freuH'></q></dir></style></legend>

        幻数问题的解决方案......?

        Solution for Magic Number issue......?(幻数问题的解决方案......?)
        <legend id='3m9Ho'><style id='3m9Ho'><dir id='3m9Ho'><q id='3m9Ho'></q></dir></style></legend>
          <tbody id='3m9Ho'></tbody>

        <small id='3m9Ho'></small><noframes id='3m9Ho'>

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

              <tfoot id='3m9Ho'></tfoot>
                  本文介绍了幻数问题的解决方案......?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  考虑以下代码段...

                   public static UserStatus getEnum(int code) {
                      switch (code) {
                          case 0:
                              return PENDING;
                          case 1:
                              return ACTIVE;
                          case 2:
                              return SUSPENDED;
                          case 3:
                              return DELETED;
                          case 4:
                              return LOGIN_DISABLED;
                          default:
                              return null;
                          }
                  
                  }
                  

                  现在,SONAR 将案例(案例 3 和案例 4)中的 3 号和 4 号检测为幻数.

                  Now number 3 and 4 in cases(case 3 and case 4) are detected as magic numbers by SONAR.

                  为避免该问题,我将代码段更改如下...

                  To avoid that issue I changed my code segment as follows...

                   public static UserStatus getEnum(int code) {        
                      final int Pending=0;
                      final int Active=1;
                      final int Suspended=2;
                      final int Deleted= 3;
                      final int Login_details=4;
                  
                      switch (code) {
                          case Pending:
                              return PENDING;
                          case Active:
                              return ACTIVE;
                          case Suspended:
                              return SUSPENDED;
                          case Deleted:
                              return DELETED;
                          case Login_details:
                              return LOGIN_DISABLED;
                          default:
                              return null;
                      }
                  }
                  

                  在这种情况下,这是解决幻数问题的好方法吗?

                  Is this a good way to solve the magic number issue in this kind of scenario ?.

                  推荐答案

                  我猜你想避免在代码中使用整数文字.您的解决方案不是特别有效,因为它只是将文字移动到方法的顶部.它获得了一点好处,因为它为常量赋予了有意义的名称,但这些名称是方法私有的.

                  I gather that you want to avoid using integer literals in the code. Your solution is not particularly effective because it simply moves the literals to the top of the method. It gains a little bit because it gives meaningful names to the constants, but these names are private to the method.

                  更好的方法是将数字定义为接口中的字段.然后,您可以静态导入字段并将它们用作常量的符号名称.

                  A better approach would be to define the numbers as fields in an interface. You can then statically import the fields and use them as symbolic names for the constants.

                  如果枚举的声明顺序与常量相同:

                  If the enum is declared in the same order as the constants:

                  enum UserStatus {PENDING, ACTIVE, SUSPENDED, DELETED, LOGIN_DISABLED}
                  

                  你可以做另一个技巧:

                  public static UserStatus getEnum(int code) {
                      UserStatus[] values = UserStatus.values();
                      return (code >= 0 && code < values.length) ? values[code] : null;
                  }
                  

                  但是,这会在常量值和枚举声明之间建立联系.这可能没问题,具体取决于在调用 getEnum 时生成实际参数值的位置.

                  However, this creates a linkage between the constant values and the declaration of the enum. This may be okay, depending on where the actual parameter values are generated in calls to getEnum.

                  这篇关于幻数问题的解决方案......?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

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

                  相关文档推荐

                  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 等)

                    <tfoot id='AjDJh'></tfoot>

                      <tbody id='AjDJh'></tbody>
                      <bdo id='AjDJh'></bdo><ul id='AjDJh'></ul>
                    • <legend id='AjDJh'><style id='AjDJh'><dir id='AjDJh'><q id='AjDJh'></q></dir></style></legend>

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

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