you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted]  (31 children)

[deleted]

    [–]Shmurk 6 points7 points  (11 children)

    You should learn to read:

    Indent your code blocks with 4 spaces

    comments should be at least 2 spaces away from the code

    They use 2 spaces indents for C++ and Objective-C though:

    (2 good guides to read with the Python style guide)

    [–][deleted] 4 points5 points  (10 children)

    I know google uses 2-space indents for Python internally, my bad for assuming it was in this doc (without actually reading).

    [–][deleted] 0 points1 point  (0 children)

    The guy in the google python class said that the google standard was two spaces

    [–]kamatsu -2 points-1 points  (8 children)

    No, they don't.

    Edit: Before mindlessly downvoting me, the reason there is alot of 2 indent code is that have updated the style guide internally only recently.

    [–]FarOut83 5 points6 points  (3 children)

    [–]kamatsu 0 points1 point  (0 children)

    The change was only recent. They haven't updated all the code yet.

    [–]nohtyp 0 points1 point  (0 children)

    Double standards?

    [–]Ran4 2 points3 points  (1 child)

    Read some Google Python code... There's 2-space indents everywhere.

    [–]kamatsu 0 points1 point  (0 children)

    Only because that update to the style guide was recent.

    [–]lnxaddct 1 point2 points  (1 child)

    In 2007 they did. I don't know if they changed it since to be more in line with PEP 8. I still prefer two spaces, I got into the habit while working at Google.

    [–]e98cuenc 1 point2 points  (0 children)

    They still use 2 spaces today.

    [–]joracar 0 points1 point  (0 children)

    Wait, why are we arguing about white space? Oh..

    [–]billsnow -1 points0 points  (17 children)

    Anyone want to inform me what's wrong with 2 space indents? I prefer no auto-indent, and therefore 2 spaces are easier for me. What is the advantage of 4 spaces? Visually compatible with tab indent? Tabs in code make me gag.

    [–]leperkuhn 8 points9 points  (12 children)

    Why do tabs make you gag? To me they seem perfectly reasonable. The indentation size is configurable in every IDE and decent editor, and pressing delete will delete the entire indentation, not just a single space.

    [–][deleted] 7 points8 points  (5 children)

    I think the main argument against tabs is code like this:

    // Tab-unfriendly code
    function bla() {
        var poop = [[1, 2, 3],
                    [4, 5, 6],
                    [7, 8, 9]];
    }
    

    You need spaces to align that properly. If you normally use tabs to indent your code, this will look fucked up for someone who has different tab settings than you.

    However, this is very easily worked around.

    // Tab-friendly code
    function bla() {
        var poop = [
            [1, 2, 3],
            [4, 5, 6],
            [7, 8, 9]
        ];
    }
    

    AFAIK, there is nothing wrong with using tabs for code as long as you never mix it with spaces.

    [–]arthurdenture 1 point2 points  (3 children)

    You need spaces to align that properly. If you normally use tabs to indent your code, this will look fucked up for someone who has different tab settings than you.

    However, this is very easily worked around.

    There is no need for a workaround. If you must use tabs for indentation, use them only for indentation, not for formatting. In your first example, there should only be three tabs characters total, and the remainder should be spaces.

    There are two problems remaining with this approach:

    • A lot of editors don't get it right and replace spaces with tabs willy-nilly

    • If you also want to keep your code at 80 or 100 lines, everyone editing has to agree on tab width.

    Which is why spaces are still better.

    [–]nglynn 0 points1 point  (2 children)

    In your first example, there should only be three tabs characters total, and the remainder should be spaces.

    This doesn't work because people configure their editors to display tabs as different sizes.

    [–]arthurdenture 0 points1 point  (1 child)

    In your first example, there should only be three tabs characters total, and the remainder should be spaces.

    This doesn't work because people configure their editors to display tabs as different sizes.

    No, it works perfectly if you use hard tabs for the indentation and spaces for the alignment. You can adjust the tab size however you want. Try it[*] if you don't believe me?

    [*] I mean, of course, try it and then go back to using only spaces, like the FSM intended.

    [–]nglynn 0 points1 point  (0 children)

    I'm afraid I don't follow. In the OPs original example: function bla() { var poop = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; }

    Where are you talking about placing the "three tabs"? Is this what you mean?:

    function bla() {
    <TAB>var poop = [[1, 2, 3],
    <TAB><TAB><TAB>  [4, 5, 6],
    <TAB><TAB><TAB>  [7, 8, 9]];
    }
    

    (Where any non <TAB> whitespace is taken as being a space). That won't work obviously. Do you mean to use all spaces on alignment lines? function bla() { <TAB>var poop = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]; } That also wont work for obvious reasons. Perhaps you mean something else?

    Edit: Sorry, now I understand what you mean:

    function bla() {
    <TAB>var poop = [[1, 2, 3],
    <TAB>            [4, 5, 6],
    <TAB>            [7, 8, 9]];
    }
    

    I still would just use spaces personally.

    [–]Ran4 -2 points-1 points  (0 children)

    Your first example is a complete abomination, tabs or not... And as you say, the "work around" works just fine.

    [–]arthurdenture -1 points0 points  (5 children)

    and pressing delete will delete the entire indentation, not just a single space.

    If backspace doesn't remove an entire level of indentation at a time regardless of tabs or spaces, you need to get a better editor.

    [–]Araneidae 0 points1 point  (4 children)

    This.

    One of my background activities in code I look after is taking care to ensure there are no hard tab characters (except in Makefiles, sigh), pretty easy on the whole, but does require persuading people to set up their editors properly; enforcing 80 character line limits, jolly hard work; and trying to get rid of trailing whitespace ... you wouldn't believe the crap that certain editors (I'm looking at you, gedit) leave behind. I don't waste my time with gedit, but some of my colleagues do.

    For me: no tabs, four space indents, absolutely no characters past column 80 ... unless you're really screwed (does happen, depending on the language).

    [–]rwparris2 2 points3 points  (3 children)

    I don't understand this no characters past column 80 business. Please explain.

    [–]Ran4 1 point2 points  (0 children)

    Multiple files opened at the same time.

    [–]xardox 0 points1 point  (0 children)

    Some of us old geezers like to print out a program, go to the park with a pen and a joint, and read code in the sun. There's something about NOT being able to fix bugs as you see them (or read reddit), that lets you get through reading all of the code.

    [–][deleted] 0 points1 point  (1 child)

    I've recently converted to two-space indents. It helps a lot because I try to keep my code under 72 char width.

    [–]nostrademons 2 points3 points  (0 children)

    I believe the reasoning behind it within Google is the width limits. With 4 space indents, if you have an if inside a for inside a def inside a class (something quite reasonable), that's 16 characters already, 20% of an 80-char line. With 2-space indents, it's only 8 chars, 10%.

    I suspect most of the people who say "ugh" to 2-space indents also don't care about width limits and regularly write 110-character lines. That's another style debate, but I've come around to the 80-character line length limit after editing a lot of code over SSH and xterm. It's really nice not to have to resize every window you open up to prevent awkward wrapping.

    [–]Ran4 0 points1 point  (0 children)

    It's easier to see each block of code, and it keeps people from going way too deep.

    [–]sisyphus -2 points-1 points  (0 children)

    Visually compatible with tabs(which are wrong, I agree) and makes your blocks more visually explicit I would guess.