这在语法上正确吗?

发布于 2024-11-26 04:44:13 字数 415 浏览 4 评论 0原文

我在使用 CodeIgniter 数据库迁移时注意到这段代码:

$this->migrations->verbose AND print "Creating table '{$table}'...";

$verbose 是一个配置值。

我们在办公室里就这是否是有效且可读的代码进行了辩论。它基本上取代了 IF 语句的需要,因为如果第一部分为 true,它会执行条件的第二部分。我实际上很喜欢它,但办公室里的人认为它起作用只是一个意外,而且这会更具可读性:

if( $this->migrations->verbose ) print "Creating table '{$table}'...";

你觉得怎么样?

I noticed this piece of code in using CodeIgniter database migrations:

$this->migrations->verbose AND print "Creating table '{$table}'...";

$verbose is a config value.

We had a debate in the office about whether or not this is valid and readable code. It basically replaces the need for an IF statement as it executes the 2nd part of the condition if the first part is true. I actually quite like it, but the guys in the office think it's an accident that it works and that this would be more readable:

if( $this->migrations->verbose ) print "Creating table '{$table}'...";

What do you think?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

清旖 2024-12-03 04:44:13

这称为“短路评估”,该技术经常在 shell 脚本中使用 - 例如:

rm thing_to_delete || exit 1

它在某些语言中比其他语言更常见,而且很多人确实认为它是应该避免的,因为它可以令人困惑。 (人们通常认为逻辑表达式没有副作用。)

This is called 'short-circuit evaluation', and the technique is frequently used in shell scripts - for example:

rm thing_to_delete || exit 1

It's more common in some languages than others, and a lot of people do see it as something to be avoided, as it can be confusing. (People usually assume that logical expressions don't have side effects.)

划一舟意中人 2024-12-03 04:44:13

它是有效的,但它肯定不可读。我认为尝试将 Perl 风格的语法结构引入 PHP 中,即使它们确实有效,也不是一个好主意。

It's valid, but it sure isn't readable. I don't think trying to introduce Perl-style syntax constructs into PHP, even if they do work, is a good idea.

你与昨日 2024-12-03 04:44:13

这是绝对有效的。可读性与否纯粹是主观的。 :)

It's absolutely valid. Whether or not it's readable is purely subjective. :)

往事风中埋 2024-12-03 04:44:13

“这在语法上正确吗?”

当然是:) 否则 PHP 会给你一个解析错误。该代码可以工作,并且在语义上也是正确的 PHP。这很像使用: $v = $v || "default value"; 如果还没有默认值,则为 $v 分配一个默认值。

"Is this syntactically correct?"

Of course it is :) Otherwise PHP would give you a parse error. The code works and is also semantically correct PHP. It's a lot like using: $v = $v || "default value"; To assign a default value to $v if there isn't one already.

夜清冷一曲。 2024-12-03 04:44:13

因此,虽然这两个片段具有相同的效果,但我强烈投票支持第二个片段(if (...) print ...)。代码的目的是与其他程序员进行交流,就像与机器进行交流一样。如果该语言的习惯用法(以及使用它的程序员)在这种情况下使用短路评估,那么可能会没问题。然而,PHP 通常不会在语句中使用短路求值 - 大多数 PHP 程序员(实际上是一般程序员)都会在这里再三考虑。

编码时尽量避免意外,并编码出您的确切含义。如果这是一个仅在某个变量为 true 时才应执行的条件测试,请使用条件测试构造使其成为真正的条件测试 - 即此处的 if

So, whilst the two snippets have the same effect, I'd vote strongly for the second (if (…) print ...). Code is about communicating to other programmers as much as it is about communicating to the machine. If the idioms of the language (and thus the programmers using it) were to use short circuit evaluation for such situations, then it would probably be fine. However, PHP does not generally use short-circuit evaluation in statements - most PHP programmers (and indeed, programmers in general) would do a double-take here.

Code for least surprise, and code your exact meaning. If this is a conditional test that should execution only if some variable is true, make it a real conditional test using the conditional test construct - that is, here, if.

说不完的你爱 2024-12-03 04:44:13

逻辑运算符是从左到右的短路和求值操作数,因此这段代码的作用与使用 if 语句的版本完全相同。

这是一个完全有效的代码,如果您有优秀的开发人员,那么所有人都应该可以阅读它。如果它在团队中造成混乱,那么就不要使用它,但最好教他们它的实际用途。

Logic operators are short circuiting and evaluation operands from left to right, so this code does exactly what the version with if statement.

It's a completely valid code, and if you have good developers it should be also readable to all of them. If it causes confusion in the team then don't use it, but it would be better to teach them what it actually does.

安静 2024-12-03 04:44:13

如果可读性是指代码与代码实际执行操作的自然语言描述的相似度,那么使用 if 更具可读性。

If readability is the code's likeness to the natural-language-description of what the code is actually doing, then using if is more readable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文