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

jonathan1055’s picture

Project: Coder » Documentation
Version: 6.x-2.x-dev »
Component: Review/Rules » Correction/Clarification

A 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.

jerdiggity’s picture

Not sure if it matters counts or not, but on page 479 of Pro Drupal Development, Second Edition, it says:

... there should be a single space surrounding the operator (=, <, >, etc.)... In a block of related assignments, more space may be inserted between assignment operators if it promotes readability...

It also gives this as an example:

Incorrect
$var=foo ($bar,$baz)
Correct
$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 noob. :)

leehunter’s picture

Isn't this covered here: http://drupal.org/coding-standards?

Does it need to be made more explicit?

nancydru’s picture

That is the issue, Lee; we don't see this in the coding standards, nor is Coder checking it.

jhodgdon’s picture

Project: Documentation » Coder
Version: » 6.x-2.x-dev
Component: Correction/Clarification » Review/Rules

It'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?

nancydru’s picture

I would include the conditional joiners too (such as "&&" and "||" in: if ($a == $b && $c > $d || $e < $f) ...).

jhodgdon’s picture

Definitely -- || 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.

psynaptic’s picture

How about for loops?

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.

$i++
$i--
++$i
--$i

Any others you can think of?

Even the concatenation operator has spaces either side now.

nancydru’s picture

IMHO, 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.

jhodgdon’s picture

I 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.

psynaptic’s picture

Project: Documentation » Coder
Version: » 6.x-2.x-dev
Component: Correction/Clarification » Review/Rules

I'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!

jonathan1055’s picture

john morahan’s picture

Here are the rules I use, they could form the basis of a patch if anyone is interested...

    array(
      '#type' => 'regex',
      '#value' => '(?:\w|\)|\])(?:[<>=!%^&*\/?|]|-(?![->])|\+(?!\+))',
      '#warning' => 'Most operators should have a space before',
      '#severity' => 'minor',
    ),
    array(
      '#type' => 'regex',
      '#value' => '(?:[%^*|<?\/]|(?:(?<!\+)\+|(?<!-)-(?!\w|\())(?!\$)|(?<!-)>|(?<=&)&)(?:\w|\(|\[|\$)',
      '#never' => '^<\?php$', // this only seems to happen with the patch review
      '#warning' => 'Most operators should have a space after',
      '#severity' => 'minor',
    ),
klausi’s picture

Issue summary: View changes
Status: Active » Closed (won't fix)

Coder 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.