Tabs vs Spaces



  • @VaelynPhi said:

    I have no patience for four-space indenting, and I can't understand why anyone does

    http://imgur.com/Cq4zs2l


  • Discourse touched me in a no-no place

    @ben_lubar said:

    Most things I've seen default to 8-space tabs. People who write code in languages with lots of indenting tend to use 2-space tabs.

    If you've got enough indenting that that's a major readability gain, refactoring to reduce the indentation (and increase the sub-program comprehensibility) will be an even bigger gain.

    3-space indents. 😈



  • I have found a little problem with tabs: commenting code.

    With spaces, you usually replace the first space of the line, so the commented code is still indented with the other.

    Spaces FTW. Leave your tabs for MSWord.



  • Repeat after me: Tabs indent, spaces align.

    I represent a tab with four underscores ____ here:

    class SomeClass {
    ____int my_function(int      arg1,      // argument 1
    ____                string   arg2,      // I have a small novel to write about this arg,
    ____                                    // so let's just fall into the next line
    ____                MyClass  arg3       // So nice to be able to comment everything!
    ____)  {
    ________// actual code
    ________bool ret = frobble(arg1)
    ____}
    }
    

    It works perfectly no matter whether a tab is two spaces, four spaces, or sixteen spaces.

    FWIW, I prefer that a tab is represented by two or three spaces, except when I'm diving into code I'm unfamiliar with, where four spaces adds clarity.

    The most awful possible combination is :set tabstop=8 shiftwidth=4 noexpandtab and there's an awful lot of code written that way. Indent level 1 = 4 spaces, indent level 2 = 1 tab, indent level 3 = 1 tab + 4 spaces, indent level 4 = 2 tabs...



  • That's nice, but probably more elaborate than what I'd trust most coders with. You just know things would turn into a pigsty within a week and you'd end up policing and fixing everything all the time.


  • FoxDev

    Spabs



  • Aligning things is pretty rare, so any mess people make would not be too bad, I hope.


  • Discourse touched me in a no-no place

    @ben_lubar said:

    as long as if you use spaces you put some kind of modeline at the end that tells my text editor how many spaces to use.

    One per space? Or was that a braino?


  • Java Dev

    We're actually in the tabstop=8 shiftwidth=4 noexpandtab camp. I'd just as soon switch to the expandtab version, but any disturbance of the status quo is likely to incur a fight that is likely to end in a top-down enforcing of the corporate coding style guide. About all I know about the corporate coding style guide is that it's hundreds of pages long. Kind of a sword of damocles that one.

    I do align code in tables on occasion, and whether I use tabs or spaces varies with the usecase.
    If I align a struct definition, it's usually with tabs (type in column 4, label in column 16 or 24, comment in column 40 or 48).
    However I also occasionally align function invocations and other patters that repeat a bunch of time in subsequent lines.

    db_bind_int   (query, "id",    id    );
    db_bind_string(query, "name",  name  );
    db_bind_string(query, "value", value );
    


  • @mott555 said:

    because the barebones text editors don't support configuring tab width. You must switch to 64-bit Windows 8

    And what barebones text editor on Unix doesn't support configuring tab width?

    The only thing I'd change in vim is to add adjustable tab width, to edit tab-separated column data easily. When you have one column with (say) an e-mail address and lots of 1-8 char columns it's annoying to have to adjust tab width to the longest e-mail address.

    For the record, I'm of the opinion that in a sane IDE the programmer should not have to care about tabs vs. spaces other than knowing that a single press on the Tab key indicates that the programmer wants to indent one more level than the IDE guessed, and Backspace one less.


  • BINNED

    @RaceProUK said:

    Spabs

    Not sure if that sounds dirty as in covered with mud or dirty as in the kind of thing grown ups do when they close the bedroom door


  • Java Dev

    The version of vi included in busybox does not seem to support setting shiftwidth or tabstop.
    I don't think ed handles them at all (apart from what your terminal does), although whether you consider that an editor is up to you.

    Full vim supports configurable tab stops. I think you can even configure it per tab inidividually (so have 1st at 8, 2nd at 13, 3rd at 20, etc) but I'm unable to find a suitable reference offhand.
    In general both with vim and emacs, it's a safe bet anything is possible unless proven otherwise.



  • I use tabs because I like an 8-column indent step more than I like an 80-column limitation.

    People who use tabs in a text file to mean anything other than "jump to the next 8-column boundary" are Doing It Wrong; I hold those people responsible for all the spurious claims about there being no convention for what a tab means.

    People who mix tabs and spaces in the same source file are Doing It Wrong. People who mix tabs and spaces on the same line need LART therapy. People who check files out of source control, make no change other than altering the whitespace in accordance with their own religious principles and then check them in again need LART therapy followed by slow disembowelment.


  • 🚽 Regular

    @tar said:

    Yah, my editor of choice allows me to use Ctrl+T and Ctrl+D to indent or dedent a line of text one tabstop at a time
    I use < and >. Tomayto, tomahto.

    Discourse handles <kbd><</kbd> surprising well. :O

    @blakeyrat said:

    [tabular layout] Why would that be in your code?

    I do that frequently, in new code, if not in a hurry. Sue me.
    @blakeyrat said:
    That doesn't solve the "backspace doesn't work when a code editor is set to spaces" problem.
    That problem is only in your head. Backspace isn't the opposite of Tab; <nobr>Shift+Tab</nobr> is. Tab indents, Shift+tab outdents.

    Backspace is for deleting characters, which is what Space is.

    That indentantion works by inserting a tabulation character is just an implementation detail.

    Trolling aside, it annoys me too.


    @ben_lubar said:

    http://play.golang.org/p/axgNIXh6xw
    Feature request for @sockbot: flag every post by Ben containing a link to http://play.golang.org.

    Wait, cancel that. No use spamming the moderators.


    @lucas said:

    @VaelynPhi said:
    I have no patience for four-space indenting, and I can't understand why anyone does

    <Image of "tab_size": 4 in sublime-settings>
    I think what @VaelynPhi doesn't have patience for is the use of spaces, not the particular size of indentations.

    I see use_tab_stops is true in your picture.


    @Eldelshell said:

    With spaces, you usually replace the first space of the line, so the commented code is still indented with the other.
    Speak for yourself. Not all languages start comments with a single character.
    In fact, I'd ass-pull that for every language that start comments with # there are two C-like languages using // and /* + */.

    And I insert — not replace — characters. Also, I'd argue that not every one comments code at the left margin, and instead comment at the start of the visible line, after indentation. No replacing there, even if you wanted.

    Besides, your editor should be the one commenting code for you.


    @hhaamu said:

    Repeat after me: Tabs indent, spaces align.
    I couldn't agree more. We need all editors to have elastic tabstops now.


    Filed under: mega post



  • Yes mistress , I shall appear as summoned.


  • 🚽 Regular

    @flabdablet said:

    People who use tabs in a text file to mean anything other than "jump to the next 8-column boundary" are Doing It Wrong;
    Unless it's a Jeff-like "Doing It Wrong", no.
    Tabs should mean "jump to next tabulation stop regardless of its size".

    @flabdablet said:

    People who mix tabs and spaces on the same line need LART therapy.

    Consider the "tabs indent, spaces align" mantra.


  • FoxDev

    @Zecc said:

    Feature request for @sockbot: flag every post by Ben containing a link to http://play.golang.org.

    Wait, cancel that. No use spamming the moderators.

    hmmm..

    write the plugin and run the bot yourself? i don't think that';s really what sockbot is all about. ;-)


  • Discourse touched me in a no-no place

    @flabdablet said:

    People who mix tabs and spaces in the same source file are Doing It Wrong. People who mix tabs and spaces on the same line need LART therapy.

    Or Python devs.
    Oh, wait, yeah .... Doing It Wrong.


  • 🚽 Regular

    I'm only half-surprised you've taken me seriously.


  • FoxDev

    @Zecc said:

    I'm only half-surprised you've taken me seriously.

    i take everyone seriously!

    i just don't always answer seriously. ;-)


  • BINNED

    @VaelynPhi said:

    I'm not trying to start a flamewar

    Said everyone who ever started a flamewar.


  • FoxDev

    @antiquarian said:

    Said everyone who ever started a flamewar.

    https://www.youtube.com/watch?v=_QyYaPWasos


  • Discourse touched me in a no-no place

    @flabdablet said:

    People who mix tabs and spaces in the same source file are Doing It Wrong. People who mix tabs and spaces on the same line need LART therapy.

    I guess you're not a big fan of Whitespace…



  • @flabdablet said:

    People who check files out of source control, make no change other than altering the whitespace in accordance with their own religious principles and then check them in again need LART therapy followed by slow disembowelment.

    Yes. These people are the worst. I'd consider setting a pre-commit hook which did a whitespace-insensitive diff of the files and refused to let them check in if you hadn't changed anything substantive. Beating them with a rubber hose or a $5 wrench is a more satisfying solution.



  • "Client wants a space between 'web' and 'site'. Ugh."

    COMMIT DENIED



  • @Zecc said:

    Ctrl+T and Ctrl+D to indent or dedent a line of text one tabstop at a time

    I use < and >. Tomayto, tomahto.

    The great thing about Vim is there's usually at least three different ways to accomplish anything. Potayto, potahto.

    @Zecc said:

    I do [tabular] frequently, in new code, if not in a hurry. Sue me.

    I'm not a fan of horizontal alignment, as I keep finding stuff like this in our codebase:

    example.alpha   = 0;
    example.beta    = 1;
    example.gamma   = 2;
    example.some_incredibly_long_option_added_by_someone_else_later = 3;
    

    Presumably the second editor didn't care as much about tabular as the first one did.



  • @tar said:

    I'm not a fan of horizontal alignment, as I keep finding stuff like this in our codebase:

    example.alpha   = 0;
    example.beta    = 1;
    example.gamma   = 2;
    example.some_incredibly_long_option_added_by_someone_else_later = 3;
    

    Presumably the second editor didn't care as much about tabular as the first one did.

    But that means every time someone adds a longer field name, you have to modify every line in the same block that that field gets used. Think of the diffs!



  • @ben_lubar said:

    you have to modify every line in the same block that that field gets used. Think of the diffs!

    Exactly! So don't use tabular layout in the first place.



  • @tar said:

    a rubber hose or a $5 wrench

    That's an inclusive OR, naturally.



  • @hhaamu said:

    Tabs indent, spaces align.
    I represent a tab with four underscores ____ here:

    Hmm, I have no idea how I can configure my editor to handle that for me automatically.

    I'm probably just going to set my editor to tabs in this case, unless spaces will cause more irritatation to you.

    /** Ode to parameters:
     *  @param arg2 - I have a small novel to write. It was a dark and stormy night
     *      when arg2 stormed into my cafe in the south of France...
     */
    int my_function(int arg1, string arg2, MyClass arg3) {
        ... }

  • Discourse touched me in a no-no place

    @tar said:

    Hmm, I have no idea how I can configure my editor to handle that for me automatically.

    ⎇+x set-variable ⏎ tab-width ⏎ 4 ⏎



  • @dkf said:

    ⎇+x set-variable ⏎ tab-width ⏎ 4 ⏎[or, you know, what dkf actually wrote]

    That can configure an editor so it magically knows when to stop indenting and start aligning, as per hhaamu's overspecified formatting rules?



  • $ go fmt



  • @Zecc said:

    Backspace is for deleting characters, which is what Space is.

    So is Tab.

    The problem is caused by the Tab key not typing the tab character, but instead typing 4 space characters, which is unquestionably broken, wrong behavior.



  • @blakeyrat said:

    From the solution browser?

    ... is there like a vastly different VS2013 that you guys have or something?

    Yeah, my experience tends to be that VS asks if you want to rename the class by the same name when you rename a file.



  • $ go fmt ur.self :P



  • $ go fmt ur.self :P
    can't load package: package ur.self: cannot find package "ur.self" in any of:
    /home/ben/.golang/src/ur.self (from $GOROOT)
    /home/ben/go/src/ur.self (from $GOPATH)
    can't load package: package :P: cannot find package ":P" in any of:
    /home/ben/.golang/src/:P (from $GOROOT)
    /home/ben/go/src/:P (from $GOPATH)


  • 🚽 Regular

    @blakeyrat said:

    So is Tab.

    The problem is caused by the Tab key not typing the tab character, but instead typing 4 space characters, which is unquestionably broken, wrong behavior.

    DID YOU EVEN READ WHAT I WROTE?

    Tab is a key you press for increasing the indentation level of a code block.
    Shift+Tab is the key combination for the opposite operation.
    Which is why you can select a block of text and press Tab and Shift+Tab and they will cancel each other nicely.

    Backspace and Delete are keys for deleting text, which is a different operation.

    It's only an accident than the same key is used to insert character U+0009.


    Filed under: channelling that other guy who complains a lot


  • @tar said:

    Hmm, I have no idea how I can configure my editor to handle that for me automatically.

    ctab.vim ought to work. (I haven't the faintest clue what your editor is, yet I feel qualified to offer you this piece of advice.)

    I'm probably just going to set my editor to tabs in this case, unless spaces will cause more irritatation to you.

    I'm perfectly fine with spaces; I just prefer tabs since they usually give airier code if needed. The people who use spaces tend to favour two-space indents, which is a tad cramped.



  • @blakeyrat said:

    I've been turning off its features as they annoy me. It's already like 60% off.

    ReSharper doesn't even appear to do anything VS doesn't already do, except give a few (pedantic dickweed) suggestions on code style, and making the entire IDE (especially intellisense and the MS test runner) run like frozen syrup. I do not get the appeal. But we've had threads on this before.

    IMHO, ReSharper did things that Visual Studio didn't use to do, but now does, so it's kinda pointless now.



  • @Zecc said:

    DID YOU EVEN READ WHAT I WROTE?

    Yes, it's just wrong.

    @Zecc said:

    Backspace and Delete are keys for deleting text, which is a different operation.

    So does Backspace delete a space character? Is that a bug? y/n?

    The point is, Backspace shouldn't do magical special-case shit. It should delete whatever character is to the left of the insertion point.



  • @blakeyrat said:

    The point is, Backspace shouldn't do magical special-case shit. It should delete whatever character is to the left of the insertion point.

    Actually, that's not the issue. Backspace always deletes exactly 1 character. The issue is that pushing one key results in 4 unicode characters being inserted into a document simultaneously.



  • @blakeyrat said:

    Yes, it's just wrong.

    @Zecc said:

    Backspace and Delete are keys for deleting text, which is a different operation.

    So does Backspace delete a space character? Is that a bug? y/n?

    The point is, Backspace shouldn't do magical special-case shit. It should delete whatever character is to the left of the insertion point.

    Indeed, whether it's a control character or not.


    Filed under: \r\n


  • Winner of the 2016 Presidential Election

    To be fair, the whole Tab being the opposite of Shift + Tab thing is something that more products adapted than just editors.

    If you have mulitple form-elements (like inputs, radios, checkpoints and whatnot) Tab advances you to the next input while Shift+Tab brings you to the previous one. (On pretty much any modern system).
    Space advances you one space to the right and backspace advances you one space to the left (deleting the last character).

    So... meh... as long as you can specify what you want tabs to do, I'd say use what you feel is the right thing... If you don't want tabs to become more spaces because you don't want the hassle of deleting them.... then don't use that. Unless you are in a team where this is standard. In which case: Either write a plug-in that replaces 4 Spaces with Tabs and reverse (depending on whether you pull or commit) and let everybody do they deem neccessary.

    Filed Under: You can also use 3423 spaces per tab but I bet that looks stupid!


  • 🚽 Regular

    @blakeyrat said:

    The point is, Backspace shouldn't do magical special-case shit. It should delete whatever character is to the left of the insertion point.
    Which it does. If it is a space, it deletes the space.

    @blakeyrat said:

    So does Backspace delete a space character?
    Yes.
    @blakeyrat said:
    Is that a bug?
    No.
    @blakeyrat said:
    y/n?
    This is not an answerable question out of context.

     
     
     
    But really, I'm done trolling you. I too get mildly annoyed that backspace doesn't outdent when it should. But I understand the rationale.



  • :set ts=4 sw=4 noet
    :retab!
    

  • 🚽 Regular

    @ben_lubar said:

    Backspace always deletes exactly 1 character. Or the selection. Or nothing if there is nothing to delete.
    FTFY



  • @Zecc said:

    Backspace always deletes exactly 1 character. Or the selection. Or nothing if there is nothing to delete.

    FTFY

    And sometimes sends you a page back in your browsing history.



  • @flabdablet said:

    People who mix tabs and spaces in the same source file are Doing It Wrong. People who mix tabs and spaces on the same line need LART therapy. People who check files out of source control, make no change other than altering the whitespace in accordance with their own religious principles and then check them in again need LART therapy followed by slow disembowelment.

    This is why I sneak my blessed whitespace changes in with other changes. Too many files here mix spaces and tabs on the same line (in the indent part) often inconsistently. Most files use a 2 spaces tab but some are 4 so alignment is all over the place in these cases.

    Luckily the "lead" checks our work with "diff -w" so he rarely notices these changes. Using diff instead of the svn tools is a WTF for another day, though. (There's a push to move to git but I'm not sure if that will be better or worse in this case!)

    (Tabs for life)



  • @Zemm said:

    Using diff instead of the svn tools is a WTF for another day, though.

    Doesn't svn just call out to diff? I know git does.


Log in to reply