我曾经听说,在一行语句中使用花括号对JavaScript是有害的。我不记得推理了,谷歌搜索也没有多大帮助。

在JavaScript中,把所有语句都用大括号括起来是一个好主意吗?

我这么问,因为似乎每个人都这么做。


当前回答

技术上没有,但是非常推荐!!

忘掉“这是个人偏好”、“代码会正常运行”、“它对我来说工作得很好”、“它更可读”之类的废话。如果你犯了一个错误,这很容易导致非常严重的问题,相信我,当你在编码时很容易犯错误(不相信吗?,看看著名的苹果go to fail漏洞)。

论据:“这是个人偏好”

不,不是的。除非你们是一个人去火星,不然不行。大多数时候会有其他人阅读/修改您的代码。在任何严肃的编码团队中,这都是被推荐的方式,所以这不是一个“个人偏好”。

参数:“代码将运行良好”

意面代码也是如此!这是否意味着可以创建它?

论据:“这对我来说很有效”

在我的职业生涯中,我见过很多因为这个问题而产生的bug。你可能不记得有多少次你注释掉了“DoSomething()”,并对为什么要调用“SomethingElse()”感到困惑:

if (condition) 
    DoSomething();
SomethingElse();

或者添加了'SomethingMore'并且没有注意到它将不会被调用(即使缩进暗示相反):

if (condition)
  DoSomething();
  SomethingMore();

这是我在现实生活中遇到的一个例子。有人想要关闭所有日志,所以他们运行find&replace "console.log" => //"console.log":

if (condition) 
   console.log("something");
SomethingElse();

看到问题了吗?

即使你认为,“这些都是微不足道的,我永远不会这么做”;记住,总会有一个团队成员的编程技能不如你(希望你不是团队中最差的!)

参数:“这更有可读性”

如果说我学过编程,那就是简单的事情很快就会变得非常复杂。这是很常见的:

if (condition) 
    DoSomething();

在使用不同的浏览器/环境/用例进行测试或添加新功能后,它会变成以下内容:

if (a != null)
   if (condition) 
      DoSomething();
   else
      DoSomethingElse(); 
      DoSomethingMore();
else 
    if (b == null)
         alert("error b");
    else 
         alert("error a");

和这个比较:

 if (a != null) {
    if (condition) { 
       DoSomething();
    }
    else {
       DoSomethingElse();
       DoSomethingMore();
    }
 } else if (b == null) {
    alert("error b");
 } else {
    alert("error a");
 }

PS:在上面的例子中注意到错误的人会得到加分。

其他回答

有很多好的答案,所以我不会重复,除了说我的“规则”时,大括号可以省略:条件'返回'或'抛出'(例如)作为他们的唯一声明。在这种情况下,流控制已经清楚地表明它正在终止:

由于终止流控制,即使是“坏情况”也可以快速识别(并修复)。这个概念/结构“规则”也适用于许多语言。

if (x)
    return y;
    always();

当然,这也是为什么人们可能会使用linter..

不存在可维护性问题!

你们所有人的问题就是到处都用分号。多个语句不需要花括号。如果你想添加一个语句,只需使用逗号。

if (a > 1)
 alert("foo"),
 alert("bar"),
 alert("lorem"),
 alert("ipsum");
else
 alert("blah");

这是有效的代码,将像您期望的那样运行!

大括号是不必要的.....但无论如何都要加进去

....why should you add braces in if statements if they are not necessary? Because there's a chance that it could cause confusion. If you're dealing with a project with multiple people, from different frameworks and languages, being explicit reduces the chances of errors cropping up by folks misreading each other's code. Coding is hard enough as it is without introducing confusion. But if you are the sole developer, and you prefer that coding style, then by all means, it is perfectly valid syntax.

作为一个普遍的哲学:避免写代码,但如果你必须写,那么让它明确。

if (true){console.log("always runs");}

if (true) console.log("always runs too, but what is to be gained from the ambiguity?");
    console.log("this always runs even though it is indented, but would you expect it to?")

^声明:这是个人观点-意见可能会有所不同。请咨询您的CTO以获得个性化的编码建议。如果编码头痛持续,请咨询医生。

语句的开始缩进级别应该等于它上面的开大括号的数量。(不包括引号或注释大括号或预处理器指令中的大括号)

否则,K&R将是很好的缩进样式。为了修复它们的风格,我建议在一行中放置简短的if语句。

if (foo) bar();    // I like this. It's also consistent with Python FWIW

而不是

if (foo)
   bar();   // not so good

如果我正在编写一个编辑器,我会让它的自动格式按钮将bar吸到与foo相同的行,并且如果你在bar之前按下return,我会让它在bar周围插入括号:

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

然后,在if语句的主体中在bar的上方或下方添加新语句就很容易且一致了

if (foo) {
  bar();    // consistent
  baz();    // easy to read and maintain
}

javascript有很多问题。看看JavaScript架构师Douglas Crockford谈论的if语句似乎很好,但return语句可能会引入一个问题。

return
{
    ok:false;
}
//silent error (return undefined)

return{
    ok:true;
}
//works well in javascript