Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between using tab and space when we do source code formatting?

Tags:

When we want to give some formatting to code to make it more readable. We can use both Tab and Space (by pressing space bar on keyboard). Many time I give space manually.

I've some confusions

  • Is there any difference between them?
  • Is using tab better than spaces?
  • Does using space increase the size of document and tab not?
  • Is there any differences between single tab/space and multiple. in terms of page size?
  • Is there any difference on using tab or space in HTML, CSS and Javascript?

Dreamweaver has this option in preferences

enter image description here

like image 637
Jitendra Vyas Avatar asked Oct 11 '11 15:10

Jitendra Vyas


People also ask

Should I use tab or space in code?

The analysis performed by the team at Stack Overflow found that programmers who use spaces instead of tabs are making more money. David Robinson, the data scientist who performed this study found that programmers using space over tabs made an average of 9 percent more each year than their tab using counterparts.

Is tab and space same?

Tabs need less characters and are formatted by users (some display as 2, some as 4 spaces), what can make some code look bad on some other environment. Spaces consume more KBs but look equal everywhere.

Why would you use spaces over tabs?

If a couple of people work on same file it is highly possible to generate unnecessary conflicts. Using spaces instead of tabs makes it possible to easily catch such an accidental space on eyeball and this is probably a reason, why using them become a standard.

Should I use tabs or spaces in Python?

Tabs or Spaces? Spaces are the preferred indentation method. Tabs should be used solely to remain consistent with code that is already indented with tabs. Python disallows mixing tabs and spaces for indentation.


3 Answers

Tabs need less characters and are formatted by users (some display as 2, some as 4 spaces), what can make some code look bad on some other environment.

Spaces consume more KBs but look equal everywhere.

Good editors have retab-functions to convert those.

In JS and CSS it does not matter, HTML should not matter, but can in some cases.

like image 133
campino2k Avatar answered Oct 09 '22 20:10

campino2k


Using tabs is annoying because some editors interpret a tab as 4 spaces, some others as 8 spaces, and some others as 2 spaces, which makes the indentation completely wrong if tabs and spaces are used in the same file.

I always use spaces only to avoid this problem.

It could slightly increase the download size of your pages, but you could also minify the JavaScript and the CSS files, and/or use gzip on-the-fly compression to mitigate this small problem.

like image 34
JB Nizet Avatar answered Oct 09 '22 18:10

JB Nizet


Using Tabs :

  • Tab takes less space
  • Tab is user system defined :: So in my case if i prefer 2space::tab i can view it that way
  • Moving one indentation level back is lot easier if you use tabs .

Using Spaces :

  • Tab space ratio usually defaults to 1:8 . So on all 'newbie' systems your code will be difficult to read . Also if you view your code on github / pastebin there again it will be some what awkward .

My take : Go with tabs for development , find replace '\t' with ' ' [4 spaces] and then for release minify [ this strips tabs and spaces ] .

like image 27
Anil Shanbhag Avatar answered Oct 09 '22 18:10

Anil Shanbhag