Skip to content

BSL019 — Cyclomatic complexity

Summary

Cyclomatic complexity

Identifiers

Field Value
Rule code BSL019
Compatible alias CyclomaticComplexity
Severity WARNING
Enabled by default Yes
Implemented Yes
Tags brain-overload, complexity

Behavior

  • The public identifier BSL019 and alias CyclomaticComplexity are stable.
  • The rule reports the cases documented on this page.
  • Suppressions and project configuration are applied before publication.
  • The rule requires neither an external analyzer nor network access.

Configuration and suppression

BSL### is the primary stable identifier. The compatible alias is accepted in select, ignore, and compatible block suppression comments.

[tool.onec-hbk-bsl]
select = ["BSL019"]
ignore = ["CyclomaticComplexity"]

All three suppression families support both a current line and a range. When an opening comment follows code, it affects only that line. Use any one form:

  • noqa:
Value = "example";  // noqa: BSL019
  • bsl-disable:
Value = "example";  // bsl-disable: BSL019
  • compatible BSLLS form:
Value = "example";  // BSLLS:CyclomaticComplexity-off

When the same opening comment is on a line by itself, it starts a range. Close it with the matching marker from the same family:

// noqa: BSL019
// code without this diagnostic
// noqa-enable: BSL019

// bsl-disable: BSL019
// code without this diagnostic
// bsl-enable: BSL019

// BSLLS:CyclomaticComplexity-off
// code without this diagnostic
// BSLLS:CyclomaticComplexity-on

To disable the rule until the end of the file, omit the closing noqa-enable, bsl-enable, or BSLLS:…-on marker.

Opening and closing markers must belong to the same family.

Description

Cyclomatic complexity of the program code is one of the oldest metrics, it was first mentioned by Thomas McCab in 1976. Cyclomatic complexity shows the minimum number of required tests. The most effective way to reduce cyclomatic complexity is to decompose the code, split the methods into simpler ones, and also optimize logical expressions.

Cyclomatic complexity increases by 1 for each of following constructions

  • For ... To .. Do
  • For each ... Of ... Do
  • If ... Then
  • ElsIf ... Then
  • Else
  • Try ... Except ... EndTry
  • GoTo ~Label
  • Binary operations AND ... OR
  • Ternary operator
  • Procedure
  • Function

Examples

Function ServerModuleManager(Name)                                                      // 1
    ObjectFounded = False;                                                              // 0
                                                                                        // 0
    NameParts = StrSplit(Name, ".");                                                    // 0
    If NameParts.Count() = 2 Then                                                       // 1
                                                                                        // 0
        TypeName = Upper(NameParts[0]);                                                 // 0
        ObjectName = NameParts[1];                                                      // 0
                                                                                        // 0
        If TypeName = Upper("Constants") Then                                           // 1
            If Metadata.Constants.Find(ObjectName) <> Undefined Then                    // 1
                ObjectFounded = True;                                                   // 0
            EndIf;                                                                      // 0
        ElsIf TypeName = Upper("InformationRegisters") Then                            // 1
            If Metadata.InformationRegisters.Find(ObjectName) <> Undefined Then         // 1
                ObjectFounded = True;                                                   // 0
            EndIf;                                                                      // 0
        Else                                                                            // 1
            ObjectFounded = False;                                                      // 0
        EndIf;                                                                          // 0
    EndIf;                                                                              // 0
                                                                                        // 0
    If Not ObjectFounded Then                                                           // 1
        Raise СтроковыеФункцииКлиентСервер.ПодставитьПараметрыВСтроку(                  // 0
            НСтр("ru = 'Объект метаданных ""%1"" не найден,                             // 0
                       |либо для него не поддерживается получение модуля менеджера.'"), // 0
            Name);                                                                      // 0
    EndIf;                                                                              // 0
    SetSafeMode(True);                                                                  // 0
    Module = Eval(Name);                                                                // 0
    F = ?(SomeCondition1, True, Undefined);                                             // 1
    А = ?(SomeCondition1, True, ?(SomeCondition2, False, Undefined));                   // 2
    M = True Or 7;                                                                      // 1
    Return Module;                                                                      // 0
EndFunction                                                                              // Total 12

Sources