else or return?

后端 未结 19 917
故里飘歌
故里飘歌 2021-01-01 13:27

Which one out of following two is best wrt to performance and standard practice. How does .NET internally handles these two code snippets?

Code1

If(r         


        
19条回答
  •  长情又很酷
    2021-01-01 14:08

    Both styles are commonplace, and religious wars have been fought over them. :-)

    I normally do this:

    • If the test is expressing method contract semantics, e.g. checking input parameters for validity, then choose option 2.
    • Otherwise, choose option 1.

    However, arguably a more important rule is "which is more readable and/or maintainable for the next developer who looks at the code?".

    The performance difference is negligible, as others have said.

提交回复
热议问题