A Question of Style
-
What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?
If the decision is up to me I prefer tabs (and tab size 4). The reason is that in any decent editor it's possible to configure the tab size so everyone can view and edit the source code with their own preferred tab size. Most document viewers have the same possibility, e.g. the viewer and compare tool in ClearCase. Naturally, if I encounter source code I have to edit and the indentation is based on spaces, I continue to use spaces in order not to mess up the source code. At my current assignment the coding guidelines states that spaces should be used and the "indentation size" is 2(!) spaces. In my opinion that indentation size is too small and makes the source code hard to read and follow. However, I only use tabs for indentation and possibly preceding comments at the end of a line. In all other cases I use spaces.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
Must admit, I prefer tabs, so I can indent 4 spaces, my colleague can indent 2 spaces - without changing source. All is well if using the same editor - but sometimes using notepad etc. stuffs up the tabs
If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')
TheArtistFormallyKnownAsMaxxx wrote:
I can indent 4 spaces, my colleague can indent 2 spaces
Different indents in the same source? :~
-
TheArtistFormallyKnownAsMaxxx wrote:
I can indent 4 spaces, my colleague can indent 2 spaces
Different indents in the same source? :~
Yeah, because the editor interprets the tab to mean different things for different people. The underlying file consistently has 1 tab for an indent.
-
Yeah, because the editor interprets the tab to mean different things for different people. The underlying file consistently has 1 tab for an indent.
:doh: I was thinking of tabs expanded to spaces.
-
Take an anvil and a desert spoon. Place both in freezer overnight. Remove from freezer. Place one or more of your testicles on the anvil. Have a friend/colleague/significant other rap, sharply, on your testicle with the spoon. Still think
There is nothing worse than having to hit the backspace key n-4 times.
?
If I knew then what I know today, then I'd know the same now as I did then - then what would be the point? .\\axxx (That's an 'M')
-
Not this again :rolleyes: Ramblings in no particular order: Because Windows and WYSIWYG and such are new-tech. I began programming with a dumb terminal, and the tabs were set there (not in software), and that remains true when I use OpenVMS (except now I use a terminal emulator). I've opened too many files only to find the code misaligned because the last person to touch it used TABs. On Windows I mostly use Edit, which gives four SPACEs per tab. I generally avoid Notepad because it keeps the TABs (eight SPACEs wide X| ). Of course, as long as a file uses one or the other and not a mixture of both it should be OK, but it's easier to untabbify than to tabbify. With SPACEs you know what you're getting.
I used to use 3 spaces for indenting when I worked with punched cards and paper tape. That way, you could still have reasonable levels of nesting within the 80 chars on a card. Now that I have disc files, I use the TAB key - it is only one key stroke (which saves on storage) and it does not matter so much if I exceed 80 chars per line. I even allow myself to reach the 80th column when writing comment lines - in the punch card days, cols 73 to 80 were saved for sequence numbers so you could resort your deck after dropping them; I even (shock horror) allow in-line comments to slightly break the 80 column rule; if more than slightly, the comment goes on its own line with the code underneath. To preserve visual formatting regardless of how different editors display TAB, I use TAB for indenting but then I use spaces inside the code / comments after the first non-TAB character. This keeps tables / type declarations etc lined up.
-
I am pretty sure I don't want to party with you. ;-)
Don't let my name fool you. That's my job.
Given that he's into testicles and anvils, probably not.
Software Zen:
delete this;
-
If you are using Visual Studio, you don't have to hit backspace multiple times...that's the beauty of shift+tab. You get the advantage of readable code in any editor, with the ease of formatting tabs.
Mike Marynowski wrote:
that's the beauty of shift+tab
The problem is, at least at first, remembering the shift part, the thing can end up looking like a dogs breakfast if you are a non-touch typist.
Henry Minute If you open a can of worms, any viable solution *MUST* involve a larger can.
-
What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?
I used to be a tab fan, cause of the one backspace instead of 4. But over the last few years i've opened too many code files that have been edited by several people. Even when there's a 'standard' within a team to use 4 spaces or use tabs or whatever, some people insist on being different. If you can get everyone using spaces then there's a better chance that your indentation will stay aligned after several people have edited the same code. IMHO
Pete If minds had anuses, blogging would be what your mind would do when it had to take a dump Maddox
-
After all the years of having to press the SPACE bar four times, having the editor insert four SPACEs for a TAB is a vast relief; having to press DELETE four times is not that big a deal.
Ennis Ray Lynch, Jr. wrote:
non-modern and cross platform editors
That's what I use primarily.
When I'm not writing in a .NET language I use a cross platform editor too, Vim. It has a "modeline" feature where you can put a special line in (beginning with your language's comment char) that specifies the tab stop
:: Windows batch/cmd script example, typically the last line in a file
:: shiftwidth specifies the width of auto-indenting
:: vim:tabstop=4:shiftwidth=4:noexpandtab# Perl example
vim:tabstop=4:shiftwidth=4:noexpandtab
Another popular cross platform editor (probably friendlier than Vim for a VS user), emacs, has a substantially similar capability though I personally do not know its syntax. Happily, vim also has the "retab" command so if someone sends me a file indented with spaces I can typically fix that in an instant :-\ Vin also has all number of features for auto expanding tabs with spaces, making the backspace key delete n number of spaces, etc... (and I'm reasonably confident that emacs mirrors this capability) so my main point is people should use whatever they want and fix the indenting to the project's code formatting rules before commit. Let your editor empower you! Hopefully someday I will figure out how to embed vim into VS which it can supposedly do.:cool:
-
What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?
I can only see worrying about spaces vs tabs if multiple programmers use multiple editors which handle them differently. I like spacing at multiples of 4 for my code. I just hit tab and let the editor (in Dev Studio) worry about spaces or tabs. I do sometimes see problems if I cut and paste code into something else - a document or email...
-
If the decision is up to me I prefer tabs (and tab size 4). The reason is that in any decent editor it's possible to configure the tab size so everyone can view and edit the source code with their own preferred tab size. Most document viewers have the same possibility, e.g. the viewer and compare tool in ClearCase. Naturally, if I encounter source code I have to edit and the indentation is based on spaces, I continue to use spaces in order not to mess up the source code. At my current assignment the coding guidelines states that spaces should be used and the "indentation size" is 2(!) spaces. In my opinion that indentation size is too small and makes the source code hard to read and follow. However, I only use tabs for indentation and possibly preceding comments at the end of a line. In all other cases I use spaces.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknownTwo spaces is my preference - even with dual wide screens to look at. Interestingly with modern IDEs formatting everything so well I do think that the point is pretty overcooked. When using VS I tend to let it do the formatting and you know there is a syntax problem if a line doesn't indent itself. Let the IDE handle the indents. Admittedly I started web development in the mid-90s using text editors. When I look back at 10 year old web code that I've inflicted on the world there does tend to be almost no whitespace in evidence. But now perhaps I'm prepared to ease into the pipe and slippers of the luxury of 4 indents for a tab (whether spaces or not). But an indent that big just seems too, well, big.
"If you reward everyone, there will not be enough to go around, so you offer a reward to one in order to encourage everyone." Mei Yaochen in the 'Doing Battle' section of Sun Tzu's: Art of War. .
-
Two spaces is my preference - even with dual wide screens to look at. Interestingly with modern IDEs formatting everything so well I do think that the point is pretty overcooked. When using VS I tend to let it do the formatting and you know there is a syntax problem if a line doesn't indent itself. Let the IDE handle the indents. Admittedly I started web development in the mid-90s using text editors. When I look back at 10 year old web code that I've inflicted on the world there does tend to be almost no whitespace in evidence. But now perhaps I'm prepared to ease into the pipe and slippers of the luxury of 4 indents for a tab (whether spaces or not). But an indent that big just seems too, well, big.
"If you reward everyone, there will not be enough to go around, so you offer a reward to one in order to encourage everyone." Mei Yaochen in the 'Doing Battle' section of Sun Tzu's: Art of War. .
[ftw]melvin wrote:
But an indent that big just seems too, well, big.
From my point of view that's a matter of opinion. ;) I don't argue with an opinion, but when people starts to argue that "otherwise the lines become too long and will exceed the allowed 80 chars per line" I usually get fired up. My response is almost always "rewrite your code and make it readable and beautiful". Possibly followed by "if your code has that much indentation your function has far too much complexity inside it and you should split it up into several functions", or "choose variable names that are smaller than 30+ chars". Afterwards people usually realize that the indentation size is just a matter of opinion. It's all about readability and everyone find code written the same way they write code themselves to be the most readable and that's my point when using tabs instead of spaces. Switching between tabs and spaces for indentation is fairly easy. But when it comes to where the curly braces should be put, having a space before a left/right parentheses and so on, it's not that easy. I remember that SlickEdit at least had a "code beautifier" which I fell in love with, but I haven't used SlickEdit for more than a decade so either it got improved or thrown into the bin.
"It's supposed to be hard, otherwise anybody could do it!" - selfquote
"High speed never compensates for wrong direction!" - unknown -
When I'm not writing in a .NET language I use a cross platform editor too, Vim. It has a "modeline" feature where you can put a special line in (beginning with your language's comment char) that specifies the tab stop
:: Windows batch/cmd script example, typically the last line in a file
:: shiftwidth specifies the width of auto-indenting
:: vim:tabstop=4:shiftwidth=4:noexpandtab# Perl example
vim:tabstop=4:shiftwidth=4:noexpandtab
Another popular cross platform editor (probably friendlier than Vim for a VS user), emacs, has a substantially similar capability though I personally do not know its syntax. Happily, vim also has the "retab" command so if someone sends me a file indented with spaces I can typically fix that in an instant :-\ Vin also has all number of features for auto expanding tabs with spaces, making the backspace key delete n number of spaces, etc... (and I'm reasonably confident that emacs mirrors this capability) so my main point is people should use whatever they want and fix the indenting to the project's code formatting rules before commit. Let your editor empower you! Hopefully someday I will figure out how to embed vim into VS which it can supposedly do.:cool:
I just installed and experimented with SciTE yesterday, it has settings for each language.
aquatarian wrote:
and fix the indenting to the project's code formatting rules before commit.
If only. :sigh: Eventually I wrote my own untab utility. It would be better if the code library system performed a beautify action to ensure standards compliance and eliminate (most) format-only changes.
-
What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?
The strongest argument for spaces is hanging indents -- when you wrap lines of code and indent them. If you use tabs, it only looks correct for the rest of the world that uses the same tabstops as you do. When you use spaces, it looks correct for everybody. However, when working in existing files, the rule should be to match what is already used in the file, even if it isn't your favorite. If I were dictator of the world, I'd have already decreed it so and tortured the unbelievers into compliance.. probably a good thing I'm not dictator :)
patbob
-
I used to be a tab fan, cause of the one backspace instead of 4. But over the last few years i've opened too many code files that have been edited by several people. Even when there's a 'standard' within a team to use 4 spaces or use tabs or whatever, some people insist on being different. If you can get everyone using spaces then there's a better chance that your indentation will stay aligned after several people have edited the same code. IMHO
Pete If minds had anuses, blogging would be what your mind would do when it had to take a dump Maddox
Peter Mulholland wrote:
If you can get everyone using spaces then there's a better chance
Yes, that's the crux, everyone has to follow the standard, whatever it is. However, it seems to me that the easier standard to follow is to use SPACEs, particularly when various editors or code generators are used.
-
Mike Marynowski wrote:
that's the beauty of shift+tab
The problem is, at least at first, remembering the shift part, the thing can end up looking like a dogs breakfast if you are a non-touch typist.
Henry Minute If you open a can of worms, any viable solution *MUST* involve a larger can.
Henry Minute wrote:
a non-touch typist
Like me? I never heard about Shift-Tab until that post, I just use Delete, no big deal.
-
What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?
Personal preference is tabs (3 spaces), but I am glad that other people have other preferences. I can almost always tell my code from others because of formatting. I got used to compacting my code when we only had 80x25 green screens so I could see more code on the screen at a time. I like:
if (...) {
...;
} else {
...;
}others always do:
if (...)
{
...;
}
else
{
...;
}or:
if (...)
{
...;
}
else
{
...;
}It makes it easier to spot my own code and what may have been modified by me or others. I had a friend that could spot my code anywhere. He always claimed that I could write a whole program on a single line. :rolleyes:
SS => Qualified in Submarines "We sleep soundly in our beds because rough men stand ready in the night to visit violence on those who would do us harm". Winston Churchill
-
I used to use 3 spaces for indenting when I worked with punched cards and paper tape. That way, you could still have reasonable levels of nesting within the 80 chars on a card. Now that I have disc files, I use the TAB key - it is only one key stroke (which saves on storage) and it does not matter so much if I exceed 80 chars per line. I even allow myself to reach the 80th column when writing comment lines - in the punch card days, cols 73 to 80 were saved for sequence numbers so you could resort your deck after dropping them; I even (shock horror) allow in-line comments to slightly break the 80 column rule; if more than slightly, the comment goes on its own line with the code underneath. To preserve visual formatting regardless of how different editors display TAB, I use TAB for indenting but then I use spaces inside the code / comments after the first non-TAB character. This keeps tables / type declarations etc lined up.
The only place I worked that had a standard, specified eighty. I assumed it was because we used dumb terminals (VT340s?), but I set mine to 132 columns. PRO*C also defaulted to an eighty character maximum (WTF?), but a different maximum could be specified. Now I limit myself to 112 characters because that's how many I can fit on a page 8.5" wide with half-inch margins and an 8-point font. Such lines span only about half the width of my screen when I use a WYSIWYG editor.
-
What, if any, is the practical reason for preferring spaces over tabs? I use tabs because when I need to increase or decrease an indent, it's only a single character that must be added or deleted. I noticed that the Google Guidelines disallow tabs, and use only spaces. What have they got against tabs?
I can think of one reason. When aligning columns of assignments, if comparisons, etc. When using tabs instead of spaces, say yours is set at 2, and your colleague's is set at 4, you'll find freaky alignments for the mixed code. e.g. int foo = 0; int foobar = 0; So, I prefer spaces. And a code standard to enforce the number, after deliberations of course. :) Some shops use 2, but most I've found prefer 3.
This statement is false