2

Are there any alternative patterns for reducing a multi-line if statement to something more readable or presentable? Something like:

if ($x == $y
  && $x == $z
  && $y == $v
  && $m == $t
  && $f == $x
  && $h == $g
  && $q == $w
  && $w == $p // etc
)
Ben
  • 51,770
  • 36
  • 127
  • 149
chustar
  • 12,225
  • 24
  • 81
  • 119
  • 3
    A refactor may be in order if you're finding yourself comparing that many conditions... – Brad Christie Mar 14 '11 at 18:53
  • I don't have write-access to the code that generates the two sets of variables, I just need to check if one set is a duplicate of the other. – chustar Mar 14 '11 at 18:55
  • 3
    I think that this type of statement is universal and easily understandable. Any "clever" way of doing it would probably sacrifice those traits. – Kevin Mar 14 '11 at 18:55
  • @chuster: You're telling me you don't know of any easier way to compare two objects as the same other than checking each property individually? Also, this sounds like a perfect opportunity to create an [object comparison](http://php.net/manual/en/language.oop5.object-comparison.php) method. – Brad Christie Mar 14 '11 at 18:57
  • @kjy112: Thanks, but the rep is just an added bonus to helping others and learning from others' experiences. ;-) – Brad Christie Mar 14 '11 at 19:00
  • @brad Creating new objects to hold the variables just so i can compare them seemed like overkill but i am willing to do it if its the right thing to do here. – chustar Mar 14 '11 at 19:03
  • @chustar: Personally, I would break that off in to another function. `if (objectsAreEqual($a,$b))` with a defined function solely for comparing seems more legible to me. Then, whichever method you decide on for the comparison is up to you, but gives you freedom of choice across the project, and can benchmark it later for optimizations (while keeping your condition in plain English). – Brad Christie Mar 14 '11 at 19:06
  • possible duplicate of [Dealing with big IF statements in PHP](http://stackoverflow.com/questions/2813774/dealing-with-big-if-statements-in-php) – ircmaxell Mar 14 '11 at 20:16

1 Answers1

1

You could compact this particular instance with:

if (array($x, $x, $y, $m, $f, $h, $q, $w)
 == array($y, $z, $v, $t, $x, $g, $w, $p))

But I'm not sure if this is what you want. Your single letter variable names are a bit abstract.

If you are concerned about readability, then using and over && sometimes helps.

mario
  • 144,265
  • 20
  • 237
  • 291
  • Thanks. The single letter variables are just because I don't want to type out regular variable names. – chustar Mar 14 '11 at 19:01
  • My carpel tunnel prefers `&&` personally (I also find it a bit more intuitive than "and", but that's probably just me.) – Brad Christie Mar 14 '11 at 19:01
  • @Brad: If that was a common issue for me I would even go so far and make it an URL string `"$x=$y $x=$z $y=$v"` and compare that via an url_parse/array_keys/values wrapper function. Depending on the var types. Certainly a helper function in any case. – mario Mar 14 '11 at 19:07
  • If these are in-fact objects, what's wrong with [traditional comparison](http://www.ideone.com/W31YC)? – Brad Christie Mar 14 '11 at 19:13