4

When you have a long line like this...

if ($obj->getStupidlyLongNamedThing() + $hard + $harder * $hard / $harder + $obj->getAlsoStupidLongThing([$sillyLongExpression, $thing]) > $obj->omgThisIsInsane());

If you're just going to be lazy and break it up however where where ever so it looks like crap...

if ($a->b()
____&& $a->c());

Then I urge you to do one of two things:

* Shut up your face and turn on word wrap in the IDE rather than manually doing it even worse.
* Break the long parts into variable assignments or something.
* (or get a wider screen)

Example:

$a = $obj->getStupidlyLongNamedThing();
$b = $hard + $harder * $hard / $harder;
$c = [$sillyLongExpression, $thing];
$d = $obj->getAlsoStupidLongThing($c);
$e = $obj->omgThisIsInsane();

if ($a + $b + $d > $d);

With sensible variable names.

Comments
  • 3
    If you work with visual studio, use CodeMaid.
    Will clean that shi* right up.

    Also: OH GOD KILL IT WITH FLAMMENWERFER
  • 3
    Your solution is not equivalent, it doesn't do short-circuit evaluation. It will be slower if whole value is determined by results to the left and may have additional side effects if latter parts have them.

    You can work around this by using functions/methods instead of local variables.
  • 0
    @gronostaj Most of the cases I'm seeing that's not really a big deal. It can be functions or variables, whichever is appropriate.

    I'm seeing it mostly for weird reasons. IE, some PSR said you have to have this line limit, even as a soft limit so they so they make it a strict line limit and blindly through in a new line for anything exceeding it. In PHP it's not everywhere and consuming everything, manual line wrapping, especially if people have some deploy process that blocks lines of a certain length. Then people just throw in the new line anywhere.

    Other cases are more inexplicable, as though people just don't like more than one thing per line.
Add Comment