Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PSR-2 standard for long if-conditions

Tags:

php

convention

I did not find any standard for this case:

if ($a == $b && $b == $c && $c == $d && $g == $d) {  } 

or

if (($a == $b && $b == $c) && ($c == $d && $g == $d)) {  } 

Imagine the var-names are longer and 80 letters are exceeded. How should I handle this? It could look like:

if (        $a == $b     && $b == $c     && $c == $d     && $g == $d ) {      } 
like image 210
user3631654 Avatar asked May 20 '14 11:05

user3631654


People also ask

Is PSR 2 deprecated?

Deprecated - As of 2019-08-10 PSR-2 has been marked as deprecated. PSR-12 is now recommended as an alternative. This guide extends and expands on PSR-1, the basic coding standard. The intent of this guide is to reduce cognitive friction when scanning code from different authors.

Why should you follow a PSR standard?

The PHP Standard Recommendation (PSR) is a PHP specification published by the PHP Framework Interoperability Group (PHP-FIG). It serves the standardization of programming concepts in PHP. The aim is to enable interoperability of components. The PHP-FIG is formed by several PHP frameworks founders.

What PSR-12?

PSR-12 seeks to provide a set way that both coding style tools can implement, projects can declare adherence to and developers can easily relate on between different projects for these coding style reducing cognitive friction.

What do you know about PHP coding Standards What standards have you used?

There are few guidelines which can be followed while coding in PHP. Indenting and Line Length − Use an indent of 4 spaces and don't use any tab because different computers use different setting for tab. It is recommended to keep lines at approximately 75-85 characters long for better code readability.


2 Answers

There is no recommendation / convention for this case, and as Halcyon already mentioned this is a quite exceptional case.

However, there is a recommendation for a function call with a long list of parameters:

Argument lists MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first item in the list MUST be on the next line, and there MUST be only one argument per line.

<?php $foo->bar(     $longArgument,     $longerArgument,     $muchLongerArgument ); 

So if I had to create an if-statement similar to your's, I'd do this:

if (     $a == $b &&     $b == $c &&     $c == $d &&     $g == $d ) {     // do something } 

As you can see, this is almost the same as the solution you proposed yourself, but I prefer adding the && operators after the conditions.

like image 196
Nic Wortel Avatar answered Sep 18 '22 10:09

Nic Wortel


Personally, I prefer

if ($a == $b     && $b == $c     && $c == $d     && $g == $d ) {     // code here... } 

For each line, you start with the double ampersand, indicating that the following statement is separate from the others. If you put the ampersand on the end of the line, it can become less obvious when the lines vary a lot in length.

For example;

if ($a == $b &&      $b == $c &&      $thisisamuchlongerstatementbecauseofthisvar == $d &&      $g == $d ) {     // code here... } 

In this case you have to scan the code more to know that each line is connected by a double ampersand.

like image 45
Maurice Avatar answered Sep 17 '22 10:09

Maurice