多个组合逻辑判断是 if else 更合理 还是 goto 最后输出之前更合理?

2015-09-01 10:31:27 +08:00
 l1905
比方有如下两种例子

第一种写法

```
function a () {
if ($is_pass_1 ) {
if ($is_pass_2 ) {
if ($is_pass_3 ) {
//...
//业务代码处理
return result;
}
}
}
}
```
第二种写法

```
function b () {
if (!$is_pass_1 ) {
//逻辑处理
goto xxxxx;
}
if (!$is_pass_2 ) {
//逻辑处理
goto xxxxx;
}
if (!$is_pass_3 ) {
//逻辑处理
goto xxxxx;
}
//业务代码处理

xxxxxx: #goto 到的位置
// result =
return result
}


```
6678 次点击
所在节点    PHP
61 条回复
zonghua
2015-09-03 13:41:24 +08:00
javascript 没有 goto,好像也不建议用 switch

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://ex.noerr.eu.org/t/217494

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX