Tabs vs. Spaces

Continuing the discussion from My First PHP OOP's Programme or Project - Calculate: add, subtract or Multiply:

Is the use of tabs or spaces more than personal preference?

I didn’t completely get it. Are you talking about code editor and clean coding thing?
I am using atom.

It’s not really important. When I copied you code I had to mess with the indents (tabs) to get it to format correctly. Strictly an editor setting. Should be an “expand tabs to four spaces” setting in their somewhere.

The tabs vs spaces battle has been going on since the beginning of the Computer Era. Spaces have pretty much won but there are still pockets of tab holdouts.

the tabs vs spaces battle has been going on since the beginning of the Computer Era. Spaces have pretty much won but there are still pockets of tab holdouts.

Not really. Use whichever you prefer. Tabs are unquestionbly technically superior ( http://base.thomashigginbotham.com/tabs-vs-spaces-why-is-there-a-debate/ ) but it really makes no difference.

3 Likes

Source? Full disclosure - I’m a tab guy (who sets the tab amount to be equal to 2 spaces).

1 Like

I could reference the psr-2 coding style but it’s more fun to let threads like this run their course. Do feel a bit guilty for hijacking the discussion.

1 Like

I think at a code level it doesn’t make much difference if it’s binary 00001001 (tab) or 00010100 (space). It’s what’s done with the byte that matters.

For example, If I like four spaces worth of indentation and I want the convenience of clicking the tab button once to not need to click the space bar four times, I can set up my text editor to display tab bytes as four spaces.

If I’m not consistent in using tab and sometimes do four spaces, I won’t see a difference in my text editor. But if someone opens the text file in their editor where it’s set up to display tabs as two spaces, it won’t have consistent indentation and it will look a bit messy.

Spaces don’t have this problem. A space is a space. If I indent two, others will see two. If I indent four, others will see four.

AFAIK, most CSV files are comma separated, but I have seen some that are tab separated. So I wouldn’t want to set my text editor to automatically change all tabs to any number of spaces.

2 Likes

Yeah. It’s based on preference. Though I actually think it also plays into competencies as well. A developer who has very little knowledge in developing at all whether it’s in HTML or in Ruby or in Python, the tabbing is really inconsistent. This is where there’s a grey area for tabs or spaces. I would rather much see a new developer use tabs than spaces.

It’s not much for others, but my eyes bleed when I see something like 2 spaces for a tag. Then out of no where, 3 spaces for something else. Then no space for a line break. Then out of no where again, a tab for no apparent reason. So I do think that indenting correctly makes a huge difference when trying to create readable code. I would never debug someone’s code if I see inconsistent indentations. It just makes a big mess to have to go through the entire spaghetti code to get what’s going on.

1 Like

I really don’t care whether people use tabs or spaces, as long as they are using it consistently throughout their project. So if you start with one file with tabs, then that file should only use tabs (no spaces), and all files should only use tabs. Same goes for spaces, when you use that, everything should use spaces.

In my opinion consistency in code style trumps personal preference any day. I much rather have a code base that is consistent with code styles I don’t like, than a code base that has some parts in code styles I like and some parts that don’t. The reason here is that when a code base is consistent, I don’t have to make a mental switch any time the code base switches from some code style to another, so it’s causing me less useless mental overhead.

Also, if a language has a recommended formatting, like PHP has PSR-1 and PSR-2 (which uses 4 spaces), golang has gofmt (which uses tabs), or the javascript air bnb code style (which uses 2 spaces), etc, I’d rather use that style than force my own style upon the language, so that when other programmers familiar with the language see my code they’ll feel right at home.

5 Likes

I guess I’m a tab holdout (if that’s accurate.) I despise having to hit the space bar four times, and even more having to use the backspace delete key four times, if needed. I consistently use tabs for indenting. Always have, always will.

So, I guess, no - it’s not more than a personal preference. It is precisely a personal preference.

V/r,

^ _ ^

May I suggest you use a “better” IDE or editor then.

1 Like

Tab works just fine.

2 Likes

In my favourite editor I can use any tab-width or number of spaces for the tab key, auto tab or not for new lines. And in the “Highlighters Settings” I can set different tab modes for different document types.

So, I always use the tab key to indent code, shift tab to unindent, lines or block of lines. :slight_smile:

3 Likes

How many spaces does sitepoint use to indent on here? or do they use tabs?

Didn’t know there were indents here, except in posted code of course. :slight_smile:

Where do you see indents in threads and postings or anywhere else here?

I meant in the posted code. </>

What standard do they use?

I actually don’t know. In this page’s html code the indents seems to be an inconsistent amount of spaces.

1 Like

oh, ok, thanks for letting me know that.

That is “Discourse magic”. It’s a markdown syntax thing. Basically it translates to:

When you encounter raw content preceded by 4 spaces, wrap that content in <code> tags in the generated HTML

If you want to see how Discourse indents HTML / CSS / JavaScript you can use you browsers dev tools to look at the formatting.

3 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.