Skip to content

Draft: Проверка когнитивной сложности #1261

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#### Код модулей

- Проверка когнитивной сложности методов
- Проверка цикломатической сложности методов
- В проверку использования нерекомендуемых методов (use-non-recommended-method) добавлен метод ПолучитьФорму(GetForm)

#### Запросы
Expand Down
158 changes: 158 additions & 0 deletions bundles/com.e1c.v8codestyle.bsl/markdown/cognitive-complexity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# The cognitive complexity of the method is exceeded

The high score of the cognitive complexity to complicate perception and maintainability of the written code.

The most effective way to reduce the complexity score is to decompose the code and simplify logical expressions.

## Compute rules:

### Increments:

- Conditional statements:
```bsl
If Conditional1 Then // +1 point
// Code
ElseIf Conditional2 Then // +1 point
// Code
Else // +1 point
// Code
EndIf;
```
- Trenary operator:
```bsl
?(conditional, positive_code, negative_code); // +1 point
```
- Loops:
```bsl
For Each Element In Collection Do // +1 point
// code
EndDo;
```
```bsl
For index = 1 To N Do // +1 point
// code
EndDo;
```
```bsl
While True Do // +1 point
// code
EndDo;
```
- Exception block:
```bsl
...
Exception // +1 point
// code
EndTry;
```
- GoTo:
```bsl
Goto ~Label; // +1 point
```
- boolean operands AND, OR
```bsl
conditional1 = predicate1 OR predicate2; // +1 point
conditional2 = predicate1 AND predicate2; // +1 point
```
- recursive call:
```bsl
Procedure RecursiveCall(Collection)
If conditional Then
RecursiveCall(...); // +1 point
EndIf;
EndProcedure
```

### Nesting increments:

- IF-part of conditional statement:
```bsl
If conditional1 then // +nesting level
// code
ElseIf conditional2 then
// code
Else
// code
EndIf;
```
- Trenary operator:
```bsl
?(conditional, value1, value2); // +nesting level
```
- Loops:
```bsl
For Each Element In Collection Do // +nesting level
// code
EndDo;
```
```bsl
For index = 1 To N Do // +nesting level
// code
EndDo;
```
```bsl
While True Do // +nesting level
// code
EndDo;
```
- exception block:
```bsl
...
Exception // +nesting level
// code
EndTry;
```

### Nesting level:

- Conditional statements:
```bsl
If Conditional1 Then // increment depth
// Code
ElseIf Conditional2 Then // increment depth
// Code
Else // increment depth
// Code
EndIf;
```

- Trenary operator:
```bsl
?(
conditional,
// increment depth,
// increment depth
)
```
- Loops:
```bsl
For Each Element In Collection Do
// increment depth
EndDo;
```
```bsl
For index = 1 To N Do
// increment depth
EndDo;
```
```bsl
While True Do
// increment depth
EndDo;
```
- Exception block:
```bsl
...
Exception
// increment depth
EndTry;
```
- Nesting method:
```bsl
Method(
// increment depth
NestingMethod(
// increment depth
)
);
```
65 changes: 65 additions & 0 deletions bundles/com.e1c.v8codestyle.bsl/markdown/cyclomatic-complexity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# The cyclomatic complexity of the method is exceeded

Thomas J. McCabe developed a cyclomatic metric for code testing tasks.
His proposed method calculates the number of linearly independent program execution threads.
The difficulty value corresponds to the number of tests required.

The most effective way to reduce the complexity score is to decompose the code and simplify logical expressions.

## Compute rules:

### Increments:

- Loops:
```bsl
For Item In Collection Do
// code
EndDo;
```
```bsl
For index = 1 To N Do
// code
EndDo;
```
```bsl
While Conditional Do
// code
EndDo;
```
- Conditional statements:
```bsl
If Conditional Then // +1 point
// code
EndIf;

If Conditional Then // too +1 point
// code
Else
// code
EndIf;
```
```bsl
If Conditional1 Then // +1 point
// code
ElseIf Conditional2 Then // +1 additional point
// code
ElseIf Conditional3 Then // +1 additional point
// code
Else // the Else does not increase the complexity
// code
EndIf;
```
- Try-Exception blocks:
```bsl
Try
// code
Exception
// code
EndTry;
```
- Booleands operands: AND, OR
- Trenary operator:
```bsl
?(Coditional, Value1, Value2);
```
- Procedure or Function initially has a complexity equal to 1
158 changes: 158 additions & 0 deletions bundles/com.e1c.v8codestyle.bsl/markdown/ru/cognitive-complexity.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# Превышение когнитивной сложности метода

Высокое значение показателя говорит о сложности восприятия и сопровождаемости написанного кода.

Наиболее эффективным способом уменьшения показателя сложности является декомпозиция кода и упрощение логических выражений.

## Правила вычисления:

### Конструкции увеличивающие сложность:

- Ветви условного оператора:
```bsl
Если Условие1 Тогда // +1 к сложности
// код
ИначеЕсли Условие2 Тогда // +1 к сложности
// код
Иначе // +1 к сложности
// код
КонецЕсли;
```
- тренарный оператор:
```bsl
?(условие, позитивный исход, негативный исход); // +1 к сложности
```
- циклы:
```bsl
Для Каждого Элемент Из Коллекция Цикл // +1 к сложности
// код
КонецЦикла;
```
```bsl
Для Индекс = 1 По Граница Цикл // +1 к сложности
// код
КонецЦикла;
```
```bsl
Пока Условия Цикл // +1 к сложности
// код
КонецЦикла;
```
- ветвь исключения в блоке попытки:
```bsl
...
Исключение // +1 к сложности
// код
КонецПопытки;
```
- переход к метке
```bsl
Перейти ~Метка; // +1 к сложности
```
- логические операнды И, ИЛИ
```bsl
Условие1 = Предикат1 ИЛИ Предикат2; // +1 к сложности
Условие2 = Предикат1 И Предикат2; // +1 к сложности
```
- рекурсивный вызов
```bsl
Процедура РекурсивныйОбход(Коллекция)
Если Условие Тогда
РекурсивныйОбход(...); // +1 к сложности
КонецЕсли;
КонецПроцедуры
```

### Конструкции получающие дополнительный штраф на уровень вложенности:

- Условный оператор (не зависимо от количества ветвей):
```bsl
Если Условие1 Тогда // штраф +уровень_вложенности
// код
ИначеЕсли Условие2 Тогда
// код
Иначе
// код
КонецЕсли;
```
- тренарный оператор:
```bsl
?(Условие, Значение1, Значение2); // штраф +уровень_вложенности
```
- циклы:
```bsl
Для Каждого Элемент Из Коллекция Цикл // штраф +уровень_вложенности
// код
КонецЦикла;
```
```bsl
Для Индекс = 1 По Граница Цикл // штраф +уровень_вложенности
// код
КонецЦикла;
```
```bsl
Пока Условия Цикл // штраф +уровень_вложенности
// код
КонецЦикла;
```
- ветвь исключения в попытке:
```bsl
...
Исключение // штраф +уровень_вложенности
// код
КонецПопытки;
```

### Конструкции увеличивающие уровень вложенности:

- ветви условного оператора:
```bsl
Если Условие1 Тогда
// увеличение вложенности
ИначеЕсли Условие2 Тогда
// увеличение вложенности
Иначе
// увеличение вложенности
КонецЕсли;
```

- тренарный оператор:
```bsl
?(
условие,
// увеличение вложенности,
// увеличение вложенности
)
```
- циклы:
```bsl
Для Каждого Элемент Из Коллекция Цикл
// увеличение вложенности
КонецЦикла;
```
```bsl
Для Индекс = 1 По Граница Цикл
// увеличение вложенности
КонецЦикла;
```
```bsl
Пока Условия Цикл
// увеличение вложенности
КонецЦикла;
```
- ветвь исключения в блоке попытки:
```bsl
...
Исключение
// увеличение вложенности
КонецПопытки;
```
- вложенный вызов:
```bsl
ВызовМетода(
// увеличение вложенности
ВложенныйВызов(
// увеличение вложенности
)
);
```
Loading