22

The WP core, many WP plugins, and the WP coding standards themselves use a very "generous application" of the Space character (not for indentation, but "inside" of parens and brackets). This seems to be unique to Wordpress - this style/philosophy does not seem to be present in other similar projects, PHP or otherwise.

For more information on this approach, see: https://make.wordpress.org/core/handbook/coding-standards/php/#space-usage

Example: foreach ( (array) $foo as $bar ) { ...

I'm referring to the space after foreach, after the first (, and before the final ) (and other similar spaces shown in "Space Usage" at the link above).

This style seems unnecessary to me - it requires more typing and (opinion)makes parsing code visually more difficult.(/opinion)

My desire is not to debate whether or not this style is a good idea. Rather, I simply want to understand the motives for why this is the recommended style. Even commenters on the WP coding standards are curious:

enter image description here

The answers provided to MK Safi's question are essentially:

  1. For readability
  2. Status quo (aka "That's just the way it is")

My reasoning for asking is that I personally don't see much value in adopting the WP coding standards (regarding "Space Usage") in our internal-only projects. However, I'm curious if I'm missing something.

Are there any reasons beyond the two listed above, ostensibly valid or not, for following Wordpress' "Space Usage" style?

  • 2
    You can do what you like on your internal projects, so long as you're consistent. As a side note, we use tabs rather than spaces, so we arguably require less typing, not that this matters in any way if you have a modern IDE that does all the formatting for you and can reformat to different styles for you ( e.g. sublime with packages, PHPStorm, etc ) – Tom J Nowell Jan 8 '15 at 1:18
  • Thanks for your comment, @TomJNowell! I think maybe I miscommunicated in my "question" - I'm asking less about tabs/spaces for indentation, and more about the rules mentioned under "Space Usage" at make.wordpress.org/core/handbook/coding-standards/php/…. Sorry I wasn't more clear! – rinogo Jan 8 '15 at 3:42
  • 5
    It is easier to read when you don’t have syntax highlighting. That’s at least why I am using that style in internal projects. I have to edit PHP often in a plain console with vi in a minimal configuration. – fuxia Jan 8 '15 at 5:33
  • 2
    FWIW, MediaWiki has a very similar style convention, and is actually pretty strict at enforcing it (at least in core). They even have a script for automatically adding missing spaces. All I can say is that one does get used to it, after a while. – Ilmari Karonen Jan 8 '15 at 10:55
  • 1
    @rinogo I know, comments are sometimes just comments, not answers :) – Tom J Nowell Jan 8 '15 at 12:31
13

Resoning

Regarding "white space" (no matter if tabs or spaces): It's simply a personal preference that stuck with the project.

The WP coding standards imo are a mess and can be ignored - as long as you aren't contributing to core, which is

  • a different story and
  • style guide gets ignored there as well.

"[...] it isn't retroactively applied in bulk on older code as it makes svn/git history very difficult to use. Official policy is that new code should follow the style guide, but if you happen to format adjacent code correctly then so be it, but patches that only format code, or commits that only format code are forbidden."

- @TomJNowell in the comments

Alternatives

You are better off sticking with the PSR standards (namely: 2) or stuff like the Symfony standards (or just your own).

Performance Increase & Tools

There is no profit you gain from having a coding standard (aside from having one to share and the minority that hates it, while the rest dictates it) or from having more or less tabs or spaces. In case you are worried about unnecessary disk space used or maybe slower programs, you can still compress your code (see the GitPHPHooks project) on commit. The benefit you will gain will be around max 5% from the original file space, pretty much equal to what HTML syntax compression/minification gives you. There are Node.js minify tools available via npm for that.

What I personally really found to be helpful is the PHP Linter and the _PHP Mess Detector. I incorporated both into the GitPHPHooks Library so I don't have to think or care about running it.

  • The style guide isn't ignored for Core, but it isn't retroactively applied in bulk on older code as it makes svn/git history very difficult to use. Official policy is that new code should follow the style guide, but if you happen to format adjacent code correctly then so be it, but patches that only format code, or commits that only format code are forbidden – Tom J Nowell Jan 8 '15 at 2:46
  • @TomJNowell And therefore renders the style guide useless :) Anyway, please file an edit and add that to the answer. It's noteworthy info. – kaiser Jan 8 '15 at 2:47
  • I think I wasn't super clear in my question - I'm referring less to tabs vs spaces, and more to "Space Usage" at make.wordpress.org/core/handbook/coding-standards/php/…. I'll edit the question to be clearer. – rinogo Jan 8 '15 at 3:43
  • 1
    @rinogo I got you right the first time, hence the first paragraph. Btw, I consider this more readable as well. – kaiser Jan 8 '15 at 3:45
7

Spaces after dots are normal, such as $baz . '-5', this style is used in many coding standards for operators (y + z).

This is done to improve readability, for example one of these is more readable than the other.

$cow.$dog.$cat.$table.$chocolate.$puddle.$iterator.$stuctureone.$stucturetwo

$cow . $dog . $cat . $table . $chocolate . $puddle . $iterator . $stuctureone . $stucturetwo

This becomes even more obvious when surrounded with other "code".

As for spaces around parenthesis ( 1, 2, 3 ) I have no idea, I guess the argument is for readability as well.

It can be confusing since the WordPress standards themselves have examples with parentheses in comments that do not have spaces and the codebase itself is confusing with some parts having spaces and others not (see screenshot below) even within the same function.

Most PHP standards actually do the opposite an call for.. parentheses should hug their contents. In fact most coding standards for other languages write it like: (1, 2, 3) so it's a bit of a mystery why WP does it this way.

Here is an example to compare from a WordPress function.

enter image description here

Larger version to compare : http://i.imgur.com/nTEbV7v.jpg

I prefer the one on the right especially when looking at a full screen of code, but it's a personal preference.

  • thanks for your answer! The . spacing makes sense to me, as . is really just a binary operator, just like + or -. Your thoughts on parentheses "hugging" their contents is exactly why I asked this question. This behavior, along with the even weirder rules like those for square brackets (WP says to use $foo['bar'] and $foo[ $bar ]) are exactly why I asked this question. :) – rinogo Jan 8 '15 at 17:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.