dregvw1801 2011-01-21 16:36
浏览 66
已采纳

PHP与JavaScript切换缩进

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?

  • 写回答

8条回答 默认 最新

  • dongzao4503 2011-01-21 16:39
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM