Please don't indent code with tabs

Please don't indent code with tabs

by Tim Hunt -
Number of replies: 10
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
The Moodle coding guidelines say:

Indenting should be consistently 4 spaces. Don't use tabs AT ALL.

However, just over 360 of the files .html and .php files in the source tree have tabs in them, so is this a lost cause, or should someone like MartinD do a global replace tab -> 4 spaces and check it in?

Of course, no one here is checking in code with tabs, are we!?!


(I was writing a script that did things like php -l (syntax check) on every source file in the tree, and checked for trailing whitespace and other easy to check things relating to code quality that I could use to police the code in the OU source repository. However, I have just commented out the tabs in files test because it was finding too many bad files to be useful.)
Average of ratings: -
In reply to Tim Hunt

Re: Please don't indent code with tabs

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Is there a GOOD reason for this rule of NO TABS ? I find tab indenting very convenient when programing and the "4 spaces indenting" extremely tedious...
Since Tim has found 360 files with tabs in them (and apparently working well), I'm wondering about the reason for the "rule".
Joseph
In reply to Joseph Rézeau

Re: Please don't indent code with tabs

by Mark Roggenkamp -
Yeah, tabs can be different lengths. 4 spaces is specific.

Tell your editor to expand/convert tabs to 4 spaces and you'll have the same experience you have now but with the consistency of 4 spaces.

In vim this should help:

set tabstop=4|set shiftwidth=4|set expandtab|set softtabstop=4|set showmatch

Mark


In reply to Mark Roggenkamp

Re: Please don't indent code with tabs

by Joseph Rézeau -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers Picture of Translators
Hi Mark (and others),
OK, I understand. I'm using Macromedia Dreamweaver version 8 and I've just discovered a "hidden feature" which will automatically convert tabs to 4 spaces and 4 spaces back to tabs.smile It's explained on this site.
Unfortunately, in the French version of DW I am using, the menu lines for tab to spaces and spaces to tabs have been inverted.sad That's OK, I "customized" my DW translation and now everything is OK.cool I'll just have to remember to do the conversion when I create or edit some PHP files for Moodle...
Thanks,
Joseph
In reply to Mark Roggenkamp

Re: Please don't indent code with tabs

by Cory B -
Isn't that why people like to use tabs instead of spaces? Some developers like 2 spaces and some like 4. If you use tabs you can tell your editor to view it however you like and it won't affect other developers.
In reply to Cory B

Re: Please don't indent code with tabs

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
In theory, yes, but in practice you end up with a mish mash of formatting, comments out of whack etc. I know this from experience.

We are sticking to using spaces in Moodle. Please replace tabs whenever you find them.
In reply to Martin Dougiamas

Re: Please don't indent code with tabs

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers
Please replace tabs whenever you find them.

I hope you meant that. I've just eliminated the tabs from about 25 files which only had a few odd ones.

And only then did I think about what a pain it would be to merge that from the branch to the trunk! (but it is merged now).
In reply to Cory B

Re: Please don't indent code with tabs

by sam marshall -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers
Yes, and using tabs does work much better - provided all developers are using editors that are correctly configured to convert spaces to tabs on every save. (Or that the source management system is set up with a pre-commit hook that rejects lines with leading spaces, so as to enforce this - something I'd recommend if configuring a project this way, but which may be difficult to set up if you do not control the source management system's server.)

So while I would definitely have made the other choice, I don't particularly disagree with Martin's in this case as it is a little less likely to cause invisible problems, and a little less dependent on being able to configure a source control server. (Although it would be an obvious benefit to add a pre-commit hook for Moodle's coding style, such as rejecting any .php that includes a tab character.)

Anyway, the important (ish) thing is that people stick to the determined standard, not what it is. smile

--sam
Average of ratings: Useful (1)
In reply to sam marshall

Re: Please don't indent code with tabs

by Cory B -
"Anyway, the important (ish) thing is that people stick to the determined standard, not what it is."

Agreed. I'm not submitting code right now, but if I do I'll be sure to indent with spaces. smile
In reply to Joseph Rézeau

Re: Please don't indent code with tabs

by Martin Dougiamas -
Picture of Core developers Picture of Documentation writers Picture of Moodle HQ Picture of Particularly helpful Moodlers Picture of Plugin developers Picture of Testers
It's for consistency in patches and diffs and so on.

Just set up your editor correctly and it's not an issue, you get the same convenience.
In reply to Tim Hunt

Re: Please don't indent code with tabs

by Martín Langhoff -

Strange! Around 1.4 and 1.5 release timeframes it was quite clean! Grumble...

BTW, as I don't have Tim's scripts I check for tabs doing:

perl -e 'print "\t"' > ~/.sampletab
grep -f ~/.sampletab -rl moodledir

I often do something like this for syntax checks, though AdoDB fails them for some reason:

(find . -name '*.php' -type f -print0 | xargs -0 -n1 php -l ) | grep -v 'No error'