It has come to my attention that we do not currently encourage (or require) spacing around operators. For readability, we should.
$foo = $bar; rather than $foo=$bar;
if ($foo == $bar) { rather than if ($foo==$bar) {
$foo = $bar - $baz; rather than $foo = $bar-$baz;
Additional spacing is okay (I don't encourage it, but it is common to line up "=".)
Comments
Comment #1
jonathan1055 commentedA space each side of all operators =, !=, ==, + etc is a good idea and seems to be common practice in core and many contribs, but I could not actually find it mentioned in the coding standards pages (but please correct me if it is there somewhere).
http://drupal.org/project/coder module does not check for these (as of version 6.x-2.x dev 31 May) but /scripts/code-style.pl does partly. If there is no space on either side of an operator then this is picked up by code-style.pl, and a suggestion is given to add a space on each side. But if there is a space on just one side then this passes through without a suggestion to add one on the other side.
I guess the first step is to get the standard agreed and in the documentation pages, then Coder and code-style.pl could be updated accordingly.
Comment #2
jerdiggity commentedNot sure if it
matterscounts or not, but on page 479 of Pro Drupal Development, Second Edition, it says:It also gives this as an example:
$var=foo ($bar,$baz)$var = foo($bar, $baz)If that falls under the Does-Not-Apply category, please let me know because I still (and likely always will) consider myself a :)
Comment #3
leehunter commentedIsn't this covered here: http://drupal.org/coding-standards?
Does it need to be made more explicit?
Comment #4
nancydruThat is the issue, Lee; we don't see this in the coding standards, nor is Coder checking it.
Comment #5
jhodgdonIt's in the coding standards doc now. http://drupal.org/coding-standards#operators
So, I'll change the Project of this to Coder, so that the Coder maintainers can decide whether to add it to Coder, if it isn't there currently. I wasn't sure which version to assign it to -- maybe someone could update that?
Comment #6
nancydruI would include the conditional joiners too (such as "&&" and "||" in:
if ($a == $b && $c > $d || $e < $f) ...).Comment #7
jhodgdonDefinitely -- || and && are operators, so they should have spaces around them.
If you think more examples need to be added to the Coding Standards section to make this clear, feel free to add them. The list I put in there wasn't meant to be comprehensive; my thought being that any PHP programmer would know what "operator" means. But a few more examples cannot hurt the discussion.
Comment #8
psynaptic commentedHow about
forloops?for ($i = 0; $i < 10; $i++) {...I much prefer the above as it is more readable but others prefer:
for ($i=0; $i<10; $i++) {...I can personally only think of one exception, and that is the increment/decrement operators.
Any others you can think of?
Even the concatenation operator has spaces either side now.
Comment #9
nancydruIMHO, if the operator has two operands (+, <, &&, and even .), then it should be spaced. If it only has one operand ('unary', ++, --) then it should not. That's a pretty simple, easy to remember rule.
Comment #10
jhodgdonI clarified the Operators section of http://drupal.org/coding-standards to say no space on unary operators (it already says we need a space around binary operators). Coder module checks for this are still needed, probably.
Comment #11
psynaptic commentedI'd certainly say so. Running coder should highlight issues like this. Otherwise we're effectively telling people their code is fine, when it is not!
Comment #12
jonathan1055 commentedHas this issue now been covered by #246568: Coding standards of string concatenation have changed ?
Comment #13
john morahan commentedHere are the rules I use, they could form the basis of a patch if anyone is interested...
Comment #14
klausiCoder for Drupal 6 is now frozen and only security fixes will be applied. Feel free to update this issue and reopen against 7.x-2.x or 8.x-2.x.