15

I'm testing my libraries with pdepend and some functions have incredibly high complexity. I just realized it comes from the ternary operator, but I'm not sure why.

With a function like:

  function test($a) {
        return $a > 10 ? 5:20;
  }

pdepend returns a complexity (npath) of 5. Why 5 different paths? I see only 2.

With a function like:

   function test($a) {
      if($a > 10)
         return 5;
      else
         return 20;
   }

The npath complexity is 2. Which makes sense.


Edit: Ok I had a look at the other question: PMD - NPath complexity very high with ternary operator (?

It's part of the algorithm.. Still, the function has only 2 possible paths. The algorithm doesn't make sense to me. The number of nodes do not reflect the number of paths, and it arbitrarily adds 2 to the value.

Community
  • 1
  • 1
leyou
  • 806
  • 1
  • 13
  • 25

2 Answers2

3

The algorithm that pdepend uses to determine complexity adds two for the use of the ternary operator even though they should be the same because it works the same as an if else and adds the same number of paths. From my experience you shouldn't see much of an actual difference if any in actual application.

Matt Zera
  • 465
  • 3
  • 12
  • 1
    While it does report "more complexity", I disagree the assertion that the complexity should be the same. The algorithm is such that it *is* higher by a design choice, so cases must be made against what *is* and not what might be expected (or desired if you/I were the author :). – user2864740 Jun 13 '14 at 05:17
  • I think there's some debate over how much complexity it actually adds or should add to a program. Your point is valid. In the end it's completely up to the programmer as to how they want to interpret the information. – Matt Zera Jun 13 '14 at 05:27
2

The ternary operator makes a copy of non-object values. Meaning, in your first code sample, $a is copied then the result is returned. Source. In the second code sample, no such copy is made.

My assumption, then, is that the extra paths involved come from making the copy.

Other resources to look at:

http://fabien.potencier.org/article/48/the-php-ternary-operator-fast-or-not

https://drupal.org/node/1838368

http://www.mail-archive.com/internals@lists.php.net/msg51926.html

Mark Miller
  • 7,442
  • 2
  • 16
  • 22
  • 1
    But does pdepend take this into account when determining the metrics? (That is, it could be true but entirely unrelated.) – user2864740 Jun 13 '14 at 05:18
  • @user2864740 I don't know, I'm not really familiar with pdepend, but I can deduce that, yes it does, considering the OPs question. The two statements are identical - except for the behind-the-scenes copy procedure performed with the ternary. – Mark Miller Jun 13 '14 at 05:24
  • 1
    Thanks, I didn't know about the copy. However it should not be considered as a 3rd path by pdepend. It's like adding "$b = $a;" before a if/else statement. There are still only 2 possible paths. – leyou Jun 13 '14 at 05:57
  • @leyou Ya, you might be right. Perhaps, though, the higher npath complexity is derived from considering the `copy of $a` *and* `$a` - thus, in a sense, two `if-else` statements ... not quite sure how that would equate to 5 though... – Mark Miller Jun 13 '14 at 05:59