Imagine a developer at a fictional logistics company, Kaveri Freight, writing a function that works out the surcharge on a parcel. It looks harmless. If the parcel is over 20 kg, add a heavy-weight charge. If the destination is in the north-east, add a remote-area charge. If the customer chose express, add an express charge. Then return the total.

Three decisions, no loops, maybe twelve lines. A tester is asked: how many tests does this function need? Let us answer with a measure invented for exactly this question.

Counting paths the way McCabe did

In 1976 Thomas McCabe proposed cyclomatic complexity, a number taken from the control-flow graph of a function: boxes for statements, arrows for the possible jumps. For a single function the number equals the count of decision points plus one. Our surcharge function has three decisions, so its complexity is 4.

What does 4 mean? It is the number of linearly independent paths through the code. Think of them as a basis: a minimal set of paths from which every other route can be built by combination. For Kaveri’s function one basis could be: no surcharge applies; only heavy applies; only remote applies; only express applies. Four tests, and each decision has been taken both ways at least once, so branch coverage is complete.

So the first reading: cyclomatic complexity is the minimum number of tests that gives you branch coverage by the basis path method. Fewer than that and some branch has not been visited. (Strictly, you can sometimes reach full branch coverage with fewer tests than the complexity number, when one test runs several decisions the same way, and the basis-path number is the conservative guide. Treat it as a floor for thorough path testing, not as an exact law.)

And the number you cannot afford

Those three decisions are independent, so the full set of true or false combinations is 2 times 2 times 2, which is 8. Complexity 4 gives a minimum, and 8 is the exhaustive list. Perhaps the combination heavy plus express triggers a rule the developer got wrong, say a cap on the total. The four basis tests would never run it. That is why complexity is a floor and why this method is paired with other techniques, such as decision tables for business rules, where interactions are the whole point.

Now watch the growth. Add three more checks, for insured parcels, fragile goods and festival-season loading. Six independent decisions: complexity 7, but 64 combinations. The basis count rises by one per decision, while the exhaustive count doubles. The widening gap is what makes testing hard and what makes large functions dangerous.

How tools count, and where they differ

Most static-analysis tools compute the number automatically, but not identically. Each if, loop and case branch adds one. Whether a compound condition with AND or OR adds one per operator varies by tool, and so does the treatment of exception handlers and ternary expressions. So 12 in one tool might be 9 in another. Pick the tool, note the settings, and compare only against its own history.

McCabe himself suggested 10 as a sensible upper limit for a function, and many teams use a limit around that; some allow up to 15 or 20. These are conventions. There is no law of nature at 10, and a flat switch statement with 25 simple cases can be easy to read and test while a nested function scoring 8 is a nightmare. Use the number as a pointer to where to look, not as a verdict.

Using it as a tester

  • Rank risk. When a release changes twenty functions, the ones with the highest complexity and the most recent edits deserve your attention first. Defects cluster in complicated code that gets changed often.
  • Sanity-check the test count. A function of complexity 11 covered by two unit tests has untested paths by definition. You do not need to read the code closely to know that.
  • Push back on design. If a function needs 20 tests to cover its paths, ask whether it should be three functions of complexity 6 or 7. Smaller units are cheaper to test thoroughly, and the interactions you worried about can be tested at the level where they truly occur.

Back at Kaveri Freight, the sensible outcome is to keep the surcharge function small, write the four basis tests, add the two or three combinations the business rules say are special, and move on. Total of six or seven tests, chosen on purpose, rather than eight written blindly or four written because a tool said so.

Figures and names in the examples are illustrative.

Leave a Reply

Trending