Regarding Java switch statements - using return and omitting breaks in each case

前端 未结 10 2090
隐瞒了意图╮
隐瞒了意图╮ 2021-02-01 12:29

Given this method, does this represent some egregious stylistic or semantic faux pas:

private double translateSlider(int sliderVal) {
    switch (sliderVal) {
           


        
相关标签:
10条回答
  • 2021-02-01 12:44

    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.

    0 讨论(0)
  • 2021-02-01 12:45

    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?

    0 讨论(0)
  • 2021-02-01 12:46

    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; break;
        case 2: retval = 0.8; break;
        case 3: retval = 0.7; break;
        case 4: retval = 0.6; break;
        case 0:
        default: break;
      }
      return retval;
    }
    

    Thus eliminating multiple exits from the method and utilizing the language logically. (ie while sliderVal is an integer 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-(siderVal/10));
    

    Edit: A modulus of 4 may be in order to keep logic (ie (n-(n/10))%4))

    0 讨论(0)
  • 2021-02-01 12:51

    Though the question is old enough it still can be referenced nowdays.

    Semantically that is exactly what Java 12 introduced (https://openjdk.java.net/jeps/325), thus, exactly in that simple example provided I can't see any problem or cons.

    0 讨论(0)
  • 2021-02-01 12:52

    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.

    0 讨论(0)
  • 2021-02-01 12:54

    I think that what you have written is perfectly fine. I also don't see any readability issue with having multiple return statements.

    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.

    0 讨论(0)
提交回复
热议问题