Is it ok if I omit curly braces in Java? [closed]

2018-12-31 22:39发布

I've searched for this, but couldn't find an answer and for whatever reason I was too ashamed to ask professor, due to that feeling when hundreds of people stare at you...

Anyhow, my question is what's the importance of having brackets? Is it OK if I omit them? Example:

for (int i = 0; i < size; i++)  {
   a += b;
}

vs

for (int i = 0; i < size; i++)
   a += b;

I know both of them will work, but if I omit the brackets (which I tend to do a lot, due to visibility) will that change anything, anything at all? As I said, I know it works, I tested it dozen of times, but now some of my uni assignments are getting larger, and for some reason I have irrational fear that in the long run, this my cause some problems? Is there a reason to fear that?

16条回答
看淡一切
2楼-- · 2018-12-31 23:17

If you have a single statement you can omit the brackets, for more that one statements brackets is necessary for declaring a block of code.

When you use brackets you are declaring a block of code :

{

//Block of code
}

The brackets should be used also with only one statement when you are in a situation of nested statement for improve readability, so for example :

for( ; ; )
  if(a == b) 
    doSomething()

it is more readable written with brackets also if not necessary :

for( ; ; ) {
  if(a == b) {
    doSomething()
   }
}
查看更多
梦该遗忘
3楼-- · 2018-12-31 23:17

More support for the "always braces" group from me. If you omit braces for single-statement loops/branches, put the statement on the same line as the control-statement,

if (condition) doSomething();
for(int i = 0; i < arr.length; ++i) arr[i] += b;

that way it's harder to forget inserting braces when the body is expanded. Still, use curlies anyway.

查看更多
与君花间醉酒
4楼-- · 2018-12-31 23:18

Nowadays, it is very easy to re-indent codes to find out which block of codes is in which if or for/while. If you insist that re-indenting is hard to do, then brackets placed at wrong indentation can confuse you equally badly.

for(int i = 0; i < 100; i++) { if(i < 10) {
    doSomething();
} else { for(int j = 0; j < 5; j++) {
        doSomethingElse();
    }
}}

If you do this everywhere, your brain is going to break down in no time. Even with brackets, you are depending on indentation to visually find the start and end of code blocks.

If indentation is important, then you should already write your code in correct indentation, so other people don't need to re-indent your codes to read correctly.

If you want to argue that the previous example is too fake/deliberate, and that the brackets are there to capture careless indentation problem (especially when you copy/paste codes), then consider this:

for(int i = 0; i < 100; i++) {
    if(i < 10) {
    doSomething();
}
else {
    for(int j = 0; j < 5; j++) {
        doSomethingElse();
    }
}

Yes, it looks less serious than the previous example, but you can still get confused by such indentation.

IMHO, it is the responsibility of the person writing the code to check through the code and make sure things are indented correctly before they proceed to do other things.

查看更多
萌妹纸的霸气范
5楼-- · 2018-12-31 23:19

It won't change anything at all apart from the maintainability of your code. I've seen code like this:

for (int i = 0; i < size; i++)
   a += b;
   System.out.println("foo");

which means this:

for (int i = 0; i < size; i++)
   a += b;
System.out.println("foo");

... but which should have been this:

for (int i = 0; i < size; i++) {
   a += b;
   System.out.println("foo");
}

Personally I always include the brackets to reduce the possibility of confusion when reading or modifying the code.

The coding conventions at every company I've worked for have required this - which is not to say that some other companies don't have different conventions...

And just in case you think it would never make a difference: I had to fix a bug once which was pretty much equivalent to the code above. It was remarkably hard to spot... (admittedly this was years ago, before I'd started unit testing, which would no doubt have made it easier to diagnose).

查看更多
几人难应
6楼-- · 2018-12-31 23:19

If you remove braces, it will only read the first line of instruction. Any additional lines will not be read. If you have more than 1 line of instruction to be executed pls use curly brace - or else exception will be thrown.

查看更多
初与友歌
7楼-- · 2018-12-31 23:20

There is no difference. The main problem with the second version is you might end up writing this:

for (...) 
  do_something();
  do_something_else();

when you update that method, thinking that do_something_else() is called inside the loop. (And that leads to head-scratching debug sessions.)

There is a second problem that the brace version doesn't have, and its possibly even harder to spot:

for (int i=0; i<3; i++);
  System.out.println("Why on earth does this print just once?");

So keep the braces unless you have a good reason, it is just a few keystrokes more.

查看更多
登录 后发表回答