SitePoint Sponsor

User Tag List

View Poll Results: What format do you use for coding?

Voters
54. You may not vote on this poll
  • A: Opening brace on same line

    34 62.96%
  • B: Opening brace indented on new line

    9 16.67%
  • C: Other

    11 20.37%
Page 3 of 3 FirstFirst 123
Results 51 to 62 of 62
  1. #51
    Community Advisor silver trophy

    Join Date
    Nov 2006
    Location
    UK
    Posts
    2,547
    Mentioned
    40 Post(s)
    Tagged
    1 Thread(s)
    I'm with Zalucius and ScallioXTX (style known as ANSI or Allman)

    Style B is known as Whitesmiths.

  2. #52
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A is known as K&R or 1TBS - the One True Brace Style.

    Allman has the opening braces on a new line, but not indented.

  3. #53
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by Stormrider View Post
    A is known as K&R or 1TBS - the One True Brace Style.
    Not quite. K&R and 1TBS are where classes and functions have braces on a new line, while control statements keep it on the same line.

    Code:
    function func()
    {
        if (condition) {
            ...
        }
    }
    K&R allows optional braces on control statements.

    Code:
    if (condition)
        func();
    1TBS makes braces mandatory.

    Code:
    if (condition) {
        func();
    }
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  4. #54
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yeh, which is what a lot of people seem to do. I use 1TBS but with functions and classes following the same rules as everything else.

  5. #55
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    I use a different line for class and functions, but the same line on if, else, foreach parts.

    Code php:
    function someFunc()
    {
        if (condition) {
            ...
        }
    }
    Same here, function, class or switch I put it on a new line, everything else gets it inline.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  6. #56
    SitePoint Addict dnordstrom's Avatar
    Join Date
    Jul 2006
    Location
    Amsterdam
    Posts
    337
    Mentioned
    18 Post(s)
    Tagged
    0 Thread(s)
    Code PHP:
    class Demo {
      function __construct() {
        echo 'Opening brace on same line.';
      }
    }

    In PHP I type opening brace on the same line, after a space. That's what I learned when I read my first C++ book and it has stuck with me since then. Neat indenting makes this very clean and easy to read in my opinion. I'm currently reading the book "Clean Code: A Handbook of Agile Software Craftsmanship" which I really recommend for both intermediate and advanced coders fine-tuning their habit of writing clean code.

    Recently I've been using Ruby though so I haven't had to care much about braces at all.

    But let's not forget what's most important of all for writing clean code; consistency. You, and your team, should settle for a coding style and go with it during the entire project. I've seen projects where different programmers use different styles and it isn't pretty.
    Daniel Nordstrom. of. Nintera(ctive)
    -- Featured post: Part 2. Writing NI.JS JavaScript
    ----- Follow me on Twitter. Got project? Contact me.
    -------- SitePoint: Community Guidelines Be A Great Member

  7. #57
    SitePoint Member w3dx's Avatar
    Join Date
    Feb 2011
    Location
    England
    Posts
    13
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I favour style "A", or rather a variation of 1TBS as mentioned earlier. Like @stormrider, I style classes and functions the same as other control structures.

    However, with "A" the 2nd to last closing brace should be indented...
    PHP Code:
    function name() { 
       function 
    last() { 
          
    // Do something 
       


    The closing brace always matches a control statement... function, if, while, etc.

    I used to code in Delphi as well, although I have never mimicked the positioning of BEGIN...END with the opening/closing braces {...} as in Allmans style - I think because the opening brace on its own always looked like a waste of space to me and by including the opening brace on the same line you could see more code without having to scroll.

    I actually find style "B" where you have indented the braces as well (Whitesmiths style) a bit confusing. If I was to give the opening brace its own line I wouldn't indent it, keeping it inline with the control structure (which is more like true 1TBS) ...
    PHP Code:
    function name() 

       function 
    last() 
       { 
          
    //Do something 
       



  8. #58
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    0
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    opening brace on a new line PLEASE people!!

  9. #59
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,684
    Mentioned
    100 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by benj80 View Post
    opening brace on a new line PLEASE people!!
    Is that a personal preference, or a style guide of some particular name that you require to be followed?
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript

  10. #60
    SitePoint Enthusiast
    Join Date
    Dec 2007
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by paul_wilkins View Post
    Is that a personal preference, or a style guide of some particular name that you require to be followed?
    Can't you see the urgency? This is how it simply must be done! PLEASE PAUL!

  11. #61
    SitePoint Guru
    Join Date
    Aug 2009
    Posts
    669
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't mind the opening brace on the same line as long as the closing brace is indented so you can see what it is closing. The number of bits I code I see with this is a disgrace:

    PHP Code:
    function name(){
       
    //do something
       
    function else(){
          
    //do something
    }

    Imagine having a rather large file to debug and all the closing braces are like that. Nasty stuff that is.
    I'll do anything to avoid working on my own code

    Are you using: if (isset($_POST['submit'])) ?
    IE has a bug and does not always send the value.

  12. #62
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I always use option A. But if it is one line conditional code. I put everything in one line with the braces on.
    Ex,

    Code PHP:
    if(userFound($username)){doThis();}

    For me option B is out of the question. I don't like it at all.
    ------------------


Bookmarks

Posting Permissions

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