有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java当一个语句被称为singleentry/singleexit时,它是什么?

我不确定我是否理解得很好,例如,Java中的if语句被称为单入口/单出口语句。 在其条件为真的情况下,这是否被视为其单一的进入点,如果为假,是否被视为其单一的退出点

if(someCondition)
   doSomething();

-(单输入/单输出)语句的示例是什么


共 (4) 个答案

  1. # 1 楼答案

    很难想象现代高级语言有多个入口点,比如面向对象、抽象和封装;但是很容易看到一个方法有多个出口。例如:

        public static int CountCommas(string text)
        {
            if (String.IsNullOrEmpty(text))
            {
                return 0;
            }
            if (text.Length == 0)
            {
                return 0;
            }
    
            int index = 0;
            int result = 0;
            while (index > 0)
            {
                index = text.IndexOf(',', index);
                if (index > 0)
                {
                    result++;
                }
            }
            return result;
        }
    
  2. # 2 楼答案

    一个出口点方法(单出口):

    public int stringLength(String s) {
      return s.length();
    }
    

    双出口点法:

    public int stringLength(String s) {
      if(s == null) {
        return 0;
      }
      return s.length();
    }
    

    下面是Martin Fowler的书《重构》中的一段话:

    I often find I use Replace Nested Conditional with Guard Clauses when I'm working with a programmer who has been taught to have only one entry point and one exit point from a method. One entry point is enforced by modern languages, and one exit point is really not a useful rule. Clarity is the key principle: if the method is clearer with one exit point, use one exit point; otherwise don't.

    在上述语句的示例中,比较这两种方法的代码:

    double getPayAmount() { 
        double result; 
        if (_isDead) result = deadAmount(); 
        else {
            if (_isSeparated) result = separatedAmount(); 
            else {
                if (_isRetired) result = retiredAmount(); 
                else result = normalPayAmount();
            };
        } 
        return result; 
    };
    

    还有几个出口点:

    double getPayAmount() { 
        if (_isDead) return deadAmount(); 
        if (_isSeparated) return separatedAmount(); 
        if (_isRetired) return retiredAmount();    
        return normalPayAmount();
    };
    

    Nested conditional code often is written by programmers who are taught to have one exit point from a method. I've found that is a too simplistic rule. When I have no further interest in a method, I signal my lack of interest by getting out. Directing the reader to look at an empty else block only gets in the way of comprehension.

  3. # 3 楼答案

    并不是说我们总是只能在某一点上返回。在检查的上下文中,在函数的最顶端尽早返回可能会更干净。但是,如果函数到处都有返回语句,则可能导致可读性降低&;混乱

    如果您看看上面答案https://stackoverflow.com/a/17295767/5143994(由Adam Siemion提供)中提供的两个代码示例,我马上就能看到嵌套问题。第一个示例(没有多次返回)可以写得更好(见下文)

    double getPayAmount() {
    
        double result;
    
        if (_isDead) {
            result = deadAmount();
        } else if (_isSeparated) {
            result = separatedAmount();
        } else if (_isRetired) {
            result = retiredAmount();
        } else {
            result = normalPayAmount();
        } 
    
        return result; 
    }
    

    上述方法的另一个优点是在最后,如果你想给结果添加一个共同的影响,你可以很容易地添加它。例如,现在你想在结束计算中增加20%的额外惩罚。

    return result * 1.2; 
    

    但在多重回报的情况下,增加这种共同影响将变得更加困难

  4. # 4 楼答案

    可能是一个例子:

    do {
    
    
    }while()
    

    或者什么的:

    int someMethod() throws Exception {
    
      try {
        someInt = someComplexOp();
        return someInt;
       }
       catch(Exception e) {
         log.error(e.getMessage(),e);
         throw e;
       }     
    } 
    

    也要通读this文章