Page 1 of 3

Wiki Coding Style: Tabs vs. Spaces

Posted: Fri Jul 01, 2011 8:13 am
by Solar
When the arguments have all been said, have a count of hands and be done with it.

The somewhat lengthy discussion is here. If you haven't taken part in it, well, your bad - it's more or less closed now (I hope). 8)

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Fri Jul 01, 2011 11:30 pm
by drew
#%^$ it!! I admit it's just a pain in the @$$ to have to hit space 4 times to make a stupid indentation. it should be a tab. your operating systems should be programmed to display them okay. make a standard on the width of the tabs. why would I press a space bar four times, when I could press a tab bar once?? it makes no sense to use space bars.

I mean... honestly... it's not that hard to make an editor that would automatically resolve the tab issue.

what? that doesn't make sense to you??

well imo, making an editor that calculates widths automatically makes a lot more sense than using spaces as indentations.

have none of you honestly thought of that??

it's practically part of the syntax for anyone that cares...

you can make your silly standard, but you can't make my follow it.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 1:38 am
by gerryg400
Are you serious ? You're not serious are you ? I don't know what editor you use but when I hit tab in my editor it indents to the next tab marker by inserting spaces.

I just can't understand the tab versus space thing. Lots of people have given reasons for using spaces instead of tabs. But, has anyone been able to provide a single reason for using tabs ?

Surely this is the least interesting part of the coding style guide. It's quite amazing that it's the one thing we argue about.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 1:54 am
by JackScott
I voted spaces. Normally I code using tabs, but they really are a pain in the neck to write in browsers.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 6:04 am
by Brendan
Hi,
gerryg400 wrote:I just can't understand the tab versus space thing. Lots of people have given reasons for using spaces instead of tabs. But, has anyone been able to provide a single reason for using tabs ?
Tabs are faster for editing. Consider assembly:

Code: Select all

    mov foo, bar                          ;Comment
Imagine your cursor is at the end of "bar" and you want to move the cursor to the start of the comment. In a perfect world it'd be like modern wordprocessors, with tab stops at arbitrary positions (recorded in the file's header), and you'd only need to press "right cursor" key once. Unfortunately the plain text file format has no header so a text editor can't support that; and you end up with maybe 5 tabs where you have to press the "right cursor" key 5 times. It's crappy, but it could be worse...

Now consider spaces - do you want to press the "right cursor" key 20 times? Is it faster to press the "end" key and use then "left cursor" key? Is it faster to shift your hand from the keyboard to the mouse (and back), and try to position the cursor using the mouse pointer? Regardless of how you do it, it's going to suck more than pressing the "right cursor" key 5 times (and suck a lot more than pressing it once).

For C it's a similar problem, except that it's more often at the start of the line.

Of course we're not talking about normal code - we're talking about HTML pages. For HTML pages, you shouldn't be using tabs or spaces - you should use "non-breaking space" (Unicode codepoint U+0000A0), or if that isn't supported properly (if the page isn't using a Unicode character encoding, or if some web browsers don't support it properly) then you should fall back to the old " " HTML sequence.

Of course we're not talking about normal HTML - we're talking about the WIki. It'd be nice if the Wiki automatically replaced spaces in code sections (and potentially tabs too) with "non-breaking space". Unfortunately it doesn't. I'm not too sure if the Wiki handles Unicode properly (but I'm certain it won't like " " in source code examples).


Cheers,

Brendan

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 6:41 am
by gerryg400
Imagine your cursor is at the end of "bar" and you want to move the cursor to the start of the comment.
I hold the option key, press the right arrow key then press the left arrow key. I'm using eclipse but even editors as old as vi have that type of navigation facility.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 7:25 am
by Kevin
Brendan wrote:

Code: Select all

    mov foo, bar                          ;Comment
Imagine your cursor is at the end of "bar" and you want to move the cursor to the start of the comment. In a perfect world it'd be like modern wordprocessors, with tab stops at arbitrary positions (recorded in the file's header), and you'd only need to press "right cursor" key once. Unfortunately the plain text file format has no header so a text editor can't support that; and you end up with maybe 5 tabs where you have to press the "right cursor" key 5 times. It's crappy, but it could be worse...
You press 'w' exactly once.

/me hopes to have started a vi vs. emacs discussion. Popcorn!

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 8:05 am
by Brendan
Hi,
gerryg400 wrote:I hold the option key, press the right arrow key then press the left arrow key. I'm using eclipse but even editors as old as vi have that type of navigation facility.
Kevin wrote:You press 'w' exactly once.
So, is the standard for all text editing (including all text editors, all word-processors, text input boxes in web browsers, etc) "hold the option key, press the right arrow key then press the left arrow key" or is the standard "press 'w' exactly once"? I tried both methods in Kwrite and neither worked; then I tried both methods in notepad++ and neither worked; therefore you're both wrong.

Maybe we've just got an inadequate file format (plain text), combined with idiotic/non-standard hacks as work-arounds that attempt to make up for problems with the inadequate file format.

When one design failure isn't enough, try lots of design failures... :)


Cheers,

Brendan

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 9:02 am
by xenos
Brendan, I totally agree with you. For code examples in HTML pages the non-breaking space is probably the best character for indentation and alignment and it would be great if the Wiki supports it (I have not checked that yet).

Personally, I use tabs for indentation and spaces for alignment:

Code: Select all

struct mystruct
{
<tab>int myvar;<sssppppaaacccceee>// Always 0.
<tab>int my_long_named_var;<space>// Never 0.
};
The tab count at the beginning of each line is equal to the indentation depth - it is independent from the preference on visual indentation, i.e., whether indentation levels should be separated by 2 spaces, 4 spaces, 8 spaces... If you open the code in an editor, you can set the tab width to anything you like. If you don't like what you see, you misconfigured your text editor. If your text editor doesn't support configuring the tab width, you're using the wrong editor.

Of course alignment should be independent of editor settings, so I use spaces for this purpose. No matter which tab width I use, the two comments from my example will always be aligned.

So the question remains: how would I align things which are at different indentation levels? Simple answer: I don't. In my code, each aligned block of comments, variables or whatever belongs to only one indentation level. For me there is simply no need to align comments across indentation levels.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 9:04 am
by Chandra
I vote for tabs because I feel comfortable with it. Personally, I'd invest time getting myself a cup of coffee, than having required to type 4 spaces for indentation. And I know very well that majority of people here, are against this.

Edit: this browser sucks.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 4:17 pm
by bluemoon
The problem with TAB indent is:

Code: Select all

foo = func ( para1,
             para2,
             para3 );
With space it's always aligned, not always for TAB.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 4:46 pm
by CommunistPancake
I would say tab, but tabs don't work in my browser.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 5:40 pm
by gerryg400
Chandra wrote:Personally, I'd invest time getting myself a cup of coffee, than having required to type 4 spaces for indentation
Chandra, doesn't your editor have a mode where it will enter 4 (or the appropriate number for alignment) spaces when you press the tab key ?

To the guys who say they prefer tabs, I also press tab to indent my code and to align comment blocks etc. I'm sure I type into my editor in almost the same way as the 'tab' guys. It's just that my editor converts them to spaces

We are not voting here which key on the keyboard should be used for entering the whitespace that formats our code (for that I vote tab), we are voting on which character should be stored in the actual source files on the wiki ?

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sat Jul 02, 2011 8:47 pm
by schilds
Oh just use a pretty printing script for the wiki already :p.

Re: Wiki Coding Style: Tabs vs. Spaces

Posted: Sun Jul 03, 2011 1:18 am
by xenos
bluemoon wrote:With space it's always aligned, not always for TAB.
That's why I use tabs for indentation, but spaces for alignment:

Code: Select all

<tab>foo = func ( para1,
<tab><ssppaaaccee>para2,
<tab><ssppaaaccee>para3 );
I know it looks cumbersome, but afaik it's the only way to both 1. keep aligned stuff always aligned, independent of tab width and 2. adjust the width of indentation levels to whatever your preference is, simply by changing the tab width setting of your editor. (Except using a code formatter, of course.) If anyone knows a better method to achieve these two goals, please let me know.

Besides, I set up Kate to show visible tab markers, so one can easily distinguish between tabs and spaces, and read off the indentation level by just counting tab markers. Yes, I know, there is also an option to show visible indentation lines. It's just my personal preference, not a general recommendation or even a standard proposal.