Modify rule S2223: Update description to include also the case of global state (#1583)

This commit is contained in:
Mary Georgiou 2023-02-22 15:19:19 +01:00 committed by GitHub
parent 13174db6cd
commit ffd8720ca0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,4 +1,11 @@
A ``++static++`` field that is neither constant nor read-only is not thread-safe. Correctly accessing these fields from different threads needs synchronization with ``++lock++``s. Improper synchronization may lead to unexpected results, thus publicly visible static fields are best suited for storing non-changing data shared by many consumers. To enforce this intent, these fields should be marked ``++readonly++`` or converted to constants.
Non-private `static` fields that are neither `const` nor `readonly` can lead to errors and unpredictable behavior.
This can happen because:
* Any object can modify these fields and alter the global state. This makes the code more difficult to read, debug and test.
* Correctly accessing these fields from different threads needs synchronization with `lock`. Improper synchronization may lead to unexpected results.
Publicly visible static fields should only be used to store shared data that does not change.
To enforce this intent, these fields should be marked `readonly` or converted to `const`.
== Noncompliant Code Example