关于 Java switch 语句 - 在每种情况下使用 return 和省略中断
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18192255/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Regarding Java switch statements - using return and omitting breaks in each case
提问by NickAbbey
Given this method, does this represent some egregious stylistic or semantic faux pas:
鉴于这种方法,这是否代表了一些令人震惊的文体或语义失礼:
private double translateSlider(int sliderVal) {
switch (sliderVal) {
case 0:
return 1.0;
case 1:
return .9;
case 2:
return .8;
case 3:
return .7;
case 4:
return .6;
default:
return 1.0;
}
}
It's clearly not in line with the Java tutorials here.
它显然不符合此处的 Java 教程。
However, It's clear, concise and so far has yielded exactly what I need. Is there a compelling, pragmatic reason to create a local variable, assign a value to it within each case, add a break to each case and return the value at the end of the method?
然而,它清晰、简洁,到目前为止已经产生了我所需要的。创建局部变量、在每个案例中为其分配一个值、为每个案例添加一个中断并在方法结束时返回值是否有令人信服的、务实的理由?
采纳答案by rocketboy
Assigning a value to a local variable and then returning that at the end is considered a good practice. Methods having multiple exits are harder to debugand can be difficult to read.
为局部变量赋值,然后在最后返回它被认为是一种很好的做法。具有多个出口的方法更难调试并且难以阅读。
That said, thats the only plus point left to this paradigm. It was originated when only low-level procedural languages were around. And it made much more sense at that time.
也就是说,这是这个范式的唯一优点。它起源于只有低级过程语言存在的时候。这在当时更有意义。
While we are on the topic you must check this out. Its an interesting read.
当我们讨论这个话题时,你必须检查一下。这是一个有趣的阅读。
回答by yshavit
Nope, what you have is fine. You could also do this as a formula (sliderVal < 5 ? (1.0 - 0.1 * sliderVal) : 1.0
) or use a Map<Integer,Double>
, but what you have is fine.
不,你所拥有的很好。您也可以将其作为公式 ( sliderVal < 5 ? (1.0 - 0.1 * sliderVal) : 1.0
) 或使用 a来执行此操作Map<Integer,Double>
,但您所拥有的一切都很好。
回答by William Morrison
I suggest you not use literals.
我建议你不要使用文字。
Other than that the style itself looks fine.
除此之外,风格本身看起来不错。
回答by DanielD
If you're going to have a method that just runs the switch and then returns some value, then sure this way works. But if you want a switch with other stuff in a method then you can't use return or the rest of the code inside the method will not execute. Notice in the tutorial how it has a print after the code? Yours would not be able to do this.
如果您将拥有一个只运行开关然后返回一些值的方法,那么请确保这种方式有效。但是如果你想在一个方法中切换其他东西,那么你不能使用 return 或者方法中的其余代码将不会执行。注意在教程中它是如何在代码后打印的?你的将无法做到这一点。
回答by SubSevn
Why not just
为什么不只是
private double translateSlider(int sliderval) {
if(sliderval > 4 || sliderval < 0)
return 1.0d;
return (1.0d - ((double)sliderval/10.0d));
}
Or similar?
或者类似的?
回答by Xperiaz X
Yes this is good. Tutorials are not always consize and neat. Not only that, creating local variables is waste of space and inefficient
是的,这很好。教程并不总是紧凑和整洁。不仅如此,创建局部变量既浪费空间又低效
回答by AlexWien
From human intelligence view your code is fine. From static code analyisis tools view there are multiple returns, which makes it harder to debug. e.g you cannot set one and only breakpoint immedeatly before return.
从人类智能的角度来看,您的代码很好。从静态代码分析工具来看,有多个返回,这使得调试变得更加困难。例如,您不能在返回之前立即设置一个且仅设置一个断点。
Further you would not hard code the 4 slider steps in an professional app. Either calculate the values by using max - min, etc., or look them up in an array:
此外,您不会在专业应用程序中硬编码 4 个滑块步骤。要么使用 max - min 等计算值,要么在数组中查找它们:
public static final double[] SLIDER_VALS = {1.0, 0.9, 0.8, 0.7, 0.6};
public static final double SLIDER_DEFAULT = 1.0;
private double translateSlider(int sliderval) {
double ret = SLIDER_DEFAULT;
if(sliderval >= 0 && sliderval < SLIDER_VALS.length) {
ret = SLIDER_VALS[sliderval];
}
return ret;
}
回答by Kevin Bhuva
I think that what you have written is perfectly fine. I also don't see any readability issue with having multiple return statements.
我认为你写的非常好。我也没有看到具有多个 return 语句的任何可读性问题。
I would always prefer to return from the point in the code when I know to return and this will avoid running logic below the return.
当我知道返回时,我总是更愿意从代码中的点返回,这将避免在返回下方运行逻辑。
There can be an argument for having a single return point for debugging and logging. But, in your code, there is no issue of debugging and logging if we use it. It is very simple and readable the way you wrote.
有一个用于调试和记录的单一返回点的论据。但是,在您的代码中,如果我们使用它,则不存在调试和日志记录的问题。它的编写方式非常简单易读。
回答by Dwight Spencer
Best case for human logic to computer generated bytecode would be to utilize code like the following:
人类逻辑到计算机生成的字节码的最佳情况是使用如下代码:
private double translateSlider(int sliderVal) {
float retval = 1.0;
switch (sliderVal) {
case 1: retval = 0.9;
case 2: retval = 0.8;
case 3: retval = 0.7;
case 4: retval = 0.6;
case 0:
default: break;
}
return retval;
}
Thus elminating multible exits from the method and utilizing the language logically. (ie while sliderVal is an interger range of 1-4 change float value else if sliderVal is 0 and all other values, retval stays the same float value of 1.0)
因此,从方法中消除多重退出并在逻辑上使用语言。(即,虽然sliderVal 是1-4 的整数范围,但如果sliderVal 为0 和所有其他值,则更改浮点值,否则retval 保持相同的浮点值1.0)
However something like this with each integer value of sliderVal being (n-(n/10))
one really could just do a lambda and get a faster results:
然而,像这样的东西,sliderVal 的每个整数值都是(n-(n/10))
一个真的可以做一个 lambda 并获得更快的结果:
private double translateSlider = (int sliderVal) -> (1.0-(sliderVal/10));
Edit:
A modulus of 4 may be in order to keep logic (ie (n-(n/10))%4)
)
编辑:4 的模数可能是为了保持逻辑(即(n-(n/10))%4)
)