What is better? if..else or multiple simple if

前端 未结 5 1700
旧巷少年郎
旧巷少年郎 2020-12-19 08:44

talking about java performance .. what is better? if..else or multiple simple if

if( condition ) {
  some_code;
  return value;
}
else if( condition ) {
  so         


        
5条回答
  •  粉色の甜心
    2020-12-19 09:15

    I would not worry about performance here as much as code readability and maintainability. As was mentioned, the performance will be essentially identical after the code is compiled.

    It is my preference to be more explicit about my conditions, instead of allowing behavior to implicitly "fall through".

    Also, the single if will not be evaluated any faster than the if else. The else path will never be checked if the case is true.

提交回复
热议问题