如何编写一个真正的 if()

发布于 2024-09-19 04:43:45 字数 268 浏览 4 评论 0原文

我总是这样写 if 语句:

if (...) {
    /* do something */
}

当我在 stackoverflow 上创建主题时,有时人们会将此代码更改为:

if (...)
{
    /* do something */
}

因此第一个 { 会转到新行。

有道理吗?真正的方法是什么?

I always write if statements like this:

if (...) {
    /* do something */
}

When I create topics on stackoverflow, sometimes people change this code to:

if (...)
{
    /* do something */
}

So the first { goes to a new line.

Does it make sense, what's the true way?

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

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

发布评论

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

评论(6

单挑你×的.吻 2024-09-26 04:43:45

我建议阅读代码完整。他对这个主题进行了相当深入的探讨(几乎用了整整一章来讨论),并给出了我认为第一点令人信服的论据。

他实际上说不要明确使用第二个,因为它未通过黑盒测试:

if (blah) 
{
    //Foo
}

当黑盒化变成:

XXXXXXXXX
X
    XXX
X

所以左大括号看起来像是块的开头,而它不是(它只是分隔符)。

相反,他推荐第一个选项(在同一行上打开大括号),或者:

if (blah)
    {
    //Foo
}

因为它通过了黑盒测试:

XXXXXXXXXX
    X
    XXXXXXX
X

注意:我不记得结束分隔符是否应该在里面块与否...我手头没有副本,所以可能是:

if (blah) 
    {
    //Foo
    }

但是,这一切都归结为一致性。为了使代码具有可读性,您需要保持代码格式的一致性。选择一个方法,然后坚持下去。不要从一个文件切换到另一个文件,或者从一个例程切换到另一个例程。只要你保持一致并实际格式化它,就不用担心......

I'd suggest reading Code Complete. He goes into quite depth on the subject (Devoting practically an entire chapter on it), and gives what I see as a convincing argument for #1.

He actually says not to use the second one explicitly since it fails the black box test:

if (blah) 
{
    //Foo
}

When black boxed turns into:

XXXXXXXXX
X
    XXX
X

So the opening brace is seeming like the start of the block, wheras it's not (it's just the delimiter).

Instead, he recommends either the first option (opening brace on the same line), or:

if (blah)
    {
    //Foo
}

Since it passes the black box test:

XXXXXXXXXX
    X
    XXXXXXX
X

Note: I can't remember if the closing delimiter is supposed to be inside the block or not... I don't have my copy handy, so it might be:

if (blah) 
    {
    //Foo
    }

However, it all boils down to consistency. In order for your code to be readable, you need to be consistent in how you format it. Pick a method, and then stick to it. Don't switch from file to file or routine to routine. So long as you're consistent and actually format it, don't worry about it...

天赋异禀 2024-09-26 04:43:45

空格在 PHP 中并不重要,因此请使用您更喜欢的任何样式(或者您正在从事的项目或您工作的公司使用的任何样式以保持一致)。从 PHP 解释器的角度来看,这两种形式是完全等效的。

Whitespace doesn't matter in PHP, so use whichever style you like better (or whatever style the project you're working on or company you work for uses to keep things consistent). Both forms are completely equivalent from the PHP interpreter's perspective.

过度放纵 2024-09-26 04:43:45

任何一个。 “正确”的完全取决于您的编码风格。我建议您遵循您喜欢的代码,除非您正在处理现有代码,在这种情况下您应该遵循该代码的约定。

Either. The "right" one depends entirely on your coding style. I recommend you follow the one you prefer, unless you are working on existing code in which case you should follow that code's convention.

不弃不离 2024-09-26 04:43:45

这通常是编码人员的偏好。有些项目有编码标准,例如 Zend Framework 或公司会以一种或另一种方式要求它。如果您正在为其他人或您的公司编码,请询问标准。如果您正在帮助开发开源项目(例如 Zend Framework),您可以阅读他们的文档,了解如何格式化代码。如果您自己做,请选择您喜欢的方式并坚持下去/保持一致。

It is generally the coders preference. Some projects have coding standards, such as Zend Framework or Company's will require it one way or the other. If you are coding for someone else or your company, ask for the standards. If you are helping with an open source project, such as Zend Framework, you would read their documentation on how you should format your code. If you are doing it for yourself, pick the way you prefer and stick to it / be consistent.

浅黛梨妆こ 2024-09-26 04:43:45

这是程序员们争论了很久的问题。这完全取决于您的个人风格和偏好。两种方式都是“正确的 PHP”。

This is something that programmers have debated for ages. It is all up to your individual style and preference. Both ways are "Proper PHP".

冧九 2024-09-26 04:43:45

它不必建立新的生产线,但有些人认为这样做是最佳做法。这只是一个可读性问题,因此应该进行定制以最适合您或您正在合作的团队。 没有真正的方法

It doesn't have to go on a new line, but some people consider it best practice to do so. It's just a matter of readability, and should therefore be tailored to best suit you or the team you're working with. There is no true way.

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