Skip to content

BSL002 — Method size

Summary

Method size

Identifiers

Field Value
Rule code BSL002
Compatible alias MethodSize
Severity ERROR
Enabled by default Yes
Implemented Yes
Tags size, brain-overload

Behavior

  • The public identifier BSL002 and alias MethodSize 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 = ["BSL002"]
ignore = ["MethodSize"]

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: BSL002
  • bsl-disable:
Value = "example";  // bsl-disable: BSL002
  • compatible BSLLS form:
Value = "example";  // BSLLS:MethodSize-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: BSL002
// code without this diagnostic
// noqa-enable: BSL002

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

// BSLLS:MethodSize-off
// code without this diagnostic
// BSLLS:MethodSize-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

There are cumbersome methods (procedures and functions) which mskes it impossible to work effectively precisely because of their huge size. A large method often arises when a developer adds new functionality to a method. “Why should I put the parameter check in a separate method, if I can write it here?”, “Why do I need to create a separate method for the search of maximum element in the array, let’s leave it here. So the code is clearer”, and other misconceptions.

There are two rules for refactoring a large method:

  • If when writing a method you want to add a comment to the code, you must put this functionality in a separate method
  • If the method takes more than 50-100 lines of code, you should determine the tasks and subtasks that it performs and try to put the subtasks in a separate method

Sources