Regarding Java switch statements - using return an

2020-05-19 03:57发布

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.

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?

9条回答
▲ chillily
2楼-- · 2020-05-19 04:11

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.

查看更多
Melony?
3楼-- · 2020-05-19 04:12

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 debug and 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.

查看更多
来,给爷笑一个
4楼-- · 2020-05-19 04:18

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)

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:

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))

查看更多
5楼-- · 2020-05-19 04:20

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?

查看更多
唯我独甜
6楼-- · 2020-05-19 04:24

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:

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;
}
查看更多
我只想做你的唯一
7楼-- · 2020-05-19 04:25

I suggest you not use literals.

Other than that the style itself looks fine.

查看更多
登录 后发表回答