简体   繁体   English

java.util类和方法中嵌套的可读性

[英]Readability of nesting in java.util classes and methods

I'm probably going to get 'Duplicated Post' but anyway. 无论如何,我可能都会收到“重复帖子”。

Since I started programming, I learned that you should avoid nested loops, if/else, try/catch etc. because the code gets hard to read and understanding the context is quite difficult. 自从我开始编程以来,我了解到应该避免嵌套循环,if / else,try / catch等,因为代码难以阅读并且理解上下文非常困难。 There are a couple of things you can do to avoid them like ENUMs and Interfaces. 您可以做一些避免这些事情的事情,例如ENUM和Interfaces。

While looking for answers, I found posts saying, that nested loops are not always bad, as long as its readable. 在寻找答案时,我发现帖子说,嵌套循环并不总是坏的,只要它可读即可。

Now while working on a project, I came across the java.util.Properties class with some very complicated methods with a lot of nesting like this: 现在,当在一个项目上工作时,我遇到了java.util.Properties类,它带有一些非常复杂的方法,并带有很多这样的嵌套:

 /*
 * Converts encoded \uxxxx to unicode chars
 * and changes special saved chars to their original forms
 */
private String loadConvert (char[] in, int off, int len, char[] convtBuf) {
    if (convtBuf.length < len) {
        int newLen = len * 2;
        if (newLen < 0) {
            newLen = Integer.MAX_VALUE;
        }
        convtBuf = new char[newLen];
    }
    char aChar;
    char[] out = convtBuf;
    int outLen = 0;
    int end = off + len;

    while (off < end) {
        aChar = in[off++];
        if (aChar == '\\') {
            aChar = in[off++];
            if(aChar == 'u') {
                // Read the xxxx
                int value=0;
                for (int i=0; i<4; i++) {
                    aChar = in[off++];
                    switch (aChar) {
                      case '0': case '1': case '2': case '3': case '4':
                      case '5': case '6': case '7': case '8': case '9':
                         value = (value << 4) + aChar - '0';
                         break;
                      case 'a': case 'b': case 'c':
                      case 'd': case 'e': case 'f':
                         value = (value << 4) + 10 + aChar - 'a';
                         break;
                      case 'A': case 'B': case 'C':
                      case 'D': case 'E': case 'F':
                         value = (value << 4) + 10 + aChar - 'A';
                         break;
                      default:
                          throw new IllegalArgumentException(
                                       "Malformed \\uxxxx encoding.");
                    }
                 }
                out[outLen++] = (char)value;
            } else {
                if (aChar == 't') aChar = '\t';
                else if (aChar == 'r') aChar = '\r';
                else if (aChar == 'n') aChar = '\n';
                else if (aChar == 'f') aChar = '\f';
                out[outLen++] = aChar;
            }
        } else {
            out[outLen++] = aChar;
        }
    }
    return new String (out, 0, outLen);
}

For me this is anything but readable. 对我来说,这是不可读的。 So I'm generally curious, if this is the best or the only way to make this. 所以我通常很好奇,这是最好的还是唯一的方法。 Because since this is as old as JDK1.0 shouldn't this have been 'cleaned' up or changed for readabilty? 因为既然它与JDK1.0一样古老,那么是否应该“清理”或更改其可读性? And there are a lot more methods with complicated and complex code. 而且还有许多使用复杂代码的方法。

thanks 谢谢

Let's try to focus on facts , not opinions here: 让我们尝试着眼于事实 ,而不是观点:

  • although "Clean Code advocates" don't like to hear that - but "clean code" rules are still "subjective. Some people think they are "holy grail", others do not agree to that. 尽管“清洁代码倡导者”不喜欢听到这样的声音-但是“清洁代码”规则仍然是“主观的”。有些人认为它们是“圣杯”,而另一些人则不同意。
  • more importantly, the code you are showing here is probably 15+ years old. 更重要的是,您在此处显示的代码可能已有15年以上的历史了。 Most likely, it would be written differently nowadays. 最有可能的是,如今的写法有所不同。

And yes, you are definitely not alone in thinking "this code looks unreadable". 是的,您肯定不是唯一一个认为“这段代码看起来不可读”的人。 To a certain degree, that is because we are not used to such code any more, and to a certain degree that is because this code is hard to read/grasp/understand. 在一定程度上,这是因为我们不习惯这样的代码更多,并在一定程度上就是因为这个代码难读/掌握/明白了。

It is low-level (like post-increments), written for speed / production usage, two nested loops, and aspects okay to me. 它是低级的(如后期增量),是针对速度 /生产用途,两个嵌套循环以及我可以接受的方面而编写的。 Some spaces and maybe comment missing. 一些空格,可能缺少评论。

This low-level character is especially evident in not using other functions. 这种低级特征在不使用其他功能时尤其明显。

However I would not see how better style would be feasible, without calling other new methods (as function decompisition would slow things down). 但是,如果不调用其他新方法,我将看不到更好的样式是可行的(因为函数分解会减慢速度)。

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

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