常见做法?

发布于 2024-11-28 15:00:29 字数 210 浏览 4 评论 0原文

我一直想知道这种简写方式(如果我们可以这么称呼的话)是否是专业 PHP 开发人员中可接受的编码实践:

foo() && bar();

虽然在

if( foo() ) {
    bar();
}

我看来,单行代码更加简洁,但我还没有看到它在任何地方使用。

I've been wondering whether this shorthand, if we may call it that, is an accepted practice of coding among pro PHP devs:

foo() && bar();

instead of

if( foo() ) {
    bar();
}

While IMO the single line code is much neater, I've not seen it being used anywhere.

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

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

发布评论

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

评论(4

情愿 2024-12-05 15:00:29

这是多种语言开发人员的常见做法。从技术上讲它没有任何问题,但从它在 StackOverflow 上出现的次数来看,我会继续说它是“仅当它是唯一有意义的事情时才使用”。大多数人都没有想到这一点。

也就是说,这是完全有效的:

foo()?bar()?bat():zonk():baz();

正如

foo() && bar() && baz();

只需记住:

function foo(){return true;} 
function bar(){echo "bar";} 
function baz(){echo "baz";  return 2;} 
echo foo()||foo()?baz():baz(); // baz2

That is a common practice among developers of a number of languages. There is technically nothing wrong with it, but judging by the number of times it shows up on StackOverflow, I'll go ahead and say that it is a "use only when it is the only thing which makes sense." Most people don't expect it.

That said, this is perfectly valid:

foo()?bar()?bat():zonk():baz();

As is

foo() && bar() && baz();

Just remember:

function foo(){return true;} 
function bar(){echo "bar";} 
function baz(){echo "baz";  return 2;} 
echo foo()||foo()?baz():baz(); // baz2
绮筵 2024-12-05 15:00:29

就我个人而言,我认为这没有什么问题。它相当普遍,因此大多数 PHP 程序员都会像理解扩展版本一样理解它,从而使其完全可以接受。

Personally, I see nothing wrong with it. It's fairly widespread, so most PHP programmers will understand it just as well as they would understand the extended version, making it perfectly acceptable.

白日梦 2024-12-05 15:00:29

最重要的部分是编写易于阅读的代码。例如,使用诸如 foo()bar() 之类的函数名称是完全没有用的,因此很难说您要比较的两者中哪一个更好。

除此之外,if 示例也有一些相当大的缺陷:

  • if 是一种语言构造,但您可以像函数一样使用它。
  • 您可以在 if 条件中添加垂直空间。这可能会使内容难以阅读,因为空间会影响视觉焦点。

另一个建议是:

if (foo()) {

但是您想比较两者:代码始终在其上下文中。可读的代码使用它的上下文。你的两个建议都是有效的,关键是你已经可以从代码中读到含义:

   conditionMet() && gotForIt();

   if (conditionMet()) goForIt();

自己决定。只是不要从一行到另一行混合,因此在整个代码中保持一种样式。

The most important part is that you write code that is easy to read. Using function names like foo() and bar() for example are totally useless, so it's hard to say which one of the two you want to compare are better.

Next to that, the if example has some considerable flaws as well:

  • if is a language construct, but you use it like a function.
  • you add vertical space into the if condition. This can make things hard to read as spaces are influencing the visual focus.

An alternative suggestion would be:

if (foo()) {

But you wanted to compare the two: Code is always in it's context. And readable code uses it's context. Both of your suggestions can be valid, the key point is that you can read the meaning from the code already:

   conditionMet() && gotForIt();

   if (conditionMet()) goForIt();

Decide for yourself. Just don't mix from one line to the other, so keep one style through your whole code.

最单纯的乌龟 2024-12-05 15:00:29

foo() && bar(); 是一个布尔表达式,因此如果不将其放入布尔上下文中(如 ifwhile 等条件),则会出错)。

代码必须讲述正在解决的问题的“故事”,使用此快捷方式并不是在讲述故事。这是一个丑陋的黑客,它不优雅。

如果您认为它“简洁”,那么您还没有达到代码意味着想法的地步,而不是代码。

foo() && bar(); is a boolean expression, so it's wrong if you don't put it inside a boolean context (a condition like if, while, etc).

The code has to tell "the story" of the problem being solved, using this shortcut is not telling the story. It's an ugly hack, it's not elegant.

If you think about it as "neat", then you haven't yet reached the point where code means ideas, not code.

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