Skip to content

Unsafe FindByCode() method usage (UnsafeFindByCode)

Type Scope Severity Activated
by default
Minutes
to fix
Tags
Code smell BSL Major Yes 5 design
suspicious

Description

The diagnostic detects the use of the FindByCode() method (or НайтиПоКоду()) for catalogs that have:

  • code uniqueness control disabled (the Check unique property is set to False)
  • or code series enabled not for the entire catalog (the Code series property is not equal to Whole catalog)

In such cases, using the FindByCode() method can lead to unexpected behavior, as the code may not be unique within the entire catalog or there may be multiple elements with the same code in different series.

Examples

Incorrect:

// Catalog without uniqueness control
CatalogWithoutControl = Catalogs.CatalogWithoutUniquenessControl.FindByCode("001");
// Catalog with code series "Within subordination"
CatalogWithSeries = Catalogs.CatalogWithSubordinationSeries.FindByCode("001");

Correct:

// Using FindByCode() method for catalog with uniqueness control
// and code series for the entire catalog
CatalogWithControl = Catalogs.CatalogWithUniquenessControl.FindByCode("001");
// Alternative option - use FindByName() method or other search methods
Catalog = Catalogs.CatalogWithoutUniquenessControl.FindByName("Element");

Sources

Snippets

Diagnostic ignorance in code

// BSLLS:UnsafeFindByCode-off
// BSLLS:UnsafeFindByCode-on

Parameter for config

"UnsafeFindByCode": false