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

什么是卫语句 #102

Open
nmsn opened this issue Oct 7, 2023 · 2 comments
Open

什么是卫语句 #102

nmsn opened this issue Oct 7, 2023 · 2 comments

Comments

@nmsn
Copy link
Owner

nmsn commented Oct 7, 2023

No description provided.

@nmsn
Copy link
Owner Author

nmsn commented Oct 7, 2023

卫语句是一种优化代码的方法,它可以降低代码的复杂程度。卫语句将复杂的条件表达式拆分成多个条件表达式,减少嵌套,通过逻辑分析将某些关键条件优先判断,简化程序流程走向。这多条的if语句就是卫语句。

以卫语句取代嵌套条件表达式的精髓就是:给某一条分支以特别的重视。如果使用if-then-else结构,你对if分支和else分支的重视是同等的。这样的代码结构传递给阅读者的消息就是:各个分支有同样的重要性。而卫语句是在告诉读者:“这种情况不是本函数的核心逻辑所关心的,如果它真发生了,请做一些必要的整理工作,然后退出。”

@nmsn
Copy link
Owner Author

nmsn commented Oct 7, 2023

https://juejin.cn/post/6844903764336312333
https://cloud.tencent.com/developer/article/1783577

特点:

  • 特殊情况,提前返回(return)
  • 无多层嵌套

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

No branches or pull requests

1 participant