-1

I am studying for an upcoming AP Computer Science Exam and I have a quick question on syntax within brackets. In the book I'm working through shows the open bracket to always be on the next line rather than being on the same line as the statement. Does it matter either way? I am used to placing the open bracket on the same line when coding, and I want to make sure I won't get docked points for continuing that habit.

Example:

if(1==1){
   //do something
}

or

if(1==1)
{
   //do something
}

I'm assuming it does not matter but I would like to make sure

rootmeanclaire
  • 808
  • 3
  • 13
  • 37
  • 1
    The compiler doesn't care. The same might not be true for your Computer Science tutor (or whoever marks your paper). You'd have to ask them. –  Apr 02 '14 at 00:41
  • It doesn't matter either way. – Kon Apr 02 '14 at 00:41
  • 1
    I see no brackets `[]`. I see parentheses `()` and braces `{}`. And brace position doesn't matter. – rgettman Apr 02 '14 at 00:41
  • It should not matter. However, writing if(x==y) is unusual and is more typically written if (x == y). – jarmod Apr 02 '14 at 00:42
  • 1
    if you are not sure then read [java code conventions](http://www.oracle.com/technetwork/java/codeconv-138413.html) – Baby Apr 02 '14 at 00:45
  • Where you put your braces is one of the great religious wars in programming. Opening brace at the end of a line of code is more common, but what you should do depends entirely on your context. You do what the people around you do for the sake of readability. I like to put my braces on their own lines because in the age of large format monitors and IDEs with collapsible code blocks, saving screen real estate is no longer an issue, but if I work on a project with others who do it the other way, then I go with the flow. – MarsAtomic Apr 02 '14 at 00:45

6 Answers6

3

The first is more commonplace in java, but both should compile just fine and will function exactly the same.

EDIT: It should be noted, however, that if you wish to use the former I personally prefer to add some spaces like so:

if (1==1) {
    //do something
}
rootmeanclaire
  • 808
  • 3
  • 13
  • 37
0

No, it does not matter. Both will be repeated in the same manner, and they both will have scopes allocated to them.

That being said, certain IDEs (and possibly the compiler) may whine about 1==1 being always true.

nanofarad
  • 40,330
  • 4
  • 86
  • 117
0

The bracket position makes no difference; white space is not relevant in java. You can write everything on one line if you like; even the entire program.

Though, for readability sake, you should split it up! :)

John Humphreys
  • 37,047
  • 37
  • 155
  • 255
  • 1
    I meant you should split up the program, not that particular bracket :p I'm personally a C/C++ guy so I like having the parenthesis on the same line as the if :) – John Humphreys Apr 02 '14 at 00:43
0

You're correct, it doesn't matter. It's a matter of personal preference. Putting the opening bracket on a new line allows the coder to more easily keep track of opening and closing brackets, and the other method is more space efficient.

Johonn
  • 149
  • 1
  • 10
0

Take a look at the Java Programming Style Guideline point 6 about Layout and Comments.

//1
while (!done) {
  doSomething();
  done = moreToDo();
}

//2
while (!done)
{
  doSomething();
  done = moreToDo();
}

The first one is recommended EXCEPT for class, interface and method should use the block layout of number 2. So it means, in your example, if you want to follow the right guideline, the layout should be like:

if (1==1) {
   //do something
}

Note that there is a space before and after the parenthesis. But although you doesn't follow the standard layout, it doesn't matter and doesn't change anything on your code.

mrjimoy_05
  • 3,452
  • 9
  • 58
  • 95
0

As other people have said, it doesn't matter for the compiler. It's more about readability, and I prefer your first method (having the opening bracket on the same line as the if statement, or any other place that may require an opening bracket.

This is effective if you're writing larger blocks of code, and your screen might be too small for you to see whether you need a closing bracket or not. You can simply hover your mouse over a closing bracket, and the IDE will show the line with the opening bracket at the top of the screen. This way, you can see which line your closing bracket lines up with, and you can fix bracket pairing problems much more easily.

ragingasiancoder
  • 616
  • 6
  • 17