0

Would there be any sort of difference using brackets or not using brackets? Is one faster than the other? Or are they both pretty much the same?

No brackets:

if(codehere == codehere)
    return;

Brackets:

if(codehere == codehere) {
    return;
}

I'm not new to Java, but I was just curious is there was any sort of performance difference.

Sorry if this is the wrong place to post this.

Matt
  • 51
  • 8
  • 4
    No, what it does do is prevent you from trying to add additional content after the `if` statement to extend it's functionality and accidently screwing up you logic. This is probably off-topic, but you should focus on only having a single entry and exit point to your code. Having `return` statements littering your method makes it difficult to follow and understand and difficult to maintain and update - IMHO... – MadProgrammer Nov 10 '14 at 23:37
  • MadProgrammer: It was just a small example. It isn't an actual piece of code that I would use. PakkuDon: Sorry.. – Matt Nov 10 '14 at 23:38
  • The difference in performance is when you leave out the brackets and therefore the code operates incorrectly. – Hot Licks Nov 10 '14 at 23:44
  • 1
    Just to offer an alternative opinion: the idea that we should be adding braces where they aren't required just in case at some point in the future some developer might add another line without understanding the context under which the line he's adding is going to run (AND not test his changes) is a rather perverse approach to defensive programming. We should demand more mindfulness from our colleagues. Like, learn how an `if` statement works! – pamphlet Nov 10 '14 at 23:48

2 Answers2

3

In this instance, there would be no difference in performance.

However, as pointed out by @MadProgrammer, coding for readability and maintainability should always be your first priority, and optimisations for performance should only be undertaken once the code works and measurements can be taken.

Using the form with braces is good for maintainability, as it reduces the risk of the following:

if(codehere == codehere)
    doSomething();
    doSomethingElse();

When the reader skims this code, they think that both doSomething() and doSomethingElse() are only called when the if statement is true - which in this case is wrong.

If you use braces in the above example:

if(codehere == codehere) {
    doSomething();
    doSomethingElse();
}

... it becomes obvious how the code is supposed to execute.

Jason
  • 11,744
  • 3
  • 42
  • 46
  • imho, I think that indentation causes more readability issues than brackets. – Alter Nov 10 '14 at 23:49
  • Indentation, or lack of? I think indentation greatly enhances readability - as long as it is consistent with the structure of the code (braces, etc). – Jason Nov 10 '14 at 23:53
  • I agree, I meant it both ways. Indentation being present or not has a greater effect on readability than brackets being present or not. I actually think that the only time brackets ever prove to be important is if proper indentation was ignored. – Alter Nov 11 '14 at 00:03
1

No, this don't change performance. But take this link, is a good read:

http://java.dzone.com/news/omitting-braces-not-just-a-mat

Ismael Di Vita
  • 1,806
  • 1
  • 20
  • 35