Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (494)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1] 2 3
  ignore  |  Print  
  Brace styles  (Read 10351 times)
0 Members and 1 Guest are viewing this topic.
Offline nickdotjava

Junior Member




I have fallen to the dark side.  I'm using DX9


« Posted 2003-06-07 17:36:33 »

This is a constant argument among my friends and I, and we're split down the middle on it.  So how do you guys normally code?

1  
2  
3  
public foo(){

}


or

1  
2  
3  
4  
public foo()
{

}

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline Tzan

Junior Member





« Reply #1 - Posted 2003-06-07 17:44:33 »

I use #2
Offline kevglass

JGO Kernel


Medals: 163
Projects: 23
Exp: 18 years


Coder, Trainee Pixel Artist, Game Reviewer


« Reply #2 - Posted 2003-06-07 17:52:54 »

You argue about this? Jeez, do you guys have alot of time.. Wink

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #3 - Posted 2003-06-07 18:24:01 »

#2 is just plain cleaner. Using #2, you can visually line up blocks of code even in potentially confusing spots. Plus, the brace in #1 can get lost if the "if/for/while" statement is wider than the screen.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #4 - Posted 2003-06-07 19:09:28 »

Anything but #2 is crap Smiley

Offline Matzon

JGO Knight


Medals: 19
Projects: 1


I'm gonna wring your pants!


« Reply #5 - Posted 2003-06-07 19:38:45 »

#1, without a shadow of doubt

Claiming that "braces get lost" is just a matter of a crappy editor!

Quote
if () {
} else {
}


is just SOOO more cleaner than
Quote
if ()
{
}
else
{
}


But at the end of the day - just choose one and make sure that _everybody_ follow the the same rule!

Offline cfmdobbie

Senior Member


Medals: 1


Who, me?


« Reply #6 - Posted 2003-06-07 19:54:26 »

Yep, consistency is what you should aim for.  Any large project with multiple people working on the same files should come up with a coding standards document before doing anything else.  Otherwise things like CVS diff become useless as different people format their code or get their IDE to format it.  Another classic argument is "spaces or tabs?"

That said, number 2 is easily the best, and people who use number 1 should be forced to eat platefuls of gravel until they change their ways. Grin

Hellomynameis Charlie Dobbie.
Offline princec

JGO Kernel


Medals: 378
Projects: 3
Exp: 16 years


Eh? Who? What? ... Me?


« Reply #7 - Posted 2003-06-07 20:10:45 »

I like gravel*  Tongue

Cas Smiley

*bikers tend to have an affinity with the stuff anyway

Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #8 - Posted 2003-06-07 20:13:22 »

>  Claiming that "braces get lost" is just a matter of a crappy editor!

*cough*bullsh*t*cough*

Nearly all editors are able to point out where the previous brace is. The problem is that if you have a long line, the brace will be beyond scroll range. Like this:

1  
2  
3  
if(thisIsAVariable > myVariable || !myVariableCache.contains(thisIsAVariable) || thisIsAVariable < 0)
{
}


Formatting it on the same line is troublesome:

1  
2  
if(thisIsAVariable > myVariable || !myVariableCache.contains(thisIsAVariable) || thisIsAVariable < 0) {
}


Of course, many will argue that you're supposed to do this:

1  
2  
3  
4  
if(thisIsAVariable > myVariable ||
    !myVariableCache.contains(thisIsAVariable) ||
    thisIsAVariable < 0) {
}


Except that is *not* cleaner. The brace gets lost in the jumble of code and a quick visual scan will often fail to locate it.


In real life though, I don't worry too much about it. I'm usually the code lead, so I simply come in and *tell* people how they're going to do it. If they don't like it, I reformat their code on them. (My quick reformat skillz with IDEs make this less painful for me than for them.)  Just call me the BCLFH*. BWHAHAHAHA!!!

* (Bastard Code Lead From Hell)

Java Game Console Project
Last Journal Entry: 12/17/04
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #9 - Posted 2003-06-07 20:37:53 »

No.. you've put your ||'s on the wrong side !
1  
2  
3  
4  
5  
if( thisIsAVariable > myVariable
 || !myVariableCache.contains(thisIsAVariable)
 || thisIsAVariable < 0)
{
}


That's the way to do it..  then your conditional logic doesn't get lost off the side of the screen with the brace.

Aligned braces just make much more sense.  The blocks are more easily recognized.  It is much clearer to read.  The awkward zig-zagging of braces is looney Smiley

In all seriousness it really only matters what you are used to... and you can get used to almost anything.  (I still prefer aligning braces though).

It is frustrating though dealing with code that is formatted the 'wrong' way.  but most good IDEs have built-in code formatters.. so just make the code look the way you want it..  If you are working on a team it is probably best to establish one format for your source code repository, but if you have good tools you can work locally any way you want.

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #10 - Posted 2003-06-07 20:43:17 »

>  No.. you've put your ||'s on the wrong side !

Sorry, my bad. If I do it, I usually put the || on the left as well. Still, my point holds. The brace gets lost in the code and will be difficult to find.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline nickdotjava

Junior Member




I have fallen to the dark side. &nbsp;I'm using DX9


« Reply #11 - Posted 2003-06-07 21:55:44 »

Thanks guys.  We argue about it because when we help each other debug, it's hard to read each other's code.  I'll be sure to show this to all of them.  I love being right.   Grin

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline Herkules

Senior Member




Friendly fire isn't friendly!


« Reply #12 - Posted 2003-06-08 06:59:11 »

I am with #2 as well, although I often felt that #1 is kind of a Java standard.  Dunno why.

In the end, this doesn't make the quality of the source code. Can be automatically converted.


HARDCODE    --     DRTS/FlyingGuns/JPilot/JXInput  --    skype me: joerg.plewe
Offline AndersDahlberg

Junior Member





« Reply #13 - Posted 2003-06-08 07:05:36 »

Nr 1 as it is the java standard  Grin

http://java.sun.com/docs/codeconv/
Offline leknor

Junior Member




ROCK!!!


« Reply #14 - Posted 2003-06-08 07:44:17 »

I used to mildly prefer #1 but now I strongly do. The main reason for that change is the way IDEA handles code folding:
and

I've enabled what IDEA calls "highlight current scope" so if the braces are off the end of the screen it is sort of a non-issue:

I also feel a very long line of code is something to be avoided and instead of accommodating long lines I try to restructure into more readable code when possible. I don't want to encourage a bad practice by making it less painful.

In the end the real reason is the Java Coding Convention from Sun. I don't have the hubris to think "my personal way" is better. My personal convention would be a preference and an opinion, not a fact, and would be weak support for an argument.
Offline abies

Senior Member





« Reply #15 - Posted 2003-06-08 09:55:09 »

Does anybody here use
1  
2  
3  
4  
while ( something )
  {
    statements;
  }


? This is style proposed by GNU, and while I can accept both #1 and #2, I would start to cry if I would have to use GNU formatting. I like the quote from Linus - he told that everybody should print out GNU coding style guide and then burn it, as a sign Smiley


Artur Biesiadowski
Offline jbanes

JGO Coder


Projects: 1


"Java Games? Incredible! Mr. Incredible, that is!"


« Reply #16 - Posted 2003-06-08 12:46:56 »

Quote
I like the quote from Linus - he told that everybody should print out GNU coding style guide and then burn it, as a sign


Linus is right. Oh, my aching eyes! I've seen this style before and I see no advantage to it. It ends up making your code look amaturish (like someone who doesn't understand spacing), and wastes horizontal space. I've seen code like this with the code lined up with the indented braces, but that looks even worse. It strikes me that this is simply part of the "RMS World Domination Plan"(TM) of getting everyone to do things *his* way without any real benefit.

Java Game Console Project
Last Journal Entry: 12/17/04
Offline swpalmer

JGO Coder




Where's the Kaboom?


« Reply #17 - Posted 2003-06-08 16:44:09 »

Yep, the GNU style is just plain brain dead.  At least the non-aligned style has the one advantage of saving a little vertical space (at the expense of readability).  This business of indenting braces is simply dumb.

Offline GergisKhan

Junior Member




&quot;C8 H10 N4 O2&quot;


« Reply #18 - Posted 2003-06-08 22:01:05 »

With most code editors now allowing for "folding code" (that is to say, a little plus-sign twiddle that allows you to hide the code between the braces at your discretion) I find there is no reason to worry about vertical space.  

For myself I always have used

public static void main( String args[] )
{
  // blah
  // blah
  // blah
}

But that's me.  I could get really evil and ask what you guys do about line and parenthesis spacing.   Grin

gK

"Go.  Teach them not to mess with us."
          -- Cao Cao, Dynasty Warriors 3
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #19 - Posted 2003-06-23 07:00:01 »

Quote
Yep, consistency is what you should aim for.  Any large project with multiple people working on the same files should come up with a coding standards document before doing anything else.


Except that, nowadays, if your IDE can't automatically "convert-on-open" (and possibly - if you're feeling generous towards people without IDE's - "de-convert-on-close") then you need to get a new IDE.

There's no longer any need for a coding standards doc for formatting purposes; fire anyone who refuses to use a decent IDE (even VI/VIM can do this kind of thing automatically). Of course, variable names etc still need a standards doc.

malloc will be first against the wall when the revolution comes...
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #20 - Posted 2003-06-23 07:05:41 »

Quote
With most code editors now allowing for "folding code" (that is to say, a little plus-sign twiddle that allows you to hide the code between the braces at your discretion) I find there is no reason to worry about vertical space.  


The most ancient of the few C++ gurus I know used to agree that the main reason for style 1 was back in the day of poor IDE's, where it was a distinct time-saver to reduce the number of lines in your source code, and save you all that scrolling!

Back when C++ was pretty new, things like this became a de-facto standard because they were most appropriate for the tools people were using. Similarly, it was back in those Dark Ages that super-short variable names were invented, which included FULL type information for the variable, using one-letter codes.

E.g.:
1  
  public static final boolean[] pSfbAmyArray;


When you can't do syntax-highlighting, and your compiler isn't too hot at "invalid type conversions", things like that can be a life saver. But for normal people nowadays it's just silly.

Equally, the fear that some people still have of writing any variable name more than about 6 characters long used to be valid. But really - is it that hard to learn to hit the TAB key? (or ctrl-space if you've been trained by MSVC)?

malloc will be first against the wall when the revolution comes...
Offline blahblahblahh

JGO Coder


Medals: 1


http://t-machine.org


« Reply #21 - Posted 2003-06-23 07:23:58 »

Quote
I used to mildly prefer #1 but now I strongly do. The main reason for that change is the way IDEA handles code folding:


Sad. Sounds like a bug, rather than a feature (if it's deliberate, it should at least be configurable).

Syntactically, the two styles are identical. So, an IDE should not differentiate between them.

malloc will be first against the wall when the revolution comes...
Offline sugarshark

Junior Member




Sugar to the sharks.


« Reply #22 - Posted 2003-06-26 07:22:38 »

I prefer style #1. Coming from an Unix background, where all the C code is in K&R style, this #2 'braces on extra lines' style wastes space and just makes my eyes ache :-)

But for method declarations which extend to the next line I place the brace on an extra line like this:
1  
2  
3  
4  
public void computeSomething(String name, List list)
    throws SomeException, OtherException
{
}

With the checkstyle tool this method is called nlow.

This is an never ending subject, because everyone, including me, has strong preferences how code should look like. Which is quite understandable if you work with code every day of your life. That these preferences can change over time does not hinder people to promote their current ones with religious fever  Grin

I used to think that the brain was the most wonderful organ in my body.  
Then I realized who was telling me this.
-- Emo Phillips
Offline larry

Junior Member




.. son of jor-el, kneel before zod ...


« Reply #23 - Posted 2003-06-26 08:55:31 »

geez - you guys just have too much time on your hands!
isnt this forum for people developing cutting-edge AI algorithms and Phong shaded j2me clients which make coffee on sundays and public holidays ? Grin

just kidding - its amazing how this argument surfaces over and over again!

Ive worked on some anal projects where the team leader enforces a specific style. Personally I prefer #2 and find #1 archaic and deperecated.

more importantly than syntax, is clean code design and good javadocing.
Offline Mojomonkey

Senior Member




ooh ooh eee eeee


« Reply #24 - Posted 2003-06-26 10:17:28 »

What I find humourous about this is that I use the:

block {
}

style... the reason I code this way is that is the default way Eclipse formats code, and I'm waaay to lazy to change it.

But as has been mentioned, I don't think it matters as long as it is consistent through the file(s), and across the dev team. There is nothing worse than reading Joe Smith's code and then reading John Jone's code which is a completely different style.

Don't send a man to do a monkey's work.
Offline nech_neb

Junior Member




Java for LIFE !!!


« Reply #25 - Posted 2003-06-30 17:42:58 »

Quote
Nr 1 as it is the java standard  Grin

http://java.sun.com/docs/codeconv/


I had this arguement with my team leader the other day.
He prefered #1, I prefered #2.

He sent me this exact link...
Now, correct me if I'm wrong (seeing how I just browsed
through it) but nothing in the document explicitly states
the use of the braces to be coding convention #1.

All the examples are given in Convention #1, but
the examples are mainly used for showing a
specific convention not related to the braces
convention.

Anyways, I guess I jumped on this thread a bit late...
hopefully someone still reads it ^__^
Offline leknor

Junior Member




ROCK!!!


« Reply #26 - Posted 2003-06-30 18:16:41 »

Quote
Now, correct me if I'm wrong (seeing how I just browsed through it) but nothing in the document explicitly states the use of the braces to be coding convention #1.
 See the second bullet: "Open brace "{" appears at the end of the same line as the declaration statement"
Offline nech_neb

Junior Member




Java for LIFE !!!


« Reply #27 - Posted 2003-06-30 18:28:33 »

Quote


Ahh, I stand corrected...

But just to argue for arguing's sake,
This is only for class and interface declaration.
one could argue that the rules do not specificly
apply to scope blocks.


Offline nickdotjava

Junior Member




I have fallen to the dark side. &nbsp;I'm using DX9


« Reply #28 - Posted 2003-06-30 18:34:30 »

He he he...  This thread will never die.  I've created a monster!  MWA HA HA HA HA!

But from what I've read, #2 is more commonly used.  So what good's a convention if the majority doesn't follow it?   Tongue

Oh, and I use #2.   Grin

-Nick

"Oh ya, that's trivial.  I should have it done in an hour."
Offline Mojomonkey

Senior Member




ooh ooh eee eeee


« Reply #29 - Posted 2003-06-30 18:45:45 »

Quote
So what good's a convention if the majority doesn't follow it?


It doesn't matter what the majority is following, only matters for what your team is following. IMHO It's not important to define a standard across the board, only standardize for a project.

Don't send a man to do a monkey's work.
Pages: [1] 2 3
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Dwinin (19 views)
2014-09-12 09:08:26

Norakomi (54 views)
2014-09-10 13:57:51

TehJavaDev (63 views)
2014-09-10 06:39:09

Tekkerue (31 views)
2014-09-09 02:24:56

mitcheeb (53 views)
2014-09-08 06:06:29

BurntPizza (37 views)
2014-09-07 01:13:42

Longarmx (23 views)
2014-09-07 01:12:14

Longarmx (27 views)
2014-09-07 01:11:22

Longarmx (27 views)
2014-09-07 01:10:19

mitcheeb (35 views)
2014-09-04 23:08:59
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!