Results 1 to 7 of 7
Like Tree2Likes
  • 1 Post By KevinWorkman
  • 1 Post By Tolls

Thread: Which is the more popular format for braces and what is your preference?

  1. #1
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Which is the more popular format for braces and what is your preference?

    I was tought to line my braces up like so

    Java Code:
    public class Animal 
    {
    	public void noise()
    	{
    		System.out.println("I want some tea");
    	}
    }
    but I see this form is common, although I find it more difficult to read

    Java Code:
    public class Animal_61 {
    
    	public void noise(){
    		System.out.println("Animals dont make noise");
    	}
    }
    I want to ask those with experience what they think is more common and what tone is likely to find in industry
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Which is the more popular format for braces and what is your preference?

    It's really up to you. I doubt anybody is going to begrudge you this, especially because IDEs can auto-indent however you want.

    But the standard code conventions go with the second way: Code Conventions for the Java Programming Language: 7. Statements
    "The opening brace should be at the end of the line that begins the compound statement; the closing brace should begin a line and be indented to the beginning of the compound statement."

    I would do whatever you're most comfortable with, and if you don't have a preference, go with the standard style. And both of those (your preference and the standard conventions) can be overruled if you work at a place that micromanages you to the point of specifying where brackets should go.
    EscSequenceAlpha likes this.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    19

    Default Re: Which is the more popular format for braces and what is your preference?

    Industry will have a code format style embedded into its IDE, which will "correct" whatever formatting "mistakes" you might make.
    At least that's been the case at the last 4 places I've been at.
    EscSequenceAlpha likes this.
    Please do not ask for code as refusal often offends.

  4. #4
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    6

    Default Re: Which is the more popular format for braces and what is your preference?

    I by far prefer the second form you provided, and don't personally know anyone other than students early into their programming courses who use the former. I don't mean that as an insult to anyone, as it really is preference, but in practice, the first form adds a ton of whitespace. When your programs get large and deep, all the whitespace can make it really difficult to figure out what level of nesting you're in.

    This is also a matter of preference, but anything other than [comments or formatting for cleanliness] that adds a lot of in-line whitespace can make the logic difficult to follow.

    The second form looked alien to me at first (as I learned the first way) but I decided to try it for a week, and never looked back.

    Consistent and clean code is more important than anything else though, so regardless of your style, as long as you stick to it, it will give you the best result.

  5. #5
    EscSequenceAlpha is offline Senior Member
    Join Date
    Apr 2012
    Posts
    115
    Rep Power
    0

    Default Re: Which is the more popular format for braces and what is your preference?

    Thanks guys, the reason I asked this is because I find it difficult to read code the second way, I wonder if IDEs can converted between the two? for example if I was a programmer could I auto format it my way, do my job, then when I'm done autoformat back?

    actually, I bet with more experience looking at code it probably wont matter.
    Legend has it the moderators and senior members of java-forums.org were able to code skyrim using only 701 lines of java... or so the legend goes.

  6. #6
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,005
    Blog Entries
    7
    Rep Power
    20

    Default Re: Which is the more popular format for braces and what is your preference?

    Quote Originally Posted by EscSequenceAlpha View Post
    Thanks guys, the reason I asked this is because I find it difficult to read code the second way, I wonder if IDEs can converted between the two? for example if I was a programmer could I auto format it my way, do my job, then when I'm done autoformat back?

    actually, I bet with more experience looking at code it probably wont matter.
    Eclipse (I don't know anything about Netbeans) can format your code (almost) any way you want.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,691
    Rep Power
    8

    Default Re: Which is the more popular format for braces and what is your preference?

    Quote Originally Posted by EscSequenceAlpha View Post
    Thanks guys, the reason I asked this is because I find it difficult to read code the second way, I wonder if IDEs can converted between the two? for example if I was a programmer could I auto format it my way, do my job, then when I'm done autoformat back?
    Yep. Seems like a lot of extra work though.

    Quote Originally Posted by EscSequenceAlpha View Post
    actually, I bet with more experience looking at code it probably wont matter.
    Yep.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Why eclipse is more popular?
    By Callofdudey in forum New To Java
    Replies: 3
    Last Post: 10-03-2011, 03:12 PM
  2. Replies: 1
    Last Post: 05-03-2011, 10:11 AM
  3. is android that popular?
    By clydedoris in forum Android
    Replies: 2
    Last Post: 07-12-2010, 12:14 PM
  4. Right-side for left curly braces
    By zweibieren in forum AWT / Swing
    Replies: 5
    Last Post: 11-02-2009, 08:08 AM
  5. My Preference Dialog
    By schuetzejanett in forum Eclipse
    Replies: 3
    Last Post: 08-10-2007, 10:48 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •