Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PCN metric #255

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions metrics/ast.py
Expand Up @@ -325,6 +325,17 @@ def varcomp(tlist) -> float:
return (parts / variables) if variables != 0 else 0


def pcn(tlist) -> int:
"""Return number of words in the name of a class.
r:type: float
"""
classname = tlist[0][1].name
# By naming convention Java classes names use PascalCase.
# Nevertheless, this metric considers both PascalCasse and camelCase naming conventions.
words = re.findall(r'[A-Z]?[a-z]+|[A-Z]+(?=[A-Z]|$)', classname)
return len(words)


def nop(tlist) -> int:
"""Return number of polymorphic methods in main class.
Methods of nested classes are skipped.
Expand Down Expand Up @@ -392,6 +403,8 @@ class NotClassError(Exception):
f'Number of Class Annotations\n')
metric.write(f'varcomp {varcomp(tree_class)} '
f'Average number of parts in variable names\n')
metric.write(f'pcn {pcn(tree_class)} '
f'Number of words in the name of a class\n')
metric.write(f'mhf {mhf(tree_class)} '
f'Method Hiding Factor (MHF), which is the ratio of private \
and protected methods to total methods\n')
Expand Down
1 change: 1 addition & 0 deletions tests/metrics/test-ast.sh
Expand Up @@ -49,6 +49,7 @@ stdout=$2
grep "final 1" "${temp}/stdout"
grep "noca 1" "${temp}/stdout"
grep "varcomp 2.75" "${temp}/stdout"
grep "pcn 1" "${temp}/stdout"
grep "mhf 1.0" "${temp}/stdout"
grep "smhf 0" "${temp}/stdout"
grep "ahf 0" "${temp}/stdout"
Expand Down