• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spaces vs. tabs in source files

#1
There is a PHP Style Guide, but I was not find there anything about spaces vs. tabs. I think tabs are recommended, but with what width (4/8 spaces equivalent)? Just look at system messages translation project and you will see, that everyone is using his/her own conventions. So what is the oficially recommended way?
Reply

#2
PSR says spaces not tabs: http://www.php-fig.org/psr/psr-2/
Reply

#3
(03-27-2015, 05:31 AM)ircdirk Wrote: PSR says spaces not tabs: http://www.php-fig.org/psr/psr-2/

Spaces have two major disadvantages:
  1. Bigger file size
  2. Horrible editing

On PHP Coding Standards I found proposition for 4 spaced tabs.
Reply

#4
(03-27-2015, 04:15 AM)Muzikant Wrote: There is a PHP Style Guide, but I was not find there anything about spaces vs. tabs. I think tabs are recommended, but with what width (4/8 spaces equivalent)? Just look at system messages translation project and you will see, that everyone is using his/her own conventions. So what is the oficially recommended way?

It very clearly says to use tabs instead of spaces: http://www.codeigniter.com/userguide3/ge...whitespace

Tab-width is irrelevant, it is your own preference - that's the whole purpose of using tabs instead of a number of spaces.

(03-27-2015, 05:31 AM)ircdirk Wrote: PSR says spaces not tabs: http://www.php-fig.org/psr/psr-2/

CodeIgniter doesn't conform to PSR-2 and if it is all up to me - it will never do. PSR-2 is just another styleguide, not a law.
Reply

#5
The CodeIgniter Style Guide states that you should use tabs. The only significant benefit to tabs is that you can define the space width of the tabs and someone else using a different space width for their tabs won't have to deal with your preference, so use whatever works for you.

Of course, that only really matters if you are contributing code to the CodeIgniter repository.

Personally, I follow PSR-1/2 as best I can (given that I'm using CodeIgniter). I initially didn't care for the idea of using spaces, but once I actually switched my editor (more or less back to its original configuration, since most editors seem to default to using spaces), I found that the experience of editing files is not significantly different unless I am over-reliant on my arrow keys to navigate through whitespace in my files.

The secret really is to learn or configure the navigation commands in your editor of choice. In most GUI environments (at least) the Home/End keys and various combinations of the modifier keys (ctrl/alt/shift) and the arrow keys can overcome any serious benefits to using tabs or spaces. I also use a couple of different packages in SublimeText 3 to handle different types of formatting changes, so it's fairly easy to align text, switch between tabs/spaces or *nix/Windows line endings (or even have it attempt to completely change any number of other things mandated by any number of code style guidelines).

If file size is a real concern (somehow having 4 spaces in place of each tab is going to end the days of luxury space on the hard drives of development machines and servers), there are many better ways to conserve space than choosing tabs and *nix line endings (though I use the latter myself). Compression algorithms have handled consecutive characters and whitespace in text files quite well for most of the history of computers, and many file systems offer some capability for compression which is (usually) transparent to most of the applications which will edit your code, though this would not be ideal for servers (you could always add a build step to minify your PHP code before transfer to the server, the way you might for JavaScript and CSS). File size for PHP code is rarely going to matter for the runtime environment.
Reply

#6
I'm very close to releasing a CodeIgniter 3 standard for code sniffer, watch this space.
Reply

#7
(03-27-2015, 06:57 AM)Narf Wrote: It very clearly says to use tabs instead of spaces: http://www.codeigniter.com/userguide3/ge...whitespace

Tab-width is irrelevant, it is your own preference - that's the whole purpose of using tabs instead of a number of spaces.

Yes, but... Look at the system/language/english/ftp_lang.php, which is one of the core files. Aligning is made by tabs with 8 spaces equivalent. If you will have setted 4 spaces equivalent, it will brake the aligning. That is why I started this topic. Now look at the czech translation or the polish translation of the same file. Spaces are used. So there is an inconsistency. If there is no guidelines for cases like this, I have no problem with it. I was just curious. :-)
Reply

#8
(03-30-2015, 12:44 AM)Muzikant Wrote:
(03-27-2015, 06:57 AM)Narf Wrote: It very clearly says to use tabs instead of spaces: http://www.codeigniter.com/userguide3/ge...whitespace

Tab-width is irrelevant, it is your own preference - that's the whole purpose of using tabs instead of a number of spaces.

Yes, but... Look at the system/language/english/ftp_lang.php, which is one of the core files. Aligning is made by tabs with 8 spaces equivalent. If you will have setted 4 spaces equivalent, it will brake the aligning. That is why I started this topic. Now look at the czech translation or the polish translation of the same file. Spaces are used. So there is an inconsistency. If there is no guidelines for cases like this, I have no problem with it. I was just curious. :-)

Alignment != indentation

Yes, in the past different people have aligned using tabs (and with their own tab-width), but we've never had an official policy about that and it shouldn't be done anymore - it's wrong. At present, I'm rejecting any changes that do alignment with tabs and prefer no alignment at all.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.