PHP 与 JavaScript 切换缩进

发布于 2024-10-13 09:59:46 字数 964 浏览 6 评论 0原文

虽然我意识到每种语言都有自己的缩进约定,但我还是忍不住对我最近发现的一些东西感到恼火。请考虑 PHP 手册中的这段代码:

switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}

请注意,switch 语句中的每个 case 都缩进。这是有道理的,因为代码更容易阅读,并且块的主体包含在其中的一层。

然而,当我在 JSLint: ... 中测试等效的 JavaScript switch 语句时

switch (i) {
    case "apple":
        alert("i is apple");
        break;
    case "bar":
        alert("i is bar");
        break;
    case "cake":
        alert("i is cake");
        break;
}

,它显示一个错误,告诉我它应该像这样显示:

switch (i) {
case "apple":
    alert("i is apple");
    break;
case "bar":
    alert("i is bar");
    break;
case "cake":
    alert("i is cake");
    break;
}

这似乎违反直觉,因为每种情况现在都与 switch 块本身内联。我无法想象为什么这会被认为更好,更不用说触发错误了。

JSLint 是否出错,或者只是遵循约定?如果后者是正确的,为什么惯例不缩进以使其清晰?

While I realize that each language has its own convention for indentation, I can't help but be annoyed with something I've recently discovered. Consider this code from the PHP manual:

switch ($i) {
    case "apple":
        echo "i is apple";
        break;
    case "bar":
        echo "i is bar";
        break;
    case "cake":
        echo "i is cake";
        break;
}

Notice that each case is indented from the switch statement. This makes sense, as the code is easier to read and the body of the block is contained one level inside of it.

However, when I test the equivalent JavaScript switch statement in JSLint:

switch (i) {
    case "apple":
        alert("i is apple");
        break;
    case "bar":
        alert("i is bar");
        break;
    case "cake":
        alert("i is cake");
        break;
}

...it displays an error telling me that it should appear like this instead:

switch (i) {
case "apple":
    alert("i is apple");
    break;
case "bar":
    alert("i is bar");
    break;
case "cake":
    alert("i is cake");
    break;
}

It seems counterintuitive, as each case is now inline with the switch block itself. I can't imagine any reason why this would be considered better, much less trigger an error.

Is JSLint in err, or is it just following convention? If the latter is true, why wouldn't the convention be to indent for clarity?

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

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

发布评论

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

评论(8

甜点 2024-10-20 09:59:46

这是你的代码。按照您想要的方式格式化它。使用 jsLint,但如果您不同意它的建议可以改进您的代码,请不要实现它们。 jsLint 伤害了你的感情。

It's your code. Format it how you want to. Use jsLint, but if you disagree that its recommendations improve your code, don't implement them. jsLint hurts your feelings.

九厘米的零° 2024-10-20 09:59:46

在克罗克福德的书中,他指出块不会引入新的范围。 “JSLint 期望块包含 function、if、switch、while、for、do 和 try 语句,而不是其他语句。”

这也是

if (condition){
    statements; 
}

推荐的做块的方法;因为它“更有弹性”。我非常怀疑这种结构是错误的。

In Crockford's book, he states that blocks do not introduce a new scope. "JSLint expects blocks with function,if,switch,while,for,do and try statements and nowhere else."

Also that

if (condition){
    statements; 
}

Is the recommended method of doing blocks; as it is "more resilient". I highly doubt it was structured that way in error.

清旖 2024-10-20 09:59:46

只要您的缩进在逻辑上合理,您就应该以您喜欢的风格缩进。如果 JSLint 真的抱怨这一点,那么它就过于迂腐了。

As long as your indenting can be logically justified you should indent in the style you prefer. If JSLint really complains about this then it's being overly pedantic.

清欢 2024-10-20 09:59:46

虽然格式很重要,但归根结底还是你的格式。你喜欢怎么做就怎么做。您选择的特定款式是个人选择,许多款式都可以是“好”的。但是,任何“好的”格式样式都必须保持一致 - 选择您喜欢的规则并遵守

对我来说,有趣的是关于诸如是否将 { 与前面的代码放在同一行,或者在 else 周围“拥抱大括号”之类的事情的激烈争论。我认为只有共产主义者才会将 { 与他们的 if 语句放在同一行。 :)

While formatting is important, in the end it's your formatting. Do it how you like. The particular style you choose is a personal choice and many styles can be "good". However, any "good" formatting style must be consistent - pick the rules you like and stick to them.

It's kind of funny to me how debates rage over things like placing { on the same line as the preceeding code or not, or "cuddling curly braces" around an else. I think only communists place a { on the same line as their if statement. :)

一向肩并 2024-10-20 09:59:46

这只是 JS 的约定(正如 JSLint 定义的约定)。就我个人而言,我认为 PHP 在这里是错误的(不知道手册是否遵循任何约定,我知道标准库没有)。我更喜欢 JS 风格,因为如果您处于几个嵌套块中,它会变得非常令人讨厌。例如(PHP 代码):

 class Foo{
      function Bar($arr) {
          foreach($arr as $item) {
              switch ($item) {
                  case "foo":
                       // Do something
                       break;
                       // You get the idea

最终,您的选择。我不会使用 PHP 手册作为风格指南;如果不能为不同的语言使用不同的样式,请使用定义良好的 JS 样式。

It's just convention for JS (as JSLint defines convention). Personally, I think PHP would be in the wrong here (dunno if the manual follows any sort of convention, I know the standard library doesn't). I prefer the JS style because it can get really nasty if you're in a few nested blocks. For example (PHP code):

 class Foo{
      function Bar($arr) {
          foreach($arr as $item) {
              switch ($item) {
                  case "foo":
                       // Do something
                       break;
                       // You get the idea

Ultimately, your choice. I wouldn't use the PHP manual as a style guide; if you can't use different styles for different languages, use the well-defined JS style.

此岸叶落 2024-10-20 09:59:46

JavaScript 和 PHP 在解释时会解析出额外的空格,因此您可以根据需要使代码变得丑陋。就我个人而言,我更喜欢在 switch 语句中添加额外的大括号,这样我就不会错过失败错误:

switch ($someVar)
{
  case 'value':
  {
    //your code here
  } break;
  case 'something':
  {
    //more code here
  }
  case 'something else':
  {
    //some more code here
  } break;
  default:
  {
    //default code here
  } break;
}

扫描末尾大括号处的代码可以让我检查是否添加了正确的break语句。您可以看到 'something' 案例缺少一个break语句(也许是故意的,也许是一个错误)。

有些人可能不同意这种格式,但这正是我的意思。格式并不重要。解析器非常宽容。找到适合你的东西并坚持下去。

Extra whitespace is parsed out of JavaScript and PHP when they are interpreted, so you can make your code as ugly as you'd like. Personally I prefer to add extra curly brackets to my switch statements so that I don't miss fall-through errors:

switch ($someVar)
{
  case 'value':
  {
    //your code here
  } break;
  case 'something':
  {
    //more code here
  }
  case 'something else':
  {
    //some more code here
  } break;
  default:
  {
    //default code here
  } break;
}

Scanning down the code at the end braces allows me to check If I added the correct break statements. You can see that the 'something' case is missing a break statement (maybe on purpose, maybe a mistake).

Some will disagree with this format, but that's really what I'm getting at. The format doesn't matter that much. The parsers are pretty forgiving. Find something that works for you and stick to it.

去了角落 2024-10-20 09:59:46

这是他的 Java 背景泄露出来的。 JSLint 方式模仿 switch case 的官方 Java 约定 。我更喜欢你的第一个例子,但我正在学习容忍 JSLint 方式。

It's his Java background leaking through. The JSLint way mimics the official Java conventions for switch case. I prefer your first example, but I'm learning to tolerate the JSLint way.

删除会话 2024-10-20 09:59:46

我还发现这非常令人困惑,并在 Douglas Crockford 的 JavaScript 编程语言代码约定 中找到了这个原因,网址为 http://javascript.crockford.com/code.html

子句(case、catch、default、else、finally)不是语句,
所以不应该像语句那样缩进。

I also found this very confusing, and found this reason in Douglas Crockford's Code Conventions for the JavaScript Programming Language at http://javascript.crockford.com/code.html

Clauses (case, catch, default, else, finally) are not statements and
so should not be indented like statements.

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