There are few metrics that serve as accurate of a touchstone for complexity than Cyclomatic complexity; yet, this metric alone can paint a somewhat misleading picture of true complexity because it doesn’t account for conditional depth.

For example, the following C++ code snippet yields a Cyclomatic complexity value of 5:

if(HighAmountAcct != NULL)
{
  iMgrMemory->FreeUp(HighAmountAcct->GetHighPriorityMssgsResult);
}
if(AQuedAcct != NULL)
{
  iMgrMemory->FreeUp(AQuedAcct->GetQueuedMessagesResult);
}
if(NotifyAcct != NULL)
{
  AtlCleanupValueEx(&NotifyAcct->NotifyAcct, iMgrMemory);
}
if(ReplyAcct != NULL)
{
  iMgrMemory->FreeUp(ReplyAcct->ReplyQueuedMessagesResult);
}
if(RegisterAcct != NULL)
{
  AtlCleanupValueEx(&RegisterAcct->RegisterAcct, iMgrMemory);
}

Regardless of the domain or the language involved here, it’s hard to argue that this snippet of code is terribly complex. Each condition is logically separated– no one condition depends on another. Yet, compare the code above with the code below, which also yields a Cyclomatic complexity of 5:

if(ACCT_FAIL(iSettings->EnumValues(&iEnumValues)))
{
 iSettings->Remove();
}
else
{
 CHAR iActHldr[MAX_AMOUNT];
 if(ACCT_SUCCEED(iEnumValues->Get(iActHldr)))
 {
  if(ccint(iActHldr, iName, len(iName)) == 0)
  {
   sr = iSettings->Delete(iName);
   if (!ACCT_FAIL(sr))
   {
    sr = ACCT_FAIL(iSettingsMgr->Put(iSettings));
   }
  }
 }
}

Again, ignoring the domain tackled here or the perceived complexity of the language, which code snippet do you find more complex? It’s clear that the second example snippet involves conditional depth, which means one must comprehend previous state in order to ascertain the context of an individual line of code– as you get deeper into a series of conditions, the management overhead increases as well.

While Cyclomatic complexity acts as an excellent barometer of complexity, as a stand alone metric, it’s one-dimensional– it only gives a partial picture. The addition of depth gives complexity a second dimension, which yields a far more explicit view. Keep in mind though, both method snippets are testing challenges– it’s just that the second snippet is arguably harder to test.