From 9932cc140707da9458fac2c51a0c32adf9c29ae9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Joly?= Date: Mon, 25 Nov 2024 16:22:07 +0100 Subject: [PATCH 1/2] Modify rule S6194 Improve rule description for coroutine cognitive complexity CPP-4992 --- rules/S6194/rule.adoc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/rules/S6194/rule.adoc b/rules/S6194/rule.adoc index 460fc82f546..5c5ae3ad26b 100644 --- a/rules/S6194/rule.adoc +++ b/rules/S6194/rule.adoc @@ -1,6 +1,24 @@ == Why is this an issue? Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Coroutines with high Cognitive Complexity will be difficult to maintain. +Code with high cognitive complexity is hard to read, understand, test, and modify. + +As a rule of thumb, high cognitive complexity is a sign that the code should be refactored into smaller, easier-to-manage pieces. + +=== Which syntax in code does impact cognitive complexity score? + +Here are the core concepts: + +* **Cognitive complexity is incremented each time the code breaks the normal linear reading flow.** + +This concerns, for example, loop structures, conditionals, catches, switches, jumps to labels, and conditions mixing multiple operators. +* **Each nesting level increases complexity.** + +During code reading, the deeper you go through nested layers, the harder it becomes to keep the context in mind. +* **Method calls are free** + + A well-picked method name is a summary of multiple lines of code. + A reader can first explore a high-level view of what the code is performing then go deeper and deeper by looking at called functions content. + +__Note:__ This does not apply to recursive calls, those will increment cognitive score. + +The method of computation is fully detailed in the pdf linked in the resources. == Resources From 2d4f71cfa1ab4f1c1eaf5cc1697b2d0686a49f0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Joly?= Date: Mon, 25 Nov 2024 23:08:19 +0100 Subject: [PATCH 2/2] Update rules/S6194/rule.adoc Co-authored-by: Fred Tingaud <95592999+frederic-tingaud-sonarsource@users.noreply.github.com> --- rules/S6194/rule.adoc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/rules/S6194/rule.adoc b/rules/S6194/rule.adoc index 5c5ae3ad26b..fdfab5331f8 100644 --- a/rules/S6194/rule.adoc +++ b/rules/S6194/rule.adoc @@ -1,7 +1,8 @@ == Why is this an issue? -Cognitive Complexity is a measure of how hard the control flow of a function is to understand. Coroutines with high Cognitive Complexity will be difficult to maintain. -Code with high cognitive complexity is hard to read, understand, test, and modify. +"Cognitive complexity" is a measure of how hard the control flow of a function is to understand. Code with high cognitive complexity is hard to read, understand, test, and modify. + +This rule raises on coroutines with high cognitive complexity. As a rule of thumb, high cognitive complexity is a sign that the code should be refactored into smaller, easier-to-manage pieces.