1

I've been using PHPDepend to validate the quality of the code I'm writing. The one metric where I'm not in the low/average column is the average hierarchy height metric.

The definition on pdepend.org is

The Average Hierarchy Height metric is a average depth of the inheritance hierarchy. In a system of ten classes, a AHH-value of 1 can be interpreted in different ways, for example: Five classes inherit from five other classes within the analyzed application or five classes inherit from a single root class.

My head just isn't grasping what that sentence is saying, which means I'm struggling to figure out how I would go about reducing the score given.

Alan Hollis
  • 1,292
  • 3
  • 14
  • 29
  • 2
    If you drew the classes with lines in between (each one a "link"), it's the average length of the "chains" you would have from the "links". – willoller May 08 '14 at 20:35
  • Can you please define length. If I use http://chapman.id.au/figures/zend_db.fdp.png as an example, would it be the average number of lines pointing to class? – Alan Hollis May 08 '14 at 20:44
  • 1
    Be cautious with these code analysis tools. They are a tool to only give you guidelines and smells. Write code that makes sense for what you're doing... don't try to meet what the tool tells you. Most of the time those two goals are in-line, but sometimes they aren't, and at the end of the day the maintainability of your code is what is important. – Brad May 08 '14 at 20:47
  • I completely agree @Brad, and I take all of these metrics with a pinch of salt, but it's annoying me that I don't seem to "get" this particular one. – Alan Hollis May 08 '14 at 20:51
  • @AlanHollis take `Zend_Db_Adapter_Abstract` (lower left). It has 4 children, each having 0 children. So the average length is 1. The `Exception` tree has some different lengths: `_Oracle_Exception` inherits from `_Adapter_Exception` inherits from `_Exception`: that's 2 "links". `_SqlSrv_Exception` also has 2, etc – willoller May 08 '14 at 20:51
  • Also see https://en.wikipedia.org/wiki/Binary_tree - in mathy terms "length" I mentioned is called "depth" or "height". – willoller May 08 '14 at 20:56
  • I *think* I understand that, but in this instance what is deemed good quality and what is deemed bad quality? Taking the above examples in Zend_Db_Adapter_Abstract better than Zend_Db_Exception? How would I take the attached diagram and reduce the AHH metric? – Alan Hollis May 08 '14 at 20:57
  • Quality is in the eye of the beholder. IMHO, this is one of the more useless metrics - that is, I've never encountered a scenario (yet) where I was bitten by managing inheritance depth. – willoller May 08 '14 at 21:00
  • Thanks for all your comments, that does make me *feel* better about my code, but I'm still left with that nagging feeling I'm just not getting it! – Alan Hollis May 08 '14 at 21:01
  • 1
    Next you'll be trying to get 100% test coverage ;) – willoller May 08 '14 at 21:02

1 Answers1

1
class A{}

class A1 extends A{} // 1 depth

class A11 extends A1{} // 2 depth
class A12 extends A1{} // 2 depth

class B{}

class B1 extends B{} // 1 depth

class C{}

class D{}

There are 4 root class A, B, C, D

  • A hierarchy has 2 depth
  • B hierarchy has 1 depth
  • C hierarchy has 0 depth
  • D hierarchy has 0 depth

Sum of it is 3.

AHH = 3/4 = 0.75

From code

if (($count = count($this->rootClasses)) > 0) {
    $this->ahh = array_sum($this->rootClasses) / $count;

To reduce this score you could add more root classes without children at all(or lower than the deepest). Or reduce depth of hierarchy.

sectus
  • 15,605
  • 5
  • 55
  • 97