wood burning stoves*
The moose likes Ranch Office and the fly likes Coding style Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Ranch Office
Bookmark "Coding style" Watch "Coding style" New topic
Author

Coding style

Jacob George
Ranch Hand

Joined: Jun 26, 2001
Posts: 46
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

Joined: Mar 02, 2000
Posts: 3143
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.


Pounding at a thick stone wall won't move it, sometimes, you need to step back to see the way around.
Jessica Sant
Sheriff

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Dec 10, 2001
Posts: 7023
But you crazy HP Middlewarers use 3 spaces for indentation. That's just weird.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
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

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Jul 22, 2000
Posts: 9052
    
  12
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 ]

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Jacob George
Ranch Hand

Joined: Jun 26, 2001
Posts: 46
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

Joined: Oct 17, 2001
Posts: 4313

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

Joined: Jul 22, 2000
Posts: 9052
    
  12
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.
 
Consider Paul's rocket mass heater.
 
subject: Coding style