Maintaining Maximum Line Length When Using Tabs Instead of Spaces?

Style guides for various languages recommend a maximum line length range of 80–100 characters for readability and comprehension.

My question is, how do people using tabs for indentation comply with the line length limits? Because, if the length of a tab is set to 2 in your editor, the same code could be over 80 characters long in someone else's editor with tab length set to 3/4/6/8.

Some suggest that I mentally see my tab length at 8 characters. This isn't realistic and won't let me focus on programming as I'd have to check it for every other line.

So, how do you (if you use only tabs in your code) do it?


Almost all languages have a convention for tab size. Most projects have a convention for tabs size as well, which is usually (though not always) the same as the language's tab size.
For example, Ruby is 2, Python & PHP is 4, C is 8, etc.

Sticking with the project's tab length, or the language tab size if there is no obvious project tab size, is by far the most sane thing to do, since this what almost all people will use, except perhaps a few.
The ability to set a different tab size is probably the largest advantage of using tabs instead of spaces, and if a user wants to deviate from the standard, then that's fine, but there is no reasonable way to also comply with the maximum line length in every possible situation.

You can compare it to most web sites; they're designed for a 100% zoom level. Zooming in or changing the font size almost always works, but some minor things may break. It's practically impossible to design for every possible zoom level & font size, so artefacts are accepted.

If you want to make sure the code complies to both the line length and tab size, you should probably use spaces, and not "real" tabs. Personally, I feel these guidelines are exactly that: guidelines; and not strict rules, so I don't think it matters that much. Other people's opinion differs on this matter, though.

Personally, I like tabs, for a number of reasons which are not important now, but only deviate from the standard temporarily, usually when editing heavy-nested code where I would like some more clarity which block I'm editing.


You probably don't write very long lines often, so you probably won't have to reformat your code too often.

Therefore, you can use an approach where you periodically check your file for compliance, and fix anything that's out of whack (for example, you could do this before submitting the file to code-review).

Here's a trivially simple Python script that takes a file on stdin and tells you which lines exceed 80 chars:

TABSIZE = 8
MAXLENGTH = 80
for i, line in enumerate(sys.stdin):
    if len(line.rstrip('n').replace('t', ' '*TABSIZE)) > MAXLENGTH:
        print "%d: line exceeds %d characters" % (i+1, MAXLENGTH)

You can extend this in various ways: have it take a filename/directory and automatically scan everything in it, make the tab size or length configurable, etc.


Some editors can be set up to insert a number of spaces equal to your tab length instead of an actual tab (Visual Studio has this enabled by default). This allows you to use the tab key to indent while writing code, but also guarantees that the code will look the same when someone else reads it, regardless of what their tab length may be.

Things get a little trickier if the reader also needs to edit the code, as is the case in pretty much every development team. Teams will usually have tabs vs. spaces and tab length as part of their coding standard. Each developer is responsible for making their own commits align with the standard, generally by setting up their environment to do this automatically. Changes that don't meet the standard can be reverted, and the developer made to fix them before they get integrated.

链接地址: http://www.djcxy.com/p/88852.html

上一篇: 用CSS按比例调整图像大小?

下一篇: 使用制表符而不是空格时保持最大行长度?