The python tool
pymetrics can be used to measure your source code complexity. You can install it with
easy_install pymetrics. Here is an example (running in virtual environment devenv; source code is located in
src):
user1@deby:~/devenv/trunk$ ../bin/pymetrics src/greatings\
/helloworld.py
=== File: src/greatings/helloworld.py ===
Module src/greatings/helloworld.py is missing a module doc string.
Detected at line 1
Basic Metrics for module src/greatings/helloworld.py
----------------------------------------------------
1 maxBlockDepth
4 numBlocks
189 numCharacters
1 numDocStrings
1 numFcnDocStrings
2 numFunctions
7 numKeywords
16 numLines
73 numTokens
50.00 %FunctionsHavingDocStrings
Functions DocString present(+) or missing(-)
--------------------------------------------
- main
+ say
McCabe Complexity Metric for file src/greatings/helloworld.py
--------------------------------------------------------------
2 __main__
1 main
1 say
COCOMO 2's SLOC Metric for src/greatings/helloworld.py
-------------------------------------------------------
8 src/greatings/helloworld.py
*** Processed 1 module in run ***
Here is a script that let you generate report for all your files in
src directory:
#!/bin/sh
working_dir=/tmp/$USER/pymetrics
mkdir -p $working_dir
find src/ -name \*.py > $working_dir/files.txt
../bin/pymetrics --nosql --nocsv -f $working_dir/files.txt
Here is a guideline for understanding cyclomatic complexity number:
- 1 - 15: simple code, minimum risk, can be easily covered by tests
- 15 - 30: complicated code, consider refactoring before writing tests
- 30 - 50+: complex code, refactor now, almost impossible to write a good tests
Read more about software complexity
here and
here.
Score 1135 in complexity.
ReplyDeleteDo I have to do something when score is upper 1000 ?
This is extremely high... I would suggest:
ReplyDelete1. try to identify actors in your code; visualize the code in some sort of drawing so you can have an overall picture (no need for special software, just paper and pencil); develop ten items plan of what can be refactored first.
2. split functions that are longer than your screen can fit in several smaller functions, introduce modules
3. apply some sort of object oriented design to abstract certain things
4. while refactoring ensure the new code has at least 95% test coverage, start with doctest, plan unit tests (mockers will help)
5. after each stage provide some sort of regression testing to ensure nothing has been broken
...
ReplyDeleteI missread the correct result.
McCabe Complexity Metric for file src/greatings/helloworld.py
--------------------------------------------------------------
2 __main__
1 main
1 say
COCOMO 2's SLOC Metric for src/greatings/helloworld.py
-------------------------------------------------------
8 src/greatings/helloworld.py
How to understand the cocomo 2's SLOC Metric ? ( the "8" score in your code).
I have 1135, but it's cocomo 2's score.
COCOMO (Constructive Cost Model) is estimated software cost. You should take is as something relative.
ReplyDeleteYou are working on project A that has a score S1 and a support for a project B is coming to you/team next week, it would be nice to have a quick idea how project B source code is complex, what is estimated cost so you can properly plan resource available. This is where metrics help. There is something you are well aware of (project A) and you got filling of project B by simply running code metrics tool.
1-15 for a single method is probably rather high. In the 2nd edition of Code Complete by Steve McConnell it mentions 0-5 as being "probably fine", and 6-10 to be a point at which you should start thinking about simplifying. At 10+ he recommends breaking parts of the routines into subroutines.
ReplyDeleteHi Andriy,
ReplyDeleteIs there a way to get the output of PyMetrics to a format like .txt, .xml or anyother format instead of the terminal?
1. How about redirecting shell output to file?
Deletepymetrics hello.py > results.txt
2. There are a number of options available, just
try issue the following command:
pymetrics --help
Thank you very much Andriy for your quick reply. Yeah, your first option is working. But, it seems like i should try for an xml output. Can i do that?
DeleteI could not get any solution from your second option.
Eric, unfortunately there is no way to output result into xml file, however there is CSV output format that you might consider translate to XML as appropriate.
DeleteI think that is the solution Andriy. Your blog is great and appricate your quick respoces. Thank you very much.
DeleteHi, csv output of PyMetrics gives line by line details of the code. Is there anyway to get summery metrics output in csv format?
ReplyDeleteThere is no summary metrics report, you need calculate appropriate metrics out from csv file.
Delete