【问题标题】:Usability of Curly Brackets [closed]花括号的可用性[关闭]
【发布时间】:2014-04-08 19:14:45
【问题描述】:

假设我下面的代码只使用了 2 个大括号:

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught)
    if (f != null)
    System.out.println(f.toString());}

如果我这样重写它会伤害我的代码或改变它的运行方式吗?使用大括号的正确方法通常是什么?谢谢

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught){
    if (f != null){
    System.out.println(f.toString());
    }
}     } 

【问题讨论】:

  • 只要能编译,就是可读性。
  • What is generally the correct way 从字面上看,只要它是可读且一致的,任何你想要的方式。
  • 我建议系统地使用花括号。但是最重​​要并且在您的代码中缺少的是适当的缩进,以立即知道每个块属于什么。例如,您的代码没有表明 System.out.println() 调用位于 if 块内。它应该有一个额外的缩进级别。
  • 我建议你总是在循环和if 语句中使用大括号。这将有助于防止您稍后在代码“块”中插入额外的行而忘记引入大括号时出现问题。

标签: java curly-brackets


【解决方案1】:

对于单个语句,它将保持不变,但如果您想在 if 块中对多个语句进行分组,那么您必须使用正确的大括号。

if("pie"== "pie"){
    System.out.println("Hurrah!");
    System.out.println("Hurrah!2");
}

if("pie"== "pie")
    System.out.println("Hurrah!"); //without braces only this statement will fall under if
    System.out.println("Hurrah!2"); //not this one

你应该看到:Blocks

块是一组在平衡大括号之间的零个或多个语句,可以在任何允许使用单个语句的地方使用。以下示例 BlockDemo 说明了块的使用:

class BlockDemo {
     public static void main(String[] args) {
          boolean condition = true;
          if (condition) { // begin block 1
               System.out.println("Condition is true.");
          } // end block one
          else { // begin block 2
               System.out.println("Condition is false.");
          } // end block 2
     }
}

【讨论】:

  • 我建议将"pie"=="pie" 更改为更清晰的内容。在 Java 中,"pie"=="pie" 为真,因为常量映射到相同的地址,指针比较为真。通常期望它是字符串比较,但在 Java 中需要使用 equals 方法。
【解决方案2】:

通常,当您创建任何类型的循环并且只有一行代码(即只有一个以分号结尾的语句)时,您不需要{花括号}。但是,当进入循环时要执行多行时,请像这样使用 {curly-braces}:

public void listFish () {
    System.out.println( name + " with " + numFishCaught + " fish as follows: " );
        for ( Fish f: fishCaught ) {
            if ( f != null ) {
                System.out.println( f.toString() );
            }
        }
}

代码是关于它是否可以运行...我可以重写代码如下,它仍然可以完美运行:

public void listFish () { System.out.println( name + " with " + numFishCaught + " fish as follows: " ); for ( Fish f: fishCaught ) { if ( f != null ) { System.out.println( f.toString() ); } } }

排列大括号和其他东西的全部目的是为了可读性......如果你能读懂它,你通常很高兴!

【讨论】:

    【解决方案3】:

    不,它不会“伤害”您的代码。实际上,好的做法是始终使用大括号。解释一下 - 找出这四个之间的区别:

    if (2 == 2)
        System.out.println("First line");
        System.out.println("Second line");
    
    
    if (2 == 2)
        System.out.println("First line");
    System.out.println("Second line");
    
    
    if (2 == 2) {
        System.out.println("First line");
        System.out.println("Second line");
    }
    
    
    if (2 == 2){
        System.out.println("First line");
    }
    System.out.println("Second line");
    

    当使用花括号时,一切都一目了然。

    【讨论】:

      猜你喜欢
      • 2012-01-18
      • 1970-01-01
      • 1970-01-01
      • 2016-08-04
      • 2012-03-01
      • 2013-12-25
      • 2014-04-23
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多