Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

up vote 4 down vote accepted

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 :)

share|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
    
+1 for the "where I left my keys" observation - nice. –  Gary Rowe Nov 30 '10 at 16:55
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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