[RESOLVED] Indentation
Results 1 to 12 of 12

Thread: [RESOLVED] Indentation

  1. #1
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487

    resolved [RESOLVED] Indentation

    I was sitting reading indentation styles, one thing that seemed to stick out from what I was reading was allman and GNU seem identical, am I missing something?


    GNU
    PHP Code:
    if (== y
      {
         
    x++;
         
    foo();
      } 
    else 
      {
         
    x--;
         
    bar();
      } 
    Allman
    PHP Code:
    if (== y
    {
        
    x++;
        
    foo();

    else 
    {
        
    x--;
        
    bar();

    Soruce
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  2. #2
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,521
    I think there are only about two that are used much, and the poll there at your Source would tend to agree: Allman and K&R (aka "1TBS, One True Brace style").

    The PHP-FIG PSR's use Allman, I think. I have tried Allman and since there's no one here above me to say so, have reverted to 1TBS.
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  3. #3
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487
    So as long as I'm comfortable with the two main ones everything should be fine? I get the impression and I'm probably wrong but allman seems to fit PHP and K&R might be better for JavaScript/jQuery (it could be to do with the book I'm sitting looking at that makes me think so)
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  4. #4
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,531
    The indentation debate has spawned a couple of long threads here in the past (as has the allied question, spaces vs. tabs).

    In JavaScript, K&R style has the distinct advantage of avoiding a bug in that language. Once you're in the habit of writing the opening brace on the same line as the statement that introduces it, you would have to go out of your way to write the incorrect
    Code:
    return
    {
        key: value
    };
    Three design decisions collude here to cause the bug: (1) statement blocks and object literals have the same delimiters; (2) expressions ⊆ statements; and (3) automatic semicolon insertion.

    (1) means you either want to indent object literals the way you indent statement blocks and vice versa, or you want different rules for braces depending on what they're delimiting.
    (2) says that any expression is a statement - it doesn't have to be an assignment or a function call or something else with obvious side effects to stand on its own as a statement. In particular, object literals can stand on their own as statements. But if
    (3) complete statements are on distinct lines, the semicolon between them is optional - the line break is presumably enough separation. During parsing a semicolon is automatically inserted between them if there isn't one already there.

    So the JavaScript parser sees return on one line, and an object literal (which is an expression, which is a statement) on the following lines. They're on different lines so they look like two complete statements. So the semicolon between them was optional. And since there isn't one there, it is inserted.
    Code:
    return;
    {
        key: value
    };
    Interpreted as "return undefined" and execution never reaches the literal.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  5. #5
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,521
    Quote Originally Posted by Weedpacket View Post
    Interpreted as "return undefined" and execution never reaches the literal.
    And so, as I write both PHP and JavaScript, you have a reason I'm a 1TBS/K&R user ....

    Incidentally, the difference between Allman and GNU is that Allman has the curly brace in column 1 and GNU indents it to col. 2 or 3 ...
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

  6. #6
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487
    Quote Originally Posted by dalecosp View Post
    Incidentally, the difference between Allman and GNU is that Allman has the curly brace in column 1 and GNU indents it to col. 2 or 3 ...
    So basically someone stole someone else homework and changed it a bit
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  7. #7
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,531
    I think GNU did that deliberately to punish people who try to use tabs.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  8. #8
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487
    Tabbing for indentation is bad?
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  9. #9
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Contact Unit "Coping Mechanism"
    Posts
    22,531
    So say the people who say so.
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  10. #10
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    14,879
    I was always fine with tabs, and thought it made sense in that each coder could then adjust his/her editor to indent each level as much or as little as desired*; but it seems like the spacers are winning out these days in most languages. In the Ruby on Rails world, if you don't use spaces -- and it must be exactly two spaces per indent level -- you will be excommunicated.
    _______________
    * The drawback being that it looked not-so-great in any environment where you could not [easily] adjust that spacing
    "Well done....Consciousness to sarcasm in five seconds!" ~ Terry Pratchett, Night Watch

    How to Ask Questions the Smart Way (not affiliated with this site, but well worth reading)

    My Blog
    cwrBlog: simple, no-database PHP blogging framework

  11. #11
    Senior Member cluelessPHP's Avatar
    Join Date
    Apr 2015
    Location
    Scotland
    Posts
    487
    I'll happily be "wrong" every time then
    Once you had a good excuse, you opened the door to bad excuses ― Terry Pratchett, Thud
    Fresh start
    Github version
    Current date 14/10/17
    Finish date "should be" 14/04/18

    Vote on your preferred indentation method

  12. #12
    Settled 4 red convertible dalecosp's Avatar
    Join Date
    Jul 2002
    Location
    Accelerating Windows at 9.81 m/s....
    Posts
    8,521
    Quote Originally Posted by NogDog View Post
    * The drawback being that it looked not-so-great in any environment where you could not [easily] adjust that spacing
    Yeah, all those Pico/Nano users who can't be bothered to specify (or alias) "-T 4 -E", or the VI dinosaurs who won't ":1,$s/\t/ /" ....
    /!!\ mysql_ is deprecated --- don't use it! Tell your hosting company you will switch if they don't upgrade! /!!!\ ereg() is deprecated --- don't use it!

    dalecosp "God doesn't play dice." --- Einstein "Perl is hardly a paragon of beautiful syntax." --- Weedpacket

    Getting Help at All --- Collected Solutions to Common Problems --- Debugging 101 --- Unanswered Posts --- OMBE: Office Machines, Business Equipment

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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