【问题标题】:Anyone have suggestions on how to make this Java switch case more efficient/readable?有人对如何使这个 Java 开关盒更高效/更易读有任何建议吗?
【发布时间】:2017-03-27 16:09:00
【问题描述】:

这是一个非常标准的开关盒,用于复制电路的程序中。我要寻找的主要是代码的可读性和简洁性,同时不考虑效率。

编辑:没有意识到我不清楚偏移量的目的,偏移量的工作方式是将输入字符偏移等于偏移量的字符数,偏移量是一个整数。因此,例如,如果源是 'a' 并且偏移量是 2,这将返回路径数组中索引 2 处的值。

char passCurrent(char source)
 {
     source += offset;

     switch(source)
     {
     case 'a':
         return this.paths[0];
     case 'b':
         return this.paths[1];
     case 'c':
         return this.paths[2];
     case 'd':
         return this.paths[3];
     case 'e':
         return this.paths[4];
     case 'f':
         return this.paths[5];
     case 'g':
         return this.paths[6];
     case 'h':
         return this.paths[7];
     case 'i':
         return this.paths[8];
     case 'j':
         return this.paths[9];
     case 'k':
         return this.paths[10];
     case 'l':
         return this.paths[11];
     case 'm':
         return this.paths[12];
     case 'n':
         return this.paths[13];
     case 'o':
         return this.paths[14];
     case 'p':
         return this.paths[15];
     case 'q':
         return this.paths[16];
     case 'r':
         return this.paths[17];
     case 's':
         return this.paths[18];
     case 't':
         return this.paths[19];
     case 'u':
         return this.paths[20];
     case 'v':
         return this.paths[21];
     case 'w':
         return this.paths[22];
     case 'x':
         return this.paths[23];
     case 'y':
         return this.paths[24];
     case 'z':
         return this.paths[25];
     }
     return '/';
 }

【问题讨论】:

  • 您可能会在 codereview.stackexchange.com 上获得更好的结果。
  • 发布源和偏移量的声明,如果这些是字符串,那么如何使用char is switch?
  • @SotiriosDelimanolis 原样,在Code Review 上的接受度很低,因为缺少上下文。 paths 无处定义,我们不知道 source 来自哪里(或它的目的)或者这个 offset 可能是什么,或者我们为什么要处理字母表中的字母以及它是否是解决此代码正在解决的实际问题的好方法。也就是说,问题的形状(例如“希望提高可读性+效率”)确实看起来像是一个 CR 问题,在 Stack Overflow 上会偏离主题。
  • @markspace 这是我希望那些没有意识到char 是一种整数的人提出的问题。我猜这是相当一部分新程序员。除非你在某个地方学习它,否则它并不明显。这对我来说似乎是一个合法的问题。
  • @ajb 事情是,offset 相关的。如果它的值为 97 ('a') 怎么办?那么切换或减法没有意义,只是return this.path[source]...

标签: java performance syntax switch-statement


【解决方案1】:

char 只是一个数字;数字的含义与 Unicode 中字符的排列方式有关(从 0 到 127 的数字是很久以前由 ASCII 定义的,后来被纳入 Unicode)。

因此,如果source'a',它实际上是整数值97。从az的字母在Unicode中都是连续的,所以b是98,c是99等。

这意味着如果你想要a 为0,b 为1,等等,你可以通过简单的减法得到它。因此:

if (source >= 'a' && source <= 'z') {
    return this.paths[source - 97];
}

或者,等效地(并且更易读):

if (source >= 'a' && source <= 'z') {
    return this.paths[source - 'a'];
}

因为'a' 只是写 97 的另一种方式。

【讨论】:

  • 很好地解释了为什么要这样做,我只选择了另一个答案,因为三元使代码更清晰
  • 没问题。我自己可能使用过三元,但这是风格问题——我认识不喜欢三元运算符的程序员。
【解决方案2】:

消除switch 并在检查范围后使用减法。类似的,

if (source >= 'a' && source <= 'z') {
    return this.paths[source - 'a'];
}
return '/';

而且,我们可以用三元类似的方式进一步缩短它

return (source >= 'a' && source <= 'z') ? 
        this.paths[source - 'a'] : '/';

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-02-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-12
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    相关资源
    最近更新 更多