PyCon: Managing Complexity (and Testing)

Managing Complexity (and Testing)

This was a talk on various metrics for code complexity.

Knowing the number of lines of code is not enough. For instance, how many of those lines are well tested? sloccount is a project to count lines of code.

The number of unittests that a project has is also not enough information.

There are many approaches to testing code coverage. For instance, do your tests test every line? Every branch? Every path? Each of these is successively harder. Even 100% path coverage doesn't guarantee you have no bugs.

Your gut feeling for how much code coverage you have is usually too optimistic.

Remember that human brains have not kept up with Moore's law ;)

He mentioned McCabe complexity as a way to measure code complexity.

Complexity != length. A function can be very long without being complex. This is the case if it has no if statements, no loops, no early returns, etc.

There is no way to test path coverage in Python.

He mentioned PyMetrics as a Python package to test code complexity. It was written by Reg Charney, a fellow member of BayPiggies.

High code complexity is correlated with a high bug count. Duh! ;)

Dead and redundant code is 40-100% more likely to be buggy.

Halstead is another metric, but the speaker said it was difficult for him to understand.

Code reviews are good.

Figleaf is a tool for testing code coverage.