This blog entry is quick read summary about code complexity. It gives a definition of code complexity and several signs how to notice code complexity. It also provides a brief description how to measure code complexity and suggests a way to keep it low.
Most of the concepts are taken from Structure 101 help section.
This blog entry is based upon my code complexity presentation for www.greetz.com.
Definition of Code Complexity
Reg. Charney defines Code Complexity as:
the amount of effort needed to understand and modify the code correctly.
Excessive complexity: When is a code-base too complex?
Signs of Logical complexity:
- part of the design is no longer fit for its purpose
- unclear purpose
- adding new features becomes harder than it should be
- the code is hard to understand
- changes have unexpected side effects
The need to know about code complexity
Keeping track of code complexity helps to:
- Predict (future) feature implementation times
- prioritize areas of the code for structural refactoring
It will cost!
Manage complexity like debt
Howard Cunningham 'Mr. Wiki' compared complexity with debt:
"Manage complexity like debt"
skipping designs = borrowing money
maintenance headaches = interest payments
refactoring = repaying debt
cannot refactor = cash-flow insolvency
creating engineering policies = devising financial policies
Andy Hunt extends the analogy concisely:
"Just like real debt, it doesn't take much to get to the point where you can never pay it back, where you have so many problems you can never go back and address them."
What can we do? Measure Complexity!
Although we cannot specify the amount of effort needed. We can calculate a complexity index.
This index enables us to:
- Compare projects
- Monitor trends (getting more or less complex)
- Discover which items at what level are causing the complexity.
Measuring Code Complexity
Measure the complexity at every level of design breakout, method, class, package and
high-level package (High level packages contain only other high-level packages or leaf packages).
Measuring occurs by counting items and dependencies it immediately contains.
Method complexity is usually calculated by cyclomatic complexity.
About Cyclomatic complexity
What is Cyclomatic complexity?
A measurement for the complexity of a peace of source code.
What does it measure?
The number of linearly independent paths through a peace source code.
Cyclomatic Complexity @ wikipedia
Calculating Cyclomatic Complexity
To calculate cyclomatic complexity build a graph that describes the control flow of the program. The nodes of the graph correspond to the commands of a program. A directed edge connects two nodes if the second command might be executed immediately after the first command.
The number of linearly independent paths through the first method is 3, and the second is 2.
In Structure 101, class, package and high-level package complexity is calculated by counting items and dependencies they immediately contain (screenshot from Structure 101 help):
Design tangles are cyclic, package-level dependencies. Design Tangle Metric quantifies the amount of cyclic dependencies.
Defenition of Refactoring:
improving the design of existing code.
Always Refactor when adding new features to you code.
Quote Chris Chedgey:
"You use personality, charisma, leadership and/or donuts to convince your team that henceforth, they will not add any more complexity debt to the code base. Now watch what happens..."
I suggest reading Chris blog item: http://chris.headwaysoftware.com/2006/10/reducing_comple.html.
The need to Rebuild
In some cases redesign and rebuild is faster then refactoring. The complexity index of the tipping point cannot be determined.
Chris Chedgey says: "keep a lid on it"
- Monitor complexity (Structure 101)
- Keep complexity low (stay away from the need to rebuild)
Structure 101 Help see http://www.structure101.com
Cyclomatic Complexity @ wikipedia
Chris Chedgey blog entries: