2

I'm talking about something like this:

echo $form->input('general_addresss', array(
            'label' => 'Where will you go today?'
            'format' => array('before', 'input', 'after', 'label', 'after', 'error')
            ));

Do you start with one array parameter, then break a line? If it can't fit on a line, do you immediately break a line?

After of which, do you do a set number of tabs over? What happens if an array within an array has lots of properties?

Is there any particular guide you follow?

6

I do it like this:

echo $form->input
    (
        'general_addresss',
        array
        (
            'label' => 'Where will you go today?'
            'format' => array
            (
                'before',
                'input',
                'after',
                'label',
                'after',
                'error'
            )
        )
    );

It may seem like a massive overkill of whitespace, but I find it quite readable and obvious - none of my brackets are hidden and everything is nicely indented visually. YMMV ;)

Edit: Regarding the "particular guide", my particular guide are my eyes. If I can't quickly see where I left my keys in the source code, it's probably badly formatted.

p.s. You must be coming from CakePHP :)

| improve this answer | |
  • haha yes, I am using cakephp :] that's an interesting approach. I've never thought of doing it like that. That very much reminds me of .net programming in visual studio – joslinm Nov 30 '10 at 16:31
4

I would have done it like this:

echo $form->input(
    'general_addresss', array(
       'label' => 'Where will you go today?',
       'format' => array(
           'before', 
           'input', 
           'after',
           'label',
           'after',
           'error'
       )
    )
);

Syntax highlighting can be a very easy thing to confuse :) However, in your case, the example you gave is also perfectly fine. It would depend on how many nested arrays (with xx number of members) I was dealing with.

Whatever makes it easy to read and modify. I personally find one member per line much easier to enumerate (mentally) while looking to make a change, while clearly separating each member.

Of course, follow what exists, when applicable.

| improve this answer | |
0

I would generally do something like this:

array('before', 
      'input', 
      'after', 
      'label', 
      'after', 
      'error')

Although I don't know of any particular standard that spells out what "should" be done.

| improve this answer | |
0

Beside something along the lines of Jason's answer, I would create the array before the function call if it is complicated. Where complicated refers either to the array itself or the function call using that array. So, if the function call starts to look like crap, as in your example, just create a variable for the array and use that in the call.

Don't optimize prematurely. It's one variable. Error on the side of legibility and optimize to unmaintainable mess only if necessary.

| improve this answer | |

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.