Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 596
its_me Avatar asked Dec 06 '14 02:12

its_me


People also ask

Why would you use spaces instead of tabs?

Tabs Debate. Pro-spaces programmers argue that using the space bar makes the layout more flexible. However, pro-tabs users rebut saying tabs makes code more readable and aesthetically pleasing.

Should a tab be 2 or 4 spaces?

1. Indentation: tabs vs spaces. Java: 4 spaces, tabs must be set at 8 spaces. Both are acceptable.

Is tab always 4 spaces?

Sets the distance in spaces between tab stops. The default is four spaces.

Are tabs or spaces the preferred method for indentation?

Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python 3 disallows mixing the use of tabs and spaces for indentation. Python 2 code indented with a mixture of tabs and spaces should be converted to using spaces exclusively.


2 Answers

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.

like image 80
Martin Tournoij Avatar answered Oct 19 '22 11:10

Martin Tournoij


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.

like image 41
nneonneo Avatar answered Oct 19 '22 10:10

nneonneo