Measuring Cyclomatic Complexity Of Python Code

Complex code is hard to manage, hard to isolate and hard to unit test. For these reasons it is more difficult and costly to modify. In other words you should try to avoid complex code.

Many software metrics exist to measure the complexity of code. One such metric is cyclomatic complexity. Cyclomatic complexity (CC) is the measure of linearly independent paths through a program. The algorithm was developed by Thomas McCabe back in the 1970s.

CC really measures the amount of branching in a suite of code. Suites with more than seven branches are considered suboptimal and should be looked at for refactoring. The number seven was chosen because it is believed to be the average number of things a human being can concurrently hold in their head. CC is well covered on the internet so if you want to know more Google it.

I implemented the CC algorithm using a very simple AST visitor. A CC number is calculated for each Module, Class, Method and Function in a file. The program currently calculates, but does not print the results for nested classes or nested functions.

Metric Frenzy
Use metrics as a guide to show where there may be a need for refactoring. Don’t take them too seriously. Just because the complexity number is slightly above optimal doesn’t mean the code sucks. Metrics are not the definitive answer on code quality. So take them with a grain of salt.

Getting The Code
The program and unit tests are available in my Subversion repository. Just download the files into any directory on your system. You will need at least pygenie.py and cc.py.

I am probably going to create a new home on Google code for this stuff. It will be announced in a follow up post.

Running The Program
The program expects one or more Python filenames or fully qualified module names to be passed in on the command line. For example:
./pygenie.py complexity mycode.py
- or -
./pygenie.py complexity mycode.py dir0/dir1/mod.py
- or -
./pygenie.py complexity dir0.dir1.mod

Running the program will print the results to standard output. This is a proof of concept and not a polished application so don’t expect real fancy output.

Interpreting The Results
The output is a table of three columns: suite type, suite name and the complexity number. The suit type could have the following values: X for a module, F for a function, C for a class and M for a method. The suite name is the fully qualified name of a suite. The complexity number is just a simple integer representing the suite’s complexity. The rows are sorted by the complexity number in descending order.

Only things that have a high complexity number are shown by default. If you want to see all of the complexity values you can use the –verbose option. For example:
./pygenie.py complexity –verbose dir0.dir1.mod

Here is an example of running the cc.py code throught itself:

dstanek% ./pygenie.py complexity example.py
Module: example
Complexity Chart:
type name                             complexity
M    AClass.runtests                  28
F    fall_down                        10
F    run_away                         9
X    cc                               8
M    BClass.dosomething               8
F    duck_and_cover                   8

Code that is not shown because its complexity number is seven or less in not proven to be good. The design may be faulty, variables obfuscated or many other things.

Closing Thoughts
It is good practice to try to keep the complexity of code to a minimum. Code with a low complexity number is less risky to change and easier to test. This should not be the only way to judge your code, just a supplement.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Netflix Outage

Their site has been down virtually all day. What gives? Looks like Web 2.0 isn’t all its cracked up to be. :-) I wonder what Web 3.0 has to offer?

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Use Bazaar To Work On Python

The Python core team is experimenting with distributed version control. Python 2.5, 2.6 and 3.0 branches are now available under Bazaar. The Bazaar master is synced periodically from Subversion and may be slightly behind.

After the initial announcement there was some debate about which VCS to use. It was made clear that this is an experiment. No decision has been made about switching away from Subversion. This is more about experimenting with the distributed workflow. If it turns out that distributed VCS is the way to go then there would be open discussion about which system to use.

There is also an unofficial Mercurial repository. Antoine Pitrou set it up as an alternative to Bazaar. For more information read the announcement on the dev-python thread.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Horny Bear - My Favorite Valentine’s Day ecard

I’m proud to say that I work for the company that brought you the Horny Bear. The VD Is For Everybody ecard is funny too in a sick sort of way.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

PyCon 2010 in Cleveland - Update

Today was the kickoff meeting. The first thing that struck me is that we were the only bidders in the room. There were people from the other bidding cities, but I don’t think any of them were actually a part of the bidding process. So that was good. We had a strong showing with about six of us in attendance.

The PyCon location selection process is really focused on evaluating the local community. This is good for us! We have a very strong Python community. I believe that Cleveland has other strong communities that may align with us. I’m going to be sending out an email to the NOOSS and Ruby groups to see if we can get their support. Anyone know of other local groups that may be interested?

More information to follow…

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

HTTP Status Code Diagram

Someone posted a really cool activity diagram on the Python Web-SIG list a few days ago. It shows the relationship between HTTP status codes and HTTP requests. I encourage web programmers to take a look.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Cleveland Bidding For PyCon 2010

Back when PyCon was taking bids for 2008-2009 I tried to get Cleveland a bid. I was only able to call/visit a half dozen places or so. It was only me at the time so I eventually gave up as I saw how much the other bidders were able to accomplish in the same time. All I have to show for it is a bunch of hotel brochures.

I think this attempt can be different. I am going to get this thing rolling again, but I really do need some help! If you are interested in helping please let me know. We need a strong team of at least 5 people to pull this off.

I have created a bid page over at the Python wiki. If your interested in helping out email me at [dstanek AT dstanek DOT com] and add your name to the staff section of the wiki page. Also please read about the bid requirements.

If you are not able to participate in creating a bid there are other ways to help out. Do you know of a good location? Do you know of any cool extracurricular activities? Let us know and we’ll follow up on the leads.

Lets bring PyCon home in 2010!

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Memory Can Be Recovered After A Computer Is Shutdown

A research project at Princeton University has proven that memory can be recovered after a computer is shutdown. This is exactly the opposite of what I have always thought. Very interesting technique and video.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

Twitter Finally Got Me

Until now I have not seen the value of Twitter. Well actually I still don’t know if I see the value, but I decided to sign up anyway. Feel free to follow me there.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati

$HOME in Subversion

Keeping my $HOME in Subversion has made my life much easier. The chief benefit is the ease at which I can create and synchronize dotfiles across different machines. In the past I have had to figure out which box has a particular version of a config file and then manually copy it over. This is just a waste of time.

This method also benefits from all the goodness of version control: backups are easy, history, etc. I can manage all of my config files just as I do my source code.

I found a great tutorial that I used as a starting point. I highly recommend you give it a try. Once I start to deviate from the tutorial a bit more I write my own how-to.

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Reddit
  • Technorati