Cyclomatic complexity facts for kids
Cyclomatic complexity is a way to measure how complicated a computer program is. It helps us understand how many different paths a program's code can take when it runs. A smart person named Thomas J. McCabe, Sr. created this idea in 1976.
Imagine a map of your program's code. This map is called a control-flow graph. Each step or command in your program is like a point on the map (a "node"). Arrows (called "edges") show how the program moves from one step to the next. Cyclomatic complexity counts the number of unique, independent routes you can take through this map.
This measurement can be used for a whole program, or just for smaller parts like a specific function or method. It's especially helpful for testing because it tells you how many different tests you might need to check every important path in your code.
Contents
What is Cyclomatic Complexity?
How We Define It
One easy way to think about cyclomatic complexity is to count the number of different, independent paths through a piece of code.
- If your code just runs straight, without any choices or loops, its complexity is 1. There's only one path.
- If your code has one "if" statement (like "IF something is true THEN do this ELSE do that"), there are two paths. One path if "something" is true, and another if it's false. The complexity would be 2.
- If you have two "if" statements, or one "if" statement with two conditions (like "IF condition1 AND condition2"), the complexity would be 3.
Another way to figure out cyclomatic complexity is by looking at the control-flow graph of the program. This graph shows all the basic steps of the program and how they connect.
For a single part of a program (like one function), the complexity (let's call it M) can be found using a simple formula: M = E - N + 2
- E is the number of arrows (edges) in the graph.
- N is the number of points (nodes) in the graph.
Sometimes, to make the math work out, people draw the graph so that every exit point from the program connects back to the entry point. This makes the graph "strongly connected." In this case, the formula is a bit different: M = E - N + 1
No matter which formula you use, for a single program or function, the result helps you understand its complexity.
A simpler way to think about it, especially for structured programs, is that the cyclomatic complexity is usually equal to the number of decision points (like "if" statements or loops) in the program, plus one. For example, if you have `IF cond1 AND cond2 THEN ...`, this counts as two decision points because the computer checks `cond1` first, and then `cond2`.
Why is Cyclomatic Complexity Useful?
Keeping Code Simple During Development
One main reason cyclomatic complexity was created was to help programmers keep their code from becoming too complicated. The person who invented it, McCabe, suggested that if a part of a program gets too complex (for example, if its complexity goes over 10), it should be broken down into smaller, simpler parts.
This idea is still used today. Many software development groups try to keep the complexity of their code low. If a part of the code has to be more complex, they often write down why it's necessary. This helps make sure the code is easier to understand and manage.
Checking How Well a Program is Built
Cyclomatic complexity can also show how "structured" a program is. A well-structured program is usually easier to read, understand, and change. McCabe created another measure called "essential complexity" to show how close a program is to being perfectly structured.
If a program is perfectly structured, its essential complexity is 1. If it's not well-structured, this number will be higher. This helps developers find parts of the code that might be messy and hard to work with.
Helping with Software Testing
One of the most important uses of cyclomatic complexity is in software testing. It helps testers figure out how many tests are needed to thoroughly check a piece of code.
Here's why it's useful:
- The cyclomatic complexity (M) tells you the maximum number of tests needed to check every "branch" or decision point in your code.
- It also gives you a good idea of the minimum number of tests needed to cover all possible paths.
For example, imagine a program with two "if-then-else" statements one after the other:
if (c1())
f1();
else
f2();
if (c2())
f3();
else
f4();
In this example, you might think you only need two tests to check every "if" statement. But to test all possible paths, you actually need four tests. The cyclomatic complexity of this code is 3. This means you need at least 3 tests to properly check it.
If a part of your program has a high complexity number, it means there are many different paths through the code. This tells you two things: 1. It will take more effort to test that part of the code completely. 2. It will be harder for programmers to understand because they have to keep track of all those different paths.
It's not always possible to test every single path in a program, especially as programs get bigger. But cyclomatic complexity helps testers choose a good number of tests to make sure the most important paths are covered.
For instance, in the example above, if there's a rule that if `f1()` is called, then `f3()` must also be called, the code might have a bug.
- If you only do two tests (like `c1()` true and `c2()` true; or `c1()` false and `c2()` false), you might miss the bug.
- But if you use the cyclomatic complexity (which is 3) to guide your tests, you'd do a third test (like `c1()` true and `c2()` false). This test would show the bug!
Connection to Software Bugs
Many studies have looked at whether programs with higher cyclomatic complexity also have more bugs. Some studies show that yes, more complex parts of code often have more defects.
However, it's also true that larger programs tend to be more complex and have more bugs. So, it's not always clear if complexity itself causes more bugs, or if it's just that bigger programs are naturally more complex and have more places for bugs to hide. Still, many safety rules for software development suggest keeping code complexity low to help prevent errors.
Images for kids
See also
- Programming complexity
- Complexity trap
- Computer program
- Computer programming
- Control flow
- Decision-to-decision path
- Design predicates
- Essential complexity (numerical measure of "structuredness")
- Halstead complexity measures
- Software engineering
- Software testing
- Static program analysis
- Maintainability