Ah, spring. What a wonderful time of year. A time when young programmers' minds turn to thoughts of ... neverending last-man-standing filibuster arguments about code formatting.
Naturally.
And there is no argument more evergreen than the timeless debate between tabs and spaces.
On defaultly-configured Unix systems, and on ancient dumb terminals and teletypes, the tradition has been for the TAB character to mean move to the right until the current column is a multiple of 8. This is also the default in the two most popular Unix editors, Emacs and vi.In many Windows and Mac editors, the default interpretation is the same, except that multiples of 4 are used instead of multiples of 8.
A third interpretation is for the ASCII TAB character to mean indent to the next tab stop, where the tab stops are set arbitrarily: they might not necessarily be equally distanced from each other. Most word processors can do this; Emacs can do this. I don't think vi can do this, but I'm not sure.
With these three interpretations, the ASCII TAB character is essentially being used as a compression mechanism, to make sequences of SPACE-characters take up less room in the file.
So, then, the question: should code* be indented with spaces..
or with tabs?
According to Cyrus, there's a third option: an unholy melding of both tabs and spaces. Apparently you can use tab for primary indentation alignment and then spaces on top of that for detail alignment. Like so:
This way, in theory at least, the level of indent can be adjusted dynamically without destroying alignment. But I'm more inclined to think of it as combining all the complexity and pitfalls of both approaches, myself.
OK, so maybe you're an enlightened coder. You've moved beyond mere earthbound issues like tabs vs. spaces on your personal path to code nirvana. Perhaps you have some kind of fancy auto-formatter that runs on every checkin. Or, maybe you're using a next-next-generation editor that treats code as "data" and the layout (including whitespace) as a "view", making all these concerns largely irrelevant.
But there's a deeper issue here to consider. The only programming project with no disagreement whatsoever on code formatting is the one you work on alone. Wherever there are two programmers working on the same project, there are invariably disagreements about how the code should be formatted. Sometimes serious disagreements. The more programmers you add, the more divisive those disagreements get. And handling those disagreements can be .. tricky. Take this email I received from Philip Leitch:
The place where I work currently has a developer (who is also the head of the development department), who will "clean up" the code of others.That is -- reformat it, normally without changing what the code does, just changing the variable names, function names, but mainly moving things around to the way they like it.
It is a little perplexing Ö and I'm interested to see what responses people have on this issue.
One of absolute worst, worst methods of teamicide for software developers is to engage in these kinds of passive-aggressive formatting wars. I know because I've been there. They destroy peer relationships, and depending on the type of formatting, can also damage your ability to effectively compare revisions in source control, which is really scary. I can't even imagine how bad it would get if the lead was guilty of this behavior. That's leading by example, all right. Bad example.
The depressing thing about all this is that code formatting matters more than you think. Perhaps even enough to justify the endless religious wars that are fought over it. Consider the 1984 study by Soloway and Ehrlich cited in Code Complete:
Our studies support the claim that knowledge of programming plans and rules of programming discourse can have a significant impact on program comprehension. In their book called The Elements of Programming Style, Kernighan and Plauger also identify what we would call discourse rules. Our empirical results put teeth into these rules: It is not merely a matter of aesthetics that programs should be written in a particular style. Rather there is a psychological basis for writing programs in a conventional manner: programmers have strong expectations that other programmers will follow these discourse rules. If the rules are violated, then the utility afforded by the expectations that programmers have built up over time is effectively nullified. The results from the experiments with novice and advanced student programmers and with professional programmers described in this paper provide clear support for these claims.
There's actual data from honest-to-goodness experiments to support the hypothesis that consistent code formatting is worth fighting for. And there are dozens of studies backing it up, too, as Steve McConnell notes:
In their classic paper Perception in Chess, Chase and Simon reported on a study that compared the abilities of experts and novices to remember the positions of pieces in chess. When pieces were arranged on the board as they might be during a game, the experts' memories were far superior to the novices'. When the pieces were arranged randomly, there was little difference between the memories of the experts and the novices. The traditional interpretation of this result is that an expert's memory is not inherently better than a novice's but that the expert has a knowledge structure that helps him or her remember particular kinds of information. When new information corresponds to the knowledge structure -- in this case, the sensible placement of chess pieces -- the expert can remember it easily. When new information doesn't correspond to a knowledge structure -- the chess pieces are randomly positioned -- the expert can't remember it any better than the novice.A few years later, Ben Shneiderman duplicated Chase and Simon's results in the computer-programming arena and reported his results in a paper called Exploratory Experiments in Programmer Behavior. Shneiderman found that when program statements were arranged in a sensible order, experts were able to remember them better than novices. When statements were shuffled, the experts' superiority was reduced. Shneiderman's results have been confirmed in other studies. The basic concept has also been confirmed in the games Go and bridge and in electronics, music, and physics.
So yes, absurd as it may sound, fighting over whitespace characters and other seemingly trivial issues of code layout is actually justified. Within reason of course -- when done openly, in a fair and concensus building way, and without stabbing your teammates in the face along the way.
Choose tabs, choose spaces, choose whatever layout conventions make sense to you and your team. It doesn't actually matter which coding styles you pick. What does matter is that you, and everyone else on your team, sticks with those conventions and uses them consistently.
That said, only a moron would use tabs to format their code.
* unless you happen to be programming in whitespace or Python.
I'm a tab guy. It's just more efficient.
Dan Holtz on April 15, 2009 7:39 AMOnly us programming dorks would have this argument gleefully for the eleventy billionth time.
I use TABS, spaces are for old farts that use vi and emacs.
Give me my tabs and pretty colors in my fancy IDE all day long. Weeeeeeeee!
chrisb on April 15, 2009 7:51 AMthanks for the code, very useful!!!
FourQueensLasVegas on April 15, 2009 9:38 AMThis is really a simple argument.
Any of the options will work properly if all developers follow the same standard.
Any of the options will fail miserably if some developers follow the same standard, but others don't.
Only the third option, tabs for indentation plus spaces for alignment, will allow developers to change the number of spaces per indent to their personal preference without screwing up the code. A guy who likes 2-space tabs can set his personal view to be two spaces, while a guy who likes 8-space tabs can do the same. The code will still be perfectly indented either way, and using tabs+spaces even multi-line function calls will still line up no matter what your tabstop value is.
If you just use tabs, you still get the same benefit of not imposing one man's personal tabstop value preference on all, but multi-line function calls will no longer be aligned for non-standard tabstop values.
If you just use spaces, you are forced to impose one man's will on the crowd, like a new web developer who doesn't understand CSS.
This is all in the theoretical world where people follow the coding standard. In that world, tabs+spaces is the clear winner. In the real world, where people always forget about the standard and screw up, tabs + spaces is just too darn complicated for some people to understand, increasing the likelyhood of nonstandard code. Spaces-only goes from being the worst option to a potential winner because you can at least override a non-standard tabs-user by automatically converting tabs to spaces on checkin. However, you could convert spaces-at-the-start-of-a-line to tabs just as easily, so there's really no case where spaces are the winner.
Tabs are always going to win the day due to allowing fussy developers to view code in their own personal tabstop preference. Spaces just can't do that. Spaces are great if you believe your personal viewing preferences are The One True Way, though, and you'd like to force everyone else to follow them.
BBT on April 15, 2009 10:15 AMTAB. End of discussion.
EthR on April 15, 2009 10:24 AMHow do you shift multiple lines of text around if its indented with spaces? Does anyone really use spaces anymore?
joe on April 15, 2009 10:35 AMI've never heard one good argumnent for spaces and I still haven't today after reading this article and most of the comments.
Tabs provide the same readability, allow custom indentation levels at a per-programmer level, and stop people from wasting time doing worthless custom formatting with spaces to make arguments line up, etc.
People that use spaces to indent their SQL should be shot.
JohnOpincar on April 15, 2009 10:54 AMTo clarify worthless custom formatting, imagine somone that uses spaces to make their formal and actual parameter lists line up with the open paren. If you change the name of the method, you have to manually change the formatting of the method definition and every call to the method. People that use spaces are usually noobs or don't have enough real work to do and should be laid off :)
JohnOpincar on April 15, 2009 11:00 AMWhy not have version control that normalizes source code before check-in, then apply a user's formatting choice on check-out? If all somebody does is muck with the whitespace, nothing actually gets checked in.
Sure, it wouldn't be perfect - e.g. how do you handle long single line comments - but it would be better than listening to everybody belly-aching about spaces, tabs and bracket placement...
PangMao on April 15, 2009 11:01 AMSpaces are: absolute, predictable, necessary, reliable, standard, WYSIWYG.
Tabs are neither of the above.
In an ideal world tabs might provide I see it as I like it, you see it as you like it effect and may also spare some typing.
In the real world tabs are used inconsistently by the humans and interpreted inconsistently by the tools most of the time.
There is still chance to get tab usage right if only one person (you) and one tool (your editor) are involved. Unfortunately, that's not always possible. We work in teams, deal with third-party code, use multiple editors AND other tools.
On the other hand, it is absolutely trivial to get space usage right.
So at the end it is a matter of priorities. If you value consistency and reliability more than your personal convenience, you inevitably chose spaces. Otherwise (and I don't criticize anyone about it) you might stick with tabs AND spaces.
BTW I always LOL when the amount of typing is used as argument related to programming. We are *not* typists -- so it doesn't matter that much. On average a programmer produces about 100 lines of code per day, or about 0.2 character per second. Compare that to 50 words per minute (5 char/sec?) that an average typist achieves.
THE AMOUNT OF TYPING IS NOT IMPORTANT.
I'm surprised no one mentioned an old classic:
a href=http://www.jwz.org/doc/tabs-vs-spaces.htmlhttp://www.jwz.org/doc/tabs-vs-spaces.html/a">http://www.jwz.org/doc/tabs-vs-spaces.html/a">http://www.jwz.org/doc/tabs-vs-spaces.htmlhttp://www.jwz.org/doc/tabs-vs-spaces.html/a
Most programming editors allow YOU to define the number of spaces to display in a tab. Therefore one tab can equal 2 spaces, or 4 spaces or 20 spaces if you want to get pervy.
It annoys me when people use spaces, as I'm then stuck to seeing it their way. USE A TAB YOU MORON!
Tab once and save space!
bloop on April 15, 2009 12:06 PMLet's play: what's the smallest, stupidest thing that can incite controversy in nerds? This topic could very well win!
chris on April 15, 2009 12:36 PMThe universal assumption in all the discussion about tabs vs spaces seems to be that your text is always viewed in an editor where you can configure your tab width preference. But what about command line output, like more, grep, etc. This happens to be a predominant use case for me on any project, and tabs cause all kinds of chaos for that. Long live spaces!
Do 'man tabs'. On a Unixy system with a good gnu foundation you'll see that 'tabs -4' will adjust your terminal tab setting to every 4 characters.
Also, see 'expand' and 'unexpand' for automagically expanding tab characters to the matching number of spaces (or vice-versa) via piping.
If you can't remember any of the above, try apropos tabs, which will give you the list of the handful of utilities on your machine directly aimed at dealing with differing tabstop settings.
Having to set up tabs in multiple applications may be a chore, but the tab width if used properly (ie, tabs for indentation, not alignment, see above) won't mess up the representation of anything in your diff program if indeed you are too lazy to find the option to change it. The *worst* outcome is that you have to scroll more horizontally. As I said above, even if using tabs for alignment instead of spaces: if you are relying on alignment for semantic cohesion, you are already leaving a maintenance nightmare and timebomb in your code; it's not necessarily a bug that your source code control diff program makes that kind of thing look ugly!
All that having been said, I find it very amusing how many space-zombies above rationalize their choice by saying it makes their code consistent, and yet vehemently disagree over how much indentation should be provided at each tab stop. Tabs directly fix that disagreement by allowing each developer to define their own preference. Inconsistency is a feature, not a bug! Does it also bother you that your fellow developers might be looking at your code in Inconsolata or Monaco or even *gasp* Verdana instead of the Courier New font you wrote it in?
Tom Dibble on April 15, 2009 1:08 PMThe important is to have a program running, whether you use Space or Tabs.
But considering that people are going to look at your code after you, for maintenance or new development reasons, then no doubt it should look esthetically good.
For this, I personally prefer to use Space as it is more strict and more precise. Space gives you more control on how you want your code to look like.
Omar Abid over the last 5 days has been literally been SITTING on that F5 button to get that pureile 'First' statement in there.
Personally, when formatting my code, I conform to what the lead dev. or dept. tells me to do... Which is normally NOTHING :(
I swear by Tabs and whoever disagrees offends me and my religion, and should expect a programmers' Jihad.
We, the People's front of Tabspacing have only one enemy, the spacers. Oh, and Tabspacian people's front too.
We will not rest until all editors disallow more than one consecutive spaces. Our struggle is also know as Der tabspace kampf.
It seems like a lot of space fans seem unable to tell the difference between writing code formatted the way the reader wants it to look and code formatted the way the writer wants it to look.
Tabs do both, spaces only do the latter. Hopefully readers here are smart enough to realize that you should want the former.
BBT on April 15, 2009 1:28 PM@patrick:
Spacers don't hit the spacebar, they rely on their smart IDEs for converting tabs to spaces when you write them. Then again, they also preach using spaces arguing it's to help those using dumb IDEs, or even notepad :)
The only valid use of spaces is to vertical align stuff (e.g. method parameters). I can surely live without that, but some people are religious about it, so more power to them.
I'll probably be a Tabber as long as my IDE does not jump 4 spaces at once on arrow keys and hitting backspace deletes only one space instead of 4. Or 3? Or better 2? But only in the context where I did want to indent, of course (good luck at guessing that)...
I guess I'll be a moron for life.
Dan C. on April 16, 2009 3:54 AMI'm a moron too xD. Tabbing has always been the way to go for me. I have never even considered using spaces, and my hand does it automatically.
David Siaw on April 16, 2009 6:20 AMIt seems to me that the difference between 2, 4 and 8 space tabbing is akin to asking experts and novices to memorize chess pieces on a black and white tiled board versus a red and green tiled board. I agree a team should have standards so you don't get into a check-in format war at that trivial of a level but the study looks like it was more about memory relating to rationally ordered statements versus random randomly ordered statements. I just feel that comparing that to whitespace preference is a false analogy.
SoapboxIf you don't use an intelligent IDE of some form then you don't care about productivity...and notepad.exe is not an IDE./Soapbox
Matt Poland on April 16, 2009 9:12 AMUsing tab characters does indeed allow others to see things according to their preferences, but it's only a small step in the right direction. There are many more aspects of preference that need solving. The goal should be for editors to reformat the code to your preferences on load, and reformat the code to a standard format on save. (In the event that the editor can't parse the code, it can simply skip the reformatting step.)
The interaction between automatic reformatting and manual alignment should be interesting -- perhaps Nick Gravgaard's elastic tabstops would provide a solution.
Mr.'; Drop Database -- on April 16, 2009 9:31 AM
1. You can't get by without spaces.
Silly. You can't get by without the letter A. That doesn't mean you should use the letter A everywhere.
2. a mixture of tabs and spaces will inevitably lead to misaligned, hard-to-read code.
Silly. It's misaligned because people indent with different numbers of spaces. If people didn't use spaces, there would be no alignment issues!
3. you can get by without tabs: in fact, everything they do is really much better done by other means.
EXCEPT indentation because everybody wants to impose their own goofy number of spaces on other people!
convert between different sized indent amounts (but you won't find these functions anywhere)
Any this would mean spurious differences in source contro.
Spaces are: absolute, predictable, necessary, reliable, standard, WYSIWYG.
Except, it's 2, 3, 4, 5 spaces = not absolute, not necessary (for indentation), not predictable, and not reliable, and not standard!
Tabs are neither of the above.
Tabs are -one- thing.
WYSIWYG is not an accurate description for spaces.
WYSIWTS (What You See Is What They See) is what is meant here. Then the question just becomes what on earth would possess you to want to force others to see things according to your formatting preferences instead of their own?
BBT on April 16, 2009 1:19 PMAh... the joys of whitespace. I'm currently doing my project for college on an AS400. Newer versions of RPG have whitespace but the version installed on the college machine doesn't. In some ways it's actually easier but then you've forgotten an endif somewhere and it takes about ten minutes to track it down in a nested if statement...
I personally feel that tabs are quicker. I can't imagine hitting spacebar three times everytime I wish to indent. Just hit tab once and you've got your indent.
Doesn't hitting spacebar all the time give you a headache?
patrick on April 16, 2009 1:48 PMHi.
I'm the guy that made the statements about a developer changing code of others (all others in the department).
RE:
This guy seems to be complaining about a lot less trivial changes than tabs, spaces and the location of curly brackets.
No - quite definately this is about trivial changes, locations of spaces, tabs as well as Variable and Function names. This is nothing about cleaning code, but entirely about reformatting. And that is what has me scratching my head.
I take no offence to this - my code is being altered to the way the person wants and they are a boss. I get paid a good wage and they can litterally do with my code whatever they like (sell it, delete it, alter it, etc.) I'm not so arrogant to get offended that my code is changed, after I write it. I'm proud of the work I have accomplished but then it then belongs to the company.
I just thought it very strange that this person would spend the time to do reformatting just for the sake of reformatting. Is it putting their stamp on the code? Is it them trying to understand the code? Is it just that they find it easier to read?
The greatest impact is on code revision checking, because things that look like changes to the code are merely changes in white space or function names. But again, I don't relaly care too much about that.
But I'm just left puzzled as to why it even happens.
Philip on April 19, 2009 5:25 AMThere *are* decent editors who will unindent the proper amount when you hit backspace on a space-indented line.
vim on April 19, 2009 7:39 AManother moron, another tab lover :xD
avar on April 19, 2009 9:44 AMI'm a first year engineering student and recently had to complete my first major group programming project. One annoyance we ran into was different programs using different tab spaces. So code that looked perfectly tabbed in one editor was completely misaligned in another. So maybe spaces is a better solution. I personally do prefer tabs though since I'd rather hit backspace once rather than four times or hit tab once rather than the space bar four times.
Ian on April 19, 2009 11:02 AMchoosing spaces will increase your source code size if you've converted tab to spaces according to your editor setting the source code will be replaced with 4-8 tabs(whichever you specified). while tab can be represented with single character.
Anyway as you said it's upon the policy of team, group or organization to stick with particular style.
Sarath on April 20, 2009 7:35 AMIt should read: I'm a spammer and identity theif.
Philip on April 20, 2009 12:18 PMCould you help me. You can have it all. You just can't have it all at once.
I am from Jordan and learning to write in English, give true I wrote the following sentence: Airline carry on restrictions liquids.
Thank you so much for your future answers :p. Horus.
Horus on April 20, 2009 12:39 PMThe Tab button is short for Tabulate. Therefore it should be reserved for those situations where you would like to format your code into specific columns of indentation.
e.g. classes to the far left, one tab in for functions, another tab for initial code in those functions.
That was always the intention of Tab.
But the point everyone is missing is that Jeff wasn't serious about the moron bit. We REALLY need a sarcastic font so intention is obvious.
Philip on April 21, 2009 4:30 AMwow, it's interesting to see that an article about blank characters generates about double the comments then one about the meaning of open source in job interviews ;)
maybe it's because you insulted our beloved TABs...
i, personally, like tabs because it prevents me from curser-arrowing myself to death when debugging nested structures. plus, with the stroke of one key, i'm exactly at the next code level, no mistake possible. also, when you space your code, different preferences can get VERY dangerous, especially, when one guy likes two, the other likes 4 spaces - with tabs, thats a simple config setting. plus, space indented code tends to get really messy after a few revisions when people are careless. so i see use of tabs vs. spaces a bit like OOP vs. procedural languages: a good developer can get the same results with both, but OOP helps to protect us from the slobs out there (and most snobbish programmers despise non-OOP langs using mainly that argument).
and on correcting others code: to rename other peoples vars or functions should be a definitive no-no (except when sombody uses varnames like a1-z99 - that person has simpy forfit the right to touch code)! but i must confess, when i have to debug very complex strange code, i simply must reformat white spaces (bracket positions and such) or i would have a serious loss of overview and efficiency.
bradbury on April 21, 2009 5:22 AMSomebody probably already said this above, but lots of modern code editors have an option to make the tab button add spaces to your code. It functions like the tab button, but it's actually adding space characters. This is important if your code is going to be appearing in different editors. Everything reads space characters the same, but not tabs.
Devin on April 21, 2009 7:15 AMSorry for coming in so late, I was in another universe for awhile. However, to jump right in, I feel two spaces leads to not enough of an indent and four spaces uses to much room, so I prefer 3 spaces I know its an odd number but what the hay. Really it appears to be the perfect spacing for code indents. If you really can't deal with the odd number, four spaces is definitely better than two as a good programming practice if you are indenting too much then you should be breaking that code up some.
BTW, for most of those tab-freaks your editor should have a keyboard shortcut to the beginning of a line or the first character in a line. In Unix/Emacs it is crtl-a once to beginning of line and twice to get to first char. In Windows it is the home key. IF other editors don't have it and it's open source add the functionality in.
Anyway the point is tabs are usually not the way to go in multiple environments / multiple developers. IMHO.
Last point to tab-freaks, I was a self-proclaimed TAB-FREAK for many, many, many years. Reviewing code from multiple environments / multiple developers will crush your desire to see a single tab in code ever again!!!
Spaces *always* work. Tabs often don't.
Phil on April 21, 2009 7:43 AMFollow up post. Several modern editors will you to highlight/block off one or more lines of code such that when you press tab or shift-tab the editor will indent/un-indent that code with spaces not tabs unless of course you are still using tabs (see previous post).
Jim on April 21, 2009 7:50 AMNo - quite definately this is about trivial changes, locations of spaces, tabs as well as Variable and Function names. This is nothing about cleaning code, but entirely about reformatting. And that is what has me scratching my head.
If you think variable or function names are trivial, as in unimportant, well then I could hardly disagree more. They are the essence of readable code. The well named variable will usually eliminate the need for a comment, and the well named function will make code understandable in a way no amount of commenting will allow. And readable code is the heart of maintaining a large source base.
If your team lead is removing contractions and adding specificity to your variables and function names, then he is showing that he has been burnt by these things in the past and is trying to save both of you long term pain. You would be wise to learn from this.
Glenn Howes on April 21, 2009 12:19 PMWhen the team that reports to me switched from a waterfall development method to extreme programming, we decided we would actively try the shared code ownership practice. One of the recommendations of that practice was consistent formatting. We didn't have long battles, but we had dissent in the team that could not be resolved.
We were coding in Java, so we decided we would adopt Sun's Java coding standard. We were then delighted to find the open source version of jalopy which was able to format the code to enforce that coding standard.
We made that switch in 2003, and the team adapted very comfortably to having the code formatting fixed each time they compiled the code. The formatting step was an integral part of the build process, and was supported by a nightly automated task which checked out all the Java code, formatted, and checked in anything that was changed by the formatting.
That was over 5 years ago, and that code base is still being developed, still being actively formatted to a single consistent style, and I believe the developers on the project are glad for the consistent formatting. Comparing file versions is easy, and no time needs to be given to making formatting consistent. It will be made consistent by the tool.
I believe the program astyle can do the same thing for other languages, as can indent for C.
The details of the standard seem to have been less valuable to us than the fact that there was a standard and it was automatically enforced.
Mark Waite on April 23, 2009 8:27 AMI've always been a fan of both.
Personally I use tabs for html and markup languages, while I use spaces for other languages.
Tabs have a strategic advantage in html. If you don't have the plus of using gzip compression a tab is equal to that of 4 spaces. Meaning you can indent in less characters. I recall several years ago optimizing an awful table based page by simply converting spaces to tabs. Saved several Kb, which over a dial up connection made a difference.
For things that are compiled, server side, or file size doesn't matter, I prefer spaces (4 to be exact) since that seems the easiest on the eyes and the most consistent across platforms.
Robert Accettura on April 23, 2009 1:49 PMThe tab argument is tempting, but it doesn't actually abstract spaces away. Because of tab stops. Tab stops, or spaces multiple of 8 functionality mean that everyone will see tabs differently.
Spaces, if you aren't brain dead, will at least look sane on all editors. Tabs can make code look ridiculous, especially if one person uses tab stops or multiples of 8 and another interprets tabs as spaces.
Unless you refuse to use a fixed-width font, in which case there's no help for you and you've probably given up looking at code in any sensible fashion anyway.
Rick Scott on April 24, 2009 11:11 AMI don't agree with the don't use Tab argument. Every code editor I've used in my career, from multi-edit to SQL Query Analyser has the ability to customise font, colours and Tabs. In fact, if you are in an environment where Tabs can't be adjusted - perhaps you should consider changing IDE.
I think one thing missing from this debate is the fact that different developers frequently develop with different visual settings.
I find standard black on white letters a bit hard to see and after some experimenting I've found Comic Sans MS in 12 font to be used. Another developer here prefers a black screen with light coloured fonts. Yet others go defaults all the way.
But spaces look different under different fonts, but tabs don't (at least not on our environments. But I agree with Mark Waite above - automatic re-formatting makes sense.
Philip on April 26, 2009 6:43 AMThe third style makes most sense, and allows you to adjust code indentation for clarity.
It is increasingly important to use the most flexible method of indentation when dealing with multiple authors since it's not reasonable to assume that everyone else wants to read your code using the same indentation length.
The discussion should never be tabs vs. spaces, it's about correctly representing the indentation you want to achieve.
Just because your editor sucks, doesn't mean you should make others suffer while reading your code. (write once, read many times, remember?)
Why anyone prefers spaces over tabs is beyond me, it's like saying we only need a single datatype when we are representing fundamentally different things.
I don't think there is anything wrong with tabs for indenting flow control and such. If there is a problem it's far easier to convert tabs to spaces than spaces to tabs. Either way no matter what you do there will be a point where the kludge is as inescapable as it is laughable.
However there is one thing I must point out. Do not always rely on an IDE to apply white spaces. At least, don't rely on it entirely and blindly.
Of course as mentioned if you can use a tool with some parsing ability (so it knows what is a literal string, comment and so on) to clean up your code. Again as mentioned, use a constant width font.
If the option is presented to you, always turn show white space characters on. I've seen some coding horrors where the use of spaces and tabs is completely inconsistent (even on the same line OMG). Worse still due to the way tabs work and auto align I've seen spaces in a column (at least a column where tabs are concerned) that are totally useless. A particular problem I've also noticed with IDE's in particular but can generally occur in any case where white space is actually plain white is whitespace at the end of lines.
It makes my blood boil.
Joey on April 28, 2009 5:28 AM@Jonathan: That's awesome. Elastic tabstops. It addresses both problems, and ends up promoting a standard that most people use tabs / spaces to do manually anyway. With a bit of tweaking, it could even reformat code, and handle anyone's preferences quite simply.
I like.
I wonder how hard it would be to hack into Xcode and/or TextMate.
Groxx on April 28, 2009 11:14 AMwhat's amazing about this discussion is how every criticism of tabs seems to be based on the idea that it's a bad thing to let the reader view code according to their personal preferences. That sure seems like a positive thing to me!
Everyone keeps saying things that amount to But if someone else chooses to view it with different preferences than mine, they won't see it with my preferences!. Yes, that's correct. Isn't that good? If they want to view things with your preferences, they can use your preferences.
BBT on April 29, 2009 11:25 AM50 pages of comments in a few days over spaces vs tabs....
Lolz on May 1, 2009 3:15 AMThe point I'm trying to make is that tabs alone do not allow the source code to be read according to your own preferences.
The elastic-tab convention is currently the _only_ real solution to this problem.
If everyone in a project adheres to it, then everyone can read the source code in the way they see fit, and therefore avoid useless commits or other nonsense.
Tabs should only be used as the special indent character they are supposed to be and spaces for representing any kind of manual deviation from ordinary indentation.
Emacs supports the third option:
http://www.emacswiki.org/IntelligentTabs
Marius Andersen on May 24, 2009 10:41 AMthis is so cool. i think you should mix the tabs and spaces together.
Ahahaha, what a wonderful post. It just never ends.
Practicality on June 4, 2009 3:19 AMWhat kind of insanity has emacs done!?
Practicality on June 4, 2009 3:21 AMVim has it too, now:
http://www.vim.org/scripts/script.php?script_id=231
Marius Andersen on June 5, 2009 5:46 AMYes, it never ends.
Now I throw the question to all the no-tabbers. What does it matter if one editor interprets a tab as 4 or 8 or 2 or what ever width of characters. IMHO, that is what is so good about tabs. If a coder whats to see the code with 2 character wide intends, he is free to do so. If another wants to see it as 8, just set it like that. No big deal. One tab per one level of indentation. I don't understand what is so difficult about this. I can't stand some of these editors that insert spaces instead of tabs. If they'd also backspace the same about of spaces that would be fine. But no. I hit tab and then have to backspace 4 spaces. WTF? I'm lazy. ;) Oh and tab stops should not exist in a real text editor. OK? WTF is that? Tab stops? Something from a damned word processor. Yawn...
sims on June 29, 2009 6:22 AMI actually prefer to use neither. Indentation tends to push the code too far off to the side. If you don't indent anything, all of the code will be neatly lined up on the left margin.
I wrote a small script a few years back that strips any whitespace from the beginning of each line. It works great and keeps code looking nice.
Phil on July 5, 2009 10:00 AMnick said it correctly: "TABS work great in that most popular editors have preference settings related to how many spaces to use when representing a TAB, this way if one developer likes to look at it with 2 spaces and another with 4... the TAB is displayed to the developers liking."
The key here, which people who still use Notepad don't understand, is that a good editor *displays* a tab as a number of spaces, and that number is configurable (in good editors, of which Komodo is one). The source code file remains unchanged, and everybody can be happy. Separation of data and presentation has gotten into this area as well. People simply need to stop using Notepad as a software development editor (it's designed to write notes, not programs).
Roman on July 8, 2009 3:16 AMIn all of this I have yet to see the main argument that caused me to abandon hard tabs in code nearly 20 years ago: documentation. Try taking a piece of code written using hard tabs and paste it into MS Word (where tabs stops are in inches or centimeters) or an e-mail or a web page. The results vary from marginally usable to unreadable.
I can’t image how horrible the code from an “elastic tabstop” editor would look when viewed in any other application. Once you start using something like that your code stops being a plain text file and becomes non-standard. Even though it is still a text file so it can be viewed in other applications you lose the intended formatting. It takes the problems with hard tabs and amplifies them.
Assuming that the code will only be viewed or edited in a particular program or group of programs is rather short sighted. You yourself may be in a situation where you have to view or edit a file in notepad or Word or an ancient version of vi. I know I have. Using all spaces may mean that you have to type and delete multiple spaces, but, the code will appear as intended in the word processor or old text editor and if you make changes you won’t have to go back later and fix the formatting.
I currently work as a contractor and follow whatever coding standards my client prescribes, but I feel fortunate that in the last 5 years I haven’t come across a single shop that used hard tabs.
Tim on July 15, 2009 1:31 PMI think the third option -- tabs for indentation, spaces for alignment -- has an advantage when it comes to documentation. When coding on my widescreen, I prefer a large tab size for maximum legibility: four or eight columns per indentation level. However, that's way too much for paper, so I use LaTeX's listings package to set the tab size to two (\lstset{tabwidth=2}). The code will then display as if it was indented with two spaces per indentation level.
This way the original line numbers are retained as well, since I can just include the necessary lines from the source files themselves without reformatting it first.
Marius Andersen on July 19, 2009 6:10 AMI think spacing/tabbing should be controlled by the IDE. I couldn't live without the Correct Indentation function in Eclipse.
This way it doesn't matter which how to format your code. Just make sure to hit Ctrl-I before checking in :)
Jayson P on February 6, 2010 11:17 PM@Anthony: Then I guess youíre out of luck, but thereís always the option of popping your code in an ìauto-indentî aware IDE or editor.
Some of us are just too attached to console editorsÖ
If you have a standard formatting method for when code gets checked in, can't you automagicaly transform from your preferred method to 'team compliant' and of course from team compliant to your preferred?
Don't IDE's do this for you already?
Spoon on February 6, 2010 11:17 PMMake sure everyone on the team has the same tab/space settings in the IDEs, otherwise version control and code reviews are full of nonsense changes.
Also makes it easy to change your mind later if you use a tool that can auto-format. Personally I don't like auto-format but I have to admit it increases overall project readability and reduces changes in version control.
Jason Cohen on February 6, 2010 11:17 PMOh, and if your IDE's whitespace (and other formatting) isn't sophisticated enough to do what you need and share with team members...
...invest in a real IDE. Notepad is not a code editor. :-)
Jason Cohen on February 6, 2010 11:17 PMIf I try to analyse your post as a formal, logical argument
(http://en.wikipedia.org/wiki/Logical_argument), I get the following:
Premise 1: The tab character can be expanded differently in different editors.
Premise 2: Formatting code uniformly is very important for program comprehension.
Conclusion: You should not use tab.
Right? As quite common in logical arguments, there are some missing
premises. The conclusion is in no way valid without also including
the following:
Premise 3: Using tab will make the code display un-uniformly.
As you discuss in your post, indentation can be handled in three
different ways,
a) indentation is X spaces
b) tab is X spaces
c) indentation is tab (expanded to whatever number of spaces you prefer), alignment is space
where premise 3 is true for a) and b) but false for c). Since premise
3 is not always true, this means also that the above conclusion is not
always true.
Discussing premise 1 and 2 is almost like kicking in open doors;
there is no controversy with those. The controversy occurs when someone
decides we shall have such and such indentation behaviour and that
conflicts with the preferred display preference of some of the developers.
Specifying this as premises gives:
Premise 4: People have different preferences on what the optimal width of indentation is.
Premise 5: Reading code with a different indentation width than your preferred makes the code harder to read.
and the the complete argument then becomes
Premise 1: The tab character can be expanded differently in different editors.
Premise 2: Formatting code uniformly is very important for program comprehension.
Premise 4: People have different preferences on what the optimal width of indentation is.
Premise 5: Reading code with a different indentation width than your preferred makes the code harder to read.
Decision 1: Mr/Mrs/Miss/Group X decides that indentation is X spaces.
or Decision 2: Mr/Mrs/Miss/Group X decides that tab is X spaces.
Result: The code becomes uniformly formatted, but is harder to read than optimal for some of the developers.
I put argument in quotes and use result instead of conclusion since the
decisions are based on taste/personal opinion and this is not a proper
logical argument any longer.
In some cases this might be a fair enough strategy; it might be impossible to
make everyone happy. This consistent with what you write in your paragraph:
Choose tabs, choose spaces, choose whatever layout conventions make sense
to you and your team. It doesn't actually matter which coding styles
you pick. What does matter is that you, and everyone else on your team,
sticks with those conventions and uses them consistently.
However, in some way this also signals screw those developers that have
a different indentation width preferences, and as the observant reader
have noticed there is a third possible decision which will make everyone
happy with regards to reading the code (although possibly not for writing
(but this is irrelevant)).
Consider the relation between reading and writing code. Code is written
in the range of 1-10 times by one person (including some editing and
possibly pair programming). It can however possibly be read by tens (or
hundreds/thousands) of other people, several (maybe hundreds of) times.
This gives that having code that is easy to read is several orders
of magnitude more important than code that is easy to write. I.e. how
simple or complex some issue is to write is is irrelevant if it increases
readability.
The overall goal of any formatting discussion is to increase readability.
In fact that is the only important factor, to let the code be readable.
As written in the preface to the first edition of SICP: Thus, programs
must be written for people to read, and only incidentally for machines
to execute..
The best result is therefore to write the code so that everyone can
use their own preferred indentation display width without forcing this
upon others.
Premise 1: The tab character can be expanded differently in different editors.
Premise 2: Formatting code uniformly is very important for program comprehension.
Premise 4: People have different preferences on what the optimal width of indentation is.
Premise 5: Reading code with a different indentation width than your preferred makes the code harder to read.
Decision 3: Mr/Mrs/Miss/Group X decides that indentation is tab, alignment is space.
Premise 6: Decision 3 makes the source code tab expansion width agnostic
and allow everyone to display the source code with their own preferred
indentation width.
Result: The code becomes uniformly formatted and optimal for everyone to read.
Side argument:
Premise 7: Easy to read is several orders of magnitude more important than easy to write.
Premise 8: Some people will feel that decision 3 makes the code difficult to write.
Conclusion: Premise 8 significantly less important than readability improvements and can be ignored.
Premises 1, 2, 4, 5, 6, 7 and 8 are always true. Always. Please verify,
try to think of some situation where it could be false (and should you
think you found one by all means let me know). Then verify my conclusions,
it could be that I have made some mistakes? However the only way I can
see that you should be able to reach some other conclusions is if I have
Ignored some premises. What should that be?
Wow!
This is still an issue for lots of people. However, I gave up reading what everyone else said after the first 20 comments or so. Too much!
I am a team leader with a penchant for performing reformatting of non-compliant code. I only do this when I am trying to understand the code in question. We have a style guide which is ignored by one individual in particular. He doesn't think its important since the computer doesn't run source code, does it.
This used to bug the hell out of me a year ago but I have matured now.
These days, I no longer inspect his code more than others. I still do reformat checkins if I find non-compliant code when I am trying to understand something that needs changing.
I am 45. I believe that tabs are the right way but WTF do I really know. After all, I've only been doing this for 20 years. I stick to the org's coding policy even if it is insane.
FWIW, my definition of insanity is:
* only use spaces
* 2 space indents
* 8 space indents
I prefer 4 space indents have this is what we have at my current place of employment.
We also have a 2 blank lines between methods rule which helps separate things out a bit. We like it. Other orgs might not.
Now, if you *really* want to get me going we should have a chat about how equals signs in blocks of code handling alignment should be handled.
:-)
Gordon J Milne on February 6, 2010 11:17 PMOops! Typo time.
What I meant to say was:
Now, if you *really* want to get me going we should have a chat about how equals signs in blocks of code handling *assignment* should be handled.
:-)
Gordon J Milne on February 6, 2010 11:17 PM@Nicolas: captcha should probably change once or twice... it seems the Chinese spammers have discovered the secret power of orange...
Simon Wright on February 6, 2010 11:17 PMThanks for the great article. Your last sentence got our debate a good laugh! Well done, quite unpredictable. I've linked to it from my blog on the same topic where I cover how spaces are a better choice.
There are valid arguments on both sides of the spectrum, so it really boils down who make the call in your team.
But spaces are definitely for the more experienced developers. I wouldn't go as far as saying tabbers are "morons"... but I will let you do it :-D
Milanadamovsky.blogspot.com on March 5, 2010 8:40 PMDepends on how many editors you are using.
Generally, I'm pretty good about opening up my Python scripts (which is where this debate acutally matters , no?) in a single IDE; they're one time only, generally.
But I've opened up a Python script in several different editors:
- Geany
- VIM
- Emacs
It's sad to say, but my favorite CLI editor, VIM seems to be causing all the trouble. Eventually, I'll find the time to find out how big a VIM tab stop is and write a script to de-VIMify a script (or change VIM's tab size).
Adam Marchetti on July 26, 2010 6:36 AMAdd me to the moron list. I don't think you can gloss over the semantic value of the tab that way. Using it over spaces is not a compression mechanism. A tab says to indent. Think of that as the storage MODEL. Now the VIEW can show the tab in whatever aesthetic manner the programmer is used to. Everybody can see the code the way they prefer without disturbing the underlying stored code.
It's the 21st century now. If you insist on vi or emacs, take the 30 seconds it requires to set your tabstops and shiftwidth. (I guess you can tell which editor I started off with)
Just my two cents.
Coffeedawg on March 30, 2011 4:03 PMHow is there any arguement here? I hate it when I have to mess about with someone elses code trying to align everything with spaces. Just indent things properly with tabs.
Instead its like:
4 spaces used
5 spaces used
3 spaces used
4 spaces used.
Just press tab and you don't have to worry about getting it slightly out of place. You press backspace and it takes away an indentation.
Don't come crying to me when you have RSI spamming space and backspace thousands of times... :@
Dominic Watson on July 29, 2011 1:46 AMThe comments to this entry are closed.
|
|
Traffic Stats |