2

I have this code that I want to use for calculating the Halstead complexity:

<?php
$words = explode("\n", file_get_contents('http://www.puzzlers.org/pub/wordlists/unixdict.txt'));
foreach ($words as $word) {
    $chars = str_split($word);
    sort($chars);
    $anagram[implode($chars)][] = $word;
}

$best = max(array_map('count', $anagram));
foreach ($anagram as $ana)
    if (count($ana) == $best)
        print_r($ana);
?>

I understand the principles of how it is done (see http://en.wikipedia.org/wiki/Halstead_complexity_measures), but what I don't understand and also couldn't find out after thoroughly searching, is: What exactly counts as an operator, and what counts as an operand? There doesn't seem to be a agreed upon definition.

Here's an easier example (http://www.win.tue.nl/~aserebre/2IS55/2009-2010/9.pdf) which I can follow, but can't apply in the example above.

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
TrashyMcTrash
  • 1,234
  • 2
  • 15
  • 39

1 Answers1

1

As I understand this metric, operators are all operators (==, +, ++, and so on) and keywords (for, if, break, function) of the language you are using. Operands will be every identifier, i.e. function names (self writtend functions and also built in functions), variable names and constants.

To your example:
Operators: foreach (as is obligatory, so I dont count it), [] (this is like +=, just with appanding instead of adding sth.), if, ==
Operands: $words, explode, $word, str_split, $chars, sort, $anagram, implode, $best, max, array_map, $ana, cound, print_r

Notice: This code metric seems not to be used for PHP very often, so I also have not found any reference or example for this metric used on PHP code.

AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
  • According to wikipedia, they count even the opening `(` for the `printf(`. https://en.wikipedia.org/wiki/Halstead_complexity_measures. Unless I'm counting wrong? – Alex Kwitny Sep 02 '15 at 22:03
  • @AlexKwitny Yes, they count every pair of parenthesis as an operator. – AbcAeffchen Sep 02 '15 at 22:46
  • I read that "( as is obligatory, so I don't count it". I see you highlighted `as`. I thought you were talking about the `(`. Perhaps you have an opinion on my Halstead question http://stackoverflow.com/questions/32364095/how-to-count-operators-and-operands-for-halstead-complexity-measures?noredirect=1#comment52602473_32364095 – Alex Kwitny Sep 03 '15 at 00:20