-2

Suppose I have the following code:

public class Test{
    public static void main(String arguments[]){
        if(true){
            System.out.println("Hello, world!");
        }
    }
}

It would be more efficient to do this:

public class Test{
    public static void main(String arguments[]){
        if(true) System.out.println("Hello, world!");
    }
}

Is there any reason the first method is much more commonly used besides convention?

Programah
  • 179
  • 1
  • 10

4 Answers4

3

To answer your question, NO there is no difference in efficiency between both the conventions, but I would highly recommend to use braces as the code will look formatted and it's easy for any programmer (rookie to expert) to quickly scan through your code.

Additionally, braces also help in avoiding logical errors which are very difficult to debug by ensuring that all the statements that need to be executed when condition is true or false (else section) are grouped together.

JRG
  • 4,037
  • 3
  • 23
  • 34
1

Never omit braces. Never. If I had a nickel for every time I saw someone posting a question here who got burned by omitting braces, I would have enough for several meals.

You may know right now what your intent is, but wait a few months, come back, and see if you notice the missing brace. Then you have weird problems where only the first statement of the conditional is being run, and the rest are executed regardless. The 2 keystrokes (or 1 if you have a good IDE) and a line you save are not worth the potential headache down the road.

Saving a line is a false win anyways, since it rarely helps readability.

Carcigenicate
  • 43,494
  • 9
  • 68
  • 117
  • I never ever use braces when the statement 1. is a simple conditional or loop and 2. fits in one line (I never write braceless two-lines). I've been doing this since 20 years and you won't get a single nickel from me. I do it as I consider it to be significantly more readable (it's no laziness, I even remove the braces when no more needed). YMMV. – maaartinus Jul 11 '17 at 06:23
  • 1
    @maaartinus I think for a new programmer asking this question, "never" is the right answer. It's like gotos: I think they have more situations where they can be abused than where they can be used properly, so new programmers should avoid them. Later down the road once they have more experience, they'll realize the fringe scenarios where their use is appropriate. I think both have the potential to encourage bad habits, so they shouldn't be used right away. – Carcigenicate Jul 11 '17 at 13:31
0

It would not be more efficient, except, very slightly, for you when first typing it. The compilation time difference is vanishingly small, and there is no execution time benefit at all.

user207421
  • 305,947
  • 44
  • 307
  • 483
-1

One reliable way to know if one of these is more efficient is by running the pieces of code separately a very high number of times (maybe million or more) and then compute the average time spent per run. In all likeliness, you will find that both pieces of code take the same amount of time per run in the long run because both these codes will result in similar, if not same, bytecode.

Regarding the second part that one of those is more common - that is opinion based. Personally, if it is a single line after the if-block I remove the parentheses and pull that line up next to the if-block so that it looks like if(condition) mystatement;, otherwise I put the parentheses.

displayName
  • 13,888
  • 8
  • 60
  • 75