Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于不写return的规范有啥根据吗? #130

Open
jhd124 opened this issue Oct 12, 2022 · 1 comment
Open

关于不写return的规范有啥根据吗? #130

jhd124 opened this issue Oct 12, 2022 · 1 comment

Comments

@jhd124
Copy link

jhd124 commented Oct 12, 2022

image
image
我在leecode上写代码的时候注意到有return的写法比没有return的快,有没有大佬了解这其中的道理,不写return的这条规范是处于什么目的?

@ZhangHanDong
Copy link
Contributor

leetcode 上面那个测试时间比较玄学,你多测试几次结果不一样。而且你这个个例并不能说明 有 return比没return 快,具体还得分析具体代码被编译器如何优化,和有没有return 关系应该不大。

规范里建议在半路返回的时候加 return,而最终表达式默认返回多值则无需加return,这是为了在比较复杂的逻辑中增强可读性。半路返回的值因为添加了return而比较显眼。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants