if->return vs. if->else efficiency

后端 未结 6 1193
一整个雨季
一整个雨季 2020-12-28 12:35

This may sound like a silly question, and I hesitated to post it, but still: if something needs to run only in a certain condition, which of these is more efficient:

6条回答
  •  星月不相逢
    2020-12-28 13:18

    Performance is more or less the same in both the cases. Therefore it becomes more of a style or preference question.

    I personally prefer writing if--> return i.e. case B because it makes the code to look cleaner and easy to read specially if the code comprises of complex nested conditions.

提交回复
热议问题