【发布时间】: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