0

Say I have the following code below that only uses 2 curly brackets:

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

Will it hurt my code or change the way it runs if I rewrite it this way? What is generally the correct way of using curly brackets? Thanks

public void listFish(){
System.out.println(name + " with " + numFishCaught + " fish as follows: ");
for (Fish f: fishCaught){
    if (f != null){
    System.out.println(f.toString());
    }
}     } 
Michel Tamer
  • 341
  • 1
  • 3
  • 10
  • 1
    As long as it compiles, it is then all about readability. – Sotirios Delimanolis Apr 08 '14 at 19:15
  • 2
    `What is generally the correct way` Literally any way you want as long as it's readable and consistent. – MrLore Apr 08 '14 at 19:15
  • 2
    I'd recommend using curly braces systematically. But what matters the most, and is missing in your code, is proper indentation, to immediately know what belongs to each block. For example, your code doesn't make it obvious that the System.out.println() call is inside the if block. It should have an additional indentation level. – JB Nizet Apr 08 '14 at 19:18
  • 3
    I suggest you _always_ use braces for loops and `if` statements. This will help to prevent problems when you later insert an additional line into the code "block" and forget to introduce braces at that time. – GriffeyDog Apr 08 '14 at 19:18

3 Answers3

1

For a single statement it will remain same, but if you want to group more than one statement in the if block then you have to use curely braces.

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

You should see:Blocks

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example, BlockDemo, illustrates the use of blocks:

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
     }
}
codefreaK
  • 3,584
  • 5
  • 34
  • 65
  • 2
    I would suggest changing `"pie"=="pie"` to something a bit clearer. In Java `"pie"=="pie"` is true as the constants are mapped to the same address and the pointer comparison will be true. It is usually expected to be string comparison however in Java that requires the use of the `equals` method. – DrYap Apr 08 '14 at 19:39
1

Generally, when you create any type of loop and there is only one line of code(that is, only one statement that ends with a semicolon), you do not need the {curly-braces}. However, when you have more than one line that will be executed if the loop is entered, then use the {curly-braces} like so:

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

Code is all about whether or not it can run... I can rewrite the code as follows an it still will run perfectly:

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

The whole point of lining up the braces and other things is for readability... If you can read it, you're generally good to go!

CalicoZac
  • 11
  • 2
1

No, it will not "hurt" your code. Actually, the good practice is to always use curly brackets. To explain - find the difference between those four:

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");

When using curly braces everything is clear on the first sight.

makasprzak
  • 5,082
  • 3
  • 30
  • 49