Should I use return/continue statement instead of if-else?

前端 未结 13 1882
时光说笑
时光说笑 2020-12-01 10:46

In C, C++ and C# when using a condition inside a function or loop statement it\'s possible to use a continue or return statement as early as possible and g

13条回答
  •  独厮守ぢ
    2020-12-01 11:05

    The glib answer is that it all depends.

    My general feeling is that if condition is a rare, guard (e.g. check for null) or error condition then I tend to use return or continue

    If it's an expected case then I tend to use your first approach.

    Notice, however, that I said "tend". The boundary between these to conditions is vague and subject to change depending on the project and who I'm working with.

提交回复
热议问题