HackerOne users: Testing against this community violates our program's Terms of Service and will result in your bounty being denied.

Is there a coding standard for arrays?

R_JR_J Ex-FanboyMunich Admin

I've taken a look at the docs and I know there is no official coding standard, but until I only saw array(...) in Vanillas core files. Would anybody care if I would write [1, 2, 3] in a pull request?

Has there already been a discussion like that?

Comments

  • LincLinc Detroit Admin

    We typically use the newer array syntax now. It's not really a rule / standard (i.e. we're not going back and "fixing" every array), but we'd actually prefer you wrote it the new way, so go for it.

  • hgtonighthgtonight ∞ · New Moderator

    The new syntax is soooo nice.

    I can't wait for 2.3 as it requires PHP 5.4 (which introduced the new syntax).

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • R_JR_J Ex-Fanboy Munich Admin

    @hgtonight said:
    The new syntax is soooo nice.

    I can't wait for 2.3 as it requires PHP 5.4 (which introduced the new syntax).

    PHP < 5.4 isn't very important nowadays :)

    https://seld.be/notes/php-versions-stats-2016-1-edition

  • hgtonighthgtonight ∞ · New Moderator

    @R_J said:

    @hgtonight said:
    The new syntax is soooo nice.

    I can't wait for 2.3 as it requires PHP 5.4 (which introduced the new syntax).

    PHP < 5.4 isn't very important nowadays :)

    https://seld.be/notes/php-versions-stats-2016-1-edition

    Tell that to my clients that don't want to upgrade until the last possible moment. :anguished:

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • LincLinc Detroit Admin

    Great resource. I hadn't seen that, thank you. Stuff like this is extremely helpful for resolving the debate over when we move our version requirements forward. We like to be progressive (we'd totally jump to 5.6 yesterday if it was feasible), but we also don't wanna leave a majority of our install base in the cold either.

  • LincLinc Detroit Admin

    The two positions are always:

    1) Our software should just work and never break for folks, even if they don't understand upgrading PHP. This is the WordPress worldview.

    2) We want to have happy developers using the best tech. People will never upgrade their servers if the software they rely on doesn't force them.

    I think we are closer to 2 than 1, but we try to keep a healthy balance and not move too quickly.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    @Linc
    So are you saying that for the sake of compatibility this will work for now array('something', 'something else') but must now be ['something', 'somthingelse'] for all arrays including array('class'=>'my class') ?

  • hgtonighthgtonight ∞ · New Moderator

    @vrijvlinder said:
    @Linc
    So are you saying that for the sake of compatibility this will work for now array('something', 'something else') but must now be ['something', 'somthingelse'] for all arrays including `array('class'=>'my class') @Linc

    No. If you are running PHP 5.3 and encounter the short array syntax ([1, 2, 3]), it won't work. 5.4+ will parser array(1, 2, 3) as well as the new short syntax.

    Search first

    Check out the Documentation! We are always looking for new content and pull requests.

    Click on insightful, awesome, and funny reactions to thank community volunteers for their valuable posts.

  • vrijvlindervrijvlinder Papillon-Sauvage MVP

    I actually encountered that on a test installation I have, I could not figure out why so I compared two plugins and saw the short array, which I thought was weird because I was accustomed to the other, so on a guess I changed the short ones to the other and it worked.

    I guess I am still running php 5.3 , thanks for clearing that up for me. <3

  • R_JR_J Ex-Fanboy Munich Admin

    @Linc said:

    Great resource. I hadn't seen that, thank you. Stuff like this is extremely helpful for resolving the debate over when we move our version requirements forward. We like to be progressive (we'd totally jump to 5.6 yesterday if it was feasible), but we also don't wanna leave a majority of our install base in the cold either.

    If you use that for a discussion, than only present the graphics without talking to much about the base for this ;) The statistics have been made of the last months composer installs and I would assume that most of those were not done for production but for development.


    I was indifferent about the notation and found the short form useful when an array was in one line, but found it more readable when an array span over multiple lines like that:

    // Short form in a one liner
    $this->Form->button('Cancel', ['type' => 'button', 'class' => 'Button Close']);
    
    // Long form for multi line
    $configurationModule->initialize(
        array(
            'Aah.MarxBrother' => array(
                'Control' => 'CheckBoxList',
                'LabelCode' => 'Marx Brothers',
                'Items' => array('Groucho', 'Chico', 'Harpo')
            ),
        )
    );
    

    What convinced me to always use the short form was that the traditional array() notation is syntactically like the notation of a function which sounds like it has been a compromise or simply a bad idea when it has been developed like that.

  • LincLinc Detroit Admin

    @R_J said:
    If you use that for a discussion, than only present the graphics without talking to much about the base for this ;) The statistics have been made of the last months composer installs and I would assume that most of those were not done for production but for development.

    I read the background, but disagree on the conclusions. We run composer commands from production. And the actual numbers are less important than the delta per time. You'll also notice that he paints a wildly different picture than WordPress: https://wordpress.org/about/stats/ I'd rather be in a position of being able to split the difference between two very different resources than only having 1 extreme to look at.

Sign In or Register to comment.