【问题标题】:Minimize code in reference to read/write operations最小化参考读/写操作的代码
【发布时间】:2010-10-31 09:47:23
【问题描述】:

我从以下代码开始:

class Vereinfache2_edit {

    public static void main(String[] args) {

        int c1 = Integer.parseInt(args[0]);
        int c2 = Integer.parseInt(args[1]);
        int c3 = Integer.parseInt(args[2]);

        /* 1 */if (c2 - c1 == 0) {
            /* 2 */if (c1 != c3) {
                c3 += c1;
                /* 4 */System.out.println(c3);
                /* 5 */c3 *= c2;
                /* 6 */}
        }

        /* 7 */if (c1 == c3)
            /* 8 */if (c1 - c2 == 0)
            /* 9 */{
                c3 += c1;
                /* 10 */System.out.println(c3);
                /* 11 */c3 *= c1;
                /* 12 */if (c1 < c2)
                    c2 += 7;
                /* 13 */else
                    c2 += 5;
                /* 14 */}

        /* 15 */System.out.println(c1 + c2 + c3);
    }

} // end of class Vereinfache2

...我的结尾是:

class Vereinfache2 { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */       
    /*  2 */        if (c2 == c1 && c1 != c3){  
    /*  4 */              System.out.println(c3 += c2) ; 
    /*  5 */              c3 = c3 * c2 ; 
    /*  6 */        }
/*  7 */      
    /*  8 */        if ( c2 == c1 && c1 == c3){
    /* 10 */            System.out.println(c3 *= 2) ; 
    /* 11 */            c3 = c3 * c2 ; c2 = c2 + 5 ; 
    /* 14 */        }


/* 15 */       System.out.println( c1+c2+c3) ;     
        }          

}  // end of class Vereinfache2

您是否看到过死代码或可切换代码之类的其他内容?

感谢所有回答。我最终得到了这个工作版本:

class Vereinfache2 { 

        public static void main(String [] args) {

           int c1 = Integer.parseInt(args[0]) ;
           int c2 = Integer.parseInt(args[1]) ;
           int c3 = Integer.parseInt(args[2]) ;

/*  1 */       if(c2 == c1){
    /*  2 */        if (c1 != c3){  
                        c3 += c2;
    /*  4 */            System.out.println(c3) ;          
    /*  6 */        }else{
                        c3 *= 2;
    /* 10 */            System.out.println(c3) ; 
    /* 14 */        }
                    c3 *= c2; c2 += 5;
               }

/* 15 */       System.out.println(c1+c2+c3) ;      
        }          

}  // end of class Vereinfache2

【问题讨论】:

    标签: java minimize dead-code


    【解决方案1】:

    对于您的第一个版本:

          if (c2 == c1) {
            if (c1 != c3) {
              c3 += c1;
              System.out.println(c3);
              c3 *= c2;
            } else {
              c3 += c1;
              System.out.println(c3);
              c3 *= c1;
              if (c1 < c2)
                c2 += 7;
              else
                c2 += 5;
            }
          } else if (c1 < c2)
              c2 += 7;
            else
              c2 += 5;
        }
        System.out.println(c1 + c2 + c3);
      }
    }
    

    对于第二个版本:

               if (c2 == c1)
                  if( c1 != c3){  
                    System.out.println(c3 += c2) ; 
                    c3 = c3 * c2 ; 
                  } else {
                    System.out.println(c3 *= 2) ; 
                    c3 = c3 * c2 ; c2 = c2 + 5 ; 
                  }
                }          
    

    这样你就不会做同样的测试 2 次了。

    【讨论】:

    • @LucaB:在原版上工作,改进版有点搞砸了。 (c3 *= 2)的东西是从哪里进来的,参考原文。
    • 我明白了,我认为当 OP 说“......我以:”结尾时,我认为它是正确的。
    • @ArtWorkAD:您在第二个版本中缺少一些代码。这是想要的吗?
    • 是的,因为第二个版本被最小化了。我删除了一些死代码。例如。原来的 if (c1
    • 当然,如果里面没有必要。但是你想在这种情况下添加不同的数量(5 或 7)还是你不在乎?
    【解决方案2】:

    这个呢?您不需要检查 c1、c2 是否相等两次,并且可以避免一次检查 c1、c3 相等性。

    public static void main(String[] args) {
    
            int c1 = Integer.parseInt(args[0]);
            int c2 = Integer.parseInt(args[1]);
            int c3 = Integer.parseInt(args[2]);
    
            if (c2 == c1) {
            int c4 = c3 + c1;
            System.out.println(c4);
            if (c1 == c3) {
                c2 += 5;
            }
            c3 = c4 * c1;
    
        }
    
            System.out.println(c1 + c2 + c3);
        }
    

    编辑:编辑以匹配原始版本而不是最终版本。

    【讨论】:

    • 这与我的回答有何不同?
    • @LucaB - 好的..我发布这个时没有看你的答案..在我发布我的答案后实际上看了它..如果我没记错你有' else if (c1 == c3)' 在您之前的答案中...您现在将其编辑为简单的 else 并使您的答案看起来像我的..正如@Adeel 提到的那样,我在那里扔了一些话..
    • @LucaB - 顺便说一句,它与您现在的“编辑”答案没有任何不同。
    • 我为 OP 发布了一个“快速”的答案,然后“清理”了它。
    • @johnbk:您应该使用原始版本,而不是修改后的版本。它有逻辑错误,并且不会产生与原始输出相同的输出。不过,这个错误不是你的,它是由提问者介绍的。第二件事是,您没有将 c1 添加到 c3,也没有在 else 部分打印 c3。
    【解决方案3】:

    使用c3 = c3 * c2;的简写:c3 *= c2;

    【讨论】:

      【解决方案4】:
      /*  4 */              System.out.println(c3 += c2) ; 
      

      应该是

      /*  4 */              System.out.println(c3 += c1) ; 
      

      我相信,在看过您的原始版本之后。这是我的版本。

      public static void main(String[] args) {
      
          int c1 = Integer.parseInt(args[0]);
          int c2 = Integer.parseInt(args[1]);
          int c3 = Integer.parseInt(args[2]);
      
          if (c2 == c1) {
              c3 += c1;
              System.out.println(c3);
              if (c1 != c3) {
                  c3 *= c2;
              } else {
                  c3 *= c1;
                  c2 += 5;
              }
              System.out.println(c1 + c2 + c3);
          }
      }
      

      IMO,将任何内容分配给sout 中的任何人都不是一个好主意。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-09-18
        • 2011-08-23
        • 1970-01-01
        • 2013-01-22
        • 1970-01-01
        • 2022-11-24
        • 2011-01-29
        相关资源
        最近更新 更多