简体   繁体   English

递归返回

[英]Needless return in recursive method

I am writing a recursive code that counts the minimum number of operations we need to do in order that s2 is equals to s1 , the valid operations are insert 1 character (di) , delete 1 character (dc) and dn stays for do nothing 我正在编写一个递归代码,该代码计算我们需要做的最小操作数,以便s2等于s1 ,有效的操作是insert 1 character (di)delete 1 character (dc)dn stays for do nothing

private static int editDistance(String s1, String s2) {

    if((s2.isEmpty() && (dn == 0 && dc == 0 && di == 0)) || (s1.isEmpty() && (dn == 0 && dc == 0 && di == 0)))
        return Integer.max(s1.length(), s2.length());

    if(s2.isEmpty()) {
        return 0;
    } else if(s1.isEmpty()) {
        dc++;
        return 1 + editDistance(s1, rest(s2));
    } else if(s1.charAt(0) == s2.charAt(0)) {
        dn++;
        return editDistance(rest(s1), rest(s2));
    } else if(s1.charAt(0) != s2.charAt(0) && dc <= di) {
        dc++;
        return 1 + editDistance(s1, rest(s2));
    } else if(s1.charAt(0) != s2.charAt(0) && dc > di) {
        di++;
        return 1 + editDistance(rest(s1), s2);
    }

    return 0;

}

For instance if we have s1 = "home" and s2 = "hote" there will be 1 delete operation (for 't'), 1 insert operation ('m') and 3 do nothing operations. 例如,如果我们有s1 =“ home”和s2 =“ hote”,那么将有1个删除操作(对于't'),1个插入操作('m')和3个不执行任何操作。

The problem is that my statements are annidated in those if/else if branches so for my program to compile i had to put a return 0 statement at the bottom which is pointless, how could i correct this? 问题是我的语句在那些if / else if分支中被取消,因此要编译我的程序,我不得不在底部放一个return 0语句,这毫无意义,我该如何纠正呢?

Remove last if. 如果删除最后一个。 Of course you should do it only if your cases cover all possible variants. 当然,仅当您的案例涵盖所有可能的变体时,才应该这样做。 Sometimes the proper way will be to throw exception after all if else to ensure that if you don't count something, you program doesn't continue with incorrect value. 有时,正确的方法毕竟是引发异常,以确保如果您不计数,程序就不会继续使用错误的值。

private static int editDistance(String s1, String s2) {

    if((s2.isEmpty() && (dn == 0 && dc == 0 && di == 0)) || (s1.isEmpty() && (dn == 0 && dc == 0 && di == 0)))
        return Integer.max(s1.length(), s2.length());

    if(s2.isEmpty()) {
        return 0;
    } else if(s1.isEmpty()) {
        dc++;
        return 1 + editDistance(s1, rest(s2));
    } else if(s1.charAt(0) == s2.charAt(0)) {
        dn++;
        return editDistance(rest(s1), rest(s2));
    } else if(s1.charAt(0) != s2.charAt(0) && dc <= di) {
        dc++;
        return 1 + editDistance(s1, rest(s2));
    } else {
        di++;
        return 1 + editDistance(rest(s1), s2);
    }
}

If these conditions exhaust all possibilities, I would advise you not to remove any conditions (because that's documentation of why the program does what it does), but rather throw an exception - for instance AssertionError with message "this can't happen". 如果这些条件耗尽了所有可能性,我建议您不要删除任何条件(因为该文档说明了程序执行其操作的原因),而是引发异常-例如, AssertionError消息为“这不可能发生”。

That's because sometimes due to refactoring or magic, things which should not happen DO happen, and it's best not to ignore them but to crash the application instead (since it's in an inconsistent state). 这是因为有时由于重构或魔术,某些不应该发生的事情确实会发生,并且最好不要忽略它们,而要使应用程序崩溃(因为它处于不一致状态)。

Java compiler simply can't always detect impossible scenarios (it would be computationally too expensive, and in some cases - impossible - see "halting problem"). Java编译器根本无法始终检测到不可能的情况(这在计算上会过于昂贵,在某些情况下-不可能-参见“停止问题”)。

You can solve this in two ways : 您可以通过两种方式解决此问题:

  1. If there could be another possibilities that could happen if you removed the last if statement 如果您删除了最后一个if语句,是否还有其他可能发生

     private static int editDistance(String s1, String s2) { if((s2.isEmpty() && (dn == 0 && dc == 0 && di == 0)) || (s1.isEmpty() && (dn == 0 && dc == 0 && di == 0))) return Integer.max(s1.length(), s2.length()); if(s2.isEmpty()) { return 0; } else if(s1.isEmpty()) { dc++; return 1 + editDistance(s1, rest(s2)); } else if(s1.charAt(0) == s2.charAt(0)) { dn++; return editDistance(rest(s1), rest(s2)); } else if(s1.charAt(0) != s2.charAt(0) && dc <= di) { dc++; return 1 + editDistance(s1, rest(s2)); } else if(s1.charAt(0) != s2.charAt(0) && dc > di) { di++; return 1 + editDistance(rest(s1), s2); }else return 0; } 
  2. If the last if statement covers all the remained possibilities just replace the last if statement with else keyword . 如果last if语句涵盖了所有剩余的可能性,只需用else关键字替换last if语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM