CppDepend 中的圈复杂度

Cyclomatic complexity in CppDepend

我想了解圈复杂度的工作原理,有人可以向我解释为什么以下代码的分数为 3:

CommandLineAgruments Parse(int argc, char *argv[])
{
    CommandLineAgruments result;

    std::string command;
    std::vector<std::string> arguments;
    for (int i = 0; i < argc; i++)
    {
        std::string currentArg = std::string(argv[i]);
        size_t index = currentArg.find('-');
        if (index == 0)
        {
            // new commandline argument. Parse the old argument if we have one, then reset
            if (command.size() != 0)
            {
                if (m_supportedCommandLines.find(command) != m_supportedCommandLines.end())
                    m_supportedCommandLines[command](arguments, result);
                else
                    throw std::invalid_argument("Argument not supported : '" + command + "'");
            }

            command = currentArg;
            arguments.clear();
        }
        else
        {
            arguments.push_back(currentArg);
        }
    }

    // Parse last command if we have one
    if (command.size() != 0)
    {
        if (m_supportedCommandLines.find(command) != m_supportedCommandLines.end())
            m_supportedCommandLines[command](arguments, result);
        else
            throw std::invalid_argument("Argument not supported : '" + command + "'");
    }

    return result;
}

而这个函数给出的分数是 1:

void ParseOutputArgument(const std::vector<std::string> &arguments, 
CommandLineAgruments &commandLine)
{
    std::string arg = arguments[0];
    std::transform(arg.begin(), arg.end(), arg.begin(), ::tolower);
    if (arg == "file")
    {
        if (arguments.size() != 2)
            throw std::invalid_argument("Missing output file path");

        commandLine.OutputFile = arguments[1];
        commandLine.FileOutputType = CommandLineAgruments::OutputEnum::File;
    }
    else if (arg == "console")
    {
        if (arguments.size() != 1)
            throw std::invalid_argument("Invalid arguments for output");

        commandLine.FileOutputType = CommandLineAgruments::OutputEnum::Console;
    }
    else
    {
        throw std::invalid_argument("'" + arg + "' is an invalid argument for output");
    }
}

我知道函数 A 比函数 B 更复杂,但我发现很难看出函数 B 如何获得 1 分

这是CppDepend中的一个问题,计划于2018年7月推出的新版本2018.2将解决它。 感谢 Kristian 给了我们一个样本来重现它。