Poll... coding conventions

This is a cross post from a thread in the Java Programming Forum. If you haven't read this there, read it here and please reply if you have a minute.

> I'm conducting a personal study about some stuff, just

> out of curiousity.

>

> Who writes code like this:

> >publicvoid method ()

>{

>for (...)

>{

>}

>}

>

> What about this:

> >publicvoid method (){

>for (...){

>}

>}

>

> Who puts semicolons after }'s:

> >publicvoid method ()

>{

>for (...)

>{

>};

>};

>

>publicvoid method (){

>for (...){

>};

>};

>

> I'd like to know how you usually format your code.

> Additionally:

>

> 1) How old are you?

> 2) Where do you work/go to school?

> 3) What (if any) was your primary programming language

> before Java?

> 4) How did you primarily learn how to program in Java

> (book, school, examples, etc...)?

>

> I'm just curious. I have some theories that I don't

> want to share until I get a decent amount of replies.

>

> Cheers,

> Jason

[2192 byte] By [adramolek] at [2007-9-27 21:22:24]
# 1

This is my preferred convention( although the others are also used ), for the simple fact that you can easily "eye-ball" the open & closed brackets when the code gets longer.

public void method( )

{

for( int i = 0; i < 10; i++ )

{

--use only if more than one line of code,

--otherwise leave off brackets altogether

}

}

But I prefer not to give any more details. ; 0 )

ArtVandelay at 2007-7-7 3:17:07 > top of Java-index,Other Topics,Algorithms...
# 2

public static void main(String[] args) {

if (sayHello) {

System.out.println("Hello");

}

else {

System.out.println("No Hello");

}

}

(Hope it hasn't got errors in it ;-)

I always {}

my loops, experience taught me that the one

loop you don't do it too will be the one that you (or someone else)

adds a line to and forgets that the line isn't in the loop.

The bracket positions are habit. That's the way I write it, but I let

my editor's beautifier have the final say !

I'm 33, Melbourne Australia, Working as a Senior S/W Engineer.

Before Java I've mainly done C, C++, Ada + assorted Unix shells.

Learnt Java on a short course and then from books, samples and these

forums.

john3136 at 2007-7-7 3:17:07 > top of Java-index,Other Topics,Algorithms...
# 3

I've come to prefer the K&R bracing style. One of the big reasons

is else blocks. Whether the block of code associated with an "else" is executed or not, depends on the logical expression following the preceeding "if". So I prefer this format:

if (expr) {

doSomething();

} else {

doSomethingElse();

}

By putting the else on the same line as the if block's closing brace, it reinforces the logical connection between the else and the if.

Whereas, putting the else on its own line:

if (expr) {

doSomething();

}

else {

doSomethingElse();

}

seems less intuitive to me. It seems to be suggesting that the else is independant of the if, and could just as easily be a "while", for example.

But whenever I mention this, nobody agrees with me.

paulcw at 2007-7-7 3:17:07 > top of Java-index,Other Topics,Algorithms...
# 4

for most of my if's, if possible i will do them:

if(!expr){

return;

}

//else

doThis();

if short enough, my methods will sometimes look like:

public void getSomething(){ return something; }

and..

public void yep(){

//stuff;

}

silkm at 2007-7-7 3:17:07 > top of Java-index,Other Topics,Algorithms...
# 5

if (expr) {

doSomething();

} else {

doSomethingElse();

}

I agree with you in that I use that same convention for brackets. But I don't agree with your justification for it. In fact I don't agree with anybody's justification for why brackets should be done one way or another. One person's "seems logical" is another person's "totally confusing", and everyone tries to produce a convincing reason why it IS logical and NOT confusing. But since all of these explanations attempt to explain conflicting things, not all of them can be correct. My belief is that none of them are correct. It's like trying to explain why you should put on your right shoe before your left.

DrClap at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...
# 6

> if (expr) {

>doSomething();

> } else {

>doSomethingElse();

> }

> I agree with you in that I use that same

> convention for brackets. But I don't agree with your

> justification for it. In fact I don't agree with

> anybody's justification for why brackets should be

> done one way or another.

> [...]

> My belief is that none of them are correct. It's like

> trying to explain why you should put on your right

> shoe before your left.

I disagree.

Things should be consistent. So, if you write do {

// stuff

}while (a);

instead of do {

// stuff

}

while (a);

then you should also write if (a) {

// stuff

}else if (b) {

// stuff

}

instead of if (a) {

// stuff

}

else if (b) {

// stuff

}

However, if you write a do-while loop like do {

// stuff

}

while (a);

then you cannot distinguish it from the following while loop if only the last line is visible (the rest might be e.g. outside the viewport of your editor): while (a);

and this is clearly a Bad Thing™, so therefore you should write it the other way.

- Marcus Sundman

msundman at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...
# 7

public static void main(String [] args)

{

if (sayHello)

{

System.out.println("Hello");

}

else

{

System.out.println("No Hello");

}

}

I always use {} and always on separate lines.

I'm 27, from Gothenburg Sweden. Working as a Senior S/W Engineer.

Before Java I've mainly done C++. Learnt Java on a short course

and then from books, samples and these forums.

patrik olsson at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...
# 8

Hi Jason!

This is almost like religion, but anyway:

My example:

public void method( "" ) //Note the whitespace

throws EtcException

{

if( someexpr )//Whitespace here also

{

}

}

Bio etc:

27 Yrs old

CS student

Coding PHP and ASP + VB before Java

Myself.

Cannot wait to hear about your theories!

Regards, RM

deenen at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...
# 9

I (and all of my colleagues) use the following style:

public ReturnType

methodname( type param1 , ... )

{

if ( expr )

{

expr ;

}

methodcall (...);

So all brackets are in the same depth (int multiple of tabs (=8 spaces)) as the code, they wrap. The same is true for all loops, class and interface definitions and so on.

The method definition has the methodname on a new line starting on column 1 with the opening brace '(' immediately following. Method calls

have a space between name and opening brace.

There are quite some more conventions which we have defined. All code is checked for conformance to these conventions.

Age: 38

Senior consultant

Previous main prog-lang: C

hlemcke at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...
# 10

> > Who writes code like this:

> > > >public void method ()

> >{

> > for (...)

> > {

> > }

> >}

> >

people who have C++ background

> > What about this:

> > > >public void method () {

> > for (...) {

> > }

> >}

> >

people w/ C background

> > Who puts semicolons after }'s:

> > > >public void method ()

> >{

> > for (...)

> > {

> > };

> >};

> >

> >public void method () {

> > for (...) {

> > };

> >};

> >

same deal

> > I'd like to know how you usually format your code.

I use C++ way:

bc: more readable & symmetric...

Did you ever wonder why C style formatting in Java is still around?

ibosan at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...
# 11

Heh... I wrote up a little article on code formatting, you can see some examples of my code there. It currently resides in web space I haven't paid for since August, so mirror it if you think it's funny/useful.

http://webpages.smyrnacable.net/krum/1tbs.html

About me:

I'm 25 years old. I learned Apple BASIC in grade school, dropped out of high school so I could spend more time at the library, taught myself C and a little C++, then started playing with Java when it was the Next Big Thing. I learned mostly from books. I do not write software for a living and have no aspiration to do so. A few people actually use my SWATH scripts, though.

KJKrum at 2007-7-7 3:17:08 > top of Java-index,Other Topics,Algorithms...