• Post Reply Bookmark Topic Watch Topic
  • New Topic

What Style to follow ?  RSS feed

 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was reading the Style guide in javaRanch and found that it says to use this style

foo()
{
....
....
....
}

rather than this

foo(){
....
....
}

I was wondering why is it so? If it is because the opening and closing braces match up then the later is better because the ending brace matches to the function name
What do you prefer and Why ?

Also I was unable to understand this example under Section 3.3 ? If anyone knows Please share ....

Thank you
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason for writing
foo() {

was that it saved a line when screens were much smaller. Keeping braces vertically paired makes it easier to match a brace with its pair.
The bit about initialising means you ought not to leave un-initialised local variables. Declare them when they are actually needed, not at the beginning of the method. Note in C it may be necessary to declare local variables at the beginning of a function, but that doesn't apply in Java.
 
Gregg Bolinger
Ranch Hand
Posts: 15304
6
Chrome IntelliJ IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The reason for writing
foo() {

was that it saved a line when screens were much smaller. Keeping braces vertically paired makes it easier to match a brace with its pair.


Unless you use a semi-modern IDE. Then it doesn't matter. What does matter more than anything is being consistant and adhering to your team's coding style.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Gregg Bolinger wrote:What does matter more than anything is being consistant and adhering to your team's coding style.

+1

heck, i'd +1000 it if I could.
 
Gary Ba
Ranch Hand
Posts: 161
Firefox Browser Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also I was unable to understand this example under Section 3.3 ?


There is another forum called Cattle Drive and it follows the Javaranch Programming Style Guide. As far as the code from Section 3.3 (comparing the first two example), the second example does not declare

and assign something..

but instead it does the two lines in one line...

The same goes for the third example with


The Cattle Drive strictly follows that programming style guide. As you can see from examples...less code, easy to read.

I hope this helps,
Gary
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brace placement is a religious war; "better" is subjective. I personally can't *stand* having opening braces on their own lines, but everyone has their own opinion.
 
Jai Mani
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank You everyone for your valuable thoughts , This cleared up things !!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!