Multiline control structure expressions style

187 views
Skip to first unread message

Sherbrow

unread,
Jan 27, 2013, 6:18:58 PM1/27/13
to php...@googlegroups.com
Hello PHP enthusiasts,

I recently was in a situation where I had to tell a novice how to write code that would not look too messy.

I came across an if statement with some complex checking - quite long too - and the Control Structure section of PSR-2 doesn't give specific information on this case.

My question would be, where does the PHP-FIG stand on this matter ? Or on what do they disagree ? I can think of several alternatives :

if ($var1 && $var2 || ($varX % $varY == 1)) {
    // ...

if ($var1 &&
    $var2 ||

    ($varX % $varY == 1)) {
    // ...

if (
    $var1
    && $var2
    || ($varX % $varY == 1)
 ) {
    // ...

These are the combinations of the nicest and ugliest things I could think of right now. The last one seems the nicest to me, if the first one isn't possible.

Thank you for your involvement.

Sebastian Krebs

unread,
Jan 27, 2013, 7:06:12 PM1/27/13
to php...@googlegroups.com
Hi,

Just want to point out, that you consider refactoring, when you face too complex conditions. That something looks messy is not always a question _how_ it is written, but _what_ is written.

My opinion: When it is not too long (let say 120, or sometimes 150 characters), than everything should on one line, else I use the last one. It's easier to overview, when the operator, that connects the single conditions together, is not hidden at different positions at the end of every line.

Regards,
Sebastian


2013/1/28 Sherbrow <sherb...@gmail.com>

--
You received this message because you are subscribed to the Google Groups "PHP Framework Interoperability Group" group.
To post to this group, send email to php...@googlegroups.com.
To unsubscribe from this group, send email to php-fig+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msg/php-fig/-/x0MwotlWrqwJ.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
github.com/KingCrunch

Paul Jones

unread,
Jan 27, 2013, 9:08:35 PM1/27/13
to php...@googlegroups.com

On Jan 27, 2013, at 5:18 PM, Sherbrow wrote:

> I came across an if statement with some complex checking - quite long too - and the Control Structure section of PSR-2 doesn't give specific information on this case.
>
> My question would be, where does the PHP-FIG stand on this matter ? Or on what do they disagree ? I can think of several alternatives :
>
> if ($var1 && $var2 || ($varX % $varY == 1)) {
> // ...
>
> if ($var1 &&
> $var2 ||
> ($varX % $varY == 1)) {
> // ...
>
> if (
> $var1
> && $var2
> || ($varX % $varY == 1)
> ) {
> // ...
>
> These are the combinations of the nicest and ugliest things I could think of right now. The last one seems the nicest to me, if the first one isn't possible.
>
> Thank you for your involvement.

There is no existing PSR that expresses a recommendation on this matter.

It my personal opinion, as I have stated earlier elsewhere, that this is a classic candidate for "extract to explaining variable." Instead of leaving the entire condition set inside the parenthesis, extract it to a variable and then use the variable as the condition.

For example:

$cond = $var1
&& $var2 || ($varX % $varY == 1);

if ($cond) {
// ...
}

Hope that helps.


-- pmj

Patrick Mulvey

unread,
Jan 28, 2013, 12:36:43 AM1/28/13
to php...@googlegroups.com
Shouldn't this conversation be moved to the coding style group?

As for your question, I'm inclined to agree with Paul - looks like a great example of a condition that should be extracted to a variable (or variables). This is by far the most readable solution.

Andreas Möller

unread,
Jan 28, 2013, 1:47:59 AM1/28/13
to php...@googlegroups.com, php...@googlegroups.com
Further to what Paul suggested, have a look at Martin Fowler's catalog of refactorings:


Nonetheless, when it comes to statements like that, I introduce a new line and indent before the operator (similar to string concatenation and usage of the ternary statement). 


Best regards,

Andreas

Sherbrow

unread,
Jan 28, 2013, 2:02:03 PM1/28/13
to php...@googlegroups.com
Thank you for your input.

I will move the topic to the CS mailing list and delete this one in the coming days.

Finally, it seems that I should use one single line if it "fits" and still easy to understand (quite obvious) or introduce variables. The question of line breaks and indentation is still the same with variables, and I'm sure there are legit complex conditions out there - but we'll see that on the other list.

It's too bad the PSR-2 doesn't say anything about that.

Note: the CS mailing list is not easy to find if you come from the github fig-standards project.

Cheers.

Chris De David

unread,
Jan 28, 2013, 9:04:07 PM1/28/13
to php...@googlegroups.com
I practice and prefer the last one (#3).
Reply all
Reply to author
Forward
0 new messages