Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Coding style

 
Jacob George
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I was just going through the coding styles discussed under http://www.javaranch.com/style.jsp. But I was surprised to find
class x
{
}
style of coding instead of
class x{
}
I thought that former is the C/C++ style of coding and latter is Java. Also, when I checked the source code downloaded with JDK, that too was done using the latter coding style. So which is the actual coding style that should be followed?
regards Jacob
 
Angela Poynton
Ranch Hand
Posts: 3143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey we haven't had THIS discussion for a while!
Basically the prefered style can change from person to person, many companies have specific style guidelines, mine for instance. It IS a preference though. The Style Guide here is aimed at helping people make their code as neat, tidy and readable as possible, some people may feel that they prefer alternatives though.
However, since in the industry developers are generally asked to conform to some kind of style rules, we made our own style guide primarily for the use of those taking part in The Cattle Drive, to pass an assignment there your code must obey the Javaranch rules, just like here where I work, to get your code past a code review it must obey the company rules.
Basically there is no right or wrong, only what those who set the rules deem to be preferable.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find this way much easier to read. You can easily count brackets and make sure they're all lined up correctly.

I remember reading somewhere that this style was mostly for books (it takes up a lot fewer lines). For what its worth the above style is what we use here at HP Middleware as well.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But you crazy HP Middlewarers use 3 spaces for indentation. That's just weird.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not to mention that perverse lack of space after keywords like if and for. :roll: What are those, method calls?
I thought that former is the C/C++ style of coding and latter is Java.
Not at all - plenty of people use the latter in C/C++, and plenty use the former in Java. It so happens that Sun's official style is the latter, so that's what you see in JDK source code. And as Jessica notes, it's more common in written books, because it takes fewer lines, and paper costs money. But for code stored in files rather than printed on paper, this is not really a concern.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Dirk Schreckmann:
But you crazy HP Middlewarers use 3 spaces for indentation. That's just weird.

I guess the 3 space thing was a compromise between the 2 space people and the 4 space people...
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, Peter van der Linden states in the new edition of Just Java 2 that the K&R way, the one that looks like

class SoAndSo{
}

is the "Java way". It is more correctly the "Sun way" since that is what their Style Guide requires.
[ August 09, 2002: Message edited by: Marilyn de Queiroz ]
 
Jacob George
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
The reason why I asked is that, here at our company I was advocating for the needs to have coding standards, conventions and was taking class on them. From u'r response, it seems that, there is nothing like "Java style" coding. I firmly believe that C/C++ coding style is entirely different from that of Java(On seeing coding/naming conventions from Sun). Also, from the posts, it appears that u people are strictly following the indentation tabs specified by Sun; but not in the case of braces.
 
Jessica Sant
Sheriff
Posts: 4313
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you'll find that coding standards vary from company to company and possibly even project to project. But no matter what they are -- I believe they're VERY important. It goes to maintainability of code and readability -- you're not the only one who will look at your code after your done. And more likely than not, at some point in the future, someone will have to come back and edit your code.
I hate looking at code like this:
 
Marilyn de Queiroz
Sheriff
Posts: 9066
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jacob George:
From u'r response, it seems that, there is nothing like "Java style" coding. I firmly believe that C/C++ coding style is entirely different from that of Java(On seeing coding/naming conventions from Sun). Also, from the posts, it appears that u people are strictly following the indentation tabs specified by Sun; but not in the case of braces.

You may have misinterpreted my post. I agree that C/C++ coding style is entirely different from that of Java, particularly in the matter of coding/naming conventions.

What I meant to say is that there is no "java convention" regarding brace placement -- some use the K&R style like you see in Sun's Style Guide and some use the pascal style you see in the JavaRanch Style Guide

Also, style guides will vary regarding the number of spaces used for indenting from 2 spaces to 4 spaces.

However, I believe you will find more similarities than differences between the two above mentioned Style Guides.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic