1 .. title:: clang-tidy - readability-function-cognitive-complexity
3 readability-function-cognitive-complexity
4 =========================================
6 Checks function Cognitive Complexity metric.
8 The metric is implemented as per the `COGNITIVE COMPLEXITY by SonarSource
9 <https://www.sonarsource.com/docs/CognitiveComplexity.pdf>`_ specification
10 version 1.2 (19 April 2017).
17 Flag functions with Cognitive Complexity exceeding this number.
20 .. option:: DescribeBasicIncrements
22 If set to `true`, then for each function exceeding the complexity threshold
23 the check will issue additional diagnostics on every piece of code (loop,
24 `if` statement, etc.) which contributes to that complexity. See also the
25 examples below. Default is `true`.
27 .. option:: IgnoreMacros
29 If set to `true`, the check will ignore code inside macros. Note, that also
30 any macro arguments are ignored, even if they should count to the complexity.
31 As this might change in the future, this option isn't guaranteed to be
32 forward-compatible. Default is `false`.
37 There are three basic building blocks of a Cognitive Complexity metric:
42 The following structures increase the function's Cognitive Complexity metric
45 * Conditional operators:
50 - ``cond ? true : false``
56 - C++11 range-based ``for()``
61 * ``goto LABEL``, ``goto *(&&LABEL))``,
62 * sequences of binary logical operators:
64 - ``boolean1 || boolean2``
65 - ``boolean1 && boolean2``
70 While by itself the nesting level does not change the function's Cognitive
71 Complexity metric, it is tracked, and is used by the next, third building block.
72 The following structures increase the nesting level (by `1`):
74 * Conditional operators:
79 - ``cond ? true : false``
85 - C++11 range-based ``for()``
95 * GNU statement expression
96 * Apple Block Declaration
101 This is where the previous basic building block, `Nesting level`_, matters.
102 The following structures increase the function's Cognitive Complexity metric by
103 the current `Nesting level`_:
105 * Conditional operators:
108 - ``cond ? true : false``
114 - C++11 range-based ``for()``
123 The simplest case. This function has Cognitive Complexity of `0`.
129 Slightly better example. This function has Cognitive Complexity of `1`.
133 int function1(bool var) {
134 if(var) // +1, nesting level +1
139 Full example. This function has Cognitive Complexity of `3`.
143 int function3(bool var1, bool var2) {
144 if(var1) { // +1, nesting level +1
145 if(var2) // +2 (1 + current nesting level of 1), nesting level +1
152 In the last example, the check will flag `function3` if the option Threshold is
153 set to `2` or smaller. If the option DescribeBasicIncrements is set to `true`,
154 it will additionally flag the two `if` statements with the amounts by which they
155 increase to the complexity of the function and the current nesting level.
160 The metric is implemented with two notable exceptions:
161 * `preprocessor conditionals` (``#ifdef``, ``#if``, ``#elif``, ``#else``,
162 ``#endif``) are not accounted for.
163 * `each method in a recursion cycle` is not accounted for. It can't be fully
164 implemented, because cross-translational-unit analysis would be needed,
165 which is currently not possible in clang-tidy.