Results 1 to 15 of 15
Like Tree2Likes
  • 2 Post By SurfMan

Thread: Code formatting?

  1. #1
    Nozz is offline Member
    Join Date
    Apr 2014
    Posts
    49
    Rep Power
    0

    Default Code formatting?

    Is there any guide how to write code to make it look good? Is there any standard?
    For example I wonder what is better.
    This:
    Java Code:
    if(true)
    {
           // sth
    }
    Or this:
    Java Code:
    if(true){
           // sth
    }
    Well, even without any guide etc, everyone knows few basic rules. Its simple, if you see code, you write your like that what you have seen. But I want to make it more clear, better, and fix my misakes.

  2. #2
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Code formatting?

    both are acceptable.

  3. #3
    superhaNds is offline Senior Member
    Join Date
    Apr 2013
    Location
    Sweden
    Posts
    264
    Rep Power
    2

    Default Re: Code formatting?

    There are java code conventions forgot to mention. Look it up

  4. #4
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Code formatting?

    This document should keep you entertained for a few hours: Code Conventions for Java | Steve Yohanan
    Nozz and kneitzel like this.
    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Code formatting?

    Oh nice link, the article on the official Oracle site is broken beyond repair :/
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Code formatting?

    I simply let Eclipse do the silly work: Ctrl-A, Ctrl-Shift-F and my entire source file is neatly formatted (the way K&R did it; I hate that Pascal-like curly bracket positioning *).

    kind regards,

    Jos

    (*) Eclipse can do that too ...
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Code formatting?

    You don't need to select anything to have the formatting done though. CTRL+SHIFT+F is enough :)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Code formatting?

    Quote Originally Posted by gimbal2 View Post
    You don't need to select anything to have the formatting done though. CTRL+SHIFT+F is enough :)
    Oh yeah? Well, I prefer to do a Ctrl-A before I do a Ctrl-Shift-F; so there. Nowhere it is stated that doing a Ctrl-A before a Ctrl-Shift-F is forbidden and illegal and my fingers are used to doing a Ctrl-A Ctril-Shift-F; it feels more natural than just a naked Ctrl-Shift-F.

    kind regards,

    Jos (<--- Ctrl-Shift-F to you too)
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Code formatting?

    Boohoo, Jos said a bad key combo :( I'm telling the teacher.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Code formatting?

    The debate on which code conventions you should use have started many battles where much blood was shed. Do curly brackets go on the same line or the next line? (same) Are indentations spaces or tabs? (tabs) What is the limit on characters per line? (none)

    As long as you stick to *some* standard (and at least obey the naming conventions where methods and variables start with a lower-case letter and classes start with an upper-case letter) you should be okay.

    I also do ctrl+A before ctrl+I to auto-format. Ctrl+I by itself just formats the current line.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  11. #11
    SurfMan's Avatar
    SurfMan is offline Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    947
    Rep Power
    2

    Default Re: Code formatting?

    I am using the ever superior IntelliJ IDEA, so now we have a code convention war AND an IDE war going!!!1one! Mwuahaha!!!

    "It's not fixed until you stop calling the problem weird and you understand what was wrong." - gimbal2 2013

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Code formatting?

    I don't see how spaces/tabs factor into code conventions though, that would be more controlled by the type of tooling you apply. Whether you have a tab of 4 spaces or 4 spaces, it looks the same.

    I use spaces myself, because then everything gets converted into spaces and you don't get wonky invisible space/tab combinations which tends to happen when you copy/paste stuff from somewhere else.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Code formatting?

    I agree that spaces vs tabs doesn't really matter, but that doesn't stop people from fighting about it!
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: Code formatting?

    Quote Originally Posted by KevinWorkman View Post
    I agree that spaces vs tabs doesn't really matter, but that doesn't stop people from fighting about it!
    Luckily we are more civilized, we just do fish-slapping dances.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,529
    Blog Entries
    7
    Rep Power
    20

    Default Re: Code formatting?

    Quote Originally Posted by SurfMan View Post
    I am using the ever superior IntelliJ IDEA, so now we have a code convention war AND an IDE war going!!!1one! Mwuahaha!!!
    Mwuahaha your mother; and what's that sissie with his Ctrl-Alt-L doing here? Formatting is only properly done by pressing Ctrl-A Ctrl-Shift-F; every real man (e.g. me) knows that.

    kindest regards,

    Jos (<--- doesn't drink XXX soda water, drinks Grolsch instead(
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Code formatting bug
    By DarrylBurke in forum Suggestions & Feedback
    Replies: 3
    Last Post: 05-04-2012, 04:19 PM
  2. lines truncated in code formatting
    By zweibieren in forum Suggestions & Feedback
    Replies: 6
    Last Post: 12-19-2011, 06:13 PM
  3. Code formatting Issue
    By camickr in forum Suggestions & Feedback
    Replies: 21
    Last Post: 10-08-2011, 04:19 PM
  4. Formatting Java Code
    By DoubleDee in forum Advanced Java
    Replies: 5
    Last Post: 09-18-2010, 10:33 PM
  5. Need help formatting output and some code
    By A5i19 in forum New To Java
    Replies: 6
    Last Post: 11-09-2009, 04:58 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
  •