- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的 switch 语句中有 10 个案例。他们每个人都做同样的事情,只是任务不同。如果我必须在一个 case block 中更改 1 行代码(在开发期间),那么我必须手动更改所有其他 9 个 case。
以下是每个案例陈述的场景:
如何优化或缩短它?
举例说明:
final int CONSTANT_A = 0;
final int CONSTANT_B = 1;
...
final int CONSTANT_J = 10;
int varA = 0;
int varB = 1;
...
int varJ = 10;
int anothervarA = 0;
int anothervarB = 1;
...
int anothervarJ = 10;
int action = 0;
switch(something) {
case 1:
... long lines of code here
// If I have to change the variables below
// then I have to update all other variables in
// other cases below
varA = CONSTANT_J;
anothervarA = CONSTANT_B;
... another long lines of code here
int ret = someObject.foo(varA);
... do something with ret.
action = 5;
break;
case 2:
... long lines of code here
varB = CONSTANT_I;
anothervarB = CONSTANT_C
... another long lines of code here
int ret = someObject.foo(varA);
... do something with ret.
action = 100;
break;
...
...
case 9:
... long lines of code here
varI = CONSTANT_B;
anothervarI = CONSTANT_A;
... another long lines of code here
int ret = someObject.foo(varA);
... do something with ret.
action = 100;
break;
case 10:
... long lines of code here
varK = CONSTANT_A;
anothervarJ = CONSTANT_F;
... another long lines of code here
int ret = someObject.foo(varA);
... do something with ret.
action = 4;
break;
}
最佳答案
没有什么明显的跳出,除了可能将所有代码分解到一组类(或枚举)中,并依赖多态性而不是switch
调用正确的人。例如,将您的案例加载到 Map<Integer,Foo>
-- 甚至是List<Foo>
如果它不是稀疏数组 - 然后将开关替换为 myFoos.get(something).whatever();
.
至于完成后分配变量,如果它们是成员变量,则可以让 Foos 直接设置它们;如果始终在单线程环境中调用它,则可以有 foo.whatever()
设置状态,然后获取 setter/getter 。如果是在多线程环境中,则可以有 whatever()
使用这些 setter/getter 返回一个新对象。像这样的东西:
FooResult result = myFoos().get(something).getResult(whatever, args);
varA = result.getA();
action = result.getAction();
etc
关于java - 缩短 switch block 内 case 语句中的代码行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9222601/
这个问题在这里已经有了答案: 关闭 11 年前。 Possible Duplicate: How do short URLs services work? 我经常看到来自 bitly.com 的缩短
if (args.join(" ").toLowerCase() === "are you" || args.join(" ").toLowerCase() === "are you doing")
学习Golang,想知道是否有更短的编写方法 if tiletype == 0 || tiletype == 2 { levelmap[pass
sum_num = 0 for human in humans: sum_num += human.limbs return sum_num 假设对象 human 具有属性 limbs ,如下
我想问一下是否有一种方法可以像在 MSSQL 中那样缩短这个条件,因为我也有类似的条件。 if(docType == "PO" || docType == "II" || docType == "IA
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 3 年前。 Improve this qu
我在单页上有一些 15-20 个 Highcharts (使用 slider ,每张幻灯片 1-2 个图表)、一些条形图、一些柱形图、一些饼图,具有不同的显示选项。我使用的是在我的闭包内有多种方法,其
几周前,我在下拉菜单的一些代码上得到了一些帮助,但我想知道是否可以使代码更短,因为下面显示的当前代码相当大,任何帮助都会很棒。 $(document).ready(function(){ $(
这里是新的。我只是想知道是否有可能使这个 if 语句更短且冗余更少。 if (!a && b) { if (c == d && e > 0) { return;
我有这个代码。 c = getch() if c == "r":
我有几行代码用于迭代列表中的字典,我想缩短它。它按原样完美运行,但似乎代码太多,我正在尝试了解如何在 Python 中(或一般情况下)保持代码高效。 for d in dev['devices']:
如果代码如下,如何缩短 if 语句? $a = null; $b = "foo"; if ((empty($a) && !empty($b)) || (!empty($a) && empty($b))
我需要计算应用的每日独立用户数。 我可以唯一识别用户的唯一方法是通过他们的 UUID(这是外部提供的,所以我不得不使用它)。 我知道我的每日用户数是几百万。 我想在 Redis 中使用一个 bitse
载体 a和 b可以使用 toString(width = 10) 缩短在 Base R 中导致以 .... 结尾的较短向量 但是,我想知道如何使缩短的向量以 ..., last vector elem
是否有缩短 HTML 页面的库(最好是 Python 库)?我的意思是它会生成一个可能更小的(就字符数而言,包括换行符 Silly example 可以改成: Silly example
如何缩短这段 CSS 的代码?当它在移动 View 中时,它将隐藏表格的某些列。我的表有 137 列,我只想查看 5 列。 @media only screen and (max-width: 800
我所拥有的是主目录中的文件路径,我希望将其处理为包含“~”的缩短路径。 例如,我的输入可能是:"/home/username/test"或 /home/./username/test或 /home/.
我们为文档生成一个 GUID,并且需要将该 GUID 包含在 C40 编码的条码(Type 29 2D)中,并且具有以下限制。 最长可达 25 个字符只能使用大写字母数字字符,不能使用特殊字符。 我曾
这个问题已经有答案了: Ternary operators in JavaScript without an "else" (13 个回答) 已关闭 4 年前。 我一直使用这样的三元表达式,但我不喜欢
首先,我想确保我知道这样一个事实:重新哈希是一个明智的话题。不过,我想听听您的一些意见,以及您会采取什么方法。 我正在构建一个分布式应用程序,其中节点远程创建由 UUID 标识的实体。最终,所有实体应
我是一名优秀的程序员,十分优秀!