Question on proper Array syntax

2,366 views
Skip to first unread message

John Mertic

unread,
Mar 27, 2013, 5:16:41 PM3/27/13
to php-f...@googlegroups.com
Hi everyone,

We are working on implimenting PSR-2 here at SugarCRM, and the question came up around array syntax. I don't see it explictly defined in the PSR-2 spec, but I'm assuming the convention to follow is similar to method and function calls...


In other words, can be one line or multiple, in the latter case indenting each item. Is that correct?

Thanks!

John Mertic

Andrew Eddie

unread,
Mar 27, 2013, 11:41:17 PM3/27/13
to php-f...@googlegroups.com
There's no correct answer, beacuse the PSR is silent on the matter, but I think your assumption is reasonable. It's certainly my practice.

Regards,
Andrew Eddie 

Mike van Riel

unread,
Mar 28, 2013, 2:01:53 AM3/28/13
to php-f...@googlegroups.com
As Andrew said; the PSR does not state what is to be used. 

My convention is to use a one-line variant if it fits within 80 to 120 characters width:

$a = array('a', 'b');

or if I'd want a larger array (or something that I can more easily expand on in the future) I use a multiline array where the closing parenthesis is on a newline and each element is on its own line. An interesting addition could be to have a comma following every element (even the last) as that makes adding a little easier:

$a = array( 
    'a', 
    'b', 
);

Andreas Möller

unread,
Mar 28, 2013, 2:09:55 AM3/28/13
to Mike van Riel, php-f...@googlegroups.com

$a = array( 
    'a', 
    'b', 
);

Initializing arrays like above will increase the meaningfulness of diffs when adding/removing elements. That's why I'm doing it this way and ask every developer on my team to do it as well. 


Best regards,

Andreas

Andreas Möller

unread,
Mar 28, 2013, 2:13:24 AM3/28/13
to John Mertic, php-f...@googlegroups.com

We are working on implimenting PSR-2 here at SugarCRM, and the question came up around array syntax. I don't see it explictly defined in the PSR-2 spec, but I'm assuming the convention to follow is similar to method and function calls...

By the way, there's an interesting RFC concerning trailing commas in function argument lists that I stumbled upon by following Phil Sturgeon on Twitter, I believe:



Best regards,

Andreas

Hari K T

unread,
Mar 28, 2013, 7:40:57 AM3/28/13
to php-f...@googlegroups.com
Yes you are right and I also prefer Mike van Riel answer


$a = array( 
    'a', 
    'b', 
);

By the way Andreas thanks for pointing to the RFC . But looking at it, we can see it failed with the votes.

Steven Parsley

unread,
Mar 28, 2013, 11:41:59 AM3/28/13
to php-f...@googlegroups.com
What are the thoughts on aligning the key/value pairs of an array? For example:

array (
    'long_variable' => '1',
    'short'               => '2,'
)

opposed to:

array (
    'long_variable' => '1',
    'short' => '2,'
)

Andreas Möller

unread,
Mar 28, 2013, 11:44:35 AM3/28/13
to Steven Parsley, php-f...@googlegroups.com
My opinion is the following: it's a waste of time and not worth the effort (even though you may argue that you are using a smart IDE, such as PhpStorm, which would do it for you). If you change names, you have to re-align again and again. Don't do it.

Especially painful: if you're inclined to use tabs over spaces (*ducks*), you are mixing tabs and spaces for indenting / alignment , while you shouldn't.


Best regards,

Andreas

Mike van Riel

unread,
Mar 28, 2013, 12:05:58 PM3/28/13
to Andreas Möller, Steven Parsley, php-f...@googlegroups.com
I personally prefer to align the => operators; I feel it helps me skim through the list and make reading easier and totally worth the effort of coding (write once, read often). Perhaps I'm just a Code Feng Shui type of guy but I want to my code's style to radiate unity, tranquility and peace (wow, that sounded way too new-age. Oh well).



--
You received this message because you are subscribed to a topic in the Google Groups "PHP Framework Interoperability Group - Coding Style" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/php-fig-cs/Go9HA6qhzaU/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to php-fig-cs+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.



Mike van Riel

unread,
Mar 28, 2013, 12:06:40 PM3/28/13
to Andreas Möller, Steven Parsley, php-fig-cs
I personally prefer to align the => operators; I feel it helps me skim through the list and make reading easier and totally worth the effort of coding (write once, read often).
On Thu, Mar 28, 2013 at 4:44 PM, Andreas Möller <a...@localheinz.com> wrote:

Pádraic Brady

unread,
Mar 28, 2013, 2:50:54 PM3/28/13
to Mike van Riel, Andreas Möller, Steven Parsley, php-f...@googlegroups.com
I know Zend Framework aligns the operators. I never do. I find it a
waste of time and the sole benefit is to make it look pretty. I do
testable and secure, pretty not so much ;). In any case, anyone know
if CodeSniffer has a rule concerning array structure in their PSR
checks? If they do, that's the one most likely to be imposed if you
use it in your continuous integration process. I don't use CS myself -
not doing pretty means not spending hours of my life correcting minor
PSR transgressions :P.

Paddy

--
Pádraic Brady

http://blog.astrumfutura.com
http://www.survivethedeepend.com
Zend Framework Community Review Team
Zend Framework PHP-FIG Representative
> You received this message because you are subscribed to the Google Groups
> "PHP Framework Interoperability Group - Coding Style" group.
> To unsubscribe from this group and stop receiving emails from it, send an

Larry Garfield

unread,
Mar 29, 2013, 1:27:05 AM3/29/13
to php-f...@googlegroups.com
Drupal does long-form (multi-line) arrays all the time, especially
associative arrays. It tends to improve code readability considerably.

Generally we don't bother aligning the =>, largely for the reasons
already given. Too much effort, too little gain.

--Larry Garfield

On 03/28/2013 01:50 PM, P�draic Brady wrote:
> I know Zend Framework aligns the operators. I never do. I find it a
> waste of time and the sole benefit is to make it look pretty. I do
> testable and secure, pretty not so much ;). In any case, anyone know
> if CodeSniffer has a rule concerning array structure in their PSR
> checks? If they do, that's the one most likely to be imposed if you
> use it in your continuous integration process. I don't use CS myself -
> not doing pretty means not spending hours of my life correcting minor
> PSR transgressions :P.
>
> Paddy
>
> --
> P�draic Brady

Jason Judge

unread,
Mar 29, 2013, 11:47:52 AM3/29/13
to php-f...@googlegroups.com
I personally don't align operators into columns. It adds something else to have to maintain, and quickly looks messy when multiple people are working on a script. Indenting the blocks and scope gives enough information to be able to scan and locate code easily IMO.

For some people the alignment within a line helps then scan down the lists, but I don't find it helps me do that at all; the "ragged" column of operators is something my eyes just "see" as a wavy line and finding the line I need is no problem. I've probably just been looking at code for far too many years ;-)

-- Jason

Gary Jones

unread,
Apr 4, 2013, 9:40:06 AM4/4/13
to php-f...@googlegroups.com
Like most replies here, I also prefer multiline arrays with trailing comma - easier to add inline comments about a value, friendlier for diffs, and easier to re-order lines around if necessary.

I also prefer aligning => with spaces - my editor has a keyboard shortcut, so the few seconds it takes to highlight the lines and press the shortcut more than makes up for the many times that code will be read by future me and others.

Greg Levine

unread,
May 16, 2013, 11:59:36 AM5/16/13
to php-f...@googlegroups.com
I also prefer aligning => with spaces. Devs might complain about the effort of maintaining a long list, but you aren't formatting code for yourself. You're formatting it for others. If you start a new job and look at an array that is poorly formatted, it will take you extra time to digest it. But if that one author took the extra time to align everything then they have just saved you AND many others (the problem gets compounded when more than one person is involved) time and effort when working with their code. It's just a matter of decency and having sympathy for those who follow you.

Zachary King

unread,
May 16, 2013, 12:02:51 PM5/16/13
to Greg Levine, php-f...@googlegroups.com
Besides this is something that code editors _should_ be able to do, or atleast have the ability to write a plugin that will do it.

Personally I use Sublime Text, and there are several plugins that will do this fairly well. Though sometimes they need just a little tweaking if the list is oddly formatted.


--

Paul Jones

unread,
May 16, 2013, 12:10:31 PM5/16/13
to Greg Levine, php-f...@googlegroups.com

On May 16, 2013, at 10:59 AM, Greg Levine wrote:

> I also prefer aligning => with spaces. Devs might complain about the effort of maintaining a long list, but you aren't formatting code for yourself. You're formatting it for others. If you start a new job and look at an array that is poorly formatted, it will take you extra time to digest it. But if that one author took the extra time to align everything then they have just saved you AND many others (the problem gets compounded when more than one person is involved) time and effort when working with their code. It's just a matter of decency and having sympathy for those who follow you.

Preach it, brother, preach the word.



-- pmj

Alexandru Bucur

unread,
Aug 16, 2013, 1:27:35 AM8/16/13
to php-f...@googlegroups.com, Greg Levine
Just my 2c but if you have a bigger array with 5-10 manually defined values and you align by => when you add a new entry that's bigger than the rest (so it requires moving the => for all) it makes it really ugly in a diff.
Reply all
Reply to author
Forward
0 new messages