views:

430

answers:

7

This is an subjective question, I need your feels and thoughts about coding standards, and formatting practices.

PHP Zend coding standard requires to write multiline function calls like this:

$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
);

I think the following approach is more readable:

$returnedValue = $object->longMethodName($argument1,
                                         $otherArgument,
                                         42);

Becouse the there is only one line on the left side, this indicates this is only one statement, and the arguments are closer to the method name.

Which one do YOU prefer?

+9  A: 

I like the first approach better. The latter requires more typing, and is more straining on the eye IMO. I think the eye - for the "western", left-to-right reading part of mankind at least - tends to jump to the beginning of the next line when arriving at the end of the current one, and there is too much white space to jump over in the second example. It may not be semantically 100% correct but provides a good reading flow.

Pekka
I expected your answer Pekka ;) Ok, I take these arguments into account.
erenon
+1  A: 

Neither? Option A is potentially confusing because single-indent is used for blocks of code. Option B is problematic with long argument names and/or deeply indented code.

I prefer double-indenting for continued argument lists.

Example, per erenon's request:

$returnedValue = $object->longMethodName(
        $arg1, longInlinedMethod($argFoo, $argBar) + otherLongInlinedMethod(),
        $arg2, $arg3);
while ($val) {
    someStatement();
}
sidereal
Please provide an example.
erenon
I don't like this.
Laykes
To be honest this is where I would go too. I find it a little hard to skim if the block indent is the same as parameter indent.
Keith Humm
That's the main justification. Also, I don't like the excessive whitespace resulting from putting end-paren on its own line as well as requiring a separate line for each parameter. You don't need to enjoy perl levels of code density to want to be able to see more than one or two function on a screen at once.
sidereal
+8  A: 

The second approach leaves you with one additional Problem: Line length. The Zend Coding Standard suggest that "The maximum length of any line of PHP code is 120 characters."

This means if you want good (long, descriptive) Variables names and you happen to have one for the return value, the object, a good named function and a long parameter you are much more likely to hit that 120 chars limit.

Adding to that and depending on your standard the max length may be 80 Chars or something in between 120.

Additionally i like the first one better if used repteadly

$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
);
$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
);
$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
);
$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
);

$returnedValue = $object->longMethodName($argument1,
                                         $otherArgument,
                                         42);
$returnedValue = $object->longMethodName($argument1,
                                         $otherArgument,
                                         42);
$returnedValue = $object->longMethodName($argument1,
                                         $otherArgument,
                                         42);
$returnedValue = $object->longMethodName($argument1,
                                         $otherArgument,
                                         42);

Like Pekka said, less eye jumping

edorian
Thanks for pointing the repetitive usage out.
erenon
Yeah, it makes it really obvious which one looks better.
Pekka
+2  A: 

Of the two you provided, I prefer the first.

If there is a coding standard in place, I will follow it. However, at my job there isn't, and I prefer the following:

$returnedValue = $object->longMethodName(
                                $argument1,
                                $otherArgument,
                                42
                            );

To me, it's easier to see immediately that there's a variable assignment being done (because of the way that parameters and closing parenthesis are indented. With the Zend standard, you have to actually look for the equals sign to see that it's an assignment, else it could be confused with a plain multiline function call.

One more comment... my function calls only become multiline if they will exceed 120 characters anything more than 120 characters will not be visible in my IDE at 1600x1200 resolution with the Workspace browser and Code Navigator panes open.

This line of code is only 74 chars, so I would have done this:

class myClass
{
    public function myFunction(...)
    {
        $returnedValue = $object->longMethodName($argument1, $otherArgument, 42);
    }
}
Mike
Zend cs (or at least my phpcs/Zend verifier) allows only 80 characters in a single line.
erenon
80 chars is the *recommended* maximum length. 120 is the actual maximum (@see http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html#coding-standard.php-file-formatting.general).It will be hard to stick to 80 if your code commonly contains stuff like $descriptivelyNamedVariable = $descriptivelyNamedObject->descriptivelyNamedMethod($descriptivelyNamedParameter);
Mike
+2  A: 

I like PEAR's standard and it advocates the first of your examples

$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
);

but I might instead have done this for such a short set of parameters:

$returnedValue = $object->longMethodName(
    $argument1, $otherArgument, 42
);

EDIT: oh! and for sidereal's example:

$notInlined = longInlinedMethod($argFoo, $argBar) + otherLongInlinedMethod();
$returnedValue = $object->longMethodName(
    $arg1, $notInlined, $arg3, $arg4
);
while ($val) {
    someStatement();
}
jah
+1  A: 

I typically go with the First, but with the closing bracket on the same line or at least same indent as the above.

$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42
    );

$returnedValue = $object->longMethodName(
    $argument1,
    $otherArgument,
    42);

This seems to avoid level confusion when nesting, for me.

However: my indenter in vim started doing the same-level-as-paren (2, above) and I like it. I break with it on long lines that need wrapping help, but in general, I think it leads to readable code if you're primarily scanning for method names instead of parameters.

BTW, doing that sort of indenting for nested boolean statements and such works well too.

I'll also group arguments on one line if they're not too long and they sort of make sense to group.

Justin
+1  A: 

I prefer the first, for two reasons:

  1. It allows you to use the Tab key for indentation (or hotkeys for indent/unindent), with no worry about adding additional spaces to make your arguments line up.
  2. More importantly, you don't have to modify your argument indentation if the length of your variable, object, or method name changes in the first line.
Ed Lucas