Aktuelle Änderungen Printable View Änderungen Bearbeiten
JVM > Java > WhitespaceInPythonClear TrailOne tab, one indent. Three benefits arise: 1) code can be cut and pasted between programs, 2) the individual programmer can set his display preferences any way he wants (to display a tab as three, four, eight, whatever spaces) and 3) the indentation throughout his program will always be regular.
Nothing worse than a character that looks like a sequence of spaces but isn't, that look like a DIFFERENT number of spaces depending on your display settings, and that you may not even be able to type without reconfiguring your editor! Any editor lets you type spaces last time I checked.
As for the space vs. tab issue: the rule to live by is use either tabs or spaces, just don't mix them! Pick just one or the other. Any decent editor (i.e. not notepad) handles this just fine. There are actually scripts and a warning option in the interpreter that will check this, so it's no big deal.
I assume by your "write ten statements on a line and let the editor format it" comment that you write code like this:
statement;statement;statement;statement;
In python, here's the trick: instead of typing ';', press the enter key instead. Problem solved. ;-)
Indentation consistency becomes easy if everyone agrees to use tabs. Then, each user, in their editor of choice, needs to decide what their tabstop should be.
If you don't like huge indentations, set your tabstop to 4, or dare to even try 2.
If you like lots of whitespace, set the tabstop to 8. If your editor can't do this, get yourself a good editor.
Tabs are evil.
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.
Worse, it's easy to mix using tabs and spaces to align your code. After all, it'll look lined up to you, but when I open it, I get the mess above.
I'm willing to grant that all tabs all the time would be as good as all spaces all the time...but the editor doesn't have a setting for "insert a tab for four spaces", so I'd have to concentrate pretty hard to make sure I never got a mix.
I believe I could be consistent, but I don't trust everyone else to be.
Well, I'm here to set the record straight on why tabs are a better choice. Like good object oriented design, it's simply a matter of recognizing the correct abstractions and encapsulating them appropriately...
If it uses two-space indents, it's corporate code; if it uses four-space indents, it's open source. (If it uses tabs, I didn't write it! :-)[1]
the level of indentation is exactly the number of tabs
Like the Newline, Tabs also have special meaning. Using Spaces to get to the next level of indention is no more acceptable than using Spaces to advance the cursor to the beginning of the next visual line. Tabs and Newlines contain metainformation about the code that using an arbitrary amount of spaces to get the same visual effect does not have.
Even if you do figure it out, it takes only one bad edit from somebody else to ruin the tabbing.
the semantics of tabs is what indenting is about, while, using spaces to align code is a hack.