Maintaining Maximum Line Length When Using Tabs Instead of Spaces?

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.