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

前端 未结 10 2140
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题