java - 单行 if 语句的 Java 标准是什么?

标签 java

下面哪种格式是单行 IF 语句的 JAVA 标准? 请给我提供JAVA引用资料来支持这个论点。谢谢。

语法-01:

if (counter == 10) response.redirect("www.google.com");

语法-02:

if (counter == 10) {
    response.redirect("www.google.com");
}

最佳答案

从旧的Java Code Conventions ,这是第二个。

文档的具体内容:7. 声明。 7.4 if、if-else、if-else-if-else 语句(第 12 页):

The if-else class of statements should have the following form

if (condition) {
    statements;
}

if (condition) {
    statements;
} else {
    statements;
}

if (condition) {
    statements;
} else if (condition) {
    statements;
} else if (condition) {
    statements;
}

Note: if statements always use braces {}. Avoid the following error-prone form:

if (condition) //AVOID! THIS OMITS THE BRACES {}!
    statement;

此后,由于 Java 代码约定确实很旧(自 1997 年以来),由于代码可读性,这属于个人问题/品味。 IMO 第二个就很好了。

关于java - 单行 if 语句的 Java 标准是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729206/

相关文章:

java - 如何在 MigLayout 中用背景颜色填充跨越区域?

java - Haxe:构建到 android 目标时崩溃

java - 在 XWiki 的子维基中注册基于 Java 的监听器

java - 如何更新印度铁路网站中添加的 PNR 验证码

Java Stunt 和 URI 发现

java - 将 int[] 更改为 IntArray 导致无法在应用程序中加载图像

java - 当我以编程方式在 aem 6.2 (6.1) 中创建组和用户时出现 AccessDeniedException?

java - 将现有的 JAR 转换为 OSGi 包

java - 如何仅使用私钥文件创建 java keystore ?

java - 谷歌地图不返回经度和纬度