• Post Reply Bookmark Topic Watch Topic
  • New Topic

silly question about spacing braces  RSS feed

 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi guys

noticed on a video that the instructor spaces braces like this:

i always have a tendency to do

does it really matter? is there a convention regarding this...been meaning to get this out the way

thanks


 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, it matters.  The video is using an accepted formatting.  The other accepted way is:

Notice that in both formatting methods, the last brace is on its own line.  Here is a thread on the subject.
 
jon ninpoja
Ranch Hand
Posts: 291
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey knut

which do you use?

i think

seems to make sense,as it looks easier to read...your thoughts?
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use

because I started with the C language and I think it's more compact, but the other way is perfectly acceptable.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja,

When posting any amount of code, please enclose it with code tags. That makes your code look like an actual code rather than text.
I have added code tags to your latest two posts. Please next time try to achieve that yourself. You need to select/highlight your code part and click 'code' button in the buttons panel.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jon ninpoja wrote:. . .
. . .
does it really matter? is there a convention regarding this . . .
As Knute has already said, yes, it does matter, and yes there are conventions. Not only s‍hould the final } be on a line by itself, but also the code shoul‍d be indented.. . . There are subtle differences between conventions, for example about how many spaces to go to the right/
 
Ryan McGuire
Ranch Hand
Posts: 1143
9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:As Knute has already said, yes, it does matter, and yes there are conventions. Not only s‍hould the final } be on a line by itself, but also the code shoul‍d be indented.. . . There are subtle differences between conventions, for example about how many spaces to go to the right/


I agree the closing brace should be on line by itself.  And yes, indent the code.

As for the difference between having the opening brace on a separate line versus at the end of the if() line...
If you ALWAYS use braces and everyone on your team ALWAYS uses braces, even when they aren't strictly needed, then I would say you can (but don't have to) save a little vertical space by putting the { at the end of the if () line.  On the other hand, if you sometimes leave out the braces (a practice I don't recommend), then I would put the opening one on a separate line to always make it obvious it's there.
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think there are two factors that determine which convention to follow:-
  • 1: What you have got used to, but this is overridden by...
  • 2: The style guidelines at your workplace.
  • Whichever form you use, be sure to be consistent. I usually use the first convention which I think is called BSD/Allman, but on my own PC I use 4 spaces per level and on the Ranch 3 spaces per level because the Ranch website doesn't support automatic indentation and 3 is quicker.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!